This repository has been archived by the owner on May 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameMode.py
144 lines (120 loc) · 5.02 KB
/
gameMode.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
# __main__
# Modules and files to import
from tkinter import *
from Zombie import *
from Weapon import *
from buttonClass import *
import random
##############################
class gameMode(object):
width, height = 800, 400
# Model
def __init__(self, temp):
self.initImg()
self.initButts()
self.zomb = Zombie(gameMode.width, gameMode.height/2, self.zombImg)
self.gunn = Gun(self.gunImg)
diff = random.randint(50, 150)
self.zombs = \
[Zombie(gameMode.width+i*random.randint(200, 400),\
random.choice(range(100, gameMode.height-100,50)), self.zombImg) for i in range(temp)]
self.temp = temp
self.bulls = []
self.originNum = len(self.zombs)
self.isReloading = False
self.reload = 500
self.HP = 1
self.origin = self.HP
# game status:
self.isLost = False
self.isWon = False
self.isPause = False
# bullet's position start at (135, data.gunn.cy - 7.5)
def initImg(self): # initialize images for objects
self.backImg = PhotoImage(file = 'img/background.gif')
self.zombImg = PhotoImage(file = 'img/zombie.gif').subsample(3)
self.gunImg = PhotoImage(file = 'img/gun.gif').subsample(10)
self.bullImg = PhotoImage(file = 'img/bullet.gif').subsample(20)
self.bombImg = PhotoImage(file = 'img/bomb.gif').subsample(10)
def initButts(self):
cx1, cx2, cy = gameMode.width/4, gameMode.width*3/4, gameMode.height*8/9
self.pauseButt = Elegant(cx1,cy,100,50,"Pause")
self.returnButt = Elegant(cx2,cy,100,50,"Stop")
# Controller
def mousePressed(self, event):
if self.pauseButt.isInside(event.x, event.y) and self.isPause==False:
self.isPause = True
self.pauseButt.label = "Resume"
elif self.pauseButt.isInside(event.x, event.y) and self.isPause==True:
self.isPause = False
self.pauseButt.label = "Pause"
elif self.returnButt.isInside(event.x, event.y):
return "Start"
def mouseMoved(self, event):
self.pauseButt.magicPrompt(event.x, event.y)
self.returnButt.magicPrompt(event.x, event.y)
def keyPressed(self, event):
self.gunn.keyController(event.keysym)
if event.keysym == 's':
self.bulls.append(Bullet(123, self.gunn.cy-7.5, self.bullImg))
elif (event.keysym == 'b' and self.isReloading == False):
# release a bomb
self.bulls.append(Bomb(123, self.gunn.cy-7.5, self.bombImg))
self.isReloading = True
self.reload = 500
def timerFired(self):
if not self.isPause:
for zomb in self.zombs:
zomb.move()
if zomb.cx <= 0:
self.HP -= 1
self.zombs.remove(zomb)
for bul in self.bulls:
bul.move()
if bul.cx > gameMode.width: self.bulls.remove(bul)
if len(self.bulls)>0 and len(self.zombs)>0:
# check if the bullet hit the monster
for bul in self.bulls:
for zomb in self.zombs:
d = ((bul.cx - zomb.cx)**2 + (bul.cy - zomb.cy)**2)**0.5
if d <= 30:
try: self.bulls.remove(bul)
except: pass
# data.zombs.remove(zomb)
zomb.takeHurt()
if isinstance(bul, Bomb): bul.explode(self.zombs)
if zomb.isDead: self.zombs.remove(zomb)
else:
# nothing
pass
# check game status
if len(self.zombs) == 0: return 'Win'
elif self.HP == 0: return 'Lose'
if self.isReloading and self.reload != 0:
self.reload = (self.reload - 1) % 500
if self.reload == 0:
self.reload = 500
self.isReloading = False
# Viewer
def redrawAll(self, canvas):
# draw in canvas
self.drawBackground(canvas)
self.showButts(canvas)
self.gunn.show(canvas)
for zomb in self.zombs:
zomb.show(canvas)
for bul in self.bulls:
bul.show(canvas)
self.showReload(canvas)
canvas.create_text(gameMode.width/2, 20, \
text = 'Number of Monsters left %d/%d' %(len(self.zombs), self.originNum), \
fill = 'dark red', font = 'impact 30')
def drawBackground(self, canvas):
canvas.create_image(gameMode.width/2, gameMode.height/2, image = self.backImg)
def showButts(self, canvas):
self.pauseButt.showButton(canvas)
self.returnButt.showButton(canvas)
def showReload(self, canvas):
if self.isReloading == True:
canvas.create_text(gameMode.width/2, gameMode.height*9/10, fill = 'red3',\
text='Time for reload bomb: %d' %(self.reload), font = 'impact 20')