繁体   English   中英

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

[英]Pygame - can't draw image to screen: TypeError: draw() missing 1 required positional argument: 'surface'

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

如下面的代码所示,该函数在一个类中。 该函数包含两个参数:“ 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)

我已经在以前的代码中完成了类似的工作,但是它选择不在此处工作。

注意您的呼叫技巧:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM