2016-07-29 3 views
2

우주선을 화살표 키로 좌우로 움직이고 스페이스 바를 누르면 총알이 발사되는 게임을 만들려고합니다. 나는 스페이스 바를 내 게임 충돌을 누르면이 오류가 표시되면 : 역 추적 (가장 최근 통화 마지막) : 주요 파일을 마지막으로add() 인수 후 *는 시퀀스가 ​​아니어야합니다. 설정

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) 

그리고 :

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

내 코드입니다

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 을 읽는 것을 배우는 것이 중요합니다. 일반적으로 문제의 위치를 ​​알려줍니다. 예를 들어 여기에 붙여 넣은 을 가져 가세요. 파이썬은 먼저 문제가 있다고 알려줍니다. run_game(). 그래서 파이썬은 에 넣고 게임을 실행하면 함수가 gf.check_events(ai_settings, screen, ship, bullets)이라는 문제가 발생한다고합니다. 그런 다음 총알 클래스 new_bullet = Bullet(ai_settings, screen, ship의 초기화를 살펴본 후 문제가 있습니다. 바로 다음 줄에는 TypeError이 있습니다. 이제는 파이썬이 실행 가능한 옵션 인 TypeError에 대해 정확히 무엇을 말하고 있는지 알아낼 수 있습니다. 그러나 그것을 보면서 sprites 그룹에 총알 개체를 추가 할 때 문제가 있음을 확인할 수 있습니다. 그 말은 내가 너라면, Bullet 클래스에서 검색을 시작할 것임을 의미한다. 그리고 확실히 충분히 __init__ 기능에 오자가 있습니다.

파이썬 을 읽는 법을 배우지 않으면 세계의 끝은 아니지만 장기적으로는 많은 시간을 절약 할 수 있습니다.

2

Bullet.__init__ 방법에 밑줄이 누락되었습니다 (_). __init__ 일 때 현재 _init__입니다.

이가 어떤 Bullet에 대한 __init__를 오버라이드 (override) 찾을 수 없기 때문에, 첫 번째 인수로 ai_settingsSprite.__init__ 메소드를 호출 파이썬을 초래한다. 그로 인해 문제가 발생합니다.

+0

그게 전부 야! 고맙습니다! –

+0

@MartinDimitrov 도움이 되었으면이 대답을 수락하십시오. :) – Jokab