# alien_invasion.py
import sys
import pygame
def run_game():
# Initialize game and create a screen object.
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption("Alien Invasion")
# Set the background color.
bg_color = (230, 230, 230)
# Start the main loop for the game.
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen during each pass through the loop.
screen.fill(bg_color)
# Make the most recently drown screen visible.
pygame.display.flip()
run_game()
I am using Python 3.4.4 with the appropriate PyGame version. While this code runs, for some reason the application does not close. I have to close the IDLE shell itself in order to close the window. I'm not sure why the game doesn't close.
you should never use sys.quit to exit a program. You should let the program run to the end of the script. Your problem is you need pygame.quit() if i recall in combo with IDLE to quit properly.
running = True
while running:
...
if event.type == pygame.QUIT:
running = False
and then at the very end of the script put
pygame.quit()
I moved the loop in a file called "game_functions.py"
import sys
import pygame
def check_events(bValue, pygame):
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
bValue = False
I'm not sure where I should have passed the pygame object also, but I thought that was the way to get it to work. Here is my main file:
alien_invasion.py:
# alien_invasion.py
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
# Boolean value
b_is_running = True
# Initialize game and create a screen object.
pygame.init()
ai_settings = Settings()
screen_resolution = (ai_settings.screen_width, ai_settings.screen_height)
screen = pygame.display.set_mode(screen_resolution)
pygame.display.set_caption("Alien Invasion")
# Make a ship.
ship = Ship(screen)
# Start the main loop for the game.
while b_is_running:
gf.check_events(b_is_running, pygame)
gf.update_screen(ai_settings,screen,ship)
# Respond to keypresses and mouse events.
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# b_is_running = False
# Redraw the screen during each pass through the loop.
# screen.fill(ai_settings.bg_color)
# ship.blitme()
# Make the most recently drown screen visible.
# pygame.display.flip()
run_game()
pygame.quit()
The ship gets drawn, so the function update_screen works. It's the function check_events that does that seem to work. Any thoughts?
well i dont know what gf.check_events() is doing, so i couldnt say
(May-25-2018, 09:35 PM)metulburr Wrote: [ -> ]well i dont know what gf.check_events() is doing, so i couldnt say
gf.check_events(b_is_running, pygame) is basically the for loop I had in the while loop before. It was just moved into a function.
game_functions.py
import sys
import pygame
def check_events(bValue, game):
"""Respond to keypresses and mouse events."""
for event in game.event.get():
if event.type == game.QUIT:
bValue = False
def update_screen(ai_settings, screen, ship):
screen.fill(ai_settings.bg_color)
ship.blitme()
pygame.display.flip()
alien_invasion.py
# alien_invasion.py
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
# Boolean value
b_is_running = True
# Initialize game and create a screen object.
pygame.init()
ai_settings = Settings()
screen_resolution = (ai_settings.screen_width, ai_settings.screen_height)
screen = pygame.display.set_mode(screen_resolution)
pygame.display.set_caption("Alien Invasion")
# Make a ship.
ship = Ship(screen)
# Start the main loop for the game.
while b_is_running:
# gf.check_events(b_is_running, pygame)
# Respond to keypresses and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
b_is_running = False
gf.update_screen(ai_settings,screen,ship)
# Redraw the screen during each pass through the loop.
# screen.fill(ai_settings.bg_color)
# ship.blitme()
# Make the most recently drown screen visible.
# pygame.display.flip()
run_game()
pygame.quit()
Sorry didnt see it there. If your going to put the event loop in a function then you need to return bValue to update b_is_running value. A better method would be to use a control class and just pass that same value around as an attribute to avoid function parameters all over the place.
(May-25-2018, 09:55 PM)metulburr Wrote: [ -> ]Sorry didnt see it there. If your going to put the event loop in a function then you need to return bValue to update b_is_running value. A better method would be to use a control class and just pass that same value around as an attribute to avoid function parameters all over the place.
Yeah, you're right. I'll set one up. I have done that a lot in languages like C++ and C#, etc. Python's taking some getting used to.
try python's in_built function exit(), that should work