赞
踩
关注小编,私信小编领取哟!
当然别忘了一件三连哟~~
公众号:Python日志
Python版本:3.7.8
相关模块:
pygame模块;
random模块;
pyttsx3模块;
以及一些python自带的模块。
安装Python并添加到环境变量,pip安装需要的相关模块即可。
效果展示
部分代码展示
源码领取、问题解答、资料分享交流Q裙:494958217 def main(): pygame.init() pygame.mixer.init() pygame.mixer.music.load(cfg.AUDIOPATHS['bgm']) pygame.mixer.music.play(-1, 0.0) pygame.mixer.music.set_volume(0.25) screen = pygame.display.set_mode(cfg.SCREENSIZE) pygame.display.set_caption("塔防游戏 ") # 调用游戏开始界面 start_interface = StartInterface(cfg) is_play = start_interface.update(screen) if not is_play: return # 调用游戏界面 while True: choice_interface = ChoiceInterface(cfg) map_choice, difficulty_choice = choice_interface.update(screen) game_interface = GamingInterface(cfg) game_interface.start(screen, map_path=cfg.MAPPATHS[str(map_choice)], difficulty_path=cfg.DIFFICULTYPATHS[str(difficulty_choice)]) end_interface = EndInterface(cfg) end_interface.update(screen)
class Enemy(pygame.sprite.Sprite): def __init__(self, enemy_type, cfg): assert enemy_type in range(4) pygame.sprite.Sprite.__init__(self) self.enemy_type = enemy_type self.imagepaths = [cfg.IMAGEPATHS['game']['enemy_yellow'], cfg.IMAGEPATHS['game']['enemy_red'], cfg.IMAGEPATHS['game']['enemy_pink'], cfg.IMAGEPATHS['game']['enemy_blue']] self.image = pygame.image.load(self.imagepaths[enemy_type]) self.rect = self.image.get_rect() # 走过的路(避免重复走,保证去攻击城堡) self.reached_path = [] # 在道路某个单元中移动的距离, 当cell_move_dis大于单元长度时移动到下一个到了单元并置0该变量 self.cell_move_dis = 0 # 当前所在的位置 self.coord = 3, 2 self.position = 60, 40 self.rect.left, self.rect.top = self.position if enemy_type == 0: # 最大生命值 self.max_life_value = 20 # 当前生命值 self.life_value = 20 # 速度 self.speed = 2 # 击杀奖励 self.reward = 100 # 对大本营造成的伤害 self.damage = 1 elif enemy_type == 1: self.max_life_value = 40 self.life_value = 40 self.speed = 1 self.reward = 200 self.damage = 1 elif enemy_type == 2: self.max_life_value = 60 self.life_value = 60 self.speed = 0.5 self.reward = 300 self.damage = 2 elif enemy_type == 3: self.max_life_value = 100 self.life_value = 100 self.speed = 0.2 self.reward = 500 self.damage = 4 '''不停地移动''' def move(self, cell_len): is_next_cell = False self.cell_move_dis += self.speed if self.cell_move_dis > cell_len: self.cell_move_dis = 0 is_next_cell = True return is_next_cell
效果展示
部分代码展示
import sys import cfg import pygame from modules import * '''游戏界面''' def GamingInterface(num_player, screen): # 初始化 pygame.mixer.music.load(cfg.SOUNDPATHS['Cool Space Music']) pygame.mixer.music.set_volume(0.4) pygame.mixer.music.play(-1) explosion_sound = pygame.mixer.Sound(cfg.SOUNDPATHS['boom']) fire_sound = pygame.mixer.Sound(cfg.SOUNDPATHS['shot']) font = pygame.font.Font(cfg.FONTPATH, 20) # 游戏背景图 bg_imgs = [cfg.IMAGEPATHS['bg_big'], cfg.IMAGEPATHS['seamless_space'], cfg.IMAGEPATHS['space3']] bg_move_dis = 0 bg_1 = pygame.image.load(bg_imgs[0]).convert() bg_2 = pygame.image.load(bg_imgs[1]).convert() bg_3 = pygame.image.load(bg_imgs[2]).convert() # 玩家, 子弹和小行星精灵组 player_group = pygame.sprite.Group() bullet_group = pygame.sprite.Group() asteroid_group = pygame.sprite.Group() # 产生小行星的时间间隔 asteroid_ticks = 90 for i in range(num_player): player_group.add(Ship(i+1, cfg)) clock = pygame.time.Clock() # 分数 score_1, score_2 = 0, 0 # 游戏主循环 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # --玩家一: ↑↓←→控制, j射击; 玩家二: wsad控制, 空格射击 pressed_keys = pygame.key.get_pressed() for idx, player in enumerate(player_group)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。