1

I've seen a number of similar questions out there, but none of the solutions have worked for me. This is incredibly simple, so perhaps I'm overlooking something trivial.

My code (create a display window in pygame):

import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))

When I run this file from the terminal (python file_name.py) the entire screen freezes and I have to reboot (power off my virtual machine). However, if I write these lines of code in python within the terminal directly, the window pops open as it should. What's the deal with that?

What I've tried: Putting the last line a while loop, putting this into a main function, and using time.sleep() to delay before the program ends.

python 3.7.4, pygame 2.0.0, Debian 10.3.0 (running within virtualBox 6.1)

Otherness
  • 385
  • 2
  • 16

1 Answers1

1

Your application works well. However, you haven't implemented an application loop:

import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

run = True
while run:
    clock.tick(60)
    
    # handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # update game objects
    # [...]

    # clear display
    win.fill((0, 0, 0))

    # draw game objects
    # [...]

    # update display
    pygame.display.flip()

pygame.quit()

The typical PyGame application loop has to:

repl.it/@Rabbid76/PyGame-MinimalApplicationLoop See also Event and application loop

Rabbid76
  • 202,892
  • 27
  • 131
  • 174