【发布时间】:2021-06-25 13:26:30
【问题描述】:
我正在用 PyGame 制作一个简单的平台游戏。我制作了一个平台类来从图像文件和绘制方法创建平台。我在主游戏类中也有一个方法,它指定关卡并指定要绘制的图像。
这是平台类
import pygame
from pygame.sprite import Sprite
class Platform(Sprite):
def __init__(self, xloc, yloc, imgw, imgh, img):
self.screen = pt_game.screen
self.image=pygame.image.load('images',img).convert()
self.image.convert_alpha()
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect()
self.rect.y = yloc
self.rect.x = xloc
def draw(self):
pygame.draw.rect(self.screen,self.rect)
这是主类while循环,更新屏幕方法,和级别检查方法
import sys
import pygame
from settings import Settings
from char import Char
from platform import Platform
class Main:
"""overall class to manage game assets and behavior"""
def __init__(self):
"""initialise game and create new game resources"""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Platformer")
self.char = Char(self)
def run_game(self):
"""start main game loop"""
while True:
pygame.time.delay(35)
#Watch for keyboard and mouse events
self._check_events()
self.char.update()
self._draw_levels()
self._update_screen()
def _draw_levels(self, Platform):
if self.settings.level == 1:
ground = self.Platform(0, 760, 1200, 40, 'images/ground.png')
platform1 = self.Platform(600, 800, 200, 50, 'images/Platform.png')
self.platforms = (ground, platform1)
def _update_screen(self):
#reraw the screen for each pass through the loop
self.screen.fill(self.settings.bg_color)
self.char.blitme()
[self.Platform.draw(screen) for platform in self.platforms]
#make most recently drawn screen visible
pygame.display.flip()
我得到的错误是TypeError: _draw_levels() missing 1 required positional argument: 'Platform'
我假设我没有在某个地方正确地调用该类,但我不确定在哪里。任何有关为什么会发生此错误的帮助将不胜感激!
注意:这不是完整的代码
【问题讨论】:
-
你还没有向我们展示你的
_draw_levels函数调用。它接受Platform作为 arg 但你甚至不会在该函数中使用它,除非你有它的部分。我假设因为Platform中的大写 P 函数需要Platform类作为参数,所以你的函数调用应该是main_class_instance._draw_levels(Platform) -
啊,你说得对,我没有调用'''draw_levels'''函数,谢谢!
-
方法
_draw_levels的签名是def _draw_levels(self, Platform):。但是,您在没有任何参数self._draw_levels()的情况下调用_draw_levels。错误很明显。 -
谢谢!这对你来说可能很明显,但我还是新手,所以对我来说不是那么多。感谢您的帮助!
标签: python class pygame typeerror draw