动画游戏。时间。等等()

2024-09-30 20:35:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码不工作,我不明白为什么,我正在使用模块PYGAME,我想在按下键“s时制作一个动画(三个图像)”。我的代码是:

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

pygame.init()
ventana = pygame.display.set_mode((400,300))
pygame.display.set_caption("Hi!")
imgA = pygame.image.load("IMG/fan_azul.png")
imgB = pygame.image.load("IMG/fan_naranja.png")
imgC = pygame.image.load("IMG/fan_rojo.png")
listaImg = [imgA,imgB,imgC]
POS,aux,e = 0,1,0
picture = listaImg[POS]
posX,posY,fondo = 200,100,(50,50,50)

while True:
    ventana.fill(fondo)
    #ventana.blit(picture,(posX,posY))
    for evento in pygame.event.get():
            if evento.type == QUIT:
                pygame.quit()
                sys.exit()
            elif evento.type == KEYDOWN:
                if evento.key == K_s:
                    for e in range(0,3):
                        POS = e
                        picture = listaImg[POS]
                        ventana.blit(picture,(posX,posY))
                        print "TIEMPO MEDIDO: "+str(POS)
                        pygame.time.wait(1000)
pygame.display.update()

Tags: posimageimportimgpngdisplayloadpygame
1条回答
网友
1楼 · 发布于 2024-09-30 20:35:27

避免内部循环会挂起处理事件和其他绘图到框架,这一点很重要。请记住,您通常尝试每秒获得30到60帧,而您的K嫒s处理程序本身需要3秒。保持句柄事件、更新状态和绘制屏幕部分都是分开的。为了制作动画,我通常会这样做。在

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

pygame.init()
ventana = pygame.display.set_mode((400,300))
pygame.display.set_caption("Hi!")
FONDO = (50,50,50)

class AnimatedSprite(pygame.sprite.Sprite):
    def __init__(self, position, filenames):
        pygame.sprite.Sprite.__init__(self)
        self.images = [pygame.image.load(filename) for filename in filenames]
        self.delay = 0
        self.start = 0
        self.rect = pygame.Rect(position, (0, 0))
        self._set_image(0)
    def _set_image(self, image_num):
        self.image_num = image_num
        self.image = self.images[self.image_num]
        self.rect.size = self.image.get_rect().size
    def show(self, group, delay=1.0):
        group.add(self)
        self.delay = delay
        self._set_image(0)
        self.start = time.time()
    def update(self):
        if self.alive() and time.time() - self.start > self.delay:
            if self.image_num + 1 < len(self.images):
                self._set_image(self.image_num + 1)
            else:
                self.kill()


fan = AnimatedSprite((200, 10), ["IMG/fan_azul.png",
                                 "IMG/fan_naranja.png",
                                 "IMG/fan_rojo.png"])
animation = pygame.sprite.Group()

while True:
    # Handle Events
    for evento in pygame.event.get():
            if evento.type == QUIT:
                pygame.quit()
                sys.exit()
            elif evento.type == KEYDOWN:
                if evento.key == K_s:
                    fan.show(animation)

    # Update State
    animation.update()

    # Draw
    ventana.fill(FONDO)
    animation.draw(ventana)
    pygame.display.update()

相关问题 更多 >