from visual import * import random #converts an angle in degrees to an angle in radians def rad(degrees): radians=degrees*pi/180 return radians #pause and wait for mouse or keyboard event, then continue def pause(): while True: rate(50) if scene.mouse.events: m = scene.mouse.getevent() if m.click == 'left': return elif scene.kb.keys: k = scene.kb.getkey() return #checks for a collision between two spheres def collisionSpheres(sphere1, sphere2): dist=mag(sphere1.pos-sphere2.pos) if(dist20 or spaceship.pos.x<-20: spaceship.pos=spaceship.pos-spaceship.v*dt spaceship.pos.x=-spaceship.pos.x if spaceship.pos.y>20 or spaceship.pos.y<-20: spaceship.pos=spaceship.pos-spaceship.v*dt spaceship.pos.y=-spaceship.pos.y #update positions of bullets and check if bullets go off screen for thisbullet in bulletsList: if thisbullet.pos.x>20 or thisbullet.pos.x<-20: thisbullet.visible=0 if thisbullet.pos.y>20 or thisbullet.pos.y<-20: thisbullet.visible=0 if thisbullet.visible != 0: thisbullet.pos=thisbullet.pos+thisbullet.v*dt #update positions of asteroids for thisasteroid in asteroidList: if thisasteroid.visible==1: thisasteroid.pos=thisasteroid.pos+thisasteroid.v*dt #check for collision with spaceship if(collisionConeSphere(spaceship,thisasteroid)): spaceship.visible=0 fire.visible=0 #wrap at edge of screen if thisasteroid.pos.x>20 or thisasteroid.pos.x<-20: thisasteroid.pos=thisasteroid.pos-thisasteroid.v*dt thisasteroid.pos.x=-thisasteroid.pos.x if thisasteroid.pos.y>20 or thisasteroid.pos.y<-20: thisasteroid.pos=thisasteroid.pos-thisasteroid.v*dt thisasteroid.pos.y=-thisasteroid.pos.y #check for collision with bullets for thisbullet in bulletsList: if(collisionSpheres(thisbullet,thisasteroid)and thisbullet.visible==1): thisasteroid.visible=0 thisbullet.visible=0 Nleft=0 #have to reset this before counting for thisasteroid in asteroidList: if thisasteroid.visible: Nleft=Nleft+1 #create more asteroids if all are gone if Nleft==0: createAsteroids() #update fire if thrust==0 or spaceship.visible==0: fire.visible=0 else: fire.visible=1 t=t+dt