缺少位置参数

4
import pygame
import random
import os

path = os.getcwd()

bg = pygame.transform.scale2x(pygame.image.load(f'{path}/space.jpg'))

images = [
    pygame.image.load(f'{path}/R1.png'),
    pygame.image.load(f'{path}/R2.png'),
    pygame.image.load(f'{path}/R3.png'),
    pygame.image.load(f'{path}/R4.png'),
    pygame.image.load(f'{path}/R5.png'),
    pygame.image.load(f'{path}/R6.png'),
    pygame.image.load(f'{path}/R7.png'),
    pygame.image.load(f'{path}/R8.png'),
    pygame.image.load(f'{path}/R9.png')
]

win_width = 500
win_height = 600

win = pygame.display.set_mode((win_width, win_height))

class Spacemen(object):
    def __init__(self, y, velocity):
        self.y = y
        self.velocity = 5
        self.walk_count = 1
        self.gravity = True
        self.inverted_gravity = False
        self.x = 1

    def move(self):
        if self.gravity and not self.inverted_gravity:
            self.y += self.velocity
        if not self.gravity and self.inverted_gravity:
            self.y -= self.velocity

    def draw(self, win):
        if self.x == 10:
            self.x = 1
        if self.gravity and not self.inverted_gravity:
            img = images[self.x]
        elif not self.gravity and self.inverted_gravity:
            img = pygame.transform.flip(images[self.x])
        self.x += 1

        win.blit(img, (256, self.y))

def game_window():
    win.blit(bg, (0, 0))
    Spacemen.draw(win)
    win.update

def main():
    clock = pygame.time.Clock()
    man = Spacemen(256, 5)

    running = True

    while running:

        pygame.time.delay(100)

        keys = pygame.key.get_pressed()

        for event in pygame.event.get():
            if event.type == pygame.QUIT or keys[pygame.K_ESCAPE]:
                run = False

        man.move()

        game_window()

main()

当我运行代码时,它显示我缺少一个位置参数,在第54行缺少"win"。我不知道问题出在哪里,因为当我在另一个项目中尝试类似的方法时,它完美地工作了。当我提供(win, win)时,会出现另一个错误。
AttributeError: 'pygame.Surface' object has no attribute 'x'

问题解决了吗? - Rabbid76
1个回答

2
"draw"是一个方法对象。你需要将类Spaceman实例对象传递给函数game_window,并在该实例(man)上调用方法draw
def game_window(man):
    win.blit(bg, (0, 0))
    man.draw(win)
    pygame.display.update()

将类Spacemen的实例man传递给函数game_window:
def main():
    # [...]

    man = Spacemen(256, 5)

    running = True
    while running:

        # [...]

        game_window(man) # <-----

显示屏可以通过 pygame.display.update() 或者 pygame.display.flip() 进行更新。 win.update 完全没有意义。pygame.Surface 没有实例对象 update,而对于调用语句,括号是缺失的。

win.update

pygame.display.update()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接