b0b75
b0b75

Reputation: 141

Pygame window not showing up

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.

import pygame, sys
from pygame.locals import*

pygame.init()
SCREENWIDTH = 800
SCREENHEIGHT = 800
RED = (255,0,0)
screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))

pygame.draw.rect(screen, RED, (400, 400, 20, 20),0)
screen.fill(RED)




pygame.display.update()

Upvotes: 4

Views: 11986

Answers (5)

coffay
coffay

Reputation: 1

If you're working on pycharm, just make sure you're running 'current file' instead of main or configurations. Rookie mistake, but most common.

Upvotes: 0

Daniel
Daniel

Reputation: 1

Ok, to fix this problem go into any text editor (IDLE will do fine, so will notepad) Type the following code:

YourFileName.py

pause

Then, click on 'file' then click on 'save as' and a screen should pop up asking you to name it. Name it: YourFileName.bat
Save it in a folder that you can easily open

the way that you open the screen with PyGame is simply by double clicking the file I just showed you how to create. Then it will immediately run your code in PyGame.

Upvotes: 0

kavin raja
kavin raja

Reputation: 1

use fill before using draw.rect or bilt

screen.fill(RED)
pygame.draw.rect(screen, RED, (400, 400, 20, 20),0)

Upvotes: 0

Ismael Padilla
Ismael Padilla

Reputation: 5566

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:

import pygame, sys
from pygame.locals import*

pygame.init()
SCREENWIDTH = 800
SCREENHEIGHT = 800
RED = (255,0,0)
screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))

pygame.draw.rect(screen, RED, (400, 400, 20, 20),0)
screen.fill(RED)

pygame.display.update()

# waint until user quits
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

pygame.quit()

The loop in the end will ensure that the window remains open until the user closes it.

Upvotes: 3

Anuradha
Anuradha

Reputation: 590

Please check this solution.

import pygame, sys
from pygame.locals import *

pygame.init()
SCREENWIDTH = 800
SCREENHEIGHT = 800
RED = (255, 0, 0)
screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))

while True:
    pygame.draw.rect(screen, RED, (400, 400, 20, 20), 0)
    screen.fill(RED)
    pygame.display.update()

Upvotes: 1

Related Questions