Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] PyGame does not close when I press the close button
#1
# 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.
Quote
#2
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()
Quote
#3
(May-25-2018, 05:05 PM)metulburr Wrote: 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()

Thank you. That worked.
Quote
#4
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?
Quote
#5
well i dont know what gf.check_events() is doing, so i couldnt say
Quote
#6
(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()

Quote
#7
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.
Quote
#8
(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.
Quote
#9
here is an example in a small finished game
https://github.com/metulburr/FloodIt/blo...control.py
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  PyGame: detecting key press, but not key qrani 2 140 Sep-28-2018, 11:08 PM
Last Post: qrani
  [PyGame] Problem importing pygame / installing pygame Klar 4 1,319 Dec-16-2017, 05:48 PM
Last Post: Klar
  [Pygame] Avoid SentDex pygame tutorials georgecoopers 1 1,180 Apr-09-2017, 12:40 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)