-
Notifications
You must be signed in to change notification settings - Fork 3
/
game_easy.py
286 lines (254 loc) · 8.76 KB
/
game_easy.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#thaycacac
import turtle
import math
import random
import main
def init_game(name_level, map_level):
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("A Maze Game")
screen.setup(1000, 750)
screen.tracer(0)
#register shape
wall_easy = "wall_easy.gif"
screen.addshape(wall_easy)
wall_medium = "wall_medium.gif"
screen.addshape(wall_medium)
wall_hard = "wall_hard.gif"
screen.addshape(wall_hard)
image_princess = "princess.gif"
screen.addshape(image_princess)
image_logo_teky = "logo_teky.gif"
screen.addshape(image_logo_teky)
left = "left.gif"
screen.addshape(left)
right = "right.gif"
screen.addshape(right)
top = "top.gif"
screen.addshape(top)
bottom = "bottom.gif"
screen.addshape(bottom)
image_monsters = ["monster_0.gif", "monster_1.gif", "monster_2.gif", "monster_3.gif", "monster_4.gif"]
for i in range(5):
screen.addshape(image_monsters[i])
image_treasures = ["treasure_0.gif", "treasure_1.gif", "treasure_2.gif", "treasure_3.gif", "treasure_4.gif"]
for i in range(5):
screen.addshape(image_treasures[i])
# create list
walls = []
treasures = []
monsters = []
#show hp
def show_hp(hp):
turtle.clear()
turtle.color('white')
turtle.penup()
turtle.goto(380, 305)
style = ('Courier', 20, 'bold')
turtle.write("HP: {0}".format(hp), font=style, align='center')
turtle.hideturtle()
#create logo teky
class LogoTeky(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape(image_logo_teky)
self.penup()
self.goto(-420, 330)
self.onclick(self.main)
def main(self, x, y):
turtle.clearscreen()
main.main()
turtle.done()
#create pen
class Pen(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
if name_level == "easy":
self.shape(wall_easy)
elif name_level == "medium":
self.shape(wall_medium)
else:
self.shape(wall_hard)
self.penup()
self.speed(0)
#create player
class Player(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape(bottom)
self.penup()
self.speed(0)
self.hp = 500
show_hp(self.hp)
def up(self):
self.shape(top)
move_to_x = self.xcor()
move_to_y = self.ycor() + 24
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def down(self):
self.shape(bottom)
move_to_x = self.xcor()
move_to_y = self.ycor() - 24
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def left(self):
self.shape(left)
move_to_x = self.xcor() - 24
move_to_y = self.ycor()
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def right(self):
self.shape(right)
move_to_x = self.xcor() + 24
move_to_y = self.ycor()
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def is_collision(self, other):
a = self.xcor() - other.xcor()
b = self.ycor() - other.ycor()
distance = math.sqrt((a ** 2) + (b ** 2))
if distance < 5:
return True
else:
return False
#create treasure
class Treasure(turtle.Turtle):
def __init__(self, x, y):
turtle.Turtle.__init__(self)
if name_level == "easy":
self.number_random = random.randint(0, 1)
elif name_level == "medium":
self.number_random = random.randint(0, 2)
else:
self.number_random = random.randint(3, 4)
self.shape(image_treasures[self.number_random])
self.hp = (self.number_random + 1) * 100
self.penup()
self.speed(0)
self.goto(x, y)
def destroy(self):
self.goto(2000, 2000)
self.hideturtle()
#create princess
class Princess(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape(image_princess)
self.penup()
self.speed(0)
def destroy(self):
self.goto(2000, 2000)
self.hideturtle()
#class monster
class Monster(turtle.Turtle):
def __init__(self, x, y):
turtle.Turtle.__init__(self)
if name_level == "easy":
self.number_random = random.randint(0, 1)
elif name_level == "medium":
self.number_random = random.randint(0, 2)
else:
self.number_random = random.randint(3, 4)
self.shape(image_monsters[self.number_random])
self.hp = -(self.number_random + 1) * 100
self.penup()
self.speed(0)
self.gold = -25
self.goto(x, y)
self.direction = random.choice(["up", "down", "left", "right"])
def move(self):
if self.direction == "up":
x = 0
y = 24
elif self.direction == "down":
x = 0
y = -24
elif self.direction == "left":
x = -24
y = 0
elif self.direction == "right":
x = 24
y = 0
move_to_x = self.xcor() + x
move_to_y = self.ycor() + y
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
else:
self.direction = random.choice(["up", "down", "left", "right"])
turtle.ontimer(self.move, t = random.randint(100, 300))
def destroy(self):
self.goto(2000, 2000)
self.hideturtle()
#setup level
def setup_maze(level):
for y in range(len(level)):
for x in range(len(level[y])):
character = level[y][x]
position_x = -456 + (x * 24)
position_y = 288 - (y * 24)
if character == "X":
pen.goto(position_x, position_y)
pen.stamp()
walls.append((position_x, position_y))
elif character == "P":
player.goto(position_x, position_y)
elif character == "T":
treasures.append(Treasure(position_x, position_y))
elif character == "E":
monsters.append(Monster(position_x, position_y))
elif character == "H":
princess.goto(position_x, position_y)
princess.stamp()
#create instance
pen = Pen()
player = Player()
princess = Princess()
logo_teky = LogoTeky()
#keyboard bindding
turtle.listen()
turtle.onkey(player.up, "Up")
turtle.onkey(player.down, "Down")
turtle.onkey(player.right, "Right")
turtle.onkey(player.left, "Left")
#set up level
setup_maze(map_level)
#run monster
for monster in monsters:
turtle.ontimer(monster.move, 250)
while True:
for treasure in treasures:
if player.is_collision(treasure):
player.hp += treasure.hp
show_hp(player.hp)
treasure.destroy()
treasures.remove(treasure)
for monster in monsters:
if player.is_collision(monster):
player.hp += monster.hp
show_hp(player.hp)
monster.destroy()
monsters.remove(monster)
if player.hp <= 0:
turtle.clearscreen()
turtle.color('red')
turtle.penup()
turtle.goto(0, -45)
style = ('Courier', 24, 'italic')
turtle.write('You died! Play again', font=style, align='center')
turtle.hideturtle()
main.main()
turtle.done()
break
if player.is_collision(princess) and len(treasures) == 0:
turtle.clearscreen()
turtle.color('blue')
turtle.penup()
turtle.goto(0, -45)
style = ('Courier', 24, 'italic')
turtle.write('You are winner!', font=style, align='center')
turtle.hideturtle()
main.main()
turtle.done()
break
screen.update()