python - pygame 和方法更新出错
问题描述
我开始编写代码,并一直在关注一系列视频,以使用 pygame 创建游戏。然而,即使我已经按照视频中那个人所说的一切,并且在多次检查了我的代码和他的代码后,我还是得到了这个错误: 文件“C:/Users/sdf/PycharmProjects/pygame1/pygametutorial 2/tutorail 1 template .py",第 43 行,在 all_sprites.update() TypeError: update() missing 1 required positional argument: 'self'
这是我到目前为止编写的代码:
import pygame
import random
import sprites as sprites
height = 360
width = 480
FPS = 30
White = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Tutorial")
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image.fill(green)
self.rect = self.image.get_rect()
self.rect.center = (height / 2, width / 2)
all_sprites = pygame.sprite.Group
player = Player()
all_sprites.add(player)
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
all_sprites.update()
screen.fill(black)
sprites.draw(screen)
# after every drawing flip the display
pygame.display.flip()
解决方案
我不确定是什么import sprites as sprites
,是您制作的另一个文件吗?是应该的import pygame.sprite.Sprite as sprite
吗?
all_sprites = pygame.sprite.Group
Group
是一门课,所以你需要括号
all_sprites = pygame.sprite.Group()
现在您正在调用它并且 all_sprites 是一个组对象,而不是引用该类
现在我更改sprites.draw(screen)
为all_sprites.draw(screen)
not don't have sprites
,并且不确定是什么。
推荐阅读
- html - 如果我使用 github 页面托管我的网站,如何缩短我的网址?
- javascript - 用于附加属性子项计数的递归对象数组
- reactjs - 使用 react.js 创建产品后如何正确显示产品预览页面
- python - 仅使用有限的编程概念打印帕斯卡三角
- python - 如何遍历txt文件并将值分配给Python中的字典?
- swift - 带有容器视图的滚动视图无法正确调整内容大小
- email - 如何添加为客户下订单的登录 wordpress 用户(销售人员)的用户名
- java - Android Studio 无法识别我的资源目录
- java - 映射器为使用 @JsonIgnore (resttemplate) 注释的字段获取空值
- java - RGB 到 NV21 (YUV420SP) 转换中的 U/V 值是如何计算的?