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

马丁·迪米特罗夫(Martin Dimitrov)

我正在尝试构建一个游戏,该游戏使用箭头键左右移动船,并在按下空格键时发射子弹。当我按下空格键时,我的游戏崩溃了,并显示以下错误:Traceback(最近一次通话):

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
克里斯汀·迪恩(Christian Dean)

是的,乔卡布(Jokab)是对的,您忘记了额外的下划线。但是,对于以后的实践,重要的是学习阅读Python TrackBack通常,您可以很好地了解问题所在。例如,将TrackBack您粘贴到此处。Python首先告诉您它在运行时遇到问题run_game()因此python然后说您的游戏运行函数中,调用该方法存在问题gf.check_events(ai_settings, screen, ship, bullets)然后,它查看了您对bullet项目类的初始化,new_bullet = Bullet(ai_settings, screen, ship并且对此有问题。在下一行中,它给出了TypeError现在,尽管您可以弄清楚python在说什么TypeError,这是一个可行的选择。但是仅通过查看它,您就可以确定将子弹对象添加到sprites组有问题。这意味着,如果我是你,我将在Bullet班上开始搜索并且肯定您的__init__功能中有错别字

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

pygame帮助-TypeError:*之后的add()参数必须是序列,而不是pygame.Surface

Pygame给我TypeError:*之后的add()参数必须是序列,而不是将Ball添加到Sprite组时的Ball

*之后的add()参数必须是可迭代的,而不是int Pygame

熊猫drop_duplicates-TypeError:*之后的类型对象参数必须是序列,而不是映射

Python:TypeError:*之后的参数必须是序列

*之后的参数必须是可迭代的,而不是int

*之后的_reverse_with_prefix()参数必须是可迭代的,而不是int

**之后的MetaSerialisable对象参数必须是映射,而不是unicode

类型错误:float() 参数必须是字符串或数字,而不是“元组” ValueError:使用序列设置数组元素

*之后没有通过render()参数传递的参数必须是可迭代的,而不是pygame.Surface

strptime()参数1必须为str,而不是序列时间序列转换

Django rest框架`TypeError:**之后的类型对象参数必须是一个映射,而不是被抛出的int`

TypeError:**之后的jsonify()参数必须是映射,而不是使用Flask返回JSON的列表

*之后的/ polls / 1 / vote / _reverse_with_prefix()参数处的Django TypeError必须是可迭代的,而不是int

“* 之后的浮动对象参数必须是可迭代的,而不是浮动的”不知道我做错了什么

线程 thread-2 中的异常,TypeError:* 之后的 clickc() 参数必须是可迭代的,而不是 bool

Pygame不会让我画圆错误参数3必须是长度为2的序列,而不是4

pygame错误:参数必须是rectstyle对象的序列

** 后的参数必须是映射,而不是 ChatPermissions

嵌入:参数索引必须是张量,而不是列表

* 后的参数必须是 inter 能够,而不是浮动

参数(n_neighbors)的参数值必须为序列

为什么必须在“ UseRouting”之后而不是在“ UseAuthentication”之后放置“ UseAuthentication”?

TypeError:reversed()的参数必须是一个序列

django URL Reverse:reversed()的参数必须是一个序列

TypeError:decode()参数1必须为str,而不是None

TypeError:strptime()参数1必须是str,而不是Series

float()参数必须是字符串或数字,而不是'Timestamp'

TypeError:参数1必须是pygame.Surface,而不是方法