图像更改为另一个图像在pygame中更改

时间:2013-05-15 18:34:33

标签: image python-3.x pygame

我在pygame ware制作了一个程序,一只矿龟在一个sqare中运行,但是当你在框中点击时他会爆炸,但当我将图像更改为爆炸时,它首先显示了我的乌龟 这是我的代码:

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不起作用!请帮忙

2 个答案:

答案 0 :(得分:1)

要在屏幕上显示更改,您需要调用pygame.display.update()。当您调用sleep并退出时,它不会更新屏幕,也不会看到任何更改。 时间。

编辑:

如果您希望显示新图像2秒然后关闭程序,则应使用pygame.time模块。有一个变量可以存储自上次点击以来的时间。你添加tick()的结果。当值足够高时,您可以退出pygame。

这不会冻结窗口,您将能够看到更改。

答案 1 :(得分: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)