简体   繁体   中英

Python pygame window keeps crashing

Whenever I run my code the Python Window that shows up does not respond.
Is there something wrong with my code or do I have to re-install pygame and python?
I get a black pygame window and then it turns white and says not responding?
Also I am new to this so please make this as simple as possible. I tried looking everywhere for the answer but could not get it in a way that I could understand.
Please help me out. Thanks :)

1 - Import library

import pygame
from pygame.locals import *

2 - Initialize the game

pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))

3 - Load Images

player = pygame.images.load("resources/images/dude.png")

4 - keep looping through

while 1:
    # 5 - clear the screen before drawing it again
    screen.fill(0)
    # 6 - draw the screen elements
    screen.blit(player, (100,100))
    # 7 - update the screen
    pygame.display.flip()
    # 8 - loop through the events
    for event in pygame.event.get():
        # check if the event is the X button
        if event.type==pygame.QUIT:
            # if it is quit the game
            pygame.quit()
            exit(0)

Don't import pygame.locals . It is actually unnecessary, since you are already importing pygame .

Also, as @furas said, it should be:

player = pygame.image.load("resources/images/dude.png")

Not:

player = pygame.images.load("resources/images/dude.png")

This will clear up some of the problems in your code.

From my personal experience,if you run pygame code from IDLE it often does not respond at all.Try saving your project as a .py file and then run it with python.exe.It always works for me. And as furas said use

player = pygame.image.load("resources/images/dude.png")

instead of

player = pygame.images.load("resources/images/dude.png")

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