def run_level_up(self): # 若处于前三关 if self.level <= 3: self.buying = 0 self.enemies.clear() self.enemy_count = 0 self.last_enemy_time = 0 self.towers.clear() if self.next_button.collidepoint(self.mouse_down_pos): sounds.starta.play(loops=0) music.set_volume(0.2) music.play("start.wav") self.begin_time = time.time() self.status = "playing" # 若处于彩蛋关卡 elif self.level == 4: self.buying = 0 self.enemies.clear() self.enemy_count = 0 self.last_enemy_time = 0 self.towers.clear() self.towers.append(Tower("virus4_1", (400, 325), 1)) self.towers.append(Tower("virus4_0", (500, 200), 2)) self.towers.append(Tower("virus4_1", (700, 450), 3)) if self.next_button.collidepoint(self.mouse_down_pos): sounds.starta.play(loops=0) music.set_volume(0.2) music.play("start.wav") self.begin_time = time.time() self.status = "egg"
def restart(self): sounds.starta.play(loops=0) music.set_volume(0.4) music.play("start.wav") self.level = 1 self.buying = 0 self.lives = 3 self.money = 50 self.enemies.clear() self.enemy_count = 0 self.last_enemy_time = 0 self.towers.clear()
def run(self): # 开始界面 if self.status == "begin": if self.begin_button.collidepoint(self.mouse_down_pos): self.status = "playing" sounds.starta.play(loops=0) music.set_volume(0.2) elif self.help_button.collidepoint(self.mouse_down_pos): self.status = "help" sounds.starta.play(loops=0) # 帮助界面 elif self.status == "help": if self.back_button.collidepoint(self.mouse_down_pos): sounds.starta.play(loops=0) self.status = "begin" # 暂停状态 elif self.status == "pause": if self.start_button.collidepoint(self.mouse_down_pos): if self.level <= 3: self.status = "playing" elif self.level == 4: self.status = "egg" # 游戏中 elif self.status == "playing": self.run_playing() # 下一关 elif self.status == "level up": self.run_level_up() # 彩蛋 elif self.status == "egg": self.run_egg() # 胜利 elif self.status == "win": if self.back_button.collidepoint(self.mouse_down_pos): self.restart() self.status = "begin" # 失败 elif self.status == "lose": if self.back_button.collidepoint(self.mouse_down_pos): self.restart() self.status = "begin"
def run_playing(self): # 检测游戏结果 result = self.get_result() if result == 1: # 胜利 music.stop() self.level += 1 self.status = "level up" sounds.levelup.play(loops=0) elif result == -1: # 失败 self.status = "lose" music.set_volume(0.5) music.play_once("lose.wav") # 返回到开始界面 if self.back_button.collidepoint(self.mouse_down_pos): self.restart() self.status = "begin" # 暂停游戏 if self.pause_button.collidepoint(self.mouse_down_pos): self.status = "pause" # 购买状态 if self.buying: if self.tower_in_range(self.mouse_move_pos): self.buy_tower() # 按下购买按钮,并更改状态 if self.buy_tower1.collidepoint( self.mouse_down_pos) and self.money - 50 >= 0: self.buying = 1 elif self.buy_tower2.collidepoint( self.mouse_down_pos) and self.money - 100 >= 0: self.buying = 2 elif self.buy_tower3.collidepoint( self.mouse_down_pos) and self.money - 200 >= 0: self.buying = 3 # 更新 self.update_actor()
def run_egg(self): # 返回按钮 if self.back_button.collidepoint(self.mouse_down_pos): self.restart() self.status = "begin" # 暂停游戏 if self.pause_button.collidepoint(self.mouse_down_pos): self.status = "pause" # 塔攻击 for tower in self.towers: if tower.type == 1: tower.attack(self.enemies) elif tower.type == 2: tower.frozen(self.enemies) elif tower.type == 3: tower.boom(self.enemies) # 病毒更新 for enemy in self.enemies: enemy.move(self.level) # 行走 enemy.hurt(self.towers) # 受伤 if enemy.type == 4: # boss生成病毒 enemy.boss_born(self.enemies) if enemy.health <= 0: # 检测病毒是否死亡 self.money += enemy.type * 10 sounds.coin.play(loops=0) self.enemies.remove(enemy) if enemy.pos == (925, 325): # 检测是否胜利 self.status = "win" music.set_volume(0.5) music.play_once("youwin.wav") # 购买北大学生 if self.buy_pkuer.collidepoint(self.mouse_down_pos): self.enemies.append(Enemy("pkuer", (200, 75), 5)) self.mouse_down_pos = (0, 0)
def __init__(self): self.begin_time = time.time() self.status = "begin" self.level = 1 self.buying = 0 self.mouse_down_pos = (0, 0) self.mouse_move_pos = (0, 0) self.lives = 3 self.money = 500 self.enemies = [] self.enemy_count = 0 self.last_enemy_time = 0 self.towers = [] # 音乐 music.set_volume(0.4) music.play("start.wav") # 共用界面 self.back_button = Actor("back_button", (50, 50)) # 开始界面 self.begin_button = Actor("begin_button", (500, 425)) # 帮助界面 self.help_button = Actor("help_button", (500, 550)) # 升级界面 self.egg_button = Actor("egg_button", (100, 400)) # 游戏中 self.buy_tower1 = Actor("tower1", (450, 650)) self.buy_tower2 = Actor("tower2", (625, 650)) self.buy_tower3 = Actor("tower3", (825, 650)) self.hint = Actor("tower1", (0, 0)) self.pause_button = Actor("pause_button", (100, 200)) self.start_button = Actor("start_button", (100, 300)) self.next_button = Actor("next_button", (100, 400)) # 彩蛋 self.buy_pkuer = Actor("pkuer2", (700, 650))
import pgzrun, time import configparser from sys import exit from pgzero import music from pgzero.keyboard import keyboard from random import choice cfg = configparser.ConfigParser() cfg.read("config.cfg") music.set_volume(float(cfg.get("SOUND", "volume"))) WIDTH = int(cfg.get("SCREEN", "width")) HEIGHT = int(cfg.get("SCREEN", "height")) TITLE = "Mystère à Eiffel" # à changer dead_time = 0.1 MENU = 0 JEU = 1 FIN_JEU = 2 position_curseur = 0 id_lieux = 1 rep = False etat_jeu = MENU accuse_state = False actual_musique = "" rep_accuse = False position_curseur_accuse = 0