首页 > 解决方案 > 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()

标签: pythonpygame

解决方案


我不确定是什么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,并且不确定是什么。


推荐阅读