【发布时间】:2017-04-25 13:38:02
【问题描述】:
Traceback(最近一次调用最后一次): 文件“***/Pcprojects/pygameKCC/kcc-shmup.py”,第 49 行,在 all_sprites.update()
TypeError: unbound 方法 update() 必须以 Group 实例作为第一个参数调用(什么都没有)
我正在学习一个有工作代码的教程(对他来说),而在我的生活中,我无法让这个精灵渲染。谁能告诉我我做错了什么?这是我第一次使用除了基本的弹力球屏幕保护类型之外的精灵。谢谢!
import pygame
import random
import sys
import math
#global constants
WIDTH = 360
HEIGHT = 480
FPS = 30
#define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
#YELLOW = (X, X, X)
class Player(pygame.sprite.Sprite):
#sprite for the player.
def __init__(self):
pygame.sprite.Sprite.__init__(self)
#required image and collision rectangle
self.image = pygame.Surface((50, 50))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
#itialize pygame
#itialize music mixer
pygame.init()
pygame.mixer.init()
#create screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
#create a group for all sprites to belong to
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#更新:
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
【问题讨论】:
-
在绘制精灵之后
fill()屏幕 - 这样你在从监视器上的缓冲区发送它们之前删除所有精灵(使用flip())。您必须首先fill(),然后是draw(),然后使用flip()在监视器上发送缓冲区。 -
我运行代码并没有收到此错误。
-
for event in pygame.event.get(): if event.type == pygame.QUIT: running = False #game Exit on [x] click #Update all_sprites.update() #Render/绘制 screen.fill(BLACK) all_sprites.draw(screen) pygame.display.flip() 这是我移动的摘录,但我仍然遇到同样的错误。会不会是 pycharm 问题?_eta:我不知道你是如何在评论中格式化代码的!
-
添加这个有问题的 - 它将更具可读性,每个人都会阅读它。
-
并添加完整的错误消息(Traceback)。可以有更多有用的信息。
标签: python class pygame sprite render