1
- # Haunted House with python
1
+ from tkinter import *
2
+ import sys
3
+ from tkinter .ttk import *
2
4
import time
3
5
4
- def welcome ():
5
- print (" /\\ " )
6
- print (" / \\ " )
7
- print (" /\ / \\ " )
8
- print (" / \ / \ " )
9
- print (" / \/ \_/\\ " )
10
- print (" / Welcome to \\ " )
11
- print (" / \\ " )
12
- print (" | the Haunted House! |" )
13
- print (" | ____ ___ |" )
14
- print (" | | | |___| |" )
15
- print ("__|____|____|___________|__" )
16
- print ("" )
17
- time .sleep (1 )
18
-
19
- welcome ()
20
-
21
- def showInstructions ():
22
- print ('''
23
- Commands:
24
- use [item] eg: use key
25
-
26
- shoot[enemy] eg: shoot zombie
27
-
28
- go [direction] eg: go north
29
-
30
- get [object] eg: get bandage
31
- ''' )
32
-
33
- health = 100
34
-
35
- def showStatus ():
6
+ health = 100
36
7
37
- print ('You are in the ' + currentRoom )
38
- print (rooms [currentRoom ]['desc' ])
39
- print ("Inventory : " + str (inventory ))
40
- print ("Your health is" ,health )
41
- if "item" in rooms [currentRoom ]:
42
- print ('You see a ' + rooms [currentRoom ]['item' ])
43
- print ("---------------------------" )
44
-
45
-
46
-
47
8
inventory = []
48
9
49
10
monster = []
@@ -80,80 +41,168 @@ def showStatus():
80
41
81
42
currentRoom = 'Hall'
82
43
83
- showInstructions ()
84
44
85
- while True :
45
+ def welcome ():
46
+ label = Label (window , text = "Welcome to the Haunted House!" )
47
+ label .pack ()
48
+
49
+ def show_instructions ():
50
+ instructions = Message (new_window ,text = '''
51
+ Commands:
52
+ use [item] eg: use key
53
+
54
+ shoot[enemy] eg: shoot zombie
55
+
56
+ go [direction] eg: go north
57
+
58
+ get [object] eg: get bandage
59
+ ''' )
60
+ instructions .pack ()
86
61
87
- showStatus ()
62
+ def showStatus ():
63
+ health_bar ['value' ]= health
64
+ window .update_idletasks ()
65
+ health_bar .pack ()
66
+ location_label = Label (window ,text = 'You are in the ' + currentRoom )
67
+ location_label .pack ()
68
+ desc_label = Label (window ,text = rooms [currentRoom ]['desc' ])
69
+ desc_label .pack ()
70
+ inventory_label = Label (window ,text = f"Inventory : { str (inventory )} " )
71
+ inventory_label .pack ()
72
+ if "item" in rooms [currentRoom ]:
73
+ message_label = Label (window ,text = f"You see a { rooms [currentRoom ]['item' ]} " )
74
+ message_label .pack ()
88
75
89
- move = ''
90
- while move == '' :
91
- move = input ('>' )
76
+ def game (move ):
77
+ global currentRoom
78
+ global inventory
79
+ global health
92
80
93
- move = move .lower ().split ()
94
-
95
- if move [0 ]== "shoot" :
81
+ if move [0 ]== "shoot" :
96
82
if "monster" in rooms [currentRoom ] and move [1 ] in rooms [currentRoom ]['monster' ] and "shotgun" in inventory :
97
83
del rooms [currentRoom ]['monster' ]
98
- print ( "you killed the" , move [1 ])
84
+ message_label = Label (window ,text = f"you killed the { move [1 ]} " )
85
+ message_label .pack ()
99
86
rooms [currentRoom ].update ({"monster" :"dead" },)
100
87
101
88
else :
102
- print ( "you cannot attack " )
103
-
89
+ message_label = Label ( window , text = "you cannot attack " )
90
+ message_label . pack ()
104
91
105
-
106
- if move [0 ]== "use" :
92
+ if move [0 ]== "exit" :
93
+ time .sleep (1 )
94
+ sys .exit ()
95
+
96
+ if move [0 ]== "use" :
107
97
108
98
109
99
if "bandage" in inventory and move [1 ]== "bandage" :
110
100
heal = 40
111
101
health = min (100 ,health + heal )
112
102
inventory .remove ("bandage" )
113
- print ("you recovered 40 health (max 100)" )
103
+ message_label = Label (window ,text = "you recovered 40 health (max 100)" )
104
+ message_label .pack ()
114
105
115
106
elif "key" in inventory and move [1 ]== "key" and "golden_crown" in inventory and currentRoom == "Garden" :
116
- print ("you escape with the loot, you retire in style, you win!! " )
117
- break
107
+ message_label = Label (window ,text = "you escape with the loot, you retire in style, you win!! " )
108
+ message_label .pack ()
109
+ #here the program doesn't display the message it exits after 1 second
110
+ time .sleep (1 )
111
+ sys .exit ()
118
112
119
113
120
114
elif "key" in inventory and move [1 ]== "key" in inventory and currentRoom == "Garden" :
121
- print ("you escape the house but die a pauper, you lose " )
122
- break
115
+ message_label = Label (window ,text = "you escape the house but die a pauper, you lose " )
116
+ message_label .pack ()
117
+ #here the program doesn't display the message it exits after 1 second
118
+ time .sleep (1 )
119
+ sys .exit ()
123
120
124
121
125
122
126
123
else :
127
- print ("can't use that" )
124
+ message_label = Label (window ,text = "can't use that" )
125
+ message_label .pack ()
128
126
129
127
130
128
131
- if move [0 ] == 'go' :
129
+ if move [0 ] == 'go' :
132
130
if move [1 ] in rooms [currentRoom ]:
133
131
currentRoom = rooms [currentRoom ][move [1 ]]
134
132
else :
135
- print ('You can\' t go that way!' )
133
+ message_label = Label (window ,text = r"You can\'t go that way!" )
134
+ message_label .pack ()
136
135
137
- if move [0 ] == 'get' :
136
+ if move [0 ] == 'get' :
138
137
139
138
if 'item' in rooms [currentRoom ] and move [1 ] in rooms [currentRoom ]['item' ]:
140
139
inventory += [move [1 ]]
141
- print (move [1 ] + ' got!' )
140
+ message_label = Label (window ,text = f"{ move [1 ]} got!" )
141
+ message_label .pack ()
142
142
del rooms [currentRoom ]['item' ]
143
143
144
144
else :
145
- print ("Can\' t get that" )
145
+ message_label = Label (window ,text = r"Can\'t get that" )
146
+ message_label .pack ()
146
147
147
- if 'zombie' in rooms [currentRoom ]['monster' ]:
148
- print ("A zombie attack you !!!" )
148
+ if 'zombie' in rooms [currentRoom ]['monster' ]:
149
+ message_label = Label (window ,text = "A zombie attack you !!!" )
150
+ message_label .pack ()
149
151
health = health - 30
150
152
151
- if 'ghoul' in rooms [currentRoom ]['monster' ]:
152
- print ('A ghoul attack you !!!' )
153
+ if 'ghoul' in rooms [currentRoom ]['monster' ]:
154
+ message_label = Label (window ,text = 'A ghoul attack you !!!' )
155
+ message_label .pack ()
153
156
health = health - 20
157
+
158
+ if health < 0 :
159
+ message_label = Label (window ,text = "you are dead" )
160
+ message_label .pack ()
154
161
162
+ def get_input ():
163
+ showStatus ()
164
+ input = Entry (window ,width = 25 )
165
+ input .pack ()
166
+ submit_Button = Button (window ,text = "Submit" , command = lambda :submit (input ))
167
+ submit_Button .pack ()
168
+
169
+ def submit (input ):
170
+ move = input .get ().lower ().split ()
171
+ if input .get ():
172
+ game (move )
173
+ else :
174
+ Error_Lab = Label (window ,text = "Please Enter something" )
175
+ Error_Lab .pack ()
176
+ get_input ()
177
+
178
+ window = Tk ()
179
+ window .title ("Haunted House Game" )
180
+ window .geometry ('500x500' )
181
+
182
+ new_window = Toplevel (window )
183
+ new_window .title ("Commands" )
184
+
185
+ #tried to create a scrollbar but it did not work
186
+
187
+ #frame-Frame(window)
188
+ # frame.pack(,fill=BOTH)
189
+
190
+ # canvas = Canvas(frame,yscrollcommand=scrollbar.set)
191
+ # canvas.pack(fill=BOTH, expand=True)
192
+
193
+ # scrollbar.config(command=canvas.yview)
194
+
195
+
196
+ health_bar = Progressbar (window , orient = HORIZONTAL , length = 100 , mode = 'determinate' )
197
+
198
+ welcome ()
199
+
200
+ show_instructions ()
201
+
202
+ health_meter = Label (window ,text = "Your Health" )
203
+
204
+ health_meter .pack ()
205
+
206
+ get_input ()
155
207
156
- if health <= 0 :
157
- print ("you are dead" )
158
- break
159
-
208
+ window .mainloop ()
0 commit comments