-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbrick_breaker.py
158 lines (137 loc) · 5.48 KB
/
brick_breaker.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
# -*- coding: utf-8 -*-
"""
This is a worked example of applying the Model-View-Controller (MVC)
design pattern to the creation of a simple arcade game (in this case
Brick Breaker).
We will create our game in stages so that you can see the process by
which the MVC pattern can be utilized to create clean, extensible,
and modular code.
@author: SoftDesProfs
"""
import pygame
from pygame.locals import *
import time
class PyGameWindowView(object):
""" A view of brick breaker rendered in a Pygame window """
def __init__(self, model, size):
""" Initialize the view with a reference to the model and the
specified game screen dimensions (represented as a tuple
containing the width and height """
self.model = model
self.screen = pygame.display.set_mode(size)
def draw(self):
""" Draw the current game state to the screen """
self.screen.fill(pygame.Color(0,0,0))
for brick in self.model.bricks:
pygame.draw.rect(self.screen,
pygame.Color(255, 255, 255),
pygame.Rect(brick.x,
brick.y,
brick.width,
brick.height))
pygame.draw.rect(self.screen,
pygame.Color(255, 0, 0),
pygame.Rect(self.model.paddle.x,
self.model.paddle.y,
self.model.paddle.width,
self.model.paddle.height))
pygame.display.update()
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)
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 PyGameMouseController(object):
""" A controller that uses the mouse to move the paddle """
def __init__(self,model):
self.model = model
def handle_event(self,event):
""" Handle the mouse event so the paddle tracks the mouse position """
if event.type == MOUSEMOTION:
self.model.paddle.x = event.pos[0] - self.model.paddle.width/2.0
class PyGameKeyboardController(object):
""" Handles keyboard input for brick breaker """
def __init__(self,model):
self.model = model
def handle_event(self,event):
""" Left and right presses modify the x velocity of the paddle """
if event.type != KEYDOWN:
return
if event.key == pygame.K_LEFT:
self.model.paddle.vx += -1.0
if event.key == pygame.K_RIGHT:
self.model.paddle.vx += 1.0
if __name__ == '__main__':
pygame.init()
size = (640, 480)
model = BrickBreakerModel(size)
print(model)
view = PyGameWindowView(model, size)
#controller = PyGameKeyboardController(model)
controller = PyGameMouseController(model)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
controller.handle_event(event)
model.update()
view.draw()
time.sleep(.001)
pygame.quit()