Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame sceen.fill()未正确填充颜色_Python_Pygame - Fatal编程技术网

Python Pygame sceen.fill()未正确填充颜色

Python Pygame sceen.fill()未正确填充颜色,python,pygame,Python,Pygame,我刚开始学习Pygame。我正在学习教程。 我运行了以下程序,但它显示黑色而不是蓝色: import pygame h = input("Enter the height of the window : ") w = input("Enter the width of the window : ") screen = pygame.display.set_mode((w,h)) running = True while running: event = pygame.event.po

我刚开始学习Pygame。我正在学习教程。 我运行了以下程序,但它显示黑色而不是蓝色:

import pygame

h = input("Enter the height of the window : ")
w = input("Enter the width of the window : ")
screen = pygame.display.set_mode((w,h))

running = True
while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running=0

screen.fill((0,0,1))
pygame.display.flip()

对于蓝色,应该使用
255
作为元组中的第三个元素,而不是
1

范例-

while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running=0
    screen.fill((0,0,255))
    pygame.display.flip()

它显示为黑色,因为您在游戏循环后运行screen.fill()。这意味着它只会在你玩完之后填满屏幕。 最好的做法是把它放在循环中的某个地方,然后你想画的任何精灵都应该画出来——这样,如果精灵移动,它原来的位置就会被背景着色

另外,正如其他答案中提到的:(0,0,1)是黑色的,带有最轻微的蓝色。通过谷歌搜索“rgb颜色选择器”,可以找到一个好的rgb颜色选择器

最后,您应该将“event=pygame.event.poll()”更改为“pygame.event.get()中的事件”,如图所示,因为这将允许它同时检测多个事件。显然,这个例子没有必要,但我假设它不会被作为一个只能关闭的蓝屏

import pygame

pygame.init()

h = input("Enter the height of the window : ")
w = input("Enter the width of the window : ")
screen = pygame.display.set_mode((w,h))

running = True
while running:
    for event in pygame.event.poll():
        if event.type == pygame.QUIT:
            running=0
        #any additional event checks
    screen.fill((0,0,255))
    #any additional drawings
    pygame.display.flip()

pygame.quit()

你没有用任何颜色填充-我认为黑色是默认颜色。display.set()只需设置屏幕所需的大小。填充(颜色)即可设置背景。很抱歉,出现了这个错误。在我的计算机上编写的程序是正确的,但不起作用。您是否按照我在回答中所说的填写了
255
?谢谢,它起作用了。我把它当作一个opengl程序。很高兴它能为你工作。此外,我想建议你接受这个答案,如果它对你有帮助的话。这对社区是有帮助的,但这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论。