Python Forum
[PyGame] PyGame does not close when I press the close button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] PyGame does not close when I press the close button
#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?
Reply


Messages In This Thread
RE: PyGame does not close when I press the close button - by Midnight_Sparkle3344 - May-25-2018, 08:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  cant make a door automatically close a few seconds after i open it in pygame cooImanreebro 2 2,270 Dec-15-2020, 08:40 PM
Last Post: michael1789
  PyGame: detecting key press, but not key qrani 2 3,994 Sep-28-2018, 11:08 PM
Last Post: qrani

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020