Python Forum
Pygame freezes after certain time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame freezes after certain time
#1
Hi! Trying to make a previz window for testing a puzzle game while in development, but it keeps freezing after certain time, I am very new to pygame, so not sure what's wrong! Thanks
import pygame
from random import randint

font_color = (0, 255, 200)
bkg_color = (0, 0, 0)
ltr_range = 10
gap = 20
cube_size = 800
speed = 1

matrix = [[chr(65+x+y) for y in range(ltr_range)] for x in range(ltr_range)]


def draw_matrix():
    for x in range(ltr_range):
        for y in range(ltr_range):
            win.blit(font.render(matrix[x][y], True, font_color),
                     (gap + (cube_size // ltr_range * y), gap + (cube_size // ltr_range * x)))
    pygame.display.update()


def matrix_row_turn_right(line=0, steps=0):
    empty = matrix[line][-steps:]
    for x in range(len(matrix[line]) - steps - 1, -1, -1):
        matrix[line][x + steps] = matrix[line][x]
    for y in range(len(empty)):
        matrix[line][y] = empty[y]


def matrix_col_turn_up(line=0, steps=0):
    empty = [matrix[x][line] for x in range(steps)]
    for y in range(len(matrix) - steps):
        matrix[y][line] = matrix[y + steps][line]
    for z in range(len(matrix) - steps, len(matrix)):
        matrix[z][line] = empty[abs(len(matrix) - steps - z)]


def anim_move_right(line):
    for x in range(1, cube_size // ltr_range):
        pygame.draw.rect(win, bkg_color, (0, (cube_size // ltr_range * line),
                                          cube_size, cube_size // ltr_range))
        for y in range(ltr_range):
            win.blit(font.render(matrix[line][y], True, font_color),
                     ((gap + (cube_size // ltr_range * y) + x),
                      gap + (cube_size // ltr_range * line)))
        win.blit(font.render(matrix[line][-1], True, font_color),
                 ((0 - (cube_size // ltr_range) + gap + x), gap + (cube_size // ltr_range * line)))
        pygame.time.delay(speed)
        pygame.display.update()
    matrix_row_turn_right(line, 1)
    draw_matrix()


def anim_move_up(line):
    draw_matrix()
    for x in range(1, cube_size // ltr_range):
        pygame.draw.rect(win, bkg_color, ((gap + (cube_size // ltr_range) * line),
                                          0, cube_size // ltr_range, cube_size))
        for y in range(ltr_range):
            win.blit(font.render(matrix[y][line], True, font_color),
                     (
                     gap + cube_size // ltr_range * line, (gap + (cube_size // ltr_range * y) - x)))
        win.blit(font.render(matrix[0][line], True, font_color),
                 (gap + (cube_size // ltr_range * line), (cube_size + gap - x)))
        pygame.time.delay(speed)
        pygame.display.update()
    matrix_col_turn_up(line, 1)
    draw_matrix()


pygame.init()
win = pygame.display.set_mode((cube_size, cube_size))
pygame.display.set_caption("Word Puzzle preViz")
font = pygame.font.SysFont('Ariel', cube_size // ltr_range)

run = True
while run:
    draw_matrix()
    pygame.time.delay(1000)
    for i in range(100):
        if i % 2 == 0:
            anim_move_right(randint(0, ltr_range-1))
        else:
            anim_move_up(randint(0, ltr_range-1))
    run = False

pygame.quit()
:)
Reply
#2
what's the objective of the game? I ran your script 3 times without any problems.
I'm using linux. I put in a print statement counting the loops thinking if it was odd it would be a up animation and even left to right, but it didn't hang or crash.
GJG likes this post
Reply
#3
For some reason it freezes on Windows, I'll check on linux later, but it worked once I used a while loop instead of iterator and added event check. It's a simple letter puzzle game for assembling words, kind of like 2D rubik's cube.
count = 0
while count < 100:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    if count % 2 == 0:
        anim_move_right(randint(0, ltr_range - 1))
    else:
        anim_move_up(randint(0, ltr_range - 1))
    count += 1

pygame.quit()
Thanks for checking!
Reply
#4
Quote:run = True
while run:
draw_matrix()
pygame.time.delay(1000)
for i in range(100):
if i % 2 == 0:
anim_move_right(randint(0, ltr_range-1))
else:
anim_move_up(randint(0, ltr_range-1))
run = False


pygame.quit()

It's because your loop is for i in range 100. So it's making 100 moves then then sets run to false. I don't know how that could have been different in linux.

Your program works fine! lol
Reply
#5
It ran fine for me on windows 11 with python 3.11. That is until I started clicking the mouse. If I click the mouse the application hangs and then crashes.

The problem is that your program doesn't process events. This makes windows thing the program is unresponsive, and eventually it shuts the window down. I replaced your delay calls with a function that gets events.
import pygame
from random import randint
 
font_color = (0, 255, 200)
bkg_color = (0, 0, 0)
ltr_range = 10
gap = 20
cube_size = 800
speed = 1
 
matrix = [[chr(65+x+y) for y in range(ltr_range)] for x in range(ltr_range)]
 
 
def draw_matrix():
    for x in range(ltr_range):
        for y in range(ltr_range):
            win.blit(font.render(matrix[x][y], True, font_color),
                     (gap + (cube_size // ltr_range * y), gap + (cube_size // ltr_range * x)))
    pygame.display.update()
 
 
def matrix_row_turn_right(line=0, steps=0):
    empty = matrix[line][-steps:]
    for x in range(len(matrix[line]) - steps - 1, -1, -1):
        matrix[line][x + steps] = matrix[line][x]
    for y in range(len(empty)):
        matrix[line][y] = empty[y]
 
 
def matrix_col_turn_up(line=0, steps=0):
    empty = [matrix[x][line] for x in range(steps)]
    for y in range(len(matrix) - steps):
        matrix[y][line] = matrix[y + steps][line]
    for z in range(len(matrix) - steps, len(matrix)):
        matrix[z][line] = empty[abs(len(matrix) - steps - z)]
 
 
def anim_move_right(line):
    for x in range(1, cube_size // ltr_range):
        pygame.draw.rect(win, bkg_color, (0, (cube_size // ltr_range * line),
                                          cube_size, cube_size // ltr_range))
        for y in range(ltr_range):
            win.blit(font.render(matrix[line][y], True, font_color),
                     ((gap + (cube_size // ltr_range * y) + x),
                      gap + (cube_size // ltr_range * line)))
        win.blit(font.render(matrix[line][-1], True, font_color),
                 ((0 - (cube_size // ltr_range) + gap + x), gap + (cube_size // ltr_range * line)))
        pause(speed)
        pygame.display.update()
    matrix_row_turn_right(line, 1)
    draw_matrix()
 

def pause(msec):
    for event in pygame.event.get():
        pass
    pygame.time.delay(msec)

def anim_move_up(line):
    draw_matrix()
    for x in range(1, cube_size // ltr_range):
        pygame.draw.rect(win, bkg_color, ((gap + (cube_size // ltr_range) * line),
                                          0, cube_size // ltr_range, cube_size))
        for y in range(ltr_range):
            win.blit(font.render(matrix[y][line], True, font_color),
                     (
                     gap + cube_size // ltr_range * line, (gap + (cube_size // ltr_range * y) - x)))
        win.blit(font.render(matrix[0][line], True, font_color),
                 (gap + (cube_size // ltr_range * line), (cube_size + gap - x)))
        pause(speed)
        pygame.display.update()
    matrix_col_turn_up(line, 1)
    draw_matrix()
 
 
pygame.init()
win = pygame.display.set_mode((cube_size, cube_size))
pygame.display.set_caption("Word Puzzle preViz")
font = pygame.font.SysFont('Ariel', cube_size // ltr_range)
 
run = True
while run:
    draw_matrix()
    pause(1000)
    for i in range(100):
        if i % 2 == 0:
            anim_move_right(randint(0, ltr_range-1))
        else:
            anim_move_up(randint(0, ltr_range-1))
    run = False
 
pygame.quit()
XavierPlatinum likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My game's code works but Python still freezes at random intervals. game_slayer_99 13 8,128 Dec-09-2021, 11:23 AM
Last Post: metulburr
  [PyGame] Pygame.time.wait is not behaving as expected zootechdrum 2 3,177 Mar-19-2019, 03:27 AM
Last Post: Windspar

Forum Jump:

User Panel Messages

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