2016-07-29 33 views
2

我正尝试构建一个游戏,该游戏使用箭头键左右移动船,并在空格键被按下时触发子弹。当我按下空格键,我的游戏崩溃,并显示以下错误: 回溯(最近通话最后一个):*之后的add()参数必须是一个序列,而不是设置

TypeError: add() argument after * must be a sequence, not Settings 

这里是我的代码:

class Settings(): 
    """A class to store all settings for Alien Invasion.""" 

    def __init__(self): 
     """Initialize the game's settings.""" 
     # Screen settings 
     self.screen_width = 800 
     self.screen_height = 480 
     self.bg_color = (230, 230, 230) 

     # Ship settings 
     self.ship_speed_factor = 1.5 

     # Bullet settings 
     self.bullet_speed_factor = 1 
     self.bullet_width = 3 
     self.bullet_height = 15 
     self.bullet_color = 60, 60, 60 

import pygame 
from pygame.sprite import Sprite 

class Bullet(Sprite): 
    """A class to manage bullets fired from the ship""" 

    def _init__(self, ai_settings, screen, ship): 
     """Create a bullet object at the ship's current position.""" 
     super(Bullet, self).__init__() 
     self.screen = screen 

     # Create a bullet rect at (0, 0) and then set correct position. 
     self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height) 
     self.rect.centerx = ship.rect.centerx 
     self.rect.top = ship.rect.top 

     # Store the bullet's position as a decimal value. 
     self.y = float(self.rect.y) 

     self.color = ai_settings.bullet_color 
     self.speed_factor = ai_settings.bullet_speed_factor 

    def update(self): 
     """Move the bullet up the screen""" 
     # Update the decimal position of the bullet. 
     self.y -= self.speed_factor 
     # Update the rect position. 
     self.rect.y = self.y 

    def draw_bullet(self): 
     """Draw the bullet to the screen.""" 
     pygame.draw.rect(self.screen, self.color, self.rect) 

import sys 

import pygame 

from bullet import Bullet 

def check_keydown_events(event, ai_settings, screen, ship, bullets): 
    """Respond to keypresses.""" 
    if event.key == pygame.K_RIGHT: 
     ship.moving_right = True 
    elif event.key == pygame.K_LEFT: 
     ship.moving_left = True 
    elif event.key == pygame.K_SPACE: 
     # Create a new bullet and add it to the bullets group. 
     new_bullet = Bullet(ai_settings, screen, ship) 
     bullets.add(new_bullet) 

def check_keyup_events(event, ship): 
    """Respind to key releases.""" 
    if event.key == pygame.K_RIGHT: 
     ship.moving_right = False 
    elif event.key == pygame.K_LEFT: 
     ship.moving_left = False 


def check_events(ai_settings, screen, ship, bullets): 
    """Respond to keypresses and mouse events.""" 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      sys.exit() 

     elif event.type == pygame.KEYDOWN: 
      check_keydown_events(event, ai_settings, screen, ship, bullets) 
     elif event.type == pygame.KEYUP: 
      check_keyup_events(event, ship) 

最后的主要文件:

import pygame 
from pygame.sprite import Group 

from settings import Settings 
from ship import Ship 
import game_functions as gf 

def run_game(): 
    # Initialize pygame, settings, and screen object. 
    pygame.init() 
    ai_settings = Settings() 
    screen = pygame.display.set_mode(
     (ai_settings.screen_width, ai_settings.screen_height)) 
    pygame.display.set_caption("Alien Invasion") 

    # Make a ship. 
    ship = Ship(ai_settings, screen) 
    # Make a group to store bullets in. 
    bullets = Group() 

    # Start the main loop for the game. 
    while True: 

     # Watch the keyboard and mouse events. 
     gf.check_events(ai_settings, screen, ship, bullets) 
     ship.update() 
     bullets.update() 
     gf.update_screen(ai_settings, screen, ship, bullets) 

run_game() 

跟踪:

Traceback (most recent call last): 
    File "C:\Users\martin\Desktop\python_work\alien_invasion\alien_invasion.py", line 30, in <module> 
    run_game() 
    File "C:\Users\martin\Desktop\python_work\alien_invasion\alien_invasion.py", line 25, in run_game 
    gf.check_events(ai_settings, screen, ship, bullets) 
    File "C:\Users\martin\Desktop\python_work\alien_invasion\game_functions.py", line 33, in check_events 
    check_keydown_events(event, ai_settings, screen, ship, bullets) 
    File "C:\Users\martin\Desktop\python_work\alien_invasion\game_functions.py", line 15, in check_keydown_events 
    new_bullet = Bullet(ai_settings, screen, ship) 
    File "C:\Users\martin\Anaconda3\lib\site-packages\pygame\sprite.py", line 124, in __init__ 
    self.add(*groups) 
    File "C:\Users\martin\Anaconda3\lib\site-packages\pygame\sprite.py", line 142, in add 
    self.add(*group) 
TypeError: add() argument after * must be a sequence, not Settings 

回答

1

是的Jokab是对的,你忘了额外的下划线。但是,对于未来的练习,学习阅读Python 非常重要。它通常会给你一个关于问题出在哪里的好主意。例如,你可以在这里粘贴。 Python首先告诉你它运行有问题run_game()。所以蟒蛇然后说,你的游戏运行功能它有一个问题,调用方法gf.check_events(ai_settings, screen, ship, bullets)。然后它看着你初始化的子弹类new_bullet = Bullet(ai_settings, screen, ship,并有一个问题。并在接下来的一行是它给了TypeError。现在,虽然你可以确切地知道python对TypeError的说法,这是一个可行的选择。但只是从它看,你可以确定它有一个问题添加你的子弹对象的精灵组。这意味着如果我是你,我会开始在Bullet课程中进行搜索。果然,你的__init__函数中有一个错字。

虽然如果你不学习如何阅读python 's,它并不是世界末日,但从长远来看,它将为你节省大量时间。

2

您在Bullet.__init__方法中缺少下划线_。您当前有_init__,应该是__init__

这导致在Python调用Sprite.__init__ai_settings作为第一个参数,因为它不能找到任何对Bullet覆盖__init__。这会导致问题。

+0

就是这样!谢谢! –

+0

@MartinDimitrov考虑接受这个答案,如果它帮助你。 :) – Jokab

相关问题