Python精灵播放器控件问题

时间:2016-12-13 23:53:25

标签: pygame sprite

我有一个应该允许两个玩家玩的游戏。但是,当用户按下按键时,第二个玩家不会移动。 (Plane2是第二位玩家)

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

我尝试将Plane2的更新功能移动到游戏循环中,但这并没有解决问题。

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)

我也尝试让第二个玩家成为自己的精灵组,但这也没有帮助。

2 个答案:

答案 0 :(得分: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

固定代码

答案 1 :(得分:0)

创建update()而不使用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()中的不同事件 - 不仅包括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()