Pygame window not showing up

后端 未结 3 1250
后悔当初
后悔当初 2021-01-22 16:23

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 act

相关标签:
3条回答
  • 2021-01-22 17:00

    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.

    0 讨论(0)
  • 2021-01-22 17:06

    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()
    
    0 讨论(0)
  • 2021-01-22 17:09

    use fill before using draw.rect or bilt

    screen.fill(RED)
    pygame.draw.rect(screen, RED, (400, 400, 20, 20),0)
    
    0 讨论(0)
提交回复
热议问题