#!/usr/bin/env python"""A simple starfield example. Note you can move the 'center' ofthe starfield by leftclicking in the window. This example showthe basics of creating a window, simple pixel plotting, and inputevent management"""importrandom,math,pygamefrompygame.localsimport*#constantsWINSIZE=[640,480]WINCENTER=[320,240]NUMSTARS=150definit_star():"creates new star values"dir=random.randrange(100000)velmult=random.random()*.6+.4vel=[math.sin(dir)*velmult,math.cos(dir)*velmult]returnvel,WINCENTER[:]definitialize_stars():"creates a new starfield"stars=[]forxinrange(NUMSTARS):star=init_star()vel,pos=starsteps=random.randint(0,WINCENTER[0])pos[0]=pos[0]+(vel[0]*steps)pos[1]=pos[1]+(vel[1]*steps)vel[0]=vel[0]*(steps*.09)vel[1]=vel[1]*(steps*.09)stars.append(star)move_stars(stars)returnstarsdefdraw_stars(surface,stars,color):"used to draw (and clear) the stars"forvel,posinstars:pos=(int(pos[0]),int(pos[1]))surface.set_at(pos,color)defmove_stars(stars):"animate the star values"forvel,posinstars:pos[0]=pos[0]+vel[0]pos[1]=pos[1]+vel[1]ifnot0<=pos[0]<=WINSIZE[0]ornot0<=pos[1]<=WINSIZE[1]:vel[:],pos[:]=init_star()else:vel[0]=vel[0]*1.05vel[1]=vel[1]*1.05defmain():"This is the starfield code"#create our starfieldrandom.seed()stars=initialize_stars()clock=pygame.time.Clock()#initialize and prepare screenpygame.init()screen=pygame.display.set_mode(WINSIZE)pygame.display.set_caption('pygame Stars Example')white=255,240,200black=20,20,40screen.fill(black)#main game loopdone=0whilenotdone:draw_stars(screen,stars,black)move_stars(stars)draw_stars(screen,stars,white)pygame.display.update()foreinpygame.event.get():ife.type==QUITor(e.type==KEYUPande.key==K_ESCAPE):done=1breakelife.type==MOUSEBUTTONDOWNande.button==1:WINCENTER[:]=list(e.pos)clock.tick(50)# if python says run, then we should runif__name__=='__main__':main()