我跟着这里的教程...... http://pyvideo.org/video/1718/introduction-to-pygame
我遇到了底部的追溯,似乎无法找出从哪里开始寻找问题......事实证明,幻灯片中的一行代码是&#39 ; t包含在+和 - 位中,显示他在每一步添加/删除的内容,因此当它应该被添加时不清楚。我使用Pycharms调试器逐步完成并找出问题就在那一行,现在它被注释掉了。
我很困惑为什么那条线在那里,我似乎无法找到他准备的代码示例,看看我还缺少什么(我没有听到视频中观众的任何投诉)。
这里发生了什么?
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self, *groups):
super(Player, self).__init__(*groups)
self.image = pygame.image.load('Mugger Space Cadet.jpg')
self.rect = pygame.rect.Rect((16, 32), self.image.get_size())
def update(self, dt, game):
last = self.rect.copy()
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.rect.x -= 150 * dt
if key[pygame.K_RIGHT]:
self.rect.x += 150 * dt
if key[pygame.K_UP]:
self.rect.y -= 150 * dt
if key[pygame.K_DOWN]:
self.rect.y += 150 * dt
for cell in pygame.sprite.spritecollide(self, game.walls, False):
self.rect = last
class Game(object):
def main(self, screen):
clock = pygame.time.Clock()
background = pygame.image.load('mockup level.png')
sprites = pygame.sprite.Group()
# sprites.add(background)
self.player = Player(sprites)
self.walls = pygame.sprite.Group()
block = pygame.image.load('block.png')
for x in range(0, 448, 32):
for y in range(0, 320, 32):
if x in (0, 448-32) or y in (0, 320-32):
wall = pygame.sprite.Sprite(self.walls)
wall.image = block
wall.rect= pygame.rect.Rect((x, y), block.get_size())
sprites.add(self.walls)
while True:
dt = clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return
sprites.update(dt / 1000., self)
screen.blit(background, (0, 0))
sprites.draw(screen)
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((448, 320))
Game().main(screen)
追溯......
Traceback (most recent call last):
File "C:\Python32\lib\site-packages\pygame\sprite.py", line 365, in add
self.add(*sprite)
TypeError: add() argument after * must be a sequence, not pygame.Surface
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Jacob/PycharmProjects/PygamePractice/main.py", line 92, in <module>
Game().main(screen)
File "C:/Users/Jacob/PycharmProjects/PygamePractice/main.py", line 62, in main
sprites.add(background)
File "C:\Python32\lib\site-packages\pygame\sprite.py", line 378, in add
sprite.add_internal(self)
AttributeError: 'pygame.Surface' object has no attribute 'add_internal'
答案 0 :(得分:0)
似乎你试图在函数调用中使用带有Pygame.sprite.Sprite对象的*运算符,但是你只能将它用于可迭代对象(list,tuple)。
E.g:
# define a list or tuple of arguments:
arguments = ["foo", "bar"] # or ("foo", "bar)
# The * operator will pass every item of it as own argument:
my_function(*arguments)
但你不能写
# initialize a non-iterable class:
arguments = object()
# The * operator can't split an object into multiple arguments, so an error is raised
my_function(*arguments)