-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow2.py
371 lines (319 loc) · 13.7 KB
/
window2.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import os
import sys
import time
import getch
import termios
from colorama import init
from colorama import Fore, Back, Style
from objects import Entity
from input import KeyboardInput
from objects import Power_up
import config as config
init()
FRAME_RATE = config.FRAME_RATE
class Window:
def __init__(self, level, lives, score):
size = os.get_terminal_size()
self.height = size.lines - 1
self.width = size.columns
self.entities = []
self.paddle = None
self.bricks = []
self.level = level
self.brick_no = 0
self.lives = lives
self.score = score
self.powerups = []
# create the border for the window
def makeborder(self):
self.PrintBoard = [[None for j in range(self.width)]
for i in range(self.height)]
self.Board = [[None for j in range(self.width)]
for i in range(self.height)]
a = Entity(1, 1, 1, 1, Fore.WHITE, '┃')
b = Entity(1, 1, 1, 1, Fore.WHITE, '┏')
c = Entity(1, 1, 1, 1, Fore.WHITE, '┓')
d = Entity(1, 1, 1, 1, Fore.WHITE, '━')
e = Entity(1, 1, 1, 1, Fore.WHITE, '┗')
f = Entity(1, 1, 1, 1, Fore.WHITE, '┛')
for i in range(self.height):
for j in range(self.width):
if j == 0 or j == self.width - 1:
self.Board[i][j] = a
if i == 0:
if j == 0:
self.Board[i][j] = b
elif j == self.width - 1:
self.Board[i][j] = c
else:
self.Board[i][j] = d
if i == self.height - 1:
if j == 0:
self.Board[i][j] = e
elif j == self.width - 1:
self.Board[i][j] = f
else:
self.Board[i][j] = d
elif i == self.height - 4:
if j == 0:
self.Board[i][j] = a
elif j == self.width - 1:
self.Board[i][j] = a
else:
self.Board[i][j] = d
# adding objects to the window
def checkBricks(self):
self.brick_no = 0
for brick in self.bricks:
try:
if(brick.utility != "unbreakable" and brick.strength > 0):
self.brick_no += 1
except:
if brick.strength > 0:
self.brick_no += 1
return self.brick_no
def add(self, element):
self.entities.append(element)
def addPaddle(self, element):
self.paddle = element
def addBrick(self, element):
self.bricks.append(element)
def handle_collisions(self, element):
self.handle_paddlecollision(element)
self.handle_bordercollision(element)
self.handle_brickcollision(element)
def explosion(self, exploding_brick):
ex_x = exploding_brick.x
ex_y = exploding_brick.y
ex_height = exploding_brick.height
ex_width = exploding_brick.width
for i in range(ex_x - 1, ex_x + ex_width + 1):
for j in range(ex_y - 1, ex_y + ex_height + 1):
if self.Board[j][i] != None:
try:
if self.Board[j][i].utility == "explode" and self.Board[j][i].strength != 0:
self.Board[j][i].strength = 0
self.Board[j][i].sprite = " "
self.explosion(self.Board[j][i])
else:
self.Board[j][i].strength = 0
self.Board[j][i].sprite = " "
except:
self.Board[j][i].strength = 0
self.Board[j][i].sprite = " "
def handle_brickcollision(self, element):
for brick in self.bricks:
new = brick.collide(element)
self.score += new
if new == 1:
try:
if brick.utility == "explode":
self.explosion(brick)
except:
pass
def grab(self):
pad_x = self.paddle.x
pad_y = self.paddle.y
wid_x = self.paddle.width
gap = wid_x/4
for element in self.entities:
new_x = element.x + element.vx
new_y = element.y + element.vy
if((new_x >= pad_x and new_x <= pad_x + wid_x) and (new_y == pad_y - 1)):
element.status = "onpaddle"
element.x = self.paddle.x + int(self.paddle.width / 2)
element.y = self.paddle.y - 1
element.color = Fore.RED
def showpowerups(self, powerup):
lst = []
for p in self.powerups:
if p.status == True:
lst.append(Entity(1, 1, 1, 1, Fore.WHITE, p.utility_sprite))
for i in range(len(lst)):
self.Board[self.height - 3][self.width - i - 3] = lst[i]
def checkpowerups(self):
for powerup in self.powerups:
powerup.deactivate()
self.showpowerups(powerup)
if powerup.utility == "grab" and powerup.status == True:
self.grab()
def handle_powercollision(self, element):
pad_x = self.paddle.x
pad_y = self.paddle.y
wid_x = self.paddle.width
if (element.y == pad_y and element.x >= pad_x and element.x <= pad_x + wid_x):
self.showpowerups(element)
# element.height = 1
self.powerups.append(
Power_up(self.paddle, self.entities, element.utility, element.utility_sprite))
def handle_paddlecollision(self, element):
pad_x = self.paddle.x
pad_y = self.paddle.y
wid_x = self.paddle.width
gap = wid_x/4
new_x = element.x + element.vx
new_y = element.y + element.vy
if((new_x >= pad_x and new_x <= pad_x + wid_x) and (new_y == pad_y)):
if pad_x + gap >= new_x:
element.vx = -2*(config.BALL_VX)
element.vy = -element.vy
elif pad_x + 2*gap >= new_x:
element.vx = -1*(config.BALL_VX)
element.vy = -element.vy
elif pad_x + 3*gap >= new_x:
element.vx = 1*(config.BALL_VX)
element.vy = -element.vy
elif pad_x + 4*gap >= new_x:
element.vx = 2*(config.BALL_VX)
element.vy = -element.vy
def handle_bordercollision(self, element):
if(element.y+element.vy >= self.height or element.y + element.vy <= 0):
element.vy = -element.vy
if(element.x + element.vx >= self.width or element.x + element.vx <= 0):
element.vx = -element.vx
def Make_Paddle(self):
for i in range(self.paddle.width):
self.Board[self.paddle.y][self.paddle.x+i] = self.paddle
def showlevel(self):
self.Board[self.height - 3][2] = Entity(1, 1, 1, 1, Fore.WHITE, "L")
self.Board[self.height - 3][3] = Entity(1, 1, 1, 1, Fore.WHITE, "E")
self.Board[self.height - 3][4] = Entity(1, 1, 1, 1, Fore.WHITE, "V")
self.Board[self.height - 3][5] = Entity(1, 1, 1, 1, Fore.WHITE, "E")
self.Board[self.height - 3][6] = Entity(1, 1, 1, 1, Fore.WHITE, "L")
self.Board[self.height - 3][7] = Entity(1, 1, 1, 1, Fore.WHITE, ":")
self.Board[self.height -
3][8] = Entity(1, 1, 1, 1, Fore.WHITE, str(self.level))
self.Board[self.height - 2][2] = Entity(1, 1, 1, 1, Fore.WHITE, "L")
self.Board[self.height - 2][3] = Entity(1, 1, 1, 1, Fore.WHITE, "I")
self.Board[self.height - 2][4] = Entity(1, 1, 1, 1, Fore.WHITE, "V")
self.Board[self.height - 2][5] = Entity(1, 1, 1, 1, Fore.WHITE, "E")
self.Board[self.height - 2][6] = Entity(1, 1, 1, 1, Fore.WHITE, "S")
self.Board[self.height - 2][7] = Entity(1, 1, 1, 1, Fore.WHITE, ":")
self.Board[self.height -
2][8] = Entity(1, 1, 1, 1, Fore.WHITE, str(self.lives))
self.Board[self.height - 2][self.width -
10] = Entity(1, 1, 1, 1, Fore.WHITE, "S")
self.Board[self.height - 2][self.width -
9] = Entity(1, 1, 1, 1, Fore.WHITE, "C")
self.Board[self.height - 2][self.width -
8] = Entity(1, 1, 1, 1, Fore.WHITE, "O")
self.Board[self.height - 2][self.width -
7] = Entity(1, 1, 1, 1, Fore.WHITE, "R")
self.Board[self.height - 2][self.width -
6] = Entity(1, 1, 1, 1, Fore.WHITE, "E")
self.Board[self.height - 2][self.width -
5] = Entity(1, 1, 1, 1, Fore.WHITE, ":")
self.Board[self.height -
2][self.width-4] = Entity(1, 1, 1, 1, Fore.WHITE, str(int(self.score/100)))
self.Board[self.height -
2][self.width-3] = Entity(1, 1, 1, 1, Fore.WHITE, str(int(self.score/10)))
self.Board[self.height -
2][self.width-2] = Entity(1, 1, 1, 1, Fore.WHITE, str(int(self.score % 10)))
def movepaddle(self, inp):
if inp:
if inp == 'w':
for ball in self.entities:
element = ball
ball.status = "go"
pad_x = self.paddle.x
pad_y = self.paddle.y
wid_x = self.paddle.width
gap = wid_x/4
new_x = element.x
new_y = element.y
if((new_x >= pad_x and new_x <= pad_x + wid_x)):
if pad_x + gap >= new_x:
element.vx = -2*(config.BALL_VX)
element.vy = -element.vy
elif pad_x + 2*gap >= new_x:
element.vx = -1*(config.BALL_VX)
element.vy = -element.vy
elif pad_x + 3*gap >= new_x:
element.vx = 1*(config.BALL_VX)
element.vy = -element.vy
elif pad_x + 4*gap >= new_x:
element.vx = 2*(config.BALL_VX)
element.vy = -element.vy
if inp == 'a' and self.paddle.x >= config.PADDLE_V:
self.paddle.x = self.paddle.x - config.PADDLE_V
for ball in self.entities:
if ball.status == "onpaddle":
ball.x -= config.PADDLE_V
self.Make_Paddle()
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
elif inp == 'd' and self.paddle.x <= self.width - self.paddle.width-config.PADDLE_V:
self.paddle.x = self.paddle.x + config.PADDLE_V
for ball in self.entities:
if ball.status == "onpaddle":
ball.x += config.PADDLE_V
self.Make_Paddle()
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
else:
self.Make_Paddle()
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
# inp = None
else:
self.Make_Paddle()
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
inp = None
def renderBricks(self):
for element in self.bricks:
for k in range(element.height):
for i in range(element.width):
self.Board[element.y + k][element.x + i] = element
element.move(self.height)
self.handle_powercollision(element)
def renderBalls(self):
for element in self.entities:
self.handle_collisions(element)
element.move(self.Board, self.paddle)
self.Board[element.y][element.x] = element
def renderBoard(self):
for i in range(self.height):
for j in range(self.width):
if(self.Board[i][j] != None):
pixel = self.Board[i][j].color + \
self.Board[i][j].sprite + Fore.RESET
self.PrintBoard[i][j] = pixel
else:
pixel = ' '
self.PrintBoard[i][j] = pixel
for i in range(self.height):
print(*self.PrintBoard[i], sep="")
def render(self):
Key = KeyboardInput()
while True:
begin = time.monotonic()
os.system("clear")
# makeborder
self.makeborder()
# show level lives and score
self.showlevel()
# checkpowerups
self.checkpowerups()
# adding bricks
self.renderBricks()
# checking keyboard responses
inp = Key.kbhit()
self.movepaddle(inp)
inp = None
# adding Balls to the board
self.renderBalls()
# rendering the board
self.renderBoard()
for ball in self.entities:
if len(self.entities) == 1:
if ball.y > self.paddle.y:
ball.status = "onpaddle"
ball.x = self.paddle.x + int(self.paddle.width / 2)
ball.y = self.paddle.y - 1
self.lives -= 1
if self.lives == 0:
return self.level, self.lives, self.score
else:
if ball.y > self.paddle.y:
self.entities.remove(ball)
if self.checkBricks() == 0:
return self.level + 1, self.lives, self.score
while time.monotonic() - begin < FRAME_RATE:
pass