-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
334 lines (268 loc) · 8.9 KB
/
game.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from Board import Board
from Paddles import Paddles
from Balls import Balls
from Bricks import Bricks
from PowerUps import PowerUps
from Bombs import Bombs
from Elements import BricksArray, PowerUpArray
from drawings import instructions , thankyou, trophy
from termcolor import *
import colorama
colorama.init()
import random, sys, os, time, copy, signal
from playsound import playsound
try:
import tty, termios
except ImportError:
try:
import msvcrt
except ImportError:
raise ImportError('getch not available')
else:
getch = msvcrt.getch
else:
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def alarmHandler(signum, frame):
raise AlarmException
class AlarmException(Exception):
pass
class Game():
# __init__ is a constructor and is used to initalize the attributes of the class.
def __init__(self,f,hit_paddle,TIME,X,Y,orignal,BRICK,POWER,points,level,level_time,ufo_x,ufo_y,ufo_health):
# Thses all written below are instance variables
# methods are the functions which are defined in the class to manipulate these attributes
# self is an object
self.x = X
self.y = Y
self.f = f
self.hit_paddle = hit_paddle
self.orignal = orignal
self.bricks = BRICK
self.power = POWER
self.points = points
self.level = level
self.level_time = level_time
self.ufo_x = ufo_x
self.ufo_y = ufo_y
self.ufo_health = ufo_health
self.paddle = Paddles(self.x,self.y,self.orignal,self.bricks,self.power,self.points,self.level)
self.ball = Balls(self.x,self.y,self.orignal,self.bricks,self.power,self.points,self.paddle)
self.bomb = Bombs(self.x,self.y,self.orignal,self.bricks,self.power,self.paddle,self.ball,self.level,self.ufo_x,self.ufo_y,self.ufo_health)
self.brick = Bricks(self.x,self.y,self.orignal,self.bricks,self.power,self.points,self.paddle,self.ball)
self.powerup = PowerUps(self.x,self.y,self.orignal,self.bricks,self.power,self.points,self.paddle,self.ball,self.brick)
self.board = Board(self.x,self.y,self.orignal,self.bricks,self.power,self.points,self.paddle,self.ball,self.brick,self.powerup)
self.time = TIME
self.GAMEOVER = False
def input(self,timeout=1):
signal.signal(signal.SIGALRM, alarmHandler)
signal.alarm(timeout)
try:
inp = getch()
signal.alarm(0)
if inp == 'q':
sys.exit()
# Level-Up
if inp == 'l':
return 'l'
if inp == 'p':
getch()
if inp == 'a':
self.paddle.movePaddle(self,-2)
if self.level == 3:
self.bomb.moveUFO(self,-1)
elif inp == 'd':
self.paddle.movePaddle(self,2)
if self.level == 3:
self.bomb.moveUFO(self,1)
elif inp == 's':
self.f=1
else:
pass
return ''
except AlarmException:
signal.signal(signal.SIGALRM, signal.SIG_IGN)
return ''
def update(self):
self.time += 1
self.level_time += 1
# Sound for hitting the paddle is already in bombs.py file
if self.level == 3:
self.bomb.savage_bricks(self)
self.bomb.bombfall(self)
if self.f == 1:
self.hit_paddle = self.bomb.ball_ufo_collision(self)
else:
self.ball.Ball_on_Paddle(self)
else:
self.brick.Ball_Brick_Collision(self)
self.powerup.Update_PowerUp(self)
self.powerup.Remove_PowerUp(self)
if self.f == 1:
self.hit_paddle =self.ball.moveBall(self)
self.f = self.ball.Update_Flag(self)
else:
self.ball.Ball_on_Paddle(self)
if self.hit_paddle == 1:
# Input an existing mp3 filename
mp3File = "./sounds/hit.WAV"
# Play the mp3 file
playsound(mp3File)
# Falling bricks
if self.level_time > 500:
for i in reversed(range(self.y-4)):
for j in range(1, self.x-2):
if (self.orignal[i][j] == 'b1') or (self.orignal[i][j] == 'b2') or (self.orignal[i][j] == 'b3') or (self.orignal[i][j] == 'U') or (self.orignal[i][j] == '$') or (self.orignal[i][j] == 'r'):
# self.y -5 is 1 level above of ball(here games get over)
if i == self.y - 5:
thankyou()
sys.exit()
self.orignal[i+1][j] = self.orignal[i][j]
self.orignal[i][j] = ' '
os.system("clear")
def updatedboard(self):
return self.board.Updated_org_Array(self)
def updatedbricks(self):
return self.board.Updated_Bricks_Array(self)
def updatedpoints(self):
return self.brick.Updated_Points(self)
def updatedpower(self):
return self.board.Updated_Power_Array(self)
def updatedhealth(self):
return self.bomb.updatedhealth(self)
def gameOver(self):
os.system("clear")
self.board.display()
# Input an existing mp3 filename
mp3File = "./sounds/Gameover.mp3"
# Play the mp3 file
playsound(mp3File)
if self.level == 3:
print("Bomb exploded on you :-(")
else:
print("Ball missed the paddle :-(")
self.GAMEOVER = True
print("Press any key to continue")
getch()
def gameWon(self):
print("Congrats you won the game")
trophy()
# Input an existing mp3 filename
mp3File = "./sounds/game_won.WAV"
# Play the mp3 file
playsound(mp3File)
getch()
def scoreboard(self):
print("Lives:",end='')
for _ in range(lives):
cprint("\u2764 ",'red',attrs=['bold'],end='')
points=self.brick.Updated_Points(self)
print(" Level:",self.level," SCORE:",points," time:",self.time)
if self.level == 3:
ufo_health = self.bomb.updatedhealth(self)
maxHealth = 200 # Max Health
healthDashes = 37 # Max Displayed dashes
dashConvert = int(maxHealth/healthDashes) # Get the number to divide by to convert health to dashes (being 10)
currentDashes = int(ufo_health/dashConvert) # Convert health to dash count: 80/10 => 8 dashes
remainingHealth = healthDashes - currentDashes # Get the health remaining to fill as space => 12 spaces
healthDisplay = '-' * currentDashes # Convert 8 to 8 dashes as a string: "--------"
remainingDisplay = ' ' * remainingHealth # Convert 12 to 12 spaces as a string: " "
percent = str(int((ufo_health/maxHealth)*100)) + "%" # Get the percent as a whole number: 40%
print("UFO Health: ",end='')
print("|" + healthDisplay + remainingDisplay + "|") # Print out textbased healthbar
print(" " + percent)
# This function is used to create the raw board for each new level
def create_raw_board(X, Y, orignal):
orignal = [ [' ' for j in range(X)] for i in range(Y)]
orignal[0] = ['#']*X
orignal[Y-1] = ['#']*X
for j in range(1, Y-1):
for i in range(X):
if j==Y-3:
if i==0 or i==X-10:
orignal[j][i] = '#'
else:
orignal[j][i] = ' '
else:
if i==0 or i==X-1:
orignal[j][i] = '#'
else:
orignal[j][i] = ' '
return orignal
if __name__ == "__main__":
# Input an existing mp3 filename
mp3File = "./sounds/Logo.mp3"
# Play the mp3 file
playsound(mp3File)
instructions()
getch()
points = 0
lives = 3
TIME = 0
level_time = 0 #The time spent in each level(will be used for handling falling bricks)
level = 1
new_level_flag = 0
# position of ufo
ufo_x = 32
ufo_y = 8
ufo_health = 200
# BOARD SIZE
X = 65
Y = 43
orignal = [[' ']*100]*100
orignal = create_raw_board(X, Y, orignal)
BRICK=BricksArray(orignal,level,X,Y)
POWER=PowerUpArray(orignal,X,Y)
while lives > 0:
# hard coding to remove ball from the scene(not able to remove)
if level == 3:
for i in range(Y):
for j in range(X):
if orignal[i][j] == 'b' or orignal[i][j]=='X':
orignal[i][j] = ' '
os.system("clear")
# Flag f=0 indicates that ball should be on paddle.When f is rasised to 1 then it means that ball can move freely now
f=0
# This hit_paddle will determine if ball hits the paddle or not
hit_paddle = 0
if new_level_flag == 1:
new_level_flag = 0
level += 1
if level == 4:
game.gameWon()
break
else:
level_time = 0 #New level is getting started so make it's level time to be 0
orignal = create_raw_board(X, Y, orignal)
BRICK=BricksArray(orignal,level,X,Y)
if level != 3:
POWER=PowerUpArray(orignal,X,Y)
game = Game(f,hit_paddle,TIME,X,Y,orignal,BRICK,POWER,points,level,level_time,ufo_x,ufo_y,ufo_health)
while(1):
game.update()
TIME+=1
os.system("clear")
game.board.display()
game.scoreboard()
if game.input() == 'l':
points = game.updatedpoints()
new_level_flag = 1
break
if game.GAMEOVER == True:
break
if game.GAMEOVER == True:
lives = lives -1
# We need to update the orginal array and Brick' array because both of them have changed over time so in next life u have to transfer the updated one
orignal = game.updatedboard()
BRICK = game.updatedbricks()
points = game.updatedpoints()
POWER = game.updatedpower()
ufo_health = game.updatedhealth()
thankyou()