Skip to content

Commit ebd2328

Browse files
authored
Adding python game
This is a RPG (Role Playing Game) Text-Based game . Where you are going to Trap as a warrior in jungle you have to pass hurdle to achieve points. i have also added guiding text in game for better understanding of controls.. Feel free to make changes in project. please accept this project under Hacktoberfest2022!
1 parent 1bbd324 commit ebd2328

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

Text Based Adventure Game.py

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Python Text RPG GAme
2+
3+
# defining main function
4+
def main():
5+
pass
6+
7+
if __name__ == '__main__':
8+
main()
9+
from random import randint
10+
11+
import pygame
12+
from pygame import mixer
13+
pygame.init()
14+
mixer.music.load('Everlasting Devotion.mp3')
15+
mixer.music.play(-1)
16+
17+
# Creating character of the game
18+
class Character:
19+
def __init__(self):
20+
self.name = ""
21+
self.health = 1
22+
self.health_max = 1
23+
def do_damage(self, enemy):
24+
damage = min(
25+
max(randint(0, self.health) - randint(0, enemy.health), 0),
26+
enemy.health)
27+
enemy.health = enemy.health - damage
28+
if damage == 0: print(enemy.name +" evades "+ self.name +"'s attack.")
29+
else: print(self.name+" hurts "+ enemy.name+"!")
30+
return enemy.health <= 0
31+
32+
# Creating Enemy of the game
33+
class Enemy(Character):
34+
def __init__(self, player):
35+
Character.__init__(self)
36+
self.name = 'a goblin'
37+
self.health = randint(1, player.health)
38+
39+
# Defining attributes of the character
40+
class Player(Character):
41+
def __init__(self):
42+
Character.__init__(self)
43+
self.state = 'normal'
44+
self.health = 10
45+
self.health_max = 10
46+
47+
# Defining quit function
48+
def quit(self):
49+
print(self.name+" can't find the way back home, and dies of starvation.\nR.I.P.")
50+
self.health = 0
51+
52+
# Defining help function
53+
def help(self): print(Commands.keys())
54+
55+
# Defining status function
56+
def status(self): print(self.name+"'s health: "+str(self.health)+"/"+str(self.health_max))
57+
58+
# Defining tired function
59+
def tired(self):
60+
print (self.name+" feels tired. ")
61+
self.health = max(1, self.health - 1)
62+
63+
# Defining rest function
64+
def rest(self):
65+
if self.state != 'normal': print (self.name+" can't rest now! "); self.enemy_attacks()
66+
else:
67+
print(self.name+" rests. ")
68+
if randint(0, 1):
69+
self.enemy = Enemy(self)
70+
print(self.name+ " is rudely awakened by "+self.enemy.name+ "!")
71+
self.state = ' fight '
72+
self.enemy_attacks()
73+
else:
74+
if self.health < self.health_max:
75+
self.health = self.health + 1
76+
else: print(self.name +" slept too much.") ; self.health = self.health - 1
77+
78+
# Defining epxlore function
79+
def explore(self):
80+
if self.state != 'normal':
81+
print(self.name+" is too busy right now!")
82+
self.enemy_attacks()
83+
else:
84+
print(self.name +" explores a twisty passage.")
85+
if randint(0, 1):
86+
self.enemy = Enemy(self)
87+
print (self.name+" encounters " +self.enemy.name+"!")
88+
self.state = 'fight'
89+
else:
90+
if randint(0, 1): self.tired()
91+
92+
# Defining flee function
93+
def flee(self):
94+
if self.state != 'fight': print(self.name+" runs in circles for a while.") ; self.tired()
95+
else:
96+
if randint(1, self.health + 5) > randint(1, self.enemy.health):
97+
print(self.name+" flees from "+self.enemy.name+".")
98+
self.enemy = None
99+
self.state = 'normal'
100+
else: print(self.name+" couldn't escape from "+ self.enemy.name+"!"); self.enemy_attacks()
101+
102+
# Defining attack function
103+
def attack(self):
104+
if self.state != 'fight': print(self.name +" swats the air, without notable results."); self.tired()
105+
else:
106+
if self.do_damage(self.enemy):
107+
print(self.name+ " executes "+self.enemy.name+"!")
108+
self.enemy = None
109+
self.state = 'normal'
110+
if randint(0, self.health) < 10:
111+
self.health = self.health + 1
112+
self.health_max = self.health_max + 1
113+
print (self.name+" feels stronger!" )
114+
else: self.enemy_attacks()
115+
def enemy_attacks(self):
116+
if self.enemy.do_damage(self): print (self.name+" was slaughtered by "+self.enemy.name +"!!!\nR.I.P.")
117+
118+
Commands = {
119+
'quit': Player.quit,
120+
'help': Player.help,
121+
'status': Player.status,
122+
'rest': Player.rest,
123+
'explore': Player.explore,
124+
'flee': Player.flee,
125+
'attack': Player.attack,
126+
}
127+
128+
129+
# Taking input from the user
130+
p = Player()
131+
p.name = input("What is your character's name? ")
132+
print("(type help to get a list of actions)\n")
133+
print('''On his way back to India '''+p.name+'''experienced a disastrous plane crash. Fortunately he survived. When he came to his
134+
consciousness he realised that he is on a very dense jungle. He has to survive on this jungle in order to make his route to home. ''')
135+
print(p.name+" enters a dark cave, searching for some food.")
136+
137+
while(p.health > 0):
138+
line = input("> ")
139+
args = line.split()
140+
if len(args) > 0:
141+
commandFound = False
142+
for c in Commands.keys():
143+
if args[0] == c[:len(args[0])]:
144+
Commands[c](p)
145+
commandFound = True
146+
break
147+
if not commandFound:
148+
print(p.name+" doesn't understand the suggestion.")

0 commit comments

Comments
 (0)