---
# System prepended metadata

title: 創建玩家

---

```python=

import random
import keyboard
import time

resources_list = ['stone', 'metal', 'crystal', 'food', 'water', 'energy']



        

class Monster:

    def __init__(self, hp, dmg):
        self.resource = random.choice(resources_list)
        self.resource_count = random.randint(1, 5)
    
        self.hp = hp
        self.maxHP = hp
        self.dmg = dmg 
       
        
    def monster_death(self):
        print('你擊敗了怪物！')
        
        
        
class Player:

    def __init__(self, name, hp, dmg):
        self.isAlive = True
        self.round = 0
        self.time = 0
        self.name = name
        self.ship = 0
        self.maxhp=hp
        self.hp = hp
        self.dmg = dmg
        self.base_dmg = dmg
        self.resources = {}
        for resource in resources_list:
            self.resources[resource] = 0
        
        print(f'name = {self.name}, hp = {self.hp}, dmg = {self.dmg}')
        
    def get_dmg(self):
        self.dmg = self.base_dmg * (1.25 ** self.ship)



    def add_resource(self, resource, amount):
        self.resources[resource] += amount
        print(f"你獲得了 {resource} {amount}個！現在共有{self.resources[resource]}個")

    def explore_planet(self):

        print("\n正在尋找星球...")
        time.sleep(2)
        rand = random.random()
        ch = listen_keyboard(['w', 's', 'a', 'd', 'f'])

        if ch == 'f':
            self.display_resources()
        # self.add_resource("food", -5)
        # self.add_resource("water", -5)

        if rand < 0.2: #mob
            base = 5 + 5 * self.round
            mob_hp, mob_dmg = random.randint(2 * base, 4 * base), random.randint(base, 2 * base)
            print(f"遇見了一隻大型怪物...\n血量：{mob_hp}, 攻擊力：{mob_dmg}")
            self.space_battle(Monster(mob_hp, mob_dmg))
        
        elif rand < 0.35: #light
            print("在路邊發現了一些亮點...要去看看嗎？\n (w: 前進, s: 回頭, a/d: 繞過去)")
            ch = listen_keyboard(['w', 's', 'a', 'd', 'f'])

            if ch == 'w':
                rand_num = random.random()
                if rand_num < 0.7:
                    print("發現了一些資源！")
                    time.sleep(0.5)
                    count = random.randint(1, 3)
                    resources = random.sample(resources_list, count)
                    for resource in resources:
                        self.add_resource(resource, random.randint(1, 5))
                elif rand_num < 0.8:
                    print("發現了怪物的便便")
                    time.sleep(0.5)
                # elif rand_num <1:
                    
                        
                       
                else:
                    
                    dhp = random.randint(10, self.maxhp // 5)
                    self.hp -= dhp
                    if self.hp <= 0: self.hp = 0
                    print(f"被打了！\n扣除{dhp}點血量！(你的血量剩下{self.hp})")
                    time.sleep(0.5)
                    if self.hp <= 0:
                        self.player_death()

            elif ch == 's':
                print("你逃跑了，繼續探索星球")
                time.sleep(0.5)

            elif ch == 'a' or ch == 'd':
                print("你逃跑了，繼續探索星球")
                
                time.sleep(0.5)

        elif rand < 0.55: #cave
            print("你來到了一個星球，前方是一個洞窟，你要進去看看嗎？\n(w: 進入, s: 回頭, a/d: 繞過去)")
            ch = listen_keyboard(['w', 's', 'a', 'd'])
            if ch == 'w':
                print("你進入了洞窟，發現了一些資源！")
                self.add_resource("stone", random.randint(4, 8))
                time.sleep(0.5)
                self.add_resource("metal", random.randint(2, 4))
                time.sleep(0.5)
                self.add_resource("crystal", random.randint(1, 2))
                time.sleep(0.5)

            elif ch == 's':
                print("你回頭離開了洞窟，繼續探索星球")
                time.sleep(0.5)

            elif ch == 'a' or ch == 'd':
                print("你繞過了洞窟，繼續探索星球")
                time.sleep(0.5)
        elif rand < 0.85: #trade
            print("遇到了商人，你要跟它交易嗎?")
            print("按下w交易，按下s離開")
            ch= listen_keyboard(['w','s'])
            if ch== 's':
                print("你選擇不要交易")
                pass
            if ch== 'w':
                print("你選擇繼續交易")
            
                if self.resources['stone'] > 10 and self.resources['metal'] > 10 :
                    print("你要用10個石頭和10個金屬交易一個盾牌嗎\n,按下w:要,按下s:不要")
                    time.sleep(0.2)
                    ch = listen_keyboard(['w', 's'])
                    if ch== 'w':
                        print("血量上限增加100")
                        self.maxhp+=100
                        self.hp += 100
                        print("當前生命上限為:", self.maxhp)
                        print("當前生命值為:", self.hp)
                        self.resources['stone'] -= 4
                        self.resources['metal'] -= 4
                        print("交易成功")
                    
                    if ch== 's':
                        print("你選擇不要交易")
                
                elif self.resources['food'] > 10 and self.resources['water'] > 10:
                    
                    print("你需要增加攻擊力嗎\n","按下w:要,按下s:不要")
                    time.sleep(0.2)
                    ch = listen_keyboard(['w', 's'])
                    if ch== 'w':
                        print("攻擊力增加50")
                        self.base_dmg +=50
                        self.get_dmg()
                        print("當前攻擊力為:", self.dmg)
                        self.resouces['food'] -= 3
                        self.add_resource('water', -3)
                        # self.ship += 1
                    if ch== 's':
                        print("你選擇不要交易")
                
        
                elif self.resources['energy'] <= 5 and self.resources['crystal'] <= 5 and self.resources['food'] >= 5 and self.resources['water'] >= 5:
                
                    print("你要用5個水和5個食物來交換10個能量和5個水晶嗎)\n按下w:要,按下s:不要")
                    time.sleep(0.2)
                    ch = listen_keyboard(['w', 's'])
                    if ch == 'w':
                        print("增加10個能量和5個水晶")
                        set("按下w交易，按下s離開")

                else:
                    print("商人看你可憐，決定施捨你一些物品")
                    for resource in random.sample(resources_list,random.randint(2,5)):
                        self.add_resource(resource, random.randint(1, 3))
        
            
                
            # elif self.resources['metal'] <= 9:
            #     print("你需要增加5個金屬嗎?\n按下w:要,按下s:不要")
            #     ch = listen_keyboard(['w', 's'])
                        
            #     if ch== 'w':
            #         print("增加5個金屬")
            #         self.add_resource('metal', 5)
                    
            #     elif ch == 's':
            #         print("你選擇不要交易")
                        
            # else:
            #     print("你沒有足夠的材料")
        else:
            print("你什麼都沒發現...")
            time.sleep(0.5)

        self.time += 1
            
        
        if self.time == 5:
            self.round += 1
            self.time = 0
            print(f"星球被探索完了，即將前往個星球！\n你現在擁有的資源：\n")
            time.sleep(0.5)
            self.display_resources()
            print(f"你現在的血量：{self.hp}")
            time.sleep(0.5)
            print(f"你現在的攻擊力：{self.dmg}")
            time.sleep(0.5)
            print("")
            
        if self.resources['crystal'] >= 20 and self.resources['metal'] > 15 and self.resources['energy'] >= 15:
            print("你想要消耗20水晶＋15金屬＋15能量來造一艘太空船嗎？\n按下w:要,按下s:不要)")
            ch = listen_keyboard(['w', 's', 'esc'])
            if ch == 'w':
                self.build_ship()
            # elif ch == 's' or ch == 'esc':
            #     passp
            
                
        self.explore_planet()
            
            
            
    def space_battle(self, monster : Monster):
        print("你正在與怪物戰鬥...(space/w: 攻擊, esc: 逃跑)")
        time.sleep(0.5)
        while True:
            ch = listen_keyboard([' ', 'w', 'space', 'esc'])
            if ch == ' ' or ch == 'w' or ch == 'space':
                # print('你攻擊了怪物！')
                monster.hp -= self.dmg
                if monster.hp <= 0:
                    monster.monster_death()
                    self.resources[monster.resource] += monster.resource_count
                    print(f"你獲得了 {monster.resource} {monster.resource_count}個！現在共有{self.resources[monster.resource]}個")
                    return
                else:
                    print(f"你對怪物造成了 {self.dmg}點傷害！怪物血量剩下 {monster.hp}點")
                    self.hp -= monster.dmg
                    if self.hp <= 0:
                        self.hp = 0
                    print(f"怪物對你造成了 {monster.dmg}點傷害！你的血量剩下 {self.hp}點")
                    if self.hp == 0:
                        self.player_death()
                        return
                
                
            elif ch == 'esc':
                
                if random.random() < 0.2:
                    dhp = random.randint(monster.dmg // 10, monster.dmg // 2)
                    self.hp -= dhp
                    if self.hp <= 0: self.hp = 0

                    print(f'你嘗試逃跑，但很不幸的你被怪物追上了！\n他對你造成了{dhp}點傷害！(你的血量剩下{self.hp})')
                    time.sleep(0.5)

                    if self.hp <= 0:
                        self.player_death()
                    else:
                        self.space_battle(monster)
                else:
                    print("你成功逃跑了！")
                    time.sleep(0.5)
                    return
            else:
                print("請輸入有效指令！")
                time.sleep(0.1)
            time.sleep(0.2)

    def player_atk(self, monster : Monster):
        monster.hp -= self.dmg
        if monster.hp <= 0:
            monster.monster_death()
            self.resources[monster.resource] += monster.resource_count
            print(f"你獲得了 {monster.resource} {monster.resource_count}個！現在共有{self.resources[monster.resource]}個")
        else:
            print(f"你對怪物造成了 {self.dmg}點傷害！怪物血量剩下 {monster.hp}點")
            self.hp -= monster.dmg
            if self.hp <= 0:
                self.hp = 0
            print(f"怪物對你造成了 {monster.dmg}點傷害！你的血量剩下 {self.hp}點")
            if self.hp == 0:
                self.player_death()
        
            
        
    def build_ship(self):
        # ch = listen_keyboard(['f'])

        self.minus_resource("crystal", 20)
        self.minus_resource("stone", 15)
        self.minus_resource("energy", 15)

        print("開始建造太空船...")
        self.ship += 1
        print("你成功建造了一艘太空船！",
            "接下來的每次攻擊都有太空船的火力支援,"
            "你的攻擊力上升了25%\n", sep='\n')
        self.get_dmg()
        print("當前攻擊力為：",self.dmg)
        
    
    # def trade(self):
    #     n = random.randint(0, 4)
    #     if n == 1:
    #         print("你遇到商人了,要使用10個石頭和10個金屬交換5瓶水嗎")
    #         if self.resources['stone'] > 10 and self.resoueces['metal'] > 10 :
                
    #     elif n == 2:


        
    # def communicate(self, ):
    #     random.choice(trade_communicate_list)
        
    def player_atk(self, monster : Monster):
        monster.hp -= self.dmg
        if monster.hp == 0:
            
        # re, re_c = monster.death
            self.resources[monster.resource] += monster.resource_count
            
    def add_resource(self, resource, amount):
        self.resources[resource] += amount
        print(f"你獲得了 {resource} {amount}個！現在共有{self.resources[resource]}個")

    
    def display_resources(self):
        print(f"\n-----玩家 {self.name} 的資源-----")
        for resource, amount in self.resources.items():
            print(f'{resource}: {amount}')
        
        print("---------------------------\n")
        # time.sleep(1)

    # def on_key_press(event):
    #     pass
    
    def player_death(self):
        print("很抱歉你死了！")
        exit()
            
      
        
def listen_keyboard(listen_list):
    # listen_list = ['w', 'a', 's', 'd', 'space', 'esc']
    while True:
        for ch in listen_list:
            if keyboard.is_pressed(ch):
                return ch
        
    
print("歡迎來到太空生存遊戲！")
# 創建玩家
name1=input("請輸入你的名字：")
# print(name1)
player1 = Player(name = name1, hp=500, dmg=50)

print(f"已取名叫{player1.name}")
time.sleep(0.5)
print("這個遊戲的目標為盡量的存活下去，建造一艘太空船，探索星球並進行交易")
time.sleep(0.5)
print("你的初始資源為：")
player1.display_resources()
print(f"你的初始血量為：{player1.hp}")
time.sleep(0.5)
print(f"你的初始攻擊力為：{player1.dmg}\n")

print(
    "你可以使用的指令有：", 
    "w : 繼續移動", 
    "s : 回頭/逃跑", 
    "f : 顯示資源",
    "a/d : 向別處移動", 
    "space : 進行互動", 
    "esc : 離開交互\n", sep='\n'
    )

player1.explore_planet()
# print("按下w繼續前進"）
# while True:
#       ch=listen_keyboard(['w','a','s','d'])
#       if ch == 'w':
#           print("繼續前進")
#           break
#       else :
#       print("沒差阿都不要走")

        
      
```