pygame - Making Asteroids Game in Python-Whats wrong with it? -
here source code asteroids game. used tutorial http://comp4431.wordpress.com/2010/08/11/tut-wk-3-asteroids/ followed closely could. besides of missing fetures fire, wrong it? wont display starmap. btw - new python
import pygame, random, math pygame.locals import * math import * winflags=0 screenrect=rect(0,0,800,600) #screen res nstars=100 nasteroids=10 #colors black=(0,0,0) white=(255,255,255) yellow=(255,255,0) transparent=(1,2,3) clock=pygame.time.clock() while true: clock.tick(30) class player(pygame.sprite.sprite): color=yellow thrust_value=1.0 turn_speed=5 max_speed=10.0 base_image=pygame.surface((21,11)) base_image.fill(transparent) base_image.set_colorkey(transparent) pointlist=[(0,0),(0,10),(20,5)] pygame.draw.polygon(base_image,color,pointlist) def __init__(self): pygame.sprite.sprite.__init__(self) self.image=self.base_image.copy() self.rect=self.image.get_rect(center=screenrect.center) self.vx=0 self.vy=0 self.angle=0 def update(self): self.rect.move_ip(self.vx,self.vy) self.wrap() def wrap(self): if (self.rect.center[0]<0): self.rect.move_ip(screenrect.width,0) elif (self.rect.center[0]>=screenrect.width): self.rect.move_ip(-screenrect.width,0) if (self.rect.center[1]<0): self.rect.move_ip(screenrect.height,0) elif (self.rect.center[1]>=screenrect.height): self.rect.move_ip(0,-screenrect.height) def thrust(self): a=radians(self.angle) self.vx+=self.thrust_value*cos(a) self.vy+=self.thrust_value*sin(a) vel=sqrt(self.vx*self.vx+self.vy*self.vy) if (vel>self.max_speed): self.vx=self.vx*self.max_speed/vel self.vy=self.vy*self.max_speed/vel def turn(self,direction): self.angle+=self.turn_speed*direction while (self.angle<0): self.angle+=360 while (self.angle>=360): self.angle-=360 self.image=pygame.transform.rotate(self.base_image,-self.angle) self.rect=self.image.get_rect(center=self.rect.center) def fire(self): print 'fire' class asteroid(pygame.sprite.sprite): color=yellow min_speed=2 max_speed=6 base_image = pygame.surface((40,50)) base_image.fill(transparent) base_image.set_colorkey(transparent) pointlist=[(10,0),(30,0),(40,15),(35,25),(20,50),(5,50),(0,25)] pygame.draw.polygon(base_image,color,pointlist) def __init__(self): pygame.sprite.sprite.__init__(self) self.image=self.base_image.copy() x=int(screenrect.width*random.random()) y=int(screenrect.height*random.random()) self.rect=self.image.get_rect(center=(x,y)) velocity=random.randint(self.min_speed, self.max_speed) angle=2.0*pi*random.random()#radians self.vx=cos(angle)*velocity self.vy=sin(angle)*velocity def update(self): self.rect.move_ip(self.vx,self.vy) self.wrap() def wrap(self): if (self.rect.center[0]<0): self.rect.move_ip(screenrect.width,0) elif (self.rect.center[0]>=screenrect.width): self.rect.move_ip(-screenrect.width,0) if (self.rect.center[1]<0): self.rect.move_ip(0,screenrect.height) elif (self.rect.center[1]>=screenrect.height): self.rect.move_ip(0,-screenrect.height) def main(): #init python pygame.init() #create window screen=pygame.display.set_mode(screenrect.size,winflags) #starmap background=pygame.surface(screenrect.size) background.fill(black) in range(0,nstars): x=random.randint(0,screenrect.width-1) y=random.randint(0,screenrect.height-1) pygame.draw.circle(background,white,(x,y),0) #blit screen screen.blit(background,(0,0)) pygame.display.update() player=player() render=pygame.sprite.renderupdates() render.add(player) asteroids=pygame.sprite.group() in range(0,nasteroids): asteroid=asteroid() render.add(asteroid) asteroids.add(asteroid) while true: event in pygame.event.get(): if event.type == quit or \ (event.type == keydown , event.key == k_escape): return keystate=pygame.key.get_pressed() if (keystate[k_up]): player.thrust() direction=keystate[k_right]-keystate[k_left] if (direction !=0): player.turn(direction) if (keystate[k_space]): player.fire() asteroid in pygame.sprite.spritecollide(player,asteroids,false): asteroid.kill() render.update() render.clear(screen,background) dirty=render.draw(screen) pygame.display.update(dirty) #run main() if __name__=="__main__": main()
these statements update display should inside main loop, instead of after it.
render.update() render.clear(screen,background) dirty=render.draw(screen) pygame.display.update(dirty)
also, i'm surprised program given infinite loop near beginning
while true: clock.tick(30)
Comments
Post a Comment