为什么我会收到位置参数错误

问题描述 投票:0回答:1

因此,在此类中的绘制方法中,由于某种原因,我遇到了位置参数错误。

 File "/Users/wallera/PycharmProjects/MAJORPROJECT2/maintest.py", line 21, in <module>
    game.run()
  File "/Users/wallera/PycharmProjects/MAJORPROJECT2/src/game.py", line 21, in run
    self.draw()
  File "/Users/wallera/PycharmProjects/MAJORPROJECT2/src/game.py", line 35, in draw
    self.world.draw(self.screen)
TypeError: World.draw() missing 1 required positional argument: 'screen'

问题是... world.draw() 确实有参数“screen”。这是我提供的最后一个方法。

import pygame as pg

from src.map import World


class Game:
    def __init__(self, screen: pg.Surface, clock: pg.time.Clock, window_size: pg.Vector2):
        self.screen = screen
        self.clock = clock
        self.window_size = window_size
        self.playing = True
        self.world = World

    def run(self):
        while self.playing:
            self.clock.tick(60)
            self.screen.fill((0, 0, 0))
            self.events()
            self.update()
            self.draw()
            pg.display.update()

    def events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.playing = False
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    self.playing = False

    def update(self):
        pass

    def draw(self):
        self.world.draw(self.screen)

我不确定出了什么问题,我没想到会出现这种情况,而且我不知道是什么导致了这个问题。

python pygame game-development
1个回答
0
投票

在初始化你的类时,你应该使用类 World 的对象而不是类。所以代替:

self.world = World

您应该使用:

self.world = World()
© www.soinside.com 2019 - 2024. All rights reserved.