影子奔跑猫 pygame向前移动背景演示

"""影子奔跑猫,向前移动背景演示,猫的坐标不变只是切换造型,程序其实很简单.
"""

import time
import pygame
from pygame.locals import *

本文隐藏内容 登陆 后才可以浏览
class Cat: """猫类,生成后会不断地切换造型""" def __init__(self,images): self.frames = images self.amounts = len(images) # 帧数 self.index = 0 # 初始帧索引 self.image = self.frames[self.index] self.rect = self.image.get_rect() self.interval = 0.01 # 帧切换的间隔时间 self.start_time = time.time() def update(self): if time.time() - self.start_time > self.interval : self.index += 1 self.index = self.index % self.amounts self.image = self.frames[self.index] self.start_time = time.time() if __name__ == "__main__": width,height = 960,720 screen = pygame.display.set_mode((width,height)) pygame.display.set_caption("影子奔跑猫 pygame向前移动背景演示,www.lixingqiu.com") pygame.mixer.init() pygame.mixer.music.load("The Downtown Fic.wav") pygame.mixer.music.play(-1,0) bgs = pygame.image.load("costume1.png") background = Background(bgs,screen) cat_images = [f"frames/frame{index}.png" for index in range(20)] cat_frames = [pygame.image.load(image).convert_alpha() for image in cat_images] shadow_cat = Cat(cat_frames) shadow_cat.rect.center = width//2,height//2 clock = pygame.time.Clock() running = True while running: for event in pygame.event.get(): if event.type == QUIT:running = False background.update() shadow_cat.update() background.draw() screen.blit(shadow_cat.image,shadow_cat.rect) pygame.display.update() pygame.quit()

影子奔跑猫,向前移动背景演示,猫的坐标不变只是切换造型