2017-05-19 23 views
0

問題pygameのスプライトにx位置を追加することができません

私の問題は、私は、画面上の3枚の心臓の画像を表示しようとしているが、私はそれのためのコードを書いたとき、それは私に言って、エラーを与えることです:私は彼らの左上の行の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 

例(矩形が心臓画像を表す):

***** ***** ***** 
* * * * * * 
* * * * * * 
***** ***** ***** 

各長方形は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() 

答えて

0

私はそれを考え出した、それは私が*args

の元で別のパラメータを追加することができなかったことが判明:def name(x, *args)、私はそれぞれを追加したとき、私はx POSを変更心臓の画像をリストに追加します。

コード

for i in range(self.player.lives): 
     Heart(self.allSprites, self.health).rect.x = 30 * i 
0

Spriteクラスのドキュメントに戻り、インターフェースをご確認ください。 initの2番目の引数は、スプライトが属するグループです。あなたのコード・シーケンスは、その位置に整数を送信します。Game.new()で

Heart(self.allSprites, 10 * i + 5, self.health) 

ハート。 のinit():

def __init__(self, x, *args): 
    pygame.sprite.Sprite.__init__(self, *args) 

あなたxパラメータがallSpritesにマッピングされています。 * argsには整数の組だけが含まれます。

関連する問題