我的問題是,我想在屏幕上顯示3倍心臟的圖像,但是當我寫它的代碼,它給了我一個錯誤說:
Traceback (most recent call last):
File "main.py", line 99, in <module>
Game().new()
File "main.py", line 38, in new
Heart(self.allSprites, 10 * i + 5, self.health)
File "classes\heart.py", line 5, in __init__
pygame.sprite.Sprite.__init__(self, x, *args)
File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pygame\sprite.py", line 124, in __init__
self.add(*groups)
File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pygame\sprite.py", line 142, in add
self.add(*group)
TypeError: add() argument after * must be an iterable, not int
我希望發生
我想他們的左上角是一排3倍心臟的圖像是什麼。
EX(矩形表示心臟的圖像):
***** ***** *****
* * * * * *
* * * * * *
***** ***** *****
每個矩形是30px by 30px
,並且是15px
遠離彼此。
Python代碼
class Player(pygame.sprite.Sprite):
def __init__(self, *args):
pygame.sprite.Sprite.__init__(self, *args)
self.image =
pygame.image.load("assets/img/vehicles/sports.png").convert_alpha()
# self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH/2
self.rect.bottom = HEIGHT - 10
self.velX = 0
self.displacement = 6
self.lives = 3
class Heart(pygame.sprite.Sprite):
def __init__(self, x, *args):
pygame.sprite.Sprite.__init__(self, *args)
self.image = pygame.image.load("assets/img/heart.png").convert_alpha()
self.image = pygame.transform.scale(self.image, (30, 30))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.top = 10
class Game(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
pygame.init()
pygame.mixer.init()
self.clock = pygame.time.Clock()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
self.running = True
self.clock = pygame.time.Clock()
self.allSprites = pygame.sprite.Group()
self.obstacles = pygame.sprite.Group()
self.health = pygame.sprite.Group()
self.player = Player()
def new(self):
# create a new game
self.allSprites.add(self.player)
# add obstacles to list
for i in range(OBSTICLES_AMOUNT):
Obstacle(self.allSprites, self.obstacles)
# add hearts to list
for i in range(self.player.lives):
Heart(self.allSprites, 10 * i + 5, self.health)
self.gameLoop()
def gameLoop(self):
while self.running:
self.allSprites.update()
self.update()
self.events()
self.clock.tick(FPS)
def update(self):
# updates game
self.draw()
self.collision()
pygame.display.update()
def draw(self):
# draws to screen
self.screen.fill(BACKGROUND)
self.allSprites.draw(self.screen)
for sprites in self.obstacles:
sprites.update()
for sprites in self.health:
sprites.update()