写了个简易的逃离塔克夫(水文章)

废话不多说,直接上代码(有建议放评论区):
 

import random
import time

class Player:
    def __init__(self, name, health=100):
        self.name = name
        self.health = health
        self.position = (0, 0)
        self.weapon = None
        self.inventory = []

    def move(self, direction):
        x, y = self.position
        if direction == "up":
            self.position = (x, y - 1)
        elif direction == "down":
            self.position = (x, y + 1)
        elif direction == "left":
            self.position = (x - 1, y)
        elif direction == "right":
            self.position = (x + 1, y)

    def attack(self, enemy):
        if self.weapon:
            damage = self.weapon.damage
        else:
            damage = 10
        enemy.health -= damage
        print(f"{self.name}对{enemy.name}造成了{damage}点伤害。")
        time.sleep(1)

    def pick_item(self, item):
        if isinstance(item, Weapon):
            self.weapon = item
        else:
            self.inventory.append(item)
        print(f"{self.name}捡起了{item}。")
        time.sleep(1)

    def use_item(self, item):
        if item == "medkit":
            self.health += 20
            print(f"{self.name}使用了医疗包,生命值恢复了20点。")
            time.sleep(1)

class Enemy:
    def __init__(self, name, health=100, attack_power=10):
        self.name = name
        self.health = health
        self.attack_power = attack_power
        self.position = (0, 0)  # 添加position属性并设置初始位置

    def attack(self, player):
        damage = random.randint(1, self.attack_power)
        player.health -= damage
        print(f"{self.name}对{player.name}造成了{damage}点伤害。")
        time.sleep(1)
class Weapon:
    def __init__(self, name, damage):
        self.name = name
        self.damage = damage

class Map:
    def __init__(self, size):
        self.size = size
        self.player = None
        self.enemies = []
        self.weapons = []

    def generate_random_position(self):
        x = random.randint(0, self.size - 1)
        y = random.randint(0, self.size - 1)
        return (x, y)

    def add_player(self, player):
        self.player = player
        self.player.position = self.generate_random_position()

    def add_enemy(self, enemy):
        enemy.position = self.generate_random_position()
        self.enemies.append(enemy)

    def add_weapon(self, weapon):
        position = self.generate_random_position()
        self.weapons.append((weapon, position))

    def display(self):
        for y in range(self.size):
            for x in range(self.size):
                if self.player.position == (x, y):
                    print("P", end=" ")
                elif any(enemy.position == (x, y) for enemy in self.enemies):
                    print("E", end=" ")
                elif any(position == (x, y) for _, position in self.weapons):
                    print("W", end=" ")
                else:
                    print(".", end=" ")
            print()

class Store:
    def __init__(self):
        self.weapons = [Weapon("SKS", 30), Weapon("AK47", 40)]
        self.items = ["medkit"]

    def display(self):
        print("欢迎来到商店!请选择购买的物品:")
        print("1. 武器")
        print("2. 物品")
        print("3. 离开商店")

    def buy_weapon(self, player):
        print("可购买的武器:")
        for index, weapon in enumerate(self.weapons, start=1):
            print(f"{index}. {weapon.name} (伤害: {weapon.damage})")
        choice = input("请选择要购买的武器编号:")
        try:
            index = int(choice) - 1
            weapon = self.weapons[index]
            player.pick_item(weapon)
            self.weapons.pop(index)
            print(f"{player.name}购买了{weapon.name}。")
        except (ValueError, IndexError):
            print("无效的选择。")

    def buy_item(self, player):
        print("可购买的物品:")
        for index, item in enumerate(self.items, start=1):
            print(f"{index}. {item}")
        choice = input("请选择要购买的物品编号:")
        try:
            index = int(choice) - 1
            item = self.items[index]
            player.pick_item(item)
            self.items.pop(index)
            print(f"{player.name}购买了{item}。")
        except (ValueError, IndexError):
            print("无效的选择。")

def main():
    player_name = input("请输入你的角色名字:")
    player = Player(player_name)
    enemies = [Enemy("塔克夫1"), Enemy("塔克夫2")]
    game_map = Map(10)
    game_map.add_player(player)
    game_map.enemies.extend(enemies)
    game_map.add_weapon(Weapon("SKS", 30))
    game_map.add_weapon(Weapon("AK47", 40))
    store = Store()

    print("欢迎来到逃离塔克夫大逃杀!")
    print("你需要击败敌人塔克夫并逃离这个危险的地方。")

    while player.health > 0 and any(enemy.health > 0 for enemy in enemies):
        print(f"\n{player_name}的生命值:{player.health}")
        for enemy in enemies:
            print(f"{enemy.name}的生命值:{enemy.health}")

        game_map.display()

        action = input("请选择行动:\n1. 移动\n2. 攻击\n3. 舔包\n4. 进入商店\n5. 逃跑\n")

        if action == "1":
            direction = input("请选择移动方向:\n1. 上\n2. 下\n3. 左\n4. 右\n")
            if direction == "1":
                player.move("up")
            elif direction == "2":
                player.move("down")
            elif direction == "3":
                player.move("left")
            elif direction == "4":
                player.move("right")
            else:
                print("无效的移动方向。请重新选择移动方向。")

        elif action == "2":
            for enemy in enemies:
                if player.position == enemy.position:
                    player.attack(enemy)
                    if enemy.health <= 0:
                        print(f"恭喜你击败了{enemy.name}!")
                    break
            else:
                print("附近没有敌人可以攻击。")

        elif action == "3":
            for weapon, position in game_map.weapons:
                if player.position == position:
                    player.pick_item(weapon)
                    game_map.weapons.remove((weapon, position))
                    break
            else:
                player.use_item("medkit")

        elif action == "4":
            store.display()
            choice = input("请选择操作:")
            if choice == "1":
                store.buy_weapon(player)
            elif choice == "2":
                store.buy_item(player)
            elif choice == "3":
                print("你离开了商店。")
            else:
                print("无效的选择。")

        elif action == "5":
            print("你逃跑了!游戏结束。")
            break

        else:
            print("无效的选择。请重新选择行动。")

        for enemy in enemies:
            if enemy.health > 0 and enemy.position == player.position:
                enemy.attack(player)
                if player.health <= 0:
                    print("很遗憾,你被敌人击败了!游戏结束。")
                    break

if __name__ == "__main__":
    main()

你可能感兴趣的:(整活系列,python入门教程,python,开发语言)