Temp Estcrowd
Temp Estcrowd

Reputation: 73

Cannot Fire Bullets, Program closes

I'm beginner in Python and working on a space invader game. I want to make the ship fire bullets but when I press the key for firing, no bullets are shot. After multiple presses, the program closes.

AlienGame.py

import sys
import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
import game_functions as gf


def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("TemplAReeb's First Game")
    ship = Ship(ai_settings, screen)
    bullets = Group()
    bg_color = (230, 230, 230)

   while True:
        gf.check_events(ship, bullets)
        ship.update()
        bullets.update()
        gf.update_screen(ai_settings, screen, ship, bullets)
        screen.fill(bg_color)
        ship.blitme()
        pygame.display.flip()
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()
        screen.fill(bg_color)
        ship.blitme()
        pygame.display.flip()
run_game()

gamefunctions.py

import sys
import pygame
from bullet import Bullet
def check_keydown_events (event, bullets):
    if event.key == pygame.K_SPACE:
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)

def check_events(ship, bullets):
    keys = pygame.key.get_pressed()
    ship.moving_right = keys[pygame.K_RIGHT]
    ship.moving_left = keys[pygame.K_LEFT]
    for event in pygame.event.get():
        if event.type ==  pygame.KEYDOWN:
         check_keydown_events(event, bullets)


def update_screen(ai_settings, screen, ship, bullets):
    bg_color = (230, 230, 230)
    screen.fill(bg_color)
    ship.blitme()
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    pygame.display.flip()

I'm getting the error that ai_settings in not defined

Traceback (most recent call last):
  File "C:/Users/Areeb Irfan/.PyCharmCE2018.3/config/scratches/AlienGame.py", line 32, in <module>
run_game()
  File "C:/Users/Areeb Irfan/.PyCharmCE2018.3/config/scratches/AlienGame.py", line 19, in run_game
gf.check_events(ship, bullets)
  File "C:\Users\Areeb Irfan\.PyCharmCE2018.3\config\scratches\game_functions.py", line 16, in check_events
check_keydown_events(event, bullets)
  File "C:\Users\Areeb Irfan\.PyCharmCE2018.3\config\scratches\game_functions.py", line 7, in check_keydown_events
new_bullet = Bullet(ai_settings, screen, ship)
NameError: name 'ai_settings' is not defined

Upvotes: 0

Views: 108

Answers (1)

Blckknght
Blckknght

Reputation: 104762

Your error is coming from this line in check_keydown_events:

    new_bullet = Bullet(ai_settings, screen, ship)

You're trying to pass ai_settings to the Bullet constructor, but it's not defined in the current check_keydown_events function's namespace. Unlike update_screen, you're not passing ai_settings in as an argument.

You probably need to fix that. You already know how, since you're already passing ai_settings to update_screen (where it's not currently used, did you perhaps add it to the wrong place?). You just need to extend it to check_events and check_keydown_events.

Upvotes: 1

Related Questions