Tad Perry
Tad Perry

Reputation: 1

pygame crashes when i try to resize window

This exact question was asked on this forum last April. However, the only answer was: "Your script works on my system, check and make sure that you have the pygame module intended for your version of python." So I checked this first and it checked out. (I have Python 2.7 and Pygame Version 1.9.1 release for Python 2.7.)

So it's not that, but the following code produces the same error the other person reported:

This application has requested the runtime to terminate it in an unusual way. Please contact the application's support team for more info.

I wish the original poster had said what they did to fix it, because I'm stumped. Note that I do NOT have administrator rights on the machine I'm trying to run this on. Could that be the problem?

The script is below. (This was taken directly from the book Beginning Game Development with Python and Pygame.) It runs right up to the instant you try to resize the window.

background_image_filename = 'sushiplate.jpg'

import pygame
from pygame.locals import *
from sys import exit

SCREEN_SIZE = (640, 480)

pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE, 32)

background = pygame.image.load(background_image_filename).convert()

while True:

    event = pygame.event.wait()
    if event.type == QUIT:
        exit()
    if event.type == VIDEORESIZE:
        SCREEN_SIZE = event.size
        screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE, 32)
        pygame.display.set_caption("Window resized to "+str(event.size))

    screen_width, screen_height = SCREEN_SIZE
    for y in range(0, screen_height, background.get_height()):
        for x in range(0, screen_width, background.get_width()):
            screen.blit(background, (x, y))

    pygame.display.update()

Upvotes: 0

Views: 699

Answers (2)

ninMonkey
ninMonkey

Reputation: 7511

Warning, you are drawing for every event fired. You instead want:

while True:

    for event in pygame.event.get():

        if event.type == QUIT:
            exit()
        elif event.type == VIDEORESIZE:
            # ...
        elif event.type == KEYDOWN:
            # ...
    # draw.
    pygame.display.update()

This next code doesn't make sense: What are you trying to do?

screen_width, screen_height = SCREEN_SIZE
for y in range(0, screen_height, background.get_height()):
    for x in range(0, screen_width, background.get_width()):
        screen.blit(background, (x, y))

pygame.display.update()

Upvotes: 1

Mike Housky
Mike Housky

Reputation: 4069

Try removing the color depth on the resize mode setting:

screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE)

I get the same error as you with the color depth left in, and it goes away when I remove it. I don't know why it would fail, but there's no official documentation at all on the VIDEORESIZE event, other than the Event object has size, w and h fields set to something. (Checking this is difficult right now because pygame.org is down right now, reportedly due to a server RAID error, but you can Google for the information, and view Cached pages.)

Upvotes: 0

Related Questions