pygame打飞机的游戏,写到一半有点问题
每次按下的时候会自动发射子弹,是什么问题。。。
还有要用sprite.group()怎么表达对象的属性呢?
还有子弹碰到陨石的碰撞检测,怎么写会比较简单
我看到很多游戏代码的例子有from pygame.locals import *请问是干嘛的是干嘛的
代码:
#0905209import pygameimport randomclass Plane(pygame.sprite.Sprite): def __init__(self,x_coord,y_coord): pygame.sprite.Sprite.__init__(self) self.plane=pygame.image.load("plane2.gif") self.plane=self.plane.convert() self.image=self.plane self.rect=self.image.get_rect() self.x=x_coord self.y=y_coord self.rect.center=(x_coord,y_coord) def update(self,x_change,y_change): self.x+=x_change self.rect.centerx+=x_change self.y+=y_change self.rect.centery+=y_change class Stone(pygame.sprite.Sprite): def __init__(self,): pygame.sprite.Sprite.__init__(self) self.stone=pygame.image.load("stone.gif") self.stone=self.stone.convert() self.image=self.stone self.rect=self.image.get_rect() class Bullet(pygame.sprite.Sprite): def __init__(self,x,y): pygame.sprite.Sprite.__init__(self) self.speed=0 self.x=x self.y=y self.bullets=pygame.image.load("bullet.gif") self.bullets=self.bullets.convert() self.image=self.bullets self.rect=self.image.get_rect() self.rect.center=(self.x,self.y) self.fire=False def update(self,fire): if fire==True: self.speed=10 self.x+=0 self.rect.centerx+=0 self.y-=self.speed self.rect.centery-=self.speed if self.y<=0: self.x,self.y=plane.rect.center self.rect.center=plane.rect.center if fire==False: if self.rect.centery<plane.rect.centery: self.speed=10 self.x+=0 self.rect.centerx+=0 self.y-=self.speed self.rect.centery-=self.speed screen.blit(self.bullets,self.rect) if self.y<=0: self.x,self.y=(-10,-10) self.rect.center=(-10,-10) if self.rect.centery==plane.rect.centery: self.speed=0 self.x+=0 self.rect.centerx+=0 self.y-=self.speed self.rect.centery-=self.speed if self.y<=0: self.x,self.y=(-10,-10) self.rect.center=(-10,-10) pygame.init()screen = pygame.display.set_mode((800, 600), 0, 32) background=pygame.Surface(screen.get_size())background_position=[0,0]background_image=pygame.image.load("universe.jpg").convert()pygame.display.set_caption("Thunder")plane=Plane(450,500)stone=Stone()bullet=Bullet(plane.rect.centerx,plane.rect.centery)movingplane=pygame.sprite.RenderPlain(plane)movingstones=pygame.sprite.RenderPlain()movingbullets=pygame.sprite.Group()star_list=[]movingbullets_list=[]for i in range(1): movingbullets.add(bullet) for i in range(5): x=random.randrange(0,800) y=random.randrange(0,100) star_list.append([x,y]) clock=pygame.time.Clock()########################################################################################################################################################################################################################done=Truewhile done==True: for event in pygame.event.get(): if event.type==pygame.QUIT: done=False screen.blit(background_image, background_position) for b in movingbullets: print(plane.rect.centery,b.rect.centery) if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: if plane.rect.centerx>=0: plane.rect.centerx+=-8 b.update(False) if event.key == pygame.K_RIGHT: if plane.rect.centerx<=800: plane.rect.centerx+=8 b.update(False) if event.key == pygame.K_UP: if plane.rect.centery>=0: plane.rect.centery+=-8 b.update(False) if event.key == pygame.K_DOWN: if plane.rect.bottom<=600: plane.rect.centery+=8 b.update(False) if event.key == pygame.K_SPACE: movingbullets.update(True) rectlist=movingbullets.draw(screen) pygame.display.update(rectlist) if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: plane.rect.centerx+=0 b.update(False) if event.key == pygame.K_RIGHT: plane.rect.centerx+=0 b.update(False) if event.key == pygame.K_UP: plane.rect.centery+=0 b.update(False) if event.key == pygame.K_DOWN: plane.rect.centery+=0 b.update(False) if event.key == pygame.K_SPACE: movingbullets.update(False) rectlist=movingbullets.draw(screen) pygame.display.update(rectlist) ######################################################################################################################################################################################################################## movingplane.draw(screen) for i in range(len(star_list)): screen.blit(stone.image,star_list[i])#put pic star_list[i][1]+=1 #move down if star_list[i][1]>600: y =random.randrange(-150,-50) star_list[i][1]=y x=random.randrange(0,800) star_list[i][0]=x pygame.display.flip() clock.tick(30) pygame.quit()