2013-05-15 61 views
0

我在pygame ware中製作了一個程序,我的龜在sqare中運行,但是當你在框中點擊時他會爆炸,但是當我將圖像更改爲爆炸時,它首先顯示第一個mineturtle
這裏是我的代碼:圖像改變爲另一個圖像在pygame中變化

import pygame, sys, time 
    from pygame.locals import * 

pygame.init() 

FPS = 40 # frames per second setting 
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) 

print("if you click the mine tutle box he will explode") 

cat = pygame.image.load("mineturtle.PNG") 
catx = 10 
caty = 10 
direction = 'right' 
s=15 
while True: # the main game loop 
DISPLAYSURF.fill(WHITE) 
if direction == 'right': 
    catx += s 
    if catx == 280: 
     direction = 'down' 
elif direction == 'down': 
    caty += s 
    if caty == 220: 
     direction = 'left' 
elif direction == 'left': 
    catx -= s 
    if catx == 10: 
     direction = 'up' 
elif direction == 'up': 
    caty -= s 
    if caty == 10: 
     direction = 'right' 
DISPLAYSURF.blit(cat, (catx, caty)) 

for event in pygame.event.get(): 
    if event.type == MOUSEBUTTONDOWN: 
     b = pygame.image.load("b.png") 
     time.sleep(2) 
     pygame.quit() 
     sys.exit() 
    if event.type == QUIT: 
     pygame.quit() 
     sys.exit() 

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

我已經有所有的文件在同一個地方,但是當我做圖像swich id沒有工作! PLZ幫助

回答

0

我只是做這個

import pygame, sys, time 
from pygame.locals import * 

pygame.init() 

FPS = 40 # frames per second setting 
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) 

print("if you click the mine tutle box he will explode") 

cat = pygame.image.load("mineturtle.PNG") 
catx = 10 
caty = 10`enter code here` 
direction = 'right' 
s=15 
explodetick=0 
move=True 
while True: # the main game loop 
    DISPLAYSURF.fill(WHITE) 
    #for event in pygame.event.get(): 
    #  
     if explodetick>0: 
      explodetick+=1 
     if explodetick==81: 
     pygame.quit() 
if move: 
    if direction == 'right': 
     catx += s 
     if catx == 280: 
      direction = 'down' 
    elif direction == 'down': 
     caty += s 
     if caty == 220: 
      direction = 'left' 
    elif direction == 'left': 
     catx -= s 
     if catx == 10: 
      direction = 'up' 
    elif direction == 'up': 
     caty -= s 
     if caty == 10: 
      direction = 'right' 
DISPLAYSURF.blit(cat, (catx, caty)) 

for event in pygame.event.get(): 
    if event.type == QUIT: 
     pygame.quit() 
     sys.exit() 
    if event.type == MOUSEBUTTONDOWN: 
     cat = pygame.image.load("b.png") 
     pygame.display.update() 
     explodetick=1 
     move=False 

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

要在屏幕上顯示更改,您需要調用pygame.display.update()。當您呼叫睡眠並退出時,它不會更新屏幕,也不會看到任何更改。 時間。

編輯:

如果你想展示新形象2秒鐘,然後關閉該程序,你應該使用pygame.time模塊。有一個變量可以存儲自上次點擊以來的時間。你添加tick()的結果。當這個值足夠高時,你可以退出pygame。

這不會凍結窗口,您將能夠看到更改。