|
| 1 | +import random |
| 2 | + |
| 3 | +# Enemy types with varying attack capabilities |
| 4 | +ENEMY_TYPES = [ |
| 5 | + {"name": "Goblin", "damage": 20, "chance_to_evade": 0.1}, |
| 6 | + {"name": "Orc", "damage": 30, "chance_to_evade": 0.15}, |
| 7 | + {"name": "Troll", "damage": 40, "chance_to_evade": 0.2}, |
| 8 | +] |
| 9 | + |
| 10 | +# Special attacks with cooldown periods |
| 11 | +SPECIAL_ATTACKS = [ |
| 12 | + {"name": "Fireball", "damage": 50, "cooldown": 3}, |
| 13 | + {"name": "Lightning Strike", "damage": 60, "cooldown": 5}, |
| 14 | + {"name": "Ice Nova", "damage": 45, "cooldown": 4}, |
| 15 | + {"name": "Poison Dart", "damage": 40, "cooldown": 3}, |
| 16 | +] |
| 17 | + |
| 18 | +# Initialize game variables |
| 19 | +level = 1 |
| 20 | +total_points = 0 |
| 21 | +enemy_type = ENEMY_TYPES[0] |
| 22 | +special_attack_cooldown = 0 |
| 23 | + |
| 24 | +def setup_defenses(): |
| 25 | + defenses = [] |
| 26 | + for i in range(5): |
| 27 | + defense_strength = random.randint(50 + 10*level, 100 + 10*level) |
| 28 | + defenses.append(defense_strength) |
| 29 | + return defenses |
| 30 | + |
| 31 | +def choose_enemy_type(): |
| 32 | + global enemy_type |
| 33 | + enemy_type = random.choice(ENEMY_TYPES) |
| 34 | + |
| 35 | +def use_special_attack(): |
| 36 | + global special_attack_cooldown |
| 37 | + if special_attack_cooldown == 0: |
| 38 | + special_attack = random.choice(SPECIAL_ATTACKS) |
| 39 | + special_attack_cooldown = special_attack["cooldown"] |
| 40 | + print(f"Used {special_attack['name']}! It deals {special_attack['damage']} damage!") |
| 41 | + return special_attack["damage"] |
| 42 | + else: |
| 43 | + print("Special attack is on cooldown. Keep attacking the defenses!") |
| 44 | + return 0 |
| 45 | + |
| 46 | +def upgrade_enemy(): |
| 47 | + global enemy_type |
| 48 | + enemy_type_index = ENEMY_TYPES.index(enemy_type) |
| 49 | + if enemy_type_index < len(ENEMY_TYPES) - 1: |
| 50 | + enemy_type = ENEMY_TYPES[enemy_type_index + 1] |
| 51 | + print(f"You upgraded to {enemy_type['name']}. They deal more damage now!") |
| 52 | + |
| 53 | +def attack(defenses): |
| 54 | + global special_attack_cooldown, total_points |
| 55 | + total_defense_strength = sum(defenses) |
| 56 | + print(f"Current defenses: {defenses}") |
| 57 | + print(f"Total defense strength: {total_defense_strength}") |
| 58 | + |
| 59 | + if total_defense_strength <= 0: |
| 60 | + print("Congratulations! You breached the defenses!") |
| 61 | + total_points += 100 + 20 * level |
| 62 | + return True |
| 63 | + else: |
| 64 | + while True: |
| 65 | + print("\nChoose your action:") |
| 66 | + print("1. Normal Attack") |
| 67 | + print("2. Use Special Attack") |
| 68 | + print("3. Upgrade Enemy Type") |
| 69 | + action = input("Enter the number of your action: ") |
| 70 | + |
| 71 | + if action == "1": |
| 72 | + damage = enemy_type["damage"] |
| 73 | + if random.random() < enemy_type["chance_to_evade"]: |
| 74 | + print(f"{enemy_type['name']} evaded your attack!") |
| 75 | + damage = 0 |
| 76 | + else: |
| 77 | + print(f"You attacked the defenses and caused {damage} damage!") |
| 78 | + defenses[random.randint(0, len(defenses) - 1)] -= damage |
| 79 | + return False |
| 80 | + |
| 81 | + elif action == "2": |
| 82 | + damage = use_special_attack() |
| 83 | + defenses[random.randint(0, len(defenses) - 1)] -= damage |
| 84 | + return False |
| 85 | + |
| 86 | + elif action == "3": |
| 87 | + upgrade_enemy() |
| 88 | + return False |
| 89 | + |
| 90 | + else: |
| 91 | + print("Invalid input. Please enter a valid action number.") |
| 92 | + |
| 93 | +def main(): |
| 94 | + global level, total_points, special_attack_cooldown |
| 95 | + |
| 96 | + print("Welcome to the Reverse Tower Defense Game!") |
| 97 | + print("You are controlling the enemy horde trying to breach the AI's defenses.") |
| 98 | + |
| 99 | + while True: |
| 100 | + print(f"\n--- Level {level} ---") |
| 101 | + |
| 102 | + defenses = setup_defenses() |
| 103 | + points = 0 |
| 104 | + special_attack_cooldown = 0 |
| 105 | + |
| 106 | + while True: |
| 107 | + if attack(defenses): |
| 108 | + level += 1 |
| 109 | + total_points += points |
| 110 | + break |
| 111 | + |
| 112 | + if special_attack_cooldown > 0: |
| 113 | + special_attack_cooldown -= 1 |
| 114 | + |
| 115 | + print("\nChoose the defense you want to attack:") |
| 116 | + for i in range(len(defenses)): |
| 117 | + print(f"{i+1}. Defense {i+1} - Strength: {defenses[i]}") |
| 118 | + |
| 119 | + print(f"\nLevel {level-1} completed!") |
| 120 | + print(f"Points earned in Level {level-1}: {points}") |
| 121 | + print(f"Total points: {total_points}") |
| 122 | + |
| 123 | + play_again = input("Do you want to play the next level? (yes/no): ").lower() |
| 124 | + if play_again != "yes": |
| 125 | + break |
| 126 | + |
| 127 | + print("Thanks for playing! Game Over!") |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + main() |
0 commit comments