diff --git a/Classes.pdf b/Classes.pdf new file mode 100644 index 00000000..0ff74292 Binary files /dev/null and b/Classes.pdf differ diff --git a/Pong Write-Up.pdf b/Pong Write-Up.pdf new file mode 100644 index 00000000..a047cf58 Binary files /dev/null and b/Pong Write-Up.pdf differ diff --git a/ProjectProposal.md b/ProjectProposal.md new file mode 100644 index 00000000..595d5c9f --- /dev/null +++ b/ProjectProposal.md @@ -0,0 +1,7 @@ +Grace and Quinn +Interactive Programming + + + +We want to make a two player game using pygame. Our minimal vibale product is a game of pong, and the stretch goal is airhockey. We will explore two player inputs to generate a simple game output. Quinn's learning goal is to understand the pygame library and how to use github. Grace's goal is to have a deep understanding of our code and project. We are planning on using pygame as our main input library. For our mid prject check in we want to have a clear outline of our code functions and classes as well as how we will distribute the work between us. We would also liked to have started filling in some of this skeleton code. The biggest risk is trying to have two inputs to our game, when we have not yet done anything with a single interactive input. + diff --git a/README.md b/README.md index 5f822327..27ac3617 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # InteractiveProgramming This is the base repo for the interactive programming project for Software Design, Spring 2018 at Olin College. +To run this pong program you will need to install pygame ($pip install pygame) as well as import random into your code. +The write up can be found here: https://github.com/gracemontagnino/InteractiveProgramming/blob/master/Pong%20Write-Up.pdf (Or just by looking at the Pong Write-Up File in the git repository.) +To run this game use $python3 pong.py diff --git a/collision.py b/collision.py new file mode 100644 index 00000000..8254c92c --- /dev/null +++ b/collision.py @@ -0,0 +1,193 @@ +# -*- 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, + )) + font = pygame.font.Font(None, 36) + scoretext1=font.render("Score:"+str(self.model.puck.score1), 1,(255,255,255)) + self.screen.blit(scoretext1, (440, 50)) + scoretext2=font.render("Score:"+str(self.model.puck.score2), 1,(255,255,255)) + self.screen.blit(scoretext2, (100, 50)) + 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 int(self.puck.x)==int(self.paddle.x)+10 and (int(self.paddle.y)-50)<=int(self.puck.y)<=(int(self.paddle.y)+50): + self.puck.vx=-self.puck.vx + self.puckvy=-self.puck.vy + if int(self.puck.x)==int(self.paddle2.x)-10 and (int(self.paddle2.y)-50)<=int(self.puck.y)<=(int(self.paddle2.y)+50): + 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.screen = pygame.display.set_mode(size) + self.height=height + self.x,self.y=320,240 + self.width=width + self.vx=1 + self.vy=0 + self.score1=0 + self.score2=0 + self.vx=random.choice([-.6,-.4,-.2,.2,.4,.6]) + self.vy=random.choice([-.6,-.4,-.2,.2,.4,.6]) + 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: + self.x,self.y=320,240 + self.score2=self.score2+1 + #Score2=int(self.score2) + if self.x<=0: + self.x,self.y=320,240 + self.score1=self.score1+1 + #font = pygame.font.Font(None, 36) + #Score1=int(self.score1) + self.x += self.vx + self.y+=self.vy + def draw(self, surface): + 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 + pygame.display.update() + 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() diff --git a/middle.py b/middle.py new file mode 100644 index 00000000..ca5a34e0 --- /dev/null +++ b/middle.py @@ -0,0 +1,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() diff --git a/pong.py b/pong.py new file mode 100644 index 00000000..57279b8d --- /dev/null +++ b/pong.py @@ -0,0 +1,196 @@ +# -*- 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 pong 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() + + def draw(self): + """ Draw the current game state to the screen """ + self.screen.fill(pygame.Color(0,0,0)) + #Draws the paddles and the puck + 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, + )) + #Prints the scoring + font = pygame.font.Font(None, 36) + scoretext1=font.render("Score:"+str(self.model.puck.score1), 1,(255,255,255)) + self.screen.blit(scoretext1, (440, 50)) + scoretext2=font.render("Score:"+str(self.model.puck.score2), 1,(255,255,255)) + self.screen.blit(scoretext2, (100, 50)) + 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 tracking the paddle and running collision code """ + self.paddle.update() + self.paddle2.update() + #Controls collisions between puck and paddle + if int(self.puck.x)==int(self.paddle.x)+10 and (int(self.paddle.y)-50)<=int(self.puck.y)<=(int(self.paddle.y)+50): + self.puck.vx=-self.puck.vx + self.puckvy=-self.puck.vy + if int(self.puck.x)==int(self.paddle2.x)-10 and (int(self.paddle2.y)-50)<=int(self.puck.y)<=(int(self.paddle2.y)+50): + 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 + #Keeps paddle on screen + 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.screen = pygame.display.set_mode(size) + self.height=height + self.x,self.y=320,240 + self.width=width + self.vx=1 + self.vy=0 + self.score1=0 + self.score2=0 + self.vx=random.choice([-.8,-.6,.6,.8]) + self.vy=random.choice([-.8,-.6,.6,.8]) + self.rect = pygame.Rect(self.x, self.y, self.height, self.width) + def update(self): + """ update the state of the puck and counts scoring """ + if self.y>=480 or self.y<=0: + self.vy=-self.vy + if self.x>640: + #resets position of puck after it runs off screen + self.x,self.y=320,240 + self.score2=self.score2+1 + if self.x<=0: + self.x,self.y=320,240 + self.score1=self.score1+1 + + self.x += self.vx + self.y+=self.vy + def draw(self, surface): + """Draws the actual paddles""" + 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 + pygame.display.update() + 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 pong """ + 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() diff --git a/scores.py b/scores.py new file mode 100644 index 00000000..8254c92c --- /dev/null +++ b/scores.py @@ -0,0 +1,193 @@ +# -*- 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, + )) + font = pygame.font.Font(None, 36) + scoretext1=font.render("Score:"+str(self.model.puck.score1), 1,(255,255,255)) + self.screen.blit(scoretext1, (440, 50)) + scoretext2=font.render("Score:"+str(self.model.puck.score2), 1,(255,255,255)) + self.screen.blit(scoretext2, (100, 50)) + 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 int(self.puck.x)==int(self.paddle.x)+10 and (int(self.paddle.y)-50)<=int(self.puck.y)<=(int(self.paddle.y)+50): + self.puck.vx=-self.puck.vx + self.puckvy=-self.puck.vy + if int(self.puck.x)==int(self.paddle2.x)-10 and (int(self.paddle2.y)-50)<=int(self.puck.y)<=(int(self.paddle2.y)+50): + 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.screen = pygame.display.set_mode(size) + self.height=height + self.x,self.y=320,240 + self.width=width + self.vx=1 + self.vy=0 + self.score1=0 + self.score2=0 + self.vx=random.choice([-.6,-.4,-.2,.2,.4,.6]) + self.vy=random.choice([-.6,-.4,-.2,.2,.4,.6]) + 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: + self.x,self.y=320,240 + self.score2=self.score2+1 + #Score2=int(self.score2) + if self.x<=0: + self.x,self.y=320,240 + self.score1=self.score1+1 + #font = pygame.font.Font(None, 36) + #Score1=int(self.score1) + self.x += self.vx + self.y+=self.vy + def draw(self, surface): + 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 + pygame.display.update() + 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() diff --git a/start.py b/start.py new file mode 100644 index 00000000..7aaff9aa --- /dev/null +++ b/start.py @@ -0,0 +1,132 @@ +# -*- 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)) + + 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.height, + self.model.puck.w, + self.model.puck.x, + self.model.puck.y)) + + 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() + 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.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 Puck(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.x=x + self.y=y + self.w=width + self.vx = 0.0 + + def update(self): + """ update the state of the paddle """ + self.x += self.vx + + def __str__(self): + return "Puck x coordinate=%f, y coordinate=%f, radius=%f, width=%f" % (self.x, + self.y, + self.height, + self.w) + +if __name__ == '__main__': + pygame.init() + + size = (640, 480) + + model = Model(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()