TypeError:draw()缺少1个必需的位置参数:“ win”

时间:2019-10-17 00:51:23

标签: python-3.x pygame

on_sched = [2, 3, 4, 1, 5]
max_on = 0        # Number of the highest bulb turned on
moment_ct = 0     # Count of moments with all bulbs on

for pos, bulb in enumerate(on_sched):
    max_on = max(max_on, bulb)    #Keep track of maximum bulb turned on
    # Is that bulb number the quantity of bulbs?
    if max_on == pos+1:    # Adjust for 0-indexed list
        moment_ct += 1

由于某种原因,当我运行该程序时,我收到一个错误,即class rocket(object): def __init__(self, x, y): self.image = pygame.image.load('sprites/spaceship.png') self.x = x self.y = y self.width = 72 self.height = 72 self.speed = 5 self.angle = 0 def draw(self, win): win.blit(char, (self.x,self.y)) def updateGameWindow(): win.blit(bg, (0,0)) rocket.draw(win) pygame.display.update() 显然不在参数'win'的地方。

rocket.draw(win)在程序顶部定义。

1 个答案:

答案 0 :(得分:2)

draw()需要在rocket实例上调用,而不是在类本身上调用:

rocket_instance = rocket(some_x, some_y)
rocket_instance.draw(win)

在上述代码的第一行中,您将创建火箭类的实例,在第二行中,您将在该实例上调用draw()方法,其中实例本身隐式传递为{ {1}}参数。

在示例代码中,您从此行self中收到一条错误消息,因为由于您是在类本身而不是实例上调用rocket.draw(win),因此该实例不会作为{{ 1}}参数,而是将draw()参数传递给self参数,并且一切都变了,因为现在没有预期的win参数传递。