Python pygame窗口不断崩溃

时间:2013-11-24 17:38:39

标签: python crash pygame

每当我运行我的代码时,显示的Python窗口都没有响应。
我的代码有问题还是我必须重新安装pygame和python?
我得到一个黑色的pygame窗口,然后它变成白色,说不响应?
我也是新手,所以请尽可能简单。我试着到处寻找答案,但无法以我能理解的方式得到答案。
请帮帮我。谢谢:))

1 - 导入库

import pygame
from pygame.locals import *

2 - 初始化游戏

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

3 - 加载图像

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

4 - 继续循环

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)

2 个答案:

答案 0 :(得分:2)

不要导入pygame.locals。实际上这是不必要的,因为您已经导入了pygame

另外,正如@furas所说,它应该是:

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

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

这将清除代码中的一些问题。

答案 1 :(得分:1)

根据我的个人经验,如果您从IDLE运行pygame代码,它通常根本不响应。尝试将项目保存为.py文件,然后使用python.exe运行它。它总是适用于我。 并且正如furas所说的那样使用

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

而不是

player = pygame.images.load("resources/images/dude.png")
相关问题