-
Notifications
You must be signed in to change notification settings - Fork 0
/
brick.py
43 lines (38 loc) · 1.52 KB
/
brick.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
import pygame
import os
class Brick:
def __init__(self, rect, colour, life, multiball=False, invert=False):
"""
rect is a pygame rect describing my position and size.
colour is a tuple of (red, green, blue)
life is my number of life-points!
"""
self.multiball = multiball
self.invert = invert
self.rect = rect
self.colour = colour
if life == 0:
raise RuntimeError("Cannot have a brick start with life 0")
self.life = life
colourcode = "{:02x}{:02x}{:02x}".format(colour[0], colour[1],
colour[2])
self.sprites = dict()
if life == -1:
self.sprites[-1] = pygame.image.load(
os.path.join("images", "brick_{}.png".format(colourcode)))
else:
for l in range(life, 0, -1):
self.sprites[l] = pygame.image.load(
os.path.join("images", "brick_{}_{}.png".format(
colourcode, l)))
for sprite in self.sprites.values():
if sprite.get_size() != (rect.width, rect.height):
raise RuntimeError("Brick is wrong size. Expected {}"
"".format((rect.width, rect.height)))
def draw(self, surface):
surface.blit(self.sprites[self.life], (self.rect.left, self.rect.top))
def hit(self):
if self.life > 0:
self.life -= 1
def alive(self):
return (self.life > 0 or self.life == -1)