python - Pygame - 无法将图像绘制到屏幕:TypeError:draw()缺少1个必需的位置参数:'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)
推荐阅读
- reactjs - React-tsx-以Zip格式下载多个图像文件
- jquery - Jquery 仅将当前选定的值添加到输入
- ruby - 给定一个 HTML 字符串,查找浮动 < > 的最佳方法是什么
- vscode-settings - 使用自定义键盘快捷键导航 vscode 智能感知
- python - PyQt5 功能问题
- html - 页面调整大小时有很多空白
- javascript - 如何从 Google 日历上的一个事件创建连锁事件?
- sql - 为什么这个 pg 查询这么慢?我怎样才能让它更快?
- vuejs2 - 在 nuxt 布局中使用多个 v-app 时是否有任何问题
- asp.net-core - 从角度 7 传递到我的 Asp.net 核心 WebApi 控制器的参数全部为空