pygame-无法在屏幕上绘制图像:TypeError:draw()缺少1个必需的位置参数:“ surface”

时间:2018-09-12 18:01:27

标签: python pygame

我想在屏幕上绘制一个框,但是当我调用该函数时,它说我缺少“表面”的参数

如下面的代码所示,该函数位于一个类中。该函数包含两个参数:“ self”和“ surface”,并且我将变量“ screen”替换为“ surface”,因此我可以绘制该框:

import pygame
import time
pygame.init()
(width, height) = (600, 400)
bg_colour = (100, 20, 156)

class infoBox(object):
    def __init__(self, surface):
        self.box = pygame.draw.rect(surface, (255,255,255),(0,400,400,100),2)

    def draw(self, surface):
        surface.blit(self.box)

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("battle EduGame")

clock = pygame.time.Clock()

pygame.display.flip()

gameRun = True


while gameRun:
    event = pygame.event.poll()
    if event.type == pygame.QUIT: #if the "x" is pressed
       pygame.quit() #quit game
       gameRun = False #break the loop.
       quit()

    screen.fill(bg_colour)

    infoBox.draw(screen)


    pygame.display.update()


    clock.tick(60)

我在以前的代码中已经做过完全一样的事情,但是它选择不在这里工作。

1 个答案:

答案 0 :(得分:0)

请注意您的呼叫技巧:

class infoBox(object):

    def draw(self, surface):
        surface.blit(self.box)
...

infoBox.draw(screen)

draw是一个实例方法,但是您已将其作为类方法调用。因此,您只有一个一个参数screen。您尚未提供所需的self实例……将立即需要。

您需要创建对象并使用那个来调用例程,例如

game_box = InfoBox(screen)
...
game_box.draw(screen)