2016-11-10 72 views
-1

我在pygame中做了一個非常非常基本的遊戲,其中唯一可能的動作是向左移動,向右移動並向上射擊子彈。我的問題是,當我射擊時,我的球員精靈在子彈向上移動時保持不動。我該如何補救?我對pygame很陌生,所以我會很感激任何和所有的幫助。我需要精靈在射擊子彈後繼續移動

#importing needed libraries 
import pygame,sys 
from pygame.locals import * 


#Class for the Player 
class Player(): 
    def __init__(self,surf,xpos,ypos): 
     self.image=pygame.image.load("cat1.png").convert_alpha() 
     self.x=xpos 
     self.y=ypos 
     self.surface=surf 
    def keys(self): 
     dist=10 
     key=pygame.key.get_pressed() 
     if key[pygame.K_RIGHT]: 
      if self.x<500: 
       self.x+=dist 
     elif key[pygame.K_LEFT]: 
      if self.x!=0: 
       self.x-=dist 
    def draw(self,surface): 
     self.surface.blit(self.image,(self.x,self.y)) 

#Class for the bullet which inherits the Player Class 
class Weapon(Player): 
    def __init__(self,surf,xpos,ypos,bg,wxpos,wypos): 
     Player.__init__(self,surf,xpos,ypos) 
     self.wimage=pygame.image.load("bullet.png").convert_alpha() 
     self.wx=wxpos 
     self.wy=wypos 
     self.background=bg 

    def Shoot(self): 
     dist=10 
     while self.wy>0: 
      self.surface.blit(self.background,(0,0)) 
      self.surface.blit(self.image,(self.x,self.y)) 
      self.surface.blit(self.wimage,(self.wx,self.wy)) 
      self.wy-=dist 
      pygame.display.update() 

#initialising pygame 
pygame.init() 

FPS=30 
fpsClock=pygame.time.Clock() 

#creating display window 
DISPLAYSURF=pygame.display.set_mode((577,472),0,32) 
pygame.display.set_caption('Animation') 


WHITE = (255, 255, 255) 
background=pygame.image.load("background.png") 
DISPLAYSURF.fill(WHITE) 

#creating player 
player=Player(DISPLAYSURF,50,360) 

#main game loop 
while True: 

    for event in pygame.event.get(): 
     if event.type==QUIT: 
      pygame.quit() 
      sys.exit() 
     elif event.type==MOUSEBUTTONDOWN: 
      weapon=Weapon(DISPLAYSURF,player.x,player.y,background,player.x+25,player.y) 
      player.draw(DISPLAYSURF) 
      weapon.Shoot() 

    player.keys() 
    DISPLAYSURF.blit(background,(0,0)) 
    player.draw(DISPLAYSURF) 

    pygame.display.update() 
    fpsClock.tick(FPS) 
+0

沒有詳細說明在這裏做這樣的方式,但你需要使用一個線程或子程序。基本上,你想要的是有許多對象同時運行,所以你需要並行化他們的行爲。 –

+0

你不能在'Shoot'中使用'while'循環。你必須在'while True'(主循環)內移動它。 – furas

+0

'射擊'必須是類 - 類似於玩家 - 使用'draw()'來顯示和'更新()'在主循環的每個循環中只移動幾個像素。 Mainloop會做所有的工作。 – furas

回答

0

Shoot有像播放器類draw()update()其移動子彈只是self.dist像素。如果你在主循環中調用它,那麼你會移動子彈,你可以移動玩家。

這是一個簡單的例子。瞭解更多關於pygame.sprite.Sprite()pygame.sprite.Group()pygame.Rect()

而看到Program Arcade Games With Python And Pygame

import pygame 
import sys 

# --- constants ---- 

FPS = 30 

WHITE = (255, 255, 255) 

# --- classes --- 

class Player(): 

    def __init__(self, surface, x, y): 
     self.surface = surface 
     self.image = pygame.image.load("cat1.png").convert_alpha() 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 
     self.dist = 10 

    def keys(self): 
     key = pygame.key.get_pressed() 

     if key[pygame.K_RIGHT]: 
      if self.rect.x < 500: 
       self.rect.x += self.dist 
     elif key[pygame.K_LEFT]: 
      if self.rect.x >= 0: 
       self.rect.x -= self.dist 

    def draw(self): 
     self.surface.blit(self.image, self.rect) 


class Bullet(): 

    def __init__(self, surface, x, y): 
     self.surface = surface 
     self.image = pygame.image.load("bullet.png").convert_alpha() 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 
     self.dist = 10 
     self.dead = False 

    def draw(self): 
     self.surface.blit(self.image, self.rect) 

    def update(self): 
     if self.rect.y > 0: 
      self.rect.y -= self.dist 
     else: 
      self.dead = True 

# --- main --- 

pygame.init() 

display = pygame.display.set_mode((577,472),0,32) 
pygame.display.set_caption('Animation') 

background = pygame.image.load("background.png") 
player = Player(display, 50, 360) 

fps_clock = pygame.time.Clock() 

bullets = [] 

while True: 

    # --- events --- 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 
     elif event.type == pygame.MOUSEBUTTONDOWN: 
      b = Bullet(display, player.rect.x, player.rect.y) 
      bullets.append(b) 

    # --- updates --- 

    player.keys() 

    # moves bullets 
    for b in bullets: 
     b.update() 

    # remove "dead" bullets 
    bullets = [b for b in bullets if not b.dead] 

    # --- draws --- 

    display.blit(background, (0,0)) 

    player.draw() 

    for b in bullets: 
     b.draw() 

    pygame.display.update() 

    # --- FPS --- 
    fps_clock.tick(FPS) 
+0

非常感謝!我一定會考慮這些資源! –