python - * 之后的 add() 参数必须是序列,而不是 Settings

标签 python pygame sprite addition

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

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

最佳答案

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

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

关于python - * 之后的 add() 参数必须是序列,而不是 Settings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38658684/

相关文章:

python - 如何在 Python 中查找开放阅读框

python - 切片 3d numpy 数组 - 误解

python - 如何在 matplotlib 中将图形保存为 pdf 作为光栅图像

python - Pygame 中的白盒

css - Sprite 淡入淡出不适用于 CSS

python - 相对导入的问题

python - Blitting 背景时 PyGame 滞后

python - 如何在通过检查第一个 pygame.MOUSEBUTTONDOWN 开始的循环内检测第二个 pygame.MOUSEBUTTONDOWN

graphics - 如何为不同分辨率的屏幕生成 Sprite 艺术资源?

swift - 如何在 Swift 3.0 中拖放 Sprite ?