forked from 0chroma/Python-battle-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
161 lines (152 loc) · 3.95 KB
/
main.py
File metadata and controls
161 lines (152 loc) · 3.95 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
import weapons
from enemy import *
from items import *
from player import Player
import random
# If you want to add in saving, check out the 'pickle' module.
# some configuration stuff
CHEATS = False # set to true for cheats. Good for debugging.
# menu constants
ATTACK = 1
RUN = 2
USE_ITEM = 3
EXIT = 4
class RPG:
def main(self):
"game setup, and main loop"
self.player = Player()
# for now, let's just make it a series of battles
# you can add in more stuff if you want.
for i in range(1, 5):
self.battle()
choice = "y"
while 1:
print "Use an item? (y/n)"
choice = raw_input()
if choice == "n":
break
self.item_menu()
def battle(self):
"initiate a battle sequence"
#get an enemy, give it a random weapon, and give it a random level (but no more then one less then the player)
level = range(1, self.player.level-1)
if not level:
level = 1
else:
level = random.choice(level)
enemy = Enemy(level = level, weapon=random.choice([weapons.Dagger(),
weapons.ShortSword(),
weapons.LongSword()]))
print "Enemy %s appears!" % enemy.name
raw_input()
while self.player.hp > 0 and enemy.hp > 0:
while 1:
res = self.battleScreen(enemy)
if res is EXIT:
self.exit()
break
elif res is RUN:
if random.choice([True, False, False]):
print "You got away!"
raw_input()
return
else:
print "couldn't escape!"
raw_input()
return
elif res is ATTACK:
dmg = self.player.attack(enemy)
print "Enemy took %s damage" % dmg
break
elif res is USE_ITEM:
# item_menu will return false when the user chooses back
if self.item_menu():
break
#enemy's turn
enemy.think(self.player)
raw_input()
if self.player.hp <= 0:
print "You have died."
self.exit()
elif enemy.hp <= 0:
print "Enemy %s has been defeated!" % enemy.name
expGain = enemy.level * 100
self.player.giveExp(expGain)
return
def battleScreen(self, enemy):
"prints the battle screen, and calls self.menu to get input in the form of a menu"
print ".------------------->"
print "| Level %s %s" % (enemy.level, enemy.name)
print "| HP: %s/%s" % (enemy.hp, enemy.maxHp)
print "'------------------->"
print ".------------------->"
print "| Level %s %s" % (self.player.level, self.player.name)
print "| HP: %s/%s" % (self.player.hp, self.player.maxHp)
print "'------------------->"
print ""
return self.menu()
def menu(self):
"draws the menu, and returns the action the user requested"
print ".-----------."
print "| 1. Attack |"
print "| 2. Item |"
print "| 3. Run |"
print "| 4. Exit |"
print "'-----------'"
try:
do = int(raw_input())
except ValueError:
print "invalid choice"
raw_input()
return
if do == 1:
return ATTACK
elif do == 2:
return USE_ITEM
elif do == 3:
return RUN
elif do == 4:
return EXIT
else:
print "invalid choice"
raw_input()
return
def item_menu(self):
"draws the items menu based on the user's inventory. When an item is used, it's 'use' method is called, and then it is removed from their inventory."
inv = self.player.inventory
print ".------------------->"
for i in range(0, len(inv)):
print "| %s. %s (%s)" % (i+1, inv[i].name, inv[i].extraText)
print "| X. Back"
print "'------------------->"
do = raw_input()
if do == "X" or do == "x":
return False
else:
try:
i = int(do)-1
if i < 0:
raise ValueError
item = inv[i]
item.use(self.player)
left = inv[0:i]
right = inv[i+1:len(inv)]
if i-2 < 0:
left = []
if i >= len(inv)-1:
right = []
self.player.inventory = left+right
return True
except (LookupError,ValueError):
print "invalid input"
raw_input()
return self.item_menu()
def exit(self):
"exits the game"
print "Goodbye"
exit()
# start the game
if __name__ == "__main__": # only run the game if we're the main script
rpg = RPG()
rpg.main()