-
Notifications
You must be signed in to change notification settings - Fork 1
/
Map.py
200 lines (146 loc) · 5.81 KB
/
Map.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
import pygame as pg
from pygame.locals import *
from pygame.math import Vector2 as Pos
from pygame.rect import Rect
from pygame.color import Color
import random as r
import json
from typing import Optional
from random import randint as rr
from pygame.surface import Surface
from Colors import Colors
from Bonus import Bonus
from Brick import Brick
from CommonResources import CommonResources
now = lambda: pg.time.get_ticks() / 1000
class Map :
def __init__( self,path:str ) :
self.events = CommonResources.event_holder
self.colors = CommonResources.colors
self.assets = CommonResources.assets
self.window = CommonResources.window
self.player = CommonResources.player
self.path = "none"
self.name = "none"
self.rect: Optional[Rect]= None
self.total_gap_x = 0
self.total_gap_y = 0
self.colors = {}
self.health_rows = []
self.bonus_rows = []
self.bricks = []
self.bonus_list = []
self.edge_size = 0
self.bg = Colors.BLACK
# self.bricks_break_sound = pg.mixer.Sound("./sounds/Block Break 2.wav")
@property
def ball( self ):
return CommonResources.game.ball
def reset( self ):
self.path = "none"
self.name = "none"
self.rect: Optional[Rect] = None
self.total_gap_x = 0
self.total_gap_y = 0
self.colors = {}
self.health_rows = []
self.bricks = []
self.bonus_list = []
self.edge_size = 0
def reload( self,path:str = None ):
if path is None: path = self.path
self.reset()
self.load(path)
self.create_tiles()
def load( self,path:str ):
self.path = path
f = open(path).read()
j = json.loads(f)
if 'edge_size' in j:
self.edge_size = j['edge_size']
self.ball.color = pg.color.Color(j['ball_color'])
self.bg = pg.color.Color(j['background_color'])
self.player.color = pg.color.Color(j['paddle_color'])
self.player.edge = int(j['paddle_edge_size'])
self.name = j['name']
self.health_rows = j["health_rows"]
self.bonus_rows = j['bonus_rows']
self.rect = Rect(
j['rect_x'] * self.window.size.x,
j['rect_y'] * self.window.size.y,
j['rect_w'] * self.window.size.x,
j['rect_h'] * self.window.size.y
)
self.total_gap_x = j['total_gap_x']
self.total_gap_y = j['total_gap_y']
self.colors:dict = j['colors']
def update( self ):
self.events = CommonResources.event_holder
self.colors = CommonResources.colors
self.assets = CommonResources.assets
self.window = CommonResources.window
self.player = CommonResources.player
for brick in self.bricks:
if brick.bonus is not None:
brick.bonus.update()
def create_tiles( self ) :
Y = self.rect.height / len(self.health_rows)
for y,row,bonus_row in zip(range(len(self.health_rows)),self.health_rows,self.bonus_rows):
for x,health,section_id in zip(range(len(row)),row,bonus_row):
# print(bonus,Bonus.by_number[int(bonus)])
if health <= 0: continue
X = self.rect.width / len(row)
if str(health) in self.colors:
color = Color(self.colors[str(health)])
else:
color = Colors.random_color()
rect = Rect(
self.rect.x+x*X+self.total_gap_x*X/2,
self.rect.y+y*Y+self.total_gap_y*Y/2,
X-self.total_gap_x*X,
Y-self.total_gap_y*Y
)
brick = Brick(rect,color,health)
section = Bonus.by_number[section_id]
if section not in ['empty','none']:
bonus_name = r.choice(Bonus.section_dict[section])
brick.set_bonus(80,bonus_name=bonus_name)
brick.edge_size = self.edge_size
brick.font_time = now()
self.bricks.append(brick)
def check_events( self ) :
brick_destroy_list = []
bonus_destroy_list = []
if self.bricks.__len__() == 0:
self.events.win = True
CommonResources.game.get_screen_shot()
for bonus,c in zip(self.bonus_list,range(len(self.bonus_list))):
if bonus.name == Bonus.SHRINK and (bonus.center.y - bonus.radius) > self.window.size.y:
bonus.center.y = -bonus.radius
elif (bonus.center.y - bonus.radius) > self.window.size.y or bonus.consumed:
bonus_destroy_list.append(c)
else:
bonus.check_events()
for c in bonus_destroy_list[::-1]:
self.bonus_list.pop(c)
for brick,c in zip(self.bricks,range(len(self.bricks))):
if brick.health <= 0:
brick_destroy_list.append(c)
if brick.bonus is not None:
self.bonus_list.append(brick.bonus)
for c in brick_destroy_list[::-1]:
v = self.ball.volumes
CommonResources.ball_channel.stop()
CommonResources.ball_channel.set_volume(v[0]*0.5,v[1]*0.5)
CommonResources.ball_channel.play(r.choice(self.assets.break_sounds))
self.bricks.pop(c)
def render_debug( self, surface: Surface ) :
pg.draw.rect(surface, [0, 0, 0], self.rect, width=3)
def render( self, surface: Surface ) :
surface.fill(self.bg)
for brick in self.bricks :
brick.render(surface)
for bonus in self.bonus_list:
bonus.render(surface)
if self.events.should_render_debug:
self.render_debug(surface)