user7149448
user7149448

Reputation:

Pygame script stops responding

I'm making a simple game with pygame, but when I try to run it, it just crashes immidiedly. I'm not sure if the game loop is causing it or what. Here is my current code:

import pygame, sys
from pygame import *
pygame.init()

FPS = 30
fpsClock = pygame.time.Clock()

game_window = pygame.display.set_mode((640, 360))
pygame.display.set_caption('The Old House 0.1')

black = (0, 0, 0)
white = (255, 255, 255)
draw_rect = pygame.draw.rect
window_center = (320, 180)
player_img = pygame.image.load('player.png')
keys = pygame.KEYDOWN
event_get = pygame.event.get()
player_x = 320
player_y = 180

while True:
    game_window.fill(black)
    draw_rect(game_window, white, (195, 55, 250, 250))
    for event in event_get:
        if event.type == keys:
            if event.key == pygame.K_w:
                player_y += 10
            elif event.key == pygame.K_s:
                player_y -= 10
            elif event.key == pygame.K_d:
                player_x += 10
            elif event.key == pygame.K_a:
                player_x -= 10
        elif event.type == QUIT:
            pygame.quit()
            sys.exit()

    game_window.blit(player_img, (player_x, player_y))

    pygame.display.update()
    fpsClock.tick(FPS)

And if you are wondering, I do not get any error messages, the game window just stops responding. Also the IDE I'm using (Sublime Text 3) outputs the location of the game file and my PATH.

Upvotes: 3

Views: 249

Answers (1)

CodeSurgeon
CodeSurgeon

Reputation: 2465

When I ran your code with some placeholder graphics, the window would display, then shortly afterwards freeze up as "Not responding." It turns out that you need to fetch the list of pygame events every frame, as opposed to calling pygame.event.get() once at the beginning. This makes sense, since you need to respond to mouse clicks, keyboard presses, and window closing throughout the life of your game, not just in the first frame. Making this fix, here is the working code:

import pygame, sys
from pygame import *
pygame.init()

FPS = 30
fpsClock = pygame.time.Clock()

game_window = pygame.display.set_mode((640, 360))
pygame.display.set_caption('The Old House 0.1')

black = (0, 0, 0)
white = (255, 255, 255)
window_center = (320, 180)
player_img = pygame.image.load('player1.png')
player_x = 320
player_y = 180

while True:
    game_window.fill(black)
    pygame.draw.rect(game_window, white, (195, 55, 250, 250))
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                player_y += 10
            elif event.key == pygame.K_s:
                player_y -= 10
            elif event.key == pygame.K_d:
                player_x += 10
            elif event.key == pygame.K_a:
                player_x -= 10
        elif event.type == QUIT:
            pygame.quit()
            sys.exit()

    game_window.blit(player_img, (player_x, player_y))
    pygame.display.update()
    fpsClock.tick(FPS)

Also you might be surprised that your up and down are flipped! In pygame, (0, 0) corresponds to the top left corner of your window.

Upvotes: 2

Related Questions