2016-12-13 94 views
1

我有一個應該允許兩個玩家玩的遊戲。但是,當用戶按下按鍵時,第二個玩家不會移動。 (平面2是第二個玩家)Python精靈播放器控制問題

class Plane(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.image = pygame.image.load("plane.gif") 
     self.image = self.image.convert() 
     self.rect = self.image.get_rect() 

     if not pygame.mixer: 
      print "problem with sound" 
     else: 
      pygame.mixer.init() 
      self.sndDing = pygame.mixer.Sound("ding.ogg") 
      self.sndCrash = pygame.mixer.Sound("planeCrash.ogg") 

    def update(self): 
     mousex, mousey = pygame.mouse.get_pos() 
     self.rect.center = (mousex, mousey) 

class Plane2(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.image = pygame.image.load("plane2.gif") 
     self.image = self.image.convert() 
     self.rect = self.image.get_rect() 
     self.rect.centerx = 320 
     self.rect.centery = 240 

    def update(self): 
     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_LEFT: 
        self.rect.centerx -= 8 
       if event.key == pygame.K_RIGHT: 
        self.rect.centerx += 8 
       if event.key == pygame.K_UP: 
        self.rect.centery += 8 
       if event.key == pygame.K_DOWN: 
        self.rect.centery -= 8 

我試圖爲移動的平面2更新功能進入遊戲圈,但沒有解決問題。

def gameEndless(): 
    pygame.display.set_caption("Mail Pilot!") 

    background = pygame.Surface(screen.get_size()) 
    background.fill((0, 0, 0)) 
    screen.blit(background, (0, 0)) 
    plane = Plane() 
    island = Island() 
    ocean = Ocean() 
    scoreboard = Scoreboard() 

    oceanSprites = pygame.sprite.Group(ocean) 
    islandSprites = pygame.sprite.Group(island) 
    planeSprite = pygame.sprite.Group(plane) 
    scoreSprite = pygame.sprite.Group(scoreboard) 
    bulletSprites = pygame.sprite.Group() 

    enemySprites = pygame.sprite.Group() 
    bossSprite = pygame.sprite.Group() 
    bossWeaponSprites = pygame.sprite.Group() 

    if gameType == "coop": 
     scoreboard.lives = 10 
     player2 = Plane2() 
     planeSprite.add(player2) 

我也嘗試讓第二個玩家成爲自己的精靈羣,但那也沒有幫助。

回答

0
class Plane2(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.image = pygame.image.load("plane2.gif") 
     self.image = self.image.convert() 
     self.rect = self.image.get_rect() 
     self.rect.centerx = 320 
     self.rect.centery = 240 

    def update(self): 
     keys = pygame.key.get_pressed() 
     if keys[pygame.K_UP]: 
      self.rect.centery -= 8 
     if keys[pygame.K_DOWN]: 
      self.rect.centery += 8 
     if keys[pygame.K_LEFT]: 
      self.rect.centerx -= 8 
     if keys[pygame.K_RIGHT]: 
      self.rect.centerx += 8 

固定碼

0

沒有for循環,但用爭論event

def update(self, event): 
    if event.type == pygame.KEYDOWN: 
     if event.key == pygame.K_LEFT: 
      self.rect.centerx -= 8 
     elif event.key == pygame.K_RIGHT: 
      self.rect.centerx += 8 
     elif event.key == pygame.K_UP: 
      self.rect.centery += 8 
     if event.key == pygame.K_DOWN 

,並在主循環使用update()for

for event in pygame.event.get(): 
    player1.update(event) 
    player2.update(event) 

這樣你創建update()可以處理update()中的不同事件 - 不僅是key事件,還有mouse事件或user-defined事件。

有些人使用此功能名稱handle_event。他們使用update()來改變這個不需要事件的元素 - 也就是說。設置動畫的下一幀。

# --- events --- 

for event in pygame.event.get(): 
    player1.handle_event(event) 
    player2.handle_event(event) 

# --- updates --- 

player1.update() 
player2.update()