Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame:问题源于使用键盘输入的update()函数_Python_Pygame - Fatal编程技术网

Python Pygame:问题源于使用键盘输入的update()函数

Python Pygame:问题源于使用键盘输入的update()函数,python,pygame,Python,Pygame,我试图创建一个两人游戏,其中每个人都是一个类的对象。但是,当我运行代码时,命令提示符在第43行给出了一个错误,指出update()缺少一个位置参数:“function” 另外,当我去掉update函数中的“function”参数时,我得到另一个错误,它说:“pygame.key.ScancodeWrapper”对象没有属性“left” 我对Python和Pygame比较陌生,因此非常感谢您的帮助 类名通常应使用大写字母约定。将player重命名为player,但保留player1和player2

我试图创建一个两人游戏,其中每个人都是一个类的对象。但是,当我运行代码时,命令提示符在第43行给出了一个错误,指出update()缺少一个位置参数:“function”

另外,当我去掉update函数中的“function”参数时,我得到另一个错误,它说:“pygame.key.ScancodeWrapper”对象没有属性“left”


我对Python和Pygame比较陌生,因此非常感谢您的帮助

类名通常应使用大写字母约定。将
player
重命名为
player
,但保留
player1
player2


player
是一个类,但是
update
是一个类。您必须在
player
类的
player1
player2
是类
player
的实例

player.update(按下)

player1.更新(按下)
player2.更新(按下)

但是,由于
player
是一个,我建议使用:

player1=player(左K_,右K_,“jet1.png”)
player2=player(K_a,K_d,“jet2.png”)
所有精灵=pygame.sprite.Group([player1,player2])
开=真
打开时:
# [...]
所有精灵更新(按下)
# [...]
class player(pygame.sprite.Sprite):
    lives = 5

    def __init__(self, left, right, img):
        super(player, self).__init__()
        self.surface = pygame.image.load(img).convert_alpha()
        self.rect = self.surface.get_rect()
        self.left = left
        self.left = right

    def update(self, function):
        if function[self.left]:
            self.rect.move_ip(-5, 0)
        if function[self.right]:
            self.rect.move_ip(5, 0)


player1 = player(K_LEFT, K_RIGHT, "jet1.png")
player2 = player(K_a, K_d, "jet2.png")


open = True
while open:
    for event in pygame.event.get():
        if pygame.key.get_pressed()[K_ESCAPE]:
            open = False
        elif event.type == QUIT:
            open = False

    pressed = pygame.key.get_pressed()

    player.update(pressed)

    display.blit(player1.surface, (width/4, height/4))
    display.blit(player2.surface, (.75 * width, .75 * height))