简体   繁体   English

为什么一次只能让一个玩家移动 Pygame

[英]Why can only one player move at a time Pygame

So I'm trying to add two players to my pygame game yet only one of them seems to be able to move at a time.所以我试图在我的 pygame 游戏中添加两个玩家,但似乎一次只能移动一个。 They can both move independently when the other player.move is removed.当另一个 player.move 被移除时,它们都可以独立移动。 If I remove one player.move, the one that wasn't removed can move but if I keep both player.move's, then the one I put first is the only one able to move.如果我删除一个 player.move,没有被删除的那个可以移动,但是如果我保留两个 player.move,那么我放在第一个的就是唯一一个可以移动的。 Sorry if the code is bad, it's my first time trying classes and all.对不起,如果代码不好,这是我第一次尝试课程。 Thanks in advance.提前致谢。

import pygame

pygame.init()

game_over = False
screen_width = 1680
screen_height = 1050
fps = 60
screen = pygame.display.set_mode((screen_width,screen_height), pygame.FULLSCREEN)
clock = pygame.time.Clock()

jumping = False
player_x = 0
player_y = screen_height - 100
player2_x = screen_width - 100
player2_y = screen_height - 100
gravity = 0.2

class Player:

    def __init__(self, x, y, width, height, icon, direction_x, direction_y, speed, weight, x_acc, y_acc, jumping, start_y, p_number):
        self.icon = pygame.image.load(icon)
        self.icon = pygame.transform.scale(self.icon, (width, height))
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.direction_x = direction_x
        self.direction_y = direction_y
        self.speed = speed
        self.weight = weight
        self.x_acc = x_acc
        self.y_acc = y_acc
        self.jumping = jumping
        self.start_y = start_y
        self.p_number = p_number


    def move(self):
        global game_over
        for event in pygame.event.get():            
            if event.type == pygame.QUIT:
                game_over = True
            if self.p_number == 1:
                if event.type == pygame.KEYDOWN:
                    if event.key == ord("w"):
                        self.direction_y = 1                        
                    elif event.key == ord("s"):
                        self.direction_y = -1                        
                    elif event.key == ord("d"):
                        self.direction_x = -1
                    elif event.key == ord("a"):
                        self.direction_x = 1
                    elif event.key == pygame.K_ESCAPE:
                        game_over = True
                elif event.type == pygame.KEYUP:
                    self.direction_y = 0
                    self.direction_x = 0
            if self.p_number == 2:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP:
                        self.direction_y = 1                        
                    elif event.key == pygame.K_DOWN:
                        self.direction_y = -1                        
                    elif event.key == pygame.K_RIGHT:
                        self.direction_x = -1
                    elif event.key == pygame.K_LEFT:
                        self.direction_x = 1
                    elif event.key == pygame.K_ESCAPE:
                        game_over = True
                elif event.type == pygame.KEYUP:
                    self.direction_y = 0
                    self.direction_x = 0

    def update(self):
        #if self.direction_y < 0:
            #self.y += self.speed
        if self.direction_x > 0:
            self.x -= self.speed
        elif self.direction_x < 0:
            self.x += self.speed

    def jump(self, gravity):
        if self.direction_y > 0:
            if self.y == self.start_y:
                self.y_acc = -20
                self.jumping = True

        if self.jumping:
            self.y_acc += gravity * self.weight
            self.y += self.y_acc
            if self.y_acc == 19:
                self.jumping = False

player1 = Player(player_x, player_y, 100, 100, "Icon.png", 0, 0, 5, 5, 0, 0, False, player_y, 1)
player2 = Player(player2_x, player2_y, 100, 100, "Icon2.png", 0, 0, 5, 5, 0, 0, False, player2_y, 2)

while not game_over:

    player1.move()
    player2.move()

    player1.jump(gravity)
    player2.jump(gravity)

    player1.update()
    player2.update()

    screen.fill((0,0,0))
    screen.blit(player1.icon, (player1.x, player1.y))
    screen.blit(player2.icon, (player2.x, player2.y))
    pygame.display.update()
    clock.tick(fps)

pygame.quit()

I think it would be better to separate the event from the class, so if you create a eventhandler class/function which calls the correct member function for the player will resolve this problem.我认为将事件与类分开会更好,因此如果您创建一个为播放器调用正确成员函数的事件处理程序类/函数将解决此问题。 As well the event consuming problem since only this event handler class has to be consume the events.以及事件消耗问题,因为只有这个事件处理程序类必须消耗事件。

like :喜欢 :

def Event_handler():
   if event.type == pygame.KEYDOWN:
       if event.key == ord("w"):
           player_1.up()                    
       elif event.key == ord("s"):
           player_1.down()

and so on for the other inputs等等其他输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM