下面是部分代码预览:
"""影子奔跑猫,向前移动背景演示,猫的坐标不变只是切换造型,程序其实很简单. """ import time import pygame from pygame.locals import * class Background: """不断向前移动的滚动背景""" def __init__(self,image,screen): """初始化函数""" self.screen = screen self.w = screen.get_width() # 记录屏幕宽度 self.h = screen.get_height() # 记录屏幕高度 self.image = image # image是一个surface self.image2 = image self.rect = self.image.get_rect() self.rect2 = self.image2.get_rect() self.rect2.right = 0 # 两个surface相隔一个屏幕宽度 self.dx = 50 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() 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() pygame.quit()
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)