*之后的add()参数必须是序列,而不是设置

时间:2016-07-29 12:19:24

标签: python pygame sprite 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

2 个答案:

答案 0 :(得分:2)

您的<div ng-controller="UserCtrl"> <h3>Click row to revel last name!</h3> <table class="table table-condensed"> <thead> <td>First Name</td> </thead> <tbody ng-repeat="user in users" on-finish-render> <tr> <td><a data-toggle="collapse" data-ng-href="#{{user.id}}" data-target="#{{user.id}}">{{user.firstName}}</a></td> </tr> <tr> <td class="hiddenRow"> <div id="{{user.id}}" class="collapse"> <h4>{{user.lastName}}</h4> </div> </td> </tr> </tbody> </table> 方法中缺少一个下划线_。您目前的Bullet.__init__应为_init__

这导致Python使用__init__作为第一个参数调用Sprite.__init__方法,因为它找不到ai_settings的任何重写__init__。这会导致问题。

答案 1 :(得分:1)

是Jokab是对的,你忘记了额外的下划线。但是,对于将来的练习,学习阅读Python TrackBack非常重要。它通常可以让您很好地了解问题所在。例如,请点击此处粘贴的TrackBack。 Python首先告诉你它运行run_game()时出现问题。所以python然后说 你的游戏运行函数调用方法gf.check_events(ai_settings, screen, ship, bullets)时遇到问题。然后,它查看了子弹类new_bullet = Bullet(ai_settings, screen, ship的初始化,并且遇到了问题。在接下来的一行中它是TypeError。现在,虽然你可以弄清楚python正在说什么TypeError,这是一个可行的选择。但只是从查看它可以确定将子弹对象添加到精灵组时出现问题。这意味着,如果我是你,我会在Bullet课程中开始我的搜索。确实,__init__函数中存在拼写错误。

如果你不学习如何阅读python TrackBack,这不是世界末日,从长远来看,它将为你节省大量时间。