forked from sd18spring/BrickBreakerMVC
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
71 lines (64 loc) · 2.59 KB
/
model.py
File metadata and controls
71 lines (64 loc) · 2.59 KB
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
"""
BrickBreaker model code
"""
class Brick(object):
""" Encodes the state of a brick in the game """
def __init__(self,height,width,x,y):
self.height = height
self.width = width
self.x = x
self.y = y
def __str__(self):
return "Brick height=%f, width=%f, x=%f, y=%f" % (self.height,
self.width,
self.x,
self.y)
class Paddle(object):
""" Encodes the state of the paddle in the game """
def __init__(self, height, width, x, y):
""" Initialize a paddle with the specified height, width,
and position (x,y) """
self.height = height
self.width = width
self.x = x
self.y = y
self.vx = 0.0
def update(self):
""" update the state of the paddle """
self.x += self.vx
def __str__(self):
return "Paddle height=%f, width=%f, x=%f, y=%f" % (self.height,
self.width,
self.x,
self.y)
class BrickBreakerModel(object):
""" Encodes a model of the game state """
def __init__(self, size):
self.bricks = []
self.width = size[0]
self.height = size[1]
self.brick_width = 100
self.brick_height = 20
self.brick_space = 10
for x in range(self.brick_space,
self.width - self.brick_space - self.brick_width,
self.brick_width + self.brick_space):
for y in range(self.brick_space,
self.height//2,
self.brick_height + self.brick_space):
self.bricks.append(Brick(self.brick_height,
self.brick_width,
x,
y))
self.paddle = Paddle(20, 100, 200, self.height - 30)
def update(self):
""" Update the game state (currently only tracking the paddle) """
self.paddle.update()
def __str__(self):
output_lines = []
# convert each brick to a string for outputting
for brick in self.bricks:
output_lines.append(str(brick))
output_lines.append(str(self.paddle))
# print one item per line
return "\n".join(output_lines)