0

我在 pygame ware 中制作了一个程序,一只地雷龟在一个正方形中运行,但是当你单击框时,它会爆炸,但是当我将图像更改为爆炸时,它首先显示地雷龟,
这是我的代码:

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

4

2 回答 2

1

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

编辑:

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

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

于 2013-05-15T20:57:19.373 回答
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)
于 2013-05-20T14:52:38.967 回答