2015-10-17 117 views
0

我這裏的教程中的下列... http://pyvideo.org/video/1718/introduction-to-pygamepygame的幫助 - 類型錯誤:添加()*後的參數必須是一個序列,不pygame.Surface

我跑進回溯在底部,也似乎沒有搞清楚哪裏開始尋找問題...原來有一行代碼中不包含在+和幻燈片 - 位,顯示出他添加了什麼/在每個步驟中去除,所以當它應該被添加是不清楚的。我用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

看來,如果你嘗試使用*操作上有Pygame.sprite.Sprite對象的函數調用,但你只能用它帶有可迭代對象(列表,元組)。
例如爲:

# 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) 
相關問題