-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_warriorVSdragon.py
305 lines (253 loc) · 10.8 KB
/
game_warriorVSdragon.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#width=960,height=720
from tkinter import Tk, Frame, Canvas, PhotoImage, Text, Button, messagebox, NW
import os
import random
from turtle import position
imgpath = os.path.join(os.getcwd(), 'img')
class Character:
def __init__(self, runpath, canvas, position : bool = True, x : int = 0, y : int = 500, name='Warrior', hp : int = 1000):
self.canvas = canvas
self.position = position # right:False // left:True
self.x = x
self.y = y
self.name = name
self.live = True
# run images
self.run_images = {}
self.move_count = 0
self.run_images['run0'] =(PhotoImage(file = os.path.join(runpath,"run0.png")))
self.run_images['run1'] =(PhotoImage(file = os.path.join(runpath,"run1.png")))
#self.run_images['run2'] =(PhotoImage(file = os.path.join(runpath,"run2.png")))
#self.run_images['run3'] =(PhotoImage(file = os.path.join(runpath,"run1.png")))
self.run_images['stand'] =(PhotoImage(file = os.path.join(runpath,"stand.png")))
self.run_images['attack']=(PhotoImage(file = os.path.join(runpath,"attack.png")))
self.run_images['hurt']=(PhotoImage(file = os.path.join(runpath,"hurt.png")))
self.run_images['lose0'] =(PhotoImage(file = os.path.join(runpath,"lose0.png")))
self.run_images['lose1'] =(PhotoImage(file = os.path.join(runpath,"lose1.png")))
self.run_images['win0'] =(PhotoImage(file = os.path.join(runpath,"win0.png")))
self.run_images['win1'] =(PhotoImage(file = os.path.join(runpath,"win1.png")))
self.image_on_canvas = self.canvas.create_image(self.x, self.y, anchor = NW, image = self.run_images['stand'])
# HP
self.maxhp = hp
self.hp = hp
# run_status
self.run_finish = False
# NAME HP_bar HP_status
if position:
self.canvas.create_text(105, 35, text=self.name, font=('Arial', 18))
self.HP_bar = self.canvas.create_rectangle(50, 50, 50+3*(self.maxhp//10), 70,fill='green')
self.HP_status = self.canvas.create_text(115, 85, text="[%3d/%3d]" % (self.hp,self.maxhp), font=('Arial', 18))
else:
self.canvas.create_text(860, 35, text=self.name, font=('Arial', 18))
self.HP_bar = self.canvas.create_rectangle(910-3*(self.maxhp//10), 50, 910, 70,fill='blue')
self.HP_status = self.canvas.create_text(850, 85, text="[%3d/%3d]" % (self.hp,self.maxhp), font=('Arial', 18))
def run(self):
self.move_count += 1
# change image
self.canvas.itemconfig(self.image_on_canvas, image = self.run_images['run'+str(self.move_count%2)])
if self.position == False:
move_step = -7
else:
move_step = 7
self.canvas.move(self.image_on_canvas, move_step, 0)
# TODO: show the fight img
if self.move_count < 24:
self.canvas.after(200, self.run)
else:
self.canvas.after(200, self.stand)
self.run_finish = True
def stand(self):
self.canvas.itemconfig(self.image_on_canvas, image = self.run_images['stand'])
def attack(self, enemy, dmg: int = 0):
enemy.hurted(dmg)
# action
self.canvas.itemconfig(self.image_on_canvas, image = self.run_images['attack'])
self.canvas.after(500, self.stand)
def hurted(self, hp):
self.hp -= hp
if self.hp < 0:
self.hp = 0
# HP bar update
x0, y0, x1, y1 = self.canvas.coords(self.HP_bar)
if self.position:
x1 = 50+3*(self.hp//10)
else:
x0 = 910-3*(self.hp//10)
self.canvas.coords(self.HP_bar, x0, y0, x1, y1)
# HP status update
self.canvas.itemconfig(self.HP_status, text="[%3d/%3d]" % (self.hp,self.maxhp))
# change bar color
if self.hp <= self.maxhp // 5:
self.canvas.itemconfig(self.HP_bar, fill='red')
elif self.hp <= self.maxhp // 2:
self.canvas.itemconfig(self.HP_bar, fill='yellow')
self.canvas.itemconfig(self.image_on_canvas, image = self.run_images['hurt'])
self.canvas.after(1000, self.stand)
def lose(self):
self.canvas.itemconfig(self.image_on_canvas, image = self.run_images['lose'+str(self.move_count%2)])
self.move_count+=1
self.live = False
self.canvas.after(200, self.lose)
def win(self):
self.canvas.itemconfig(self.image_on_canvas, image = self.run_images['win'+str(self.move_count%2)])
if self.position:
print(1)
if self.move_count % 2 == 0:
move_step = 30
else:
move_step = -30
self.canvas.move(self.image_on_canvas, 0, move_step)
self.move_count+=1
self.canvas.after(200, self.win)
def destory(self):
self.canvas.delete(self.image_on_canvas)
self.canvas.delete(self.HP_bar)
self.canvas.delete(self.HP_status)
class GameFrame(Frame):
window_width = 960
window_height = 720
PAUSE = False
warning = None
def __init__(self, tk, width=window_width, height=window_width):
Frame.__init__(self, tk, width=width, height=height)
self.focus_set()
self.gameover = False
# display object
self.display()
self.bind('<Return>', self.player_attack)
#self.bind('<space>', self.player_attack)
# font counter
self.font_counter = []
# check font counter
popList = []
for i in range(len(self.font_counter)):
if self.font_counter[i][1] == 0:
self.canvas.delete(self.font_counter[i][0])
popList.append(i)
else:
self.font_counter[i][1] -= 1
for count, i in enumerate(popList):
self.font_counter.pop(i - count)
# player
self.player = Character(os.path.join(imgpath, 'warrior/') ,self.canvas, position=True, x=-70, y=350, name="Warrior", hp=1000)
# dragon
self.dragon = Character(os.path.join(imgpath, 'dragon/') ,self.canvas, position=False, x=self.window_width - 200, y=350, name="dragon", hp=1000)
self.dragon_hit_count = random.randint(50, 100)
self.start_game()
def start_game(self):
self.player.run()
self.dragon.run()
self.update()
def update(self):
if self.player.run_finish == False or self.dragon.run_finish == False:
self.after(100, self.update)
return
# dragon attack in random time
if not self.PAUSE:
if self.dragon_hit_count == 0:
self.dragon_hit_count = random.randint(25, 50)
self.dragon_attack(random.randint(20, 100))
else:
self.dragon_hit_count -= 1
if self.dragon_hit_count == 0:
self.canvas.delete(self.warning)
elif self.dragon_hit_count == 20:
self.warning=self.canvas.create_text(700,150,text='CAREFUL!!',font=('Helvetica',36, 'bold'),fill = 'red')
# check font counter
popList = []
for i in range(len(self.font_counter)):
if self.font_counter[i][1] == 0:
self.canvas.delete(self.font_counter[i][0])
popList.append(i)
else:
self.font_counter[i][1] -= 1
for count, i in enumerate(popList):
self.font_counter.pop(i - count)
if self.dragon.hp == 0:
self.GAMEOVER(1)
elif self.player.hp == 0:
self.GAMEOVER(0)
else:
self.after(100, self.update)
def display(self):
self.canvas = Canvas(
self,
width=self.window_width,
height=self.window_height,
highlightthickness=0)
# Author: Carol Ling Mei Xin
path = os.path.join(imgpath, 'bg.png')
self.image_bg = PhotoImage(
file=path)
self.canvas.create_image(0, 0, image=self.image_bg, anchor=NW)
self.canvas.pack()
'''
# TextBox Creation
self.inputtxt = Text(self, height = 2, width = 20)
self.inputtxt.grid(row=1, column=0)
# Button Creation
attackButton = Button(self, text = "Attack", height = 2, width = 20, command = self.player_attack)
attackButton.grid(row=1, column=1)
restartButton = Button(self, text = "restart", height = 2, width = 20, command = self.restart)
restartButton.grid(row=1, column=2)
quitButton = Button(self, text = "quit", height = 2, width = 20, command = self.quit)
quitButton.grid(row=1, column=3)'''
def restart(self):
if self.player:
self.player.destory()
if self.dragon:
self.dragon.destory()
del self.player
del self.dragon
# player
self.player = Character(os.path.join(imgpath, 'warrior/') ,self.canvas, position=True, x=-70, y=350, name="Warrior", hp=100)
# dragon
self.dragon = Character(os.path.join(imgpath, 'dragon/') ,self.canvas, position=False, x=self.window_width - 200, y=350, name="dragon", hp=100)
self.dragon_hit_count = random.randint(1, 100)
self.start_game()
def quit(self):
self.player.destory()
self.dragon.destory()
self.destroy()
def dragon_attack(self, dmg, event=None):
self.dragon.attack(self.player, dmg)
x = random.randint(400, 700)
y = random.randint(200, 300)
c = self.canvas.create_text(x,y,text=str(dmg),font=('Helvetica',36, 'bold'),fill = 'red')
self.font_counter.append([c, 10])
def player_attack(self, dmg, event=None):
self.player.attack(self.dragon, int(dmg))
x = random.randint(400, 700)
y = random.randint(200, 300)
c = self.canvas.create_text(x,y,text=str(dmg),font=('Helvetica',36, 'bold'),fill = 'blue')
self.font_counter.append([c, 10])
def pauseGAME(self):
self.PAUSE = True
def continueGAME(self):
self.PAUSE = False
def GAMEOVER(self, state):
'''
0 -> dragon win
1 -> Warrior win
'''
self.gameover = True
# clear all word
for i in range(len(self.font_counter)):
self.canvas.delete(self.font_counter[i][0])
self.canvas.delete(self.warning)
if state == 0:
self.player.lose()
self.dragon.win()
#messagebox.showinfo('', 'GAME OVER\ndragon Wins')
else:
self.player.win()
self.dragon.lose()
#messagebox.showinfo('', 'GAME OVER\nWarriar Wins')
if __name__ == "__main__":
tk = Tk()
tk.title('KILL THE DRAGON')
tk.geometry("960x720+400+175")
tk.resizable(width=False, height=False) # unable to resize
gameframe = GameFrame(tk)
gameframe.pack()
tk.mainloop()