-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmiddle.py
More file actions
176 lines (159 loc) · 6.25 KB
/
middle.py
File metadata and controls
176 lines (159 loc) · 6.25 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
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
# -*- coding: utf-8 -*-
"""
@author: Grace Montagnino & Quinn Kelley
"""
import pygame
import random
from pygame.locals import *
import time
FPS = 50
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)
self.screenheight = pygame.display.get_surface().get_height()
# self.screenwidth = pygame.display.get_surface().get_width()
def draw(self):
""" Draw the current game state to the screen """
self.screen.fill(pygame.Color(0,0,0))
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.draw.rect(self.screen,
pygame.Color(255, 255, 255),
pygame.Rect(self.model.paddle2.x,
self.model.paddle2.y,
self.model.paddle2.width,
self.model.paddle2.height))
pygame.draw.rect(self.screen,
pygame.Color(255, 255, 255),
pygame.Rect(self.model.puck.x,
self.model.puck.y,
self.model.puck.height,
self.model.puck.width,
))
pygame.display.update()
class Model(object):
""" Encodes a model of the game state """
def __init__(self, size):
self.paddle = Paddle(50,10,0,240)
self.paddle2=Paddle(50,10,630,240)
self.puck=Puck(10,10,10,10)
def update(self):
""" Update the game state (currently only tracking the paddle) """
self.paddle.update()
self.paddle2.update()
if self.puck.x==(self.paddle.x+10) and self.puck.y==(self.paddle.y):
self.puck.vx=-self.puck.vx
self.puckvy=-self.puck.vy
if self.puck.x==(self.paddle2.x+10) and self.puck.y==(self.paddle2.y):
self.puck.vx=-self.puck.vx
self.puck.vy=-self.puck.vy
self.puck.update()
def __str__(self):
output_lines = []
# convert each brick to a string for outputting
output_lines.append(str(self.paddle))
# print one item per line
return "\n".join(output_lines)
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
self.vy = 0
def update(self):
""" update the state of the paddle & stops it from running off the screen"""
self.x += self.vx
self.y += self.vy
if self.y > 480 - self.height:
self.y = 480 - self.height
if 0 > self.y:
self.y = 0
def __str__(self):
return "Paddle height=%f, width=%f, x=%f, y=%f" % (self.height,
self.width,
self.x,
self.y)
class Puck(object):
""" Encodes the state of the paddle in the game """
def __init__(self,x,y,height,width):
""" Initialize a paddle with the specified height, width,
and position (x,y) """
self.height=height
self.x,self.y=320,240
self.width=width
#to make starting v random do someting about making vx and vy random
self.vx=random.choice([-1,-.8,-.6,-.4,-.2,.2,.4,.6,.8,1])
self.vy=random.choice([-1,-.8,-.6,-.4,-.2,.2,.4,.6,.8,1])
self.rect = pygame.Rect(self.x, self.y, self.height, self.width)
def update(self):
""" update the state of the puck """
if self.y>=480 or self.y<=0:
self.vy=-self.vy
if self.x>640 or self.x<=0:
self.vx= -self.vx
self.x += self.vx
self.y+=self.vy
def draw(self, surface):
self.screen = pygame.display.set_mode(size)
self.screen.fill(pygame.Color(0,0,0))
pygame.draw.rect(screen, (55,150,55), self.rect)
self.x = self.rect.left
self.y = self.rect.top
def __str__(self):
return "Puck x coordinate=%f, y coordinate=%f, radius=%f, width=%f" % (self.x,
self.y,
self.height,
self.w)
class PyGameKeyboardController(object):
""" Handles keyboard input for brick breaker """
def __init__(self,model):
self.model = model
def handle_event(self,event):
""" arrow keys + WS modify the y position of the paddles"""
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]:
self.model.paddle2.vy = -1
elif pressed[pygame.K_DOWN]:
self.model.paddle2.vy = 1
else:
self.model.paddle2.vy = 0
if pressed[pygame.K_w]:
self.model.paddle.vy = -1
elif pressed[pygame.K_s]:
self.model.paddle.vy = 1
else:
self.model.paddle.vy = 0
if __name__ == '__main__':
pygame.init()
size = (640, 480)
model = Model(size)
print(model)
view = PyGameWindowView(model, size)
controller = PyGameKeyboardController(model)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
controller.handle_event(event)
screen = pygame.display.set_mode(size)
screen.fill((0, 0, 0))
model.update()
view.draw()
time.sleep(.001)
time.sleep(.001)
pygame.quit()