Pygame gives me TypeError: add() argument after * must be a sequence, not Ball when adding Ball to sprite group
Pygame gives me TypeError: add() argument after * must be a sequence, not Ball when adding Ball to sprite group
最近一直在折腾pygame
,决定做一个pong clone。但是,我 运行 遇到 ball
class 的问题。
这是我的 class:
class Ball(pygame.sprite.Sprite):
""" This class represents the ball that bounces around. """
# Constructor function
def __init__(self, x, y):
# Call the parent's constructor
pygame.sprite.Sprite().__init__(self)
# Set height, width
self.image = pygame.Surface([15, 15])
self.image.fill(white)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
# Set speed vector
self.change_x = 0
self.change_y = 0
def goal(self):
if self.rect.x <= SCREEN_WIDTH:
playerscore =+ 1
print playerscore
elif self.rect.x >= 0:
aiscore =+ 1
print aiscore
def update(self):
""" Update the ball's position. """
# Get the old position, in case we need to go back to it
old_x = self.rect.x
new_x = old_x + self.change_x
self.rect.x = new_x
# Did this update cause us to hit a wall?
collide = pygame.sprite.spritecollide(self, allsprites_list, False)
if collide:
# Whoops, hit a wall. Go back to the old position
self.rect.x = old_x
self.change_x *= -1
old_y = self.rect.y
new_y = old_y + self.change_y
self.rect.y = new_y
# Did this update cause us to hit a wall?
collide = pygame.sprite.spritecollide(self, allsprites_list, False)
if collide:
# Whoops, hit a wall. Go back to the old position
self.rect.y = old_y
self.change_y *= -1
if self.rect.x < -20 or self.rect.x > screen_width + 20:
self.change_x = 0
self.change_y = 0
这会将球添加到精灵组:
self.ball = Ball(100, 250)
self.all_sprites_list.add(self.ball)
这是追溯:
Traceback (most recent call last):
File "C:/Users/Enigma/Desktop/pong.py", line 312, in <module>
main()
File "C:/Users/Enigma/Desktop/pong.py", line 290, in main
game = Game()
File "C:/Users/Enigma/Desktop/pong.py", line 218, in __init__
self.ball = Ball(100, 250)
File "C:/Users/Enigma/Desktop/pong.py", line 83, in __init__
pygame.sprite.Sprite().__init__(self)
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 114, in __init__
if groups: self.add(groups)
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 129, in add
else: self.add(*group)
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 129, in add
else: self.add(*group)
TypeError: add() argument after * must be a sequence, not Ball
我已经搜索了网络和我可以在 SO 找到的所有帖子,但是 none 似乎适用于这个特定的难题。我们将不胜感激。
我是 运行 python 2.7.9 Windows 7.
这一行:
pygame.sprite.Sprite().__init__(self)
几乎可以肯定是错误的。您想要调用 class 上的方法,而不是实例。
pygame.sprite.Sprite.__init__(self)
最近一直在折腾pygame
,决定做一个pong clone。但是,我 运行 遇到 ball
class 的问题。
这是我的 class:
class Ball(pygame.sprite.Sprite):
""" This class represents the ball that bounces around. """
# Constructor function
def __init__(self, x, y):
# Call the parent's constructor
pygame.sprite.Sprite().__init__(self)
# Set height, width
self.image = pygame.Surface([15, 15])
self.image.fill(white)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
# Set speed vector
self.change_x = 0
self.change_y = 0
def goal(self):
if self.rect.x <= SCREEN_WIDTH:
playerscore =+ 1
print playerscore
elif self.rect.x >= 0:
aiscore =+ 1
print aiscore
def update(self):
""" Update the ball's position. """
# Get the old position, in case we need to go back to it
old_x = self.rect.x
new_x = old_x + self.change_x
self.rect.x = new_x
# Did this update cause us to hit a wall?
collide = pygame.sprite.spritecollide(self, allsprites_list, False)
if collide:
# Whoops, hit a wall. Go back to the old position
self.rect.x = old_x
self.change_x *= -1
old_y = self.rect.y
new_y = old_y + self.change_y
self.rect.y = new_y
# Did this update cause us to hit a wall?
collide = pygame.sprite.spritecollide(self, allsprites_list, False)
if collide:
# Whoops, hit a wall. Go back to the old position
self.rect.y = old_y
self.change_y *= -1
if self.rect.x < -20 or self.rect.x > screen_width + 20:
self.change_x = 0
self.change_y = 0
这会将球添加到精灵组:
self.ball = Ball(100, 250)
self.all_sprites_list.add(self.ball)
这是追溯:
Traceback (most recent call last):
File "C:/Users/Enigma/Desktop/pong.py", line 312, in <module>
main()
File "C:/Users/Enigma/Desktop/pong.py", line 290, in main
game = Game()
File "C:/Users/Enigma/Desktop/pong.py", line 218, in __init__
self.ball = Ball(100, 250)
File "C:/Users/Enigma/Desktop/pong.py", line 83, in __init__
pygame.sprite.Sprite().__init__(self)
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 114, in __init__
if groups: self.add(groups)
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 129, in add
else: self.add(*group)
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 129, in add
else: self.add(*group)
TypeError: add() argument after * must be a sequence, not Ball
我已经搜索了网络和我可以在 SO 找到的所有帖子,但是 none 似乎适用于这个特定的难题。我们将不胜感激。
我是 运行 python 2.7.9 Windows 7.
这一行:
pygame.sprite.Sprite().__init__(self)
几乎可以肯定是错误的。您想要调用 class 上的方法,而不是实例。
pygame.sprite.Sprite.__init__(self)