简体   繁体   English

Pygame黑屏-不会显示我写的内容

[英]Pygame Black Screen - Won't Show What I've Written

I have begun coding in with Pygame but everytime I run my script, the Pygame window appears completely black, even though I have filled the background with white and added a spider image onto it. 我已经开始使用Pygame进行编码,但是每次我运行脚本时,即使我用白色填充背景并在上面添加了蜘蛛图像,Pygame窗口也将显示为完全黑色。 Everytime I exit the window, the white background and image briefly appear, then the window closes. 每次退出窗口时,都会短暂出现白色背景和图像,然后窗口关闭。 I have searched on many help websites and all of them seem to reach the same conclusion about a certain block of code, even though I cannot see what is wrong with my version of that block. 我在许多帮助网站上进行了搜索,尽管我看不到该代码块的版本有什么问题,但所有这些网站似乎都对某个代码块得出了相同的结论。 Here is the entire of the code: 这是完整的代码:

import pygame

pygame.init()

display_width=800
display_height=600

black=(0,0,0)
white=255,255,255
red=(255,0,0)

gameDisplay=pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Spider")
clock=pygame.time.Clock()

spiderImg=pygame.image.load("newspid.png")

def spider(x,y):
    gameDisplay.blit(spiderImg,(x,y))

x=(display_width*0.45)
y=(display_height*0.8)


Dead=False
while not Dead:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            Dead=True

gameDisplay.fill(white)
spider(x,y)

pygame.display.update()
clock.tick(60)

pygame.quit()
quit()

The block of code I was talking about (The one that always seemed to be the problem) is this: 我正在谈论的代码块(似乎总是一个问题)是:

Dead=False
while not dead:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            Dead=True

I'm not sure if it has any relevance, but any advice would be useful. 我不确定是否有任何相关性,但是任何建议都会有用。 I'm very desperate for anything right now. 我现在非常渴望任何事情。 Thanks. 谢谢。

With this loop: 有了这个循环:

Dead=False
while not dead:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            Dead=True

you check for events until you recieve the QUIT event. 您会检查事件,直到收到QUIT事件为止。 After the loop breaks, you run the following code: 循环中断后,运行以下代码:

gameDisplay.fill(white)
spider(x,y)

pygame.display.update()
clock.tick(60)

pygame.quit()
quit()

Which fills the screen with white color and draws your image, and then your script ends. 它用白色填充屏幕并绘制图像,然后脚本结束。

You want to put the drawing part into the loop, too: 您也想将绘图部分放入循环中:

Dead=False
while not Dead:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            Dead=True

    gameDisplay.fill(white)
    spider(x,y)

    pygame.display.update()
    clock.tick(60)

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

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