简体   繁体   English

python pygame屏幕显示黑色

[英]python pygame screen displays black

I am new to python and made an algorithm I would like to visualize on a screen with pygame. 我是python的新手,做了一个我想在pygame的屏幕上可视化的算法。 This is my code: 这是我的代码:

import pygame

pygame.init()

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

pygame.display.set_caption("Test screen")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()


pygame.quit()

But this always displays a black or kinda dark gray screen. 但这总是显示黑色或有点深灰色的屏幕。 Does anyone know what I'm doing wrong? 有人知道我在做什么错吗? I have tried multiple tutorials and they all give me the same screen. 我尝试了多个教程,它们都给了我相同的屏幕。

I am using MacOS 10.14 我正在使用MacOS 10.14

You need to call the fill function on the win object. 您需要在win对象上调用fill函数。 The background is always going to be black by default. 默认情况下,背景始终为黑色。 The below example shows how to turn it to white. 下例显示了如何将其变为白色。

import pygame

pygame.init()

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

pygame.display.set_caption("Test screen")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True
while run:
    pygame.time.delay(100)
    ## enter color here
    win.fill((255,255,255))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()


pygame.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM