简体   繁体   中英

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. 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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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