这是本人编写的飞机大战中途岛海战的玩家所操控的飞机类之模块。以供给有需要的人士。
下面是部分代码预览:
""" 中途岛海战玩家飞机类设计测试程序,飞机会不断地切换飞翔时的造型,按上下左右键操作飞机, 这里测试按空格键,它会爆炸,依次显示坠毁和爆炸的造型. """ import time import pygame from pygame.locals import * ''' class _Death(pygame.sprite.Sprite): 本类已废,留做读者查阅 """玩家操作的飞机爆毁时切换造型类,继承自Sprite""" def __init__(self,deadimages,pos): pygame.sprite.Sprite.__init__(self) self.index = 0 # 造型索引号 self.images = deadimages self.image = images[0] # 第一个造型 self.amounts = len(images) # 造型数量 self.interval = 0.1 # 造型切换间隔时间 self.start_time = time.time() def update(self): """更新,时间到了就换造型,造型换完了就kill""" if time.time() - self.start_time > self.interval: if self.index < self.amounts: self.image = self.images[self.index] self.index += 1 self.start_time = time.time() else: self.kill() ''' class Player(pygame.sprite.Sprite): """玩家操作的飞机类,继承自Sprite""" def __init__(self,images,deadimages,pos,keys): pygame.sprite.Sprite.__init__(self) self.index = 0 # 造型索引号 self.images = images self.image = images[0] # 第一个造型 def alt_death_costume(self): """时间到了就轮换队毁时的造型""" def alt_costume(self): """时间到了就轮换造型""" def keys_check(self): """ 按键检测 """ def update(self): """更新飞机""" def main(): player_images = ["player_images/Plane 1.png"] player_images.append("player_images/Plane 2.png") player_images.append("player_images/Plane 3.png") dead_images = ["player_images/Plane Explode.png"] dead_images.append("player_images/Death1.png") dead_images.append("player_images/Death2.png") dead_images.append("player_images/Death3.png") dead_images.append("player_images/Death4.png") dead_images.append("player_images/Death5.png") player_images = [pygame.image.load(im) for im in player_images] dead_images = [pygame.image.load(im) for im in dead_images] width,height = 960,720 screen = pygame.display.set_mode((width,height)) pygame.display.set_caption("中途岛海战玩家飞机类设计测试程序by李兴球") player_group = pygame.sprite.Group() death_group = pygame.sprite.Group() ks = [K_UP,K_DOWN,K_LEFT,K_RIGHT] pos = width//2,height-100 p = player_images,dead_images,pos,ks p1 = Player(*p) player_group.add(p1) clock = pygame.time.Clock() running = True while running: for event in pygame.event.get(): if event.type == QUIT:running = False player_group.update() screen.fill((0,0,0)) player_group.draw(screen) pygame.display.update() clock.tick(60) pygame.quit() if __name__ == "__main__": main()
如需要查看完整源代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)