-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
146 lines (115 loc) · 4.4 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from random import randint
import json
import game
from enemy import spawnEnemy
def getAttributes():
from character import getCharacter
selectedCharacter = getCharacter()
class Character:
def __init__(self, health, damage, speed, stealth):
self.health = health # Max 150
self.damage = damage # Max 100
self.speed = speed # Max 100
self.stealth = stealth # Max 50
warrior_attributes = Character(100, 50, 25, 10)
ninja_attributes = Character(80, 40, 50, 45)
hunter_attributes = Character(75, 55, 35, 25)
if selectedCharacter == "warrior":
characterAttributes = warrior_attributes
elif selectedCharacter == "ninja":
characterAttributes = ninja_attributes
elif selectedCharacter == "hunter":
characterAttributes = hunter_attributes
return characterAttributes
def encounterInput():
while True:
playerInput = input(
"What do you want to do:\n1. Attack\n2. Flee\n3. Do nothing\nSelection: "
)
if playerInput == "1":
playerInput = "Attack"
break
elif playerInput == "2":
playerInput = "Flee"
break
elif playerInput == "3":
playerInput = "Do nothing"
break
else:
print("Invalid input! Try again.")
playerInput = "Invalid"
return playerInput
def increaseEnemyDefeated():
with open("player.json", "r") as f:
data = json.load(f)
enemysDefeated = data.get("enemysDefeated")
enemysDefeated = enemysDefeated + 1
data["enemysDefeated"] = enemysDefeated
with open("player.json", "w") as f:
json.dump(data, f)
def enemyEncounter():
enemyData = spawnEnemy()
playerData = getAttributes()
while True:
didEnemyAttack = False
playerInput = encounterInput()
chanceToHit = randint(1, 3)
chanceToFlee = randint(1, 4)
chanceToWin = randint(1, 50)
if playerInput == "Attack" and chanceToHit > 1:
enemyData.health = enemyData.health - playerData.damage
if enemyData.health <= 0:
enemyData.health = 0
print(f"You hit your attack! Enemy Health: {enemyData.health}")
if enemyData.health <= 0:
print("Enemy killed!")
increaseEnemyDefeated()
break
elif playerInput == "Attack" and chanceToHit == 1:
print("You missed your attack!")
if (
playerInput == "Flee"
and chanceToFlee < 4
and playerData.speed >= enemyData.speed
):
print("You fleed!")
break
elif playerInput == "Flee" and chanceToFlee == 4:
print("You tripped and was unable to flee!")
if playerInput == "Do nothing" and chanceToWin == 1:
print("You did nothing and the enemy died! Maybe from intimidation.")
break
elif playerInput == "Do nothing" and chanceToWin != 1:
print("You did nothing!")
if playerInput != "Invalid":
# Enemy attack
enemyFleeChance = randint(1, 5)
enemyAttackChance = randint(1, 2)
if enemyAttackChance == 1:
didEnemyAttack = True
if randint(1, 2) == 1:
enemyAttackDamage = enemyData.damage - enemyFleeChance
else:
enemyAttackDamage = enemyData.damage + enemyFleeChance
playerData.health = playerData.health - enemyAttackDamage
print(
f"The {enemyData.enemy} hit you and did {enemyAttackDamage} damage!"
)
with open("player.json", "r") as f:
data = json.load(f)
data["currentHealth"] = playerData.health
with open("player.json", "w") as f:
json.dump(data, f)
if playerData.health <= 0:
print("You have died! Reseting game.")
game.resetGame()
quit()
else:
print(f"You are now at {playerData.health}")
else:
print("The enemy attacked and missed!")
if enemyFleeChance == 5 and didEnemyAttack == False:
print("Enemy has fleed!")
break
else:
continue