-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
316 lines (279 loc) · 10.4 KB
/
main.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
306
307
308
309
310
311
312
313
314
315
316
import tkinter as tk
import random2
# move vectors
ACTIMEMOVE={"Up":False,"Down":False,"Left":False,"Right":False}
COUNT=0
INGAME = 0
width=600
height=400
# Set window
window = tk.Tk()
# Set window icon
window.iconbitmap("favico.ico")
# set window title
window.title("some game")
# set window size
window.geometry('600x400+50+50')
# set background color
window.configure(background="#07cc9b")
# set window size permanently
window.minsize(width, height)
window.maxsize(width, height)
# create game frame
playShield = tk.Frame(window,
bg="#07cc9b",
height=height,
width=width)
playShield.pack()
# create game canvas
playShieldCanvas = tk.Canvas(playShield,
height=390,
bg="#6DECAF",
width=590)
# load game canvas
playShieldCanvas.pack()
playShieldCanvas.focus_set()
# player class
class player(object):
# load
def __init__(self):
# create player circle
self.player = playShieldCanvas.create_oval(50,50,100,100,
activefill="#D1D1C7",
fill="#DFDFB4",
activeoutline="#D1D1C7",
outline="#D1D1C7")
# moving part 1
def movedown(self,event):
if event.keysym == "Down":
ACTIMEMOVE["Down"]=True
if event.keysym == "Left":
ACTIMEMOVE["Left"] = True
if event.keysym == "Up":
ACTIMEMOVE["Up"]=True
if event.keysym == "Right":
ACTIMEMOVE["Right"]=True
# moving part 2
def moveup(self,event):
if event.keysym == "Down":
ACTIMEMOVE["Down"]=False
if event.keysym == "Left":
ACTIMEMOVE["Left"] = False
if event.keysym == "Up":
ACTIMEMOVE["Up"]=False
if event.keysym == "Right":
ACTIMEMOVE["Right"]=False
# moving part 3
def move(self):
if ACTIMEMOVE["Up"] == True:
x1, y1, x2, y2 = playShieldCanvas.coords(self.player)
if y1 != 0:
playShieldCanvas.coords(self.player,
x1, y1 - 5,
x2, y2 - 5)
if ACTIMEMOVE["Down"] == True:
x1, y1, x2, y2 = playShieldCanvas.coords(self.player)
if y2 != 390:
playShieldCanvas.coords(self.player,
x1,y1+5,
x2,y2+5)
if ACTIMEMOVE["Left"] == True:
x1, y1, x2, y2 = playShieldCanvas.coords(self.player)
if x1 != 0:
playShieldCanvas.coords(self.player,
x1-5, y1,
x2-5, y2)
if ACTIMEMOVE["Right"] == True:
x1, y1, x2, y2 = playShieldCanvas.coords(self.player)
if x2!=590:
playShieldCanvas.coords(self.player,
x1+5, y1,
x2+5, y2)
# get player position
def getPos(self):
return(playShieldCanvas.coords(self.player))
def dell(self):
playShieldCanvas.delete(self.player)
# virus class
class virus(object):
# load
def __init__(self):
# create counts
self.count=0
self.viruses=[]
self.lenvirus=4
# create viruses
for i in range(self.lenvirus):
x = random2.randint(0, 590)
y = random2.randint(0, 390)
f=random2.randint(30,80)
b = playShieldCanvas.create_oval(x, y,
x + f, y + f,
fill="#F61067",
outline="#F61067")
vector = random2.randint(0, 1)
vector2 = random2.randint(0, 1)
vector = [vector, vector2]
b = [b, vector]
self.viruses.append(b) # add ready virus to massive
# delete all viruses
def dell(self):
for i in self.viruses:
playShieldCanvas.delete(i[0])
# move all viruses
def move(self):
for i in self.viruses:
x1,y1,x2,y2 = playShieldCanvas.coords(i[0])
if x1<=0:
i[1][0]=1
if x2>=590:
i[1][0]=0
if y1<=0:
i[1][1]=1
if y2>=390:
i[1][1]=0
if i[1][0] == 1:
x1, y1, x2, y2 = playShieldCanvas.coords(i[0])
playShieldCanvas.coords(i[0],
x1 + 1, y1,
x2 + 1, y2)
if i[1][1] == 1:
x1, y1, x2, y2 = playShieldCanvas.coords(i[0])
playShieldCanvas.coords(i[0],
x1, y1+1,
x2, y2+1)
if i[1][0] == 0:
x1, y1, x2, y2 = playShieldCanvas.coords(i[0])
playShieldCanvas.coords(i[0],
x1 - 1, y1,
x2 - 1, y2)
if i[1][1] == 0:
x1, y1, x2, y2 = playShieldCanvas.coords(i[0])
playShieldCanvas.coords(i[0],
x1, y1-1,
x2, y2-1)
# viruses AI
def checkCount(self,pl):
# print(self.viruses)
px1,py1,px2,py2=pl.getPos()
t=0
for i in self.viruses:
x1,y1,x2,y2=playShieldCanvas.coords(i[0])
if x1 < px1 <x2 and y1<py1<y2 or x1 < px2 <x2 and y1<py2<y2:
self.count += 1
playShieldCanvas.delete(i[0])
self.viruses.pop(t)
t+=1
# retry viruses on death
def update(self):
if len(self.viruses) != self.lenvirus:
x = random2.randint(0, 590)
y = random2.randint(0, 390)
f=random2.randint(30,80)
b = playShieldCanvas.create_oval(x, y,
x + f, y + f,
fill="#F61067",
outline="#F61067")
vector = random2.randint(0, 1)
vector2 = random2.randint(0, 1)
vector = [vector, vector2]
b = [b, vector]
self.viruses.append(b)
# killer class
class killer(object):
def __init__(self):
self.size = 60
self.ingame=1
self.x1=random2.randint(0,590-self.size)
self.y1=random2.randint(0,390-self.size)
self.x2=self.x1+self.size
self.y2=self.y1+self.size
self.killer= playShieldCanvas.create_oval(self.x1,self.y1,
self.x2,self.y2,
fill="#5E239D",
activefill="#48197A",
outline="#5E239D",
activeoutline="#5E239D")
self.vector=[random2.randint(0,1),random2.randint(0,1)]
def move(self):
self.x1,self.y1,self.x2,self.y2=playShieldCanvas.coords(self.killer)
print("x1:{}, y1:{}, x2:{}, y2:{}".format(self.x1,self.y1,self.x2,self.y2))
print("vector:{}".format(self.vector))
if self.x1<=0:
self.vector[0]=1
if self.x2>=590:
self.vector[0]=0
if self.y1<=0:
self.vector[1]=1
if self.y2>=390:
self.vector[1]=0
if self.vector[0] == 1:
x1,y1,x2,y2=playShieldCanvas.coords(self.killer)
playShieldCanvas.coords(self.killer,
x1 + 1, y1,
x2 + 1, y2)
if self.vector[0] == 0:
x1, y1, x2, y2 = playShieldCanvas.coords(self.killer)
playShieldCanvas.coords(self.killer,
x1 - 1, y1,
x2 - 1, y2)
if self.vector[1] == 0:
x1, y1, x2, y2 = playShieldCanvas.coords(self.killer)
playShieldCanvas.coords(self.killer,
x1, y1-1,
x2, y2-1)
if self.vector[1] == 1:
x1, y1, x2, y2 = playShieldCanvas.coords(self.killer)
playShieldCanvas.coords(self.killer,
x1, y1 + 1,
x2, y2 + 1)
def kill(self):
px1,py1,px2,py2=pl.getPos()
if self.x1 < px1 < self.x2 and self.y1 < py1 < self.y2 or self.x1 < px2 < self.x2 and self.y1 < py2 < self.y2:
vr.dell()
self.ingame=0
playShieldCanvas.delete(self.killer)
pl.dell()
# counter
count = tk.Label(playShield, text="Loading", foreground="black",background="#6DECAF")
count.place(y=10,x=10)
# game loop func
def main():
global INGAME
# if game pending
if INGAME:
# move player
pl.move()
vr.move()
vr.checkCount(pl)
vr.update()
kl.move()
kl.kill()
if kl.ingame == 0:
INGAME=0
count.configure(text="Score is: {}".format(vr.count))
else:
restart.place(y=190, x=290)
# retry func
window.after(10,main)
# Start
def start():
restart.place_forget()
global pl, vr, kl, INGAME
# create class example
pl = player()
kl = killer()
vr = virus()
# bind move keys
playShieldCanvas.bind_all("<KeyPress>", pl.movedown)
playShieldCanvas.bind_all("<KeyRelease>", pl.moveup)
# run game loop func
INGAME = 1
imp = tk.PhotoImage(file="button.png")
# restart = tk.Button(playShield, text="restart",foreground="black",background="#6DECAF",command=start)
restart = tk.Button(playShield,image=imp, border=0,background="#6DECAF",command=start)
if __name__ == "__main__":
start()
main()
# window update loop
window.mainloop()