I recently downloaded pygame on my Mac, but for some reason, the window does not pop up with the screen or anything. I don’t get an error message, it just runs but doesn’t actually pop up a display.
JavaScript
x
17
17
1
import pygame, sys
2
from pygame.locals import*
3
4
pygame.init()
5
SCREENWIDTH = 800
6
SCREENHEIGHT = 800
7
RED = (255,0,0)
8
screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
9
10
pygame.draw.rect(screen, RED, (400, 400, 20, 20),0)
11
screen.fill(RED)
12
13
14
15
16
pygame.display.update()
17
Advertisement
Answer
Your code is currently starting, drawing a red rectangle in a window, and then ending immediatly. You should probably wait for the user to quit before closing the window. Try the following:
JavaScript
1
23
23
1
import pygame, sys
2
from pygame.locals import*
3
4
pygame.init()
5
SCREENWIDTH = 800
6
SCREENHEIGHT = 800
7
RED = (255,0,0)
8
screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
9
10
pygame.draw.rect(screen, RED, (400, 400, 20, 20),0)
11
screen.fill(RED)
12
13
pygame.display.update()
14
15
# waint until user quits
16
running = True
17
while running:
18
for event in pygame.event.get():
19
if event.type == pygame.QUIT:
20
running = False
21
22
pygame.quit()
23
The loop in the end will ensure that the window remains open until the user closes it.