2013-10-17 22 views
1

我正在使用默認的Python IDLE和Pygame進行遊戲。我正在創建一個簡單的貓動畫,但是當我嘗試運行該模塊時出現問題。一個黑色的屏幕出現,標題只是說'動畫無響應',下面我列出了用於這個動畫的代碼。謝謝您的幫助! 謝謝,剛剛編輯它,這看起來更好嗎?Python.exe在交互式shell中運行時沒有響應,與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

請修復您的縮進。 – Michael0x2a

+0

我看不到任何問題,順便說一句,我是新的。請指出在哪裏?:) – PixelPuppet

+0

如果你看看你的問題,你可以看到所有的選項卡/縮進缺失,使您的代碼無法運行。如果無法運行代碼,任何人都無法幫助您。 (你可以點擊問題左下角的「編輯」來修改你的帖子。) – Michael0x2a

回答

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) 
相關問題