Python.exe在交互式shell中运行时没有响应,与pygame一起使用

时间:2013-10-17 19:40:12

标签: python animation pygame

我正在使用默认的Python IDLE和Pygame制作游戏。我正在创建一个简单的猫动画,但是当我尝试运行模块时出现问题。刚出现黑屏,标题只是“动画不响应”,下面我列出了用于此动画的代码。谢谢您的帮助! 谢谢,只是编辑它,这看起来更好吗?

import pygame, sys
from pygame.locals import *

pygame.init()

FPS = 30
fpsClock = pygame.time.Clock()

# set up the window
DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption('Animation')

WHITE = (255, 255, 255)
catImg = pygame.image.load('cat.png')
catx = 10
caty = 10
direction = 'right'

while True: # the main game loop
    DISPLAYSURF.fill(WHITE)

    if direction == 'right':
        catx += 5
        if catx == 280:
            direction = 'down'
    elif direction == 'down':
        caty += 5
        if caty == 220:
            direction = 'left'
    elif direction == 'left':
        catx -= 5
        if caty == 10:
            direction = 'up'
    elif direction == 'up':
        caty -= 5
        if caty == 10:
            direction = 'right'
            DISPLAYSURF.blit(catImg, (catx, caty))

            for event in pygame.eventget():
                if event.type == QUIT:
                    pygame.exit()
                    sys.exit()

            pygame.display.update()
            fpsClock.tick(FPS)

1 个答案:

答案 0 :(得分:1)

您可以针对拼写错误重新编辑帖子,最后会有一个链接。

问题是事件处理永远不会运行,除非direction == 'up'caty == 10。然后窗口停止响应,因为它无法获取消息。

while True: # the main game loop
    # events 
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.exit()
            sys.exit()

    # movement
    # ... snip ...

    # drawing
    DISPLAYSURF.fill(Color("white"))

    DISPLAYSURF.blit(catImg, (catx, caty))

    pygame.display.update()
    fpsClock.tick(FPS)