From 9fe15a6f7fee6b96746d528ee20c51137fe50d07 Mon Sep 17 00:00:00 2001 From: Anne Courtney Date: Fri, 13 Jul 2018 11:52:17 -0500 Subject: [PATCH 1/6] created a paddle and blocks, got the paddle to move --- .vscode/settings.json | 3 +++ src/ball.py | 2 +- src/block.py | 8 ++++++- src/draw.py | 51 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..99be8d5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "/Users/annecourtney/.local/share/virtualenvs/Python-OOP-Toy-byPP2fy6/bin/python" +} \ No newline at end of file diff --git a/src/ball.py b/src/ball.py index 20e2792..9539f87 100644 --- a/src/ball.py +++ b/src/ball.py @@ -14,7 +14,7 @@ def __init__(self, bounds, position, velocity, color, radius): self.velocity = velocity self.bounds = bounds self.color = color - self.radius = radius + self.radius = radius -10 self.collision_rectangle = self.update_rectangle() def update_rectangle(self): diff --git a/src/block.py b/src/block.py index f7966e2..c64aaf1 100644 --- a/src/block.py +++ b/src/block.py @@ -20,9 +20,15 @@ def __init__(self, position, width, height, color): self.touched_by_ball = False - def update(self, **kwargs): + def update(self, left, right): self.touched_by_ball = False + if left: + self.rectangle.x += 2 + if right: + self.rectangle.x -= 2 + + def check_collision(self): pass diff --git a/src/draw.py b/src/draw.py index 14ff036..12148c4 100644 --- a/src/draw.py +++ b/src/draw.py @@ -1,24 +1,43 @@ import pygame #TODO: Fix intellisense import random +import sys from pygame.math import Vector2 from ball import * from block import * -SCREEN_SIZE = [640, 480] -BACKGROUND_COLOR = [255, 255, 255] +SCREEN_SIZE = [800, 400] +BACKGROUND_COLOR = [255, 250, 231] def debug_create_objects(object_list): + #main ball in game kinetic = GameBall(1, object_list, SCREEN_SIZE, Vector2(random.randint(20, SCREEN_SIZE[0] - 20), random.randint(20, SCREEN_SIZE[1] - 20)), Vector2(4*random.random() - 2, 4*random.random() - 2), - [255, 10, 0], 20) + [240, 197, 202], 20) object_list.append(kinetic) + print(object_list) - block = KineticBlock(Vector2(200,200), 100, 100, [0, 0, 255]) + # blocks at top of screen + block = KineticBlock(Vector2(200,20), 100, 30, [156, 214, 184]) object_list.append(block) - + block = KineticBlock(Vector2(400,20), 100, 30, [156, 214, 184]) + object_list.append(block) + block = KineticBlock(Vector2(600,20), 100, 30, [156, 214, 184]) + object_list.append(block) + block = KineticBlock(Vector2(200,60), 100, 30, [0, 214, 200]) + object_list.append(block) + block = KineticBlock(Vector2(400,60), 100, 30, [0, 214, 200]) + object_list.append(block) + block = KineticBlock(Vector2(600,60), 100, 30, [0, 214, 200]) + object_list.append(block) + + #paddle + paddle = KineticBlock(Vector2(400,390), 80, 20, [234, 112, 90]) + object_list.append(paddle) + + def main(): pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE) @@ -27,7 +46,7 @@ def main(): clock = pygame.time.Clock() object_list = [] # list of objects of all types in the toy - + debug_create_objects(object_list) while True: # TODO: Create more elegant condition for loop @@ -41,11 +60,25 @@ def main(): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: left = True + for object in object_list: + + if isinstance(object, Block): + object.update(left, right) + object.check_collision() + else: + object.update() + object.check_collision() + if keys[pygame.K_RIGHT]: right = True - for object in object_list: - object.update() - object.check_collision() + for object in object_list: + + if isinstance(object, Block): + object.update(left, right) + object.check_collision() + else: + object.update() + object.check_collision() # Draw Updates screen.fill(BACKGROUND_COLOR) From 486a5e0ed80308e9464b61b8ad08f65cf3829f24 Mon Sep 17 00:00:00 2001 From: Anne Courtney Date: Fri, 13 Jul 2018 13:01:06 -0500 Subject: [PATCH 2/6] working on making blocks dissapear --- src/ball.py | 2 +- src/block.py | 31 ++++++++++++++++++++++++++++--- src/draw.py | 16 ++++++++-------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/ball.py b/src/ball.py index 9539f87..07e3b9b 100644 --- a/src/ball.py +++ b/src/ball.py @@ -3,7 +3,7 @@ from pygame.math import Vector2 from pygame import Rect -from block import KineticBlock +from block import KineticBlock, TopBlock class Ball: """ diff --git a/src/block.py b/src/block.py index c64aaf1..23fccb4 100644 --- a/src/block.py +++ b/src/block.py @@ -20,18 +20,19 @@ def __init__(self, position, width, height, color): self.touched_by_ball = False - def update(self, left, right): + def update(self, *args): self.touched_by_ball = False - if left: + if args[0]: self.rectangle.x += 2 - if right: + if args[1]: self.rectangle.x -= 2 def check_collision(self): pass + def draw(self, screen, pygame): pygame.draw.rect(screen, self.color, self.rectangle) @@ -41,3 +42,27 @@ class KineticBlock(Block): pass +class TopBlock(KineticBlock): + def __init__(self, position, width, height, color): + # Create a rectangle centered around the x and y + self.position = position + self.rectangle = pygame.Rect( + position.x - (width/2), + position.y - (height/2), + width, + height) + self.color = color + self.touched_by_ball = False + + def update(self, *args): + count = 0 + if self.touched_by_ball: + for object in self.object_list: + + if object == self: + count += 1 + self.object_list.remove(object) + else: + continue + + \ No newline at end of file diff --git a/src/draw.py b/src/draw.py index 12148c4..981c9cc 100644 --- a/src/draw.py +++ b/src/draw.py @@ -19,20 +19,20 @@ def debug_create_objects(object_list): object_list.append(kinetic) print(object_list) - # blocks at top of screen - block = KineticBlock(Vector2(200,20), 100, 30, [156, 214, 184]) + #blocks at top of screen + block = TopBlock(Vector2(200,20), 100, 30, [156, 214, 184]) object_list.append(block) - block = KineticBlock(Vector2(400,20), 100, 30, [156, 214, 184]) + block = TopBlock(Vector2(400,20), 100, 30, [156, 214, 184]) object_list.append(block) - block = KineticBlock(Vector2(600,20), 100, 30, [156, 214, 184]) + block = TopBlock(Vector2(600,20), 100, 30, [156, 214, 184]) object_list.append(block) - block = KineticBlock(Vector2(200,60), 100, 30, [0, 214, 200]) + block = TopBlock(Vector2(200,60), 100, 30, [0, 214, 200]) object_list.append(block) - block = KineticBlock(Vector2(400,60), 100, 30, [0, 214, 200]) + block = TopBlock(Vector2(400,60), 100, 30, [0, 214, 200]) object_list.append(block) - block = KineticBlock(Vector2(600,60), 100, 30, [0, 214, 200]) + block = TopBlock(Vector2(600,60), 100, 30, [0, 214, 200]) object_list.append(block) - + #paddle paddle = KineticBlock(Vector2(400,390), 80, 20, [234, 112, 90]) object_list.append(paddle) From 7dfd2df1a008b7d25669ae0a65bb0391a2ed5071 Mon Sep 17 00:00:00 2001 From: Anne Courtney Date: Fri, 13 Jul 2018 13:25:45 -0500 Subject: [PATCH 3/6] added a winner and loser functionality if the ball hits the top or bottom of screen --- src/ball.py | 13 +++++++++---- src/block.py | 7 +++---- src/draw.py | 24 ++++++++++++------------ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/ball.py b/src/ball.py index 07e3b9b..bcb4a0a 100644 --- a/src/ball.py +++ b/src/ball.py @@ -1,4 +1,5 @@ import math +import pygame from pygame.math import Vector2 from pygame import Rect @@ -30,11 +31,15 @@ def update(self, **kwargs): self.position.x = self.bounds[0] - self.radius - 1 self.velocity.x *= -1 if self.position.y <= 0 + self.radius: # screen height - self.position.y = self.radius + 1 - self.velocity.y *= -1 + print("WINNER!") #if it touches the top of the screen you win + pygame.quit() + # self.position.y = self.radius + 1 + # self.velocity.y *= -1 if self.position.y >= self.bounds[1] - self.radius: - self.position.y = self.bounds[1] - self.radius - 1 - self.velocity.y *= -1 + print("You lost - Try again!") #if it touches the bottom of the screen you lose + pygame.quit() + # self.position.y = self.bounds[1] - self.radius - 1 + # self.velocity.y *= -1 self.position += self.velocity self.collision_rectangle = self.update_rectangle() diff --git a/src/block.py b/src/block.py index 23fccb4..b4c2c38 100644 --- a/src/block.py +++ b/src/block.py @@ -43,8 +43,9 @@ class KineticBlock(Block): class TopBlock(KineticBlock): - def __init__(self, position, width, height, color): - # Create a rectangle centered around the x and y + def __init__(self, position, width, height, color, object_list): + self.object_list = object_list + super().__init__(position, width, height, color) self.position = position self.rectangle = pygame.Rect( position.x - (width/2), @@ -55,12 +56,10 @@ def __init__(self, position, width, height, color): self.touched_by_ball = False def update(self, *args): - count = 0 if self.touched_by_ball: for object in self.object_list: if object == self: - count += 1 self.object_list.remove(object) else: continue diff --git a/src/draw.py b/src/draw.py index 981c9cc..ce034ef 100644 --- a/src/draw.py +++ b/src/draw.py @@ -20,18 +20,18 @@ def debug_create_objects(object_list): print(object_list) #blocks at top of screen - block = TopBlock(Vector2(200,20), 100, 30, [156, 214, 184]) - object_list.append(block) - block = TopBlock(Vector2(400,20), 100, 30, [156, 214, 184]) - object_list.append(block) - block = TopBlock(Vector2(600,20), 100, 30, [156, 214, 184]) - object_list.append(block) - block = TopBlock(Vector2(200,60), 100, 30, [0, 214, 200]) - object_list.append(block) - block = TopBlock(Vector2(400,60), 100, 30, [0, 214, 200]) - object_list.append(block) - block = TopBlock(Vector2(600,60), 100, 30, [0, 214, 200]) - object_list.append(block) + block1 = TopBlock(Vector2(200,20), 100, 30, [156, 214, 184], object_list) + object_list.append(block1) + block2 = TopBlock(Vector2(400,20), 100, 30, [156, 214, 184], object_list) + object_list.append(block2) + block3 = TopBlock(Vector2(600,20), 100, 30, [156, 214, 184], object_list) + object_list.append(block3) + block4 = TopBlock(Vector2(200,60), 100, 30, [0, 214, 200], object_list) + object_list.append(block4) + block5 = TopBlock(Vector2(400,60), 100, 30, [0, 214, 200], object_list) + object_list.append(block5) + block6 = TopBlock(Vector2(600,60), 100, 30, [0, 214, 200], object_list) + object_list.append(block6) #paddle paddle = KineticBlock(Vector2(400,390), 80, 20, [234, 112, 90]) From 018dadc6d6d2f3ef18beb08f15d3868161f997ee Mon Sep 17 00:00:00 2001 From: Anne Courtney Date: Fri, 13 Jul 2018 13:56:35 -0500 Subject: [PATCH 4/6] the blocks now remove if they are hit! --- src/draw.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/draw.py b/src/draw.py index ce034ef..18252cc 100644 --- a/src/draw.py +++ b/src/draw.py @@ -20,18 +20,32 @@ def debug_create_objects(object_list): print(object_list) #blocks at top of screen - block1 = TopBlock(Vector2(200,20), 100, 30, [156, 214, 184], object_list) + + #top row of blocks + block1 = TopBlock(Vector2(50,20), 90, 30, [156, 214, 184], object_list) object_list.append(block1) - block2 = TopBlock(Vector2(400,20), 100, 30, [156, 214, 184], object_list) + block2 = TopBlock(Vector2(150,20), 90, 30, [156, 214, 184], object_list) object_list.append(block2) - block3 = TopBlock(Vector2(600,20), 100, 30, [156, 214, 184], object_list) + block3 = TopBlock(Vector2(250,20), 90, 30, [156, 214, 184], object_list) object_list.append(block3) - block4 = TopBlock(Vector2(200,60), 100, 30, [0, 214, 200], object_list) + block4 = TopBlock(Vector2(350,20), 90, 30, [156, 214, 184], object_list) object_list.append(block4) - block5 = TopBlock(Vector2(400,60), 100, 30, [0, 214, 200], object_list) + block5 = TopBlock(Vector2(450,20), 90, 30, [156, 214, 184], object_list) object_list.append(block5) - block6 = TopBlock(Vector2(600,60), 100, 30, [0, 214, 200], object_list) + block6 = TopBlock(Vector2(550,20), 90, 30, [156, 214, 184], object_list) object_list.append(block6) + block7 = TopBlock(Vector2(650,20), 90, 30, [156, 214, 184], object_list) + object_list.append(block7) + block8 = TopBlock(Vector2(750,20), 90, 30, [156, 214, 184], object_list) + object_list.append(block8) + + #bottom row of blocks + block9 = TopBlock(Vector2(200,60), 100, 30, [0, 214, 200], object_list) + object_list.append(block9) + block10 = TopBlock(Vector2(400,60), 100, 30, [0, 214, 200], object_list) + object_list.append(block10) + block11 = TopBlock(Vector2(600,60), 100, 30, [0, 214, 200], object_list) + object_list.append(block11) #paddle paddle = KineticBlock(Vector2(400,390), 80, 20, [234, 112, 90]) From e79fad33464d8cb2dc820e372a8495defe1a66e4 Mon Sep 17 00:00:00 2001 From: Anne Courtney Date: Fri, 13 Jul 2018 14:12:10 -0500 Subject: [PATCH 5/6] small fix --- src/ball.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ball.py b/src/ball.py index bcb4a0a..ff2801e 100644 --- a/src/ball.py +++ b/src/ball.py @@ -37,7 +37,7 @@ def update(self, **kwargs): # self.velocity.y *= -1 if self.position.y >= self.bounds[1] - self.radius: print("You lost - Try again!") #if it touches the bottom of the screen you lose - pygame.quit() + # pygame.quit() # took this out momentarily because it's annoying for testing # self.position.y = self.bounds[1] - self.radius - 1 # self.velocity.y *= -1 From a78eb5ab74c1dec964cfcbdd950f070a87b6b7e4 Mon Sep 17 00:00:00 2001 From: Anne Courtney Date: Fri, 13 Jul 2018 15:04:53 -0500 Subject: [PATCH 6/6] fixed the paddle so it makes the ball bounce off --- src/ball.py | 4 ++-- src/block.py | 2 ++ src/draw.py | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ball.py b/src/ball.py index ff2801e..ba8c1d1 100644 --- a/src/ball.py +++ b/src/ball.py @@ -38,8 +38,8 @@ def update(self, **kwargs): if self.position.y >= self.bounds[1] - self.radius: print("You lost - Try again!") #if it touches the bottom of the screen you lose # pygame.quit() # took this out momentarily because it's annoying for testing - # self.position.y = self.bounds[1] - self.radius - 1 - # self.velocity.y *= -1 + self.position.y = self.bounds[1] - self.radius - 1 + self.velocity.y *= -1 self.position += self.velocity self.collision_rectangle = self.update_rectangle() diff --git a/src/block.py b/src/block.py index b4c2c38..d427fb0 100644 --- a/src/block.py +++ b/src/block.py @@ -25,8 +25,10 @@ def update(self, *args): if args[0]: self.rectangle.x += 2 + self.position.x += 2 if args[1]: self.rectangle.x -= 2 + self.position.x -= 2 def check_collision(self): diff --git a/src/draw.py b/src/draw.py index 18252cc..162e2ba 100644 --- a/src/draw.py +++ b/src/draw.py @@ -14,7 +14,7 @@ def debug_create_objects(object_list): #main ball in game kinetic = GameBall(1, object_list, SCREEN_SIZE, Vector2(random.randint(20, SCREEN_SIZE[0] - 20), random.randint(20, SCREEN_SIZE[1] - 20)), - Vector2(4*random.random() - 2, 4*random.random() - 2), + Vector2(2, 2), [240, 197, 202], 20) object_list.append(kinetic) print(object_list) @@ -50,7 +50,7 @@ def debug_create_objects(object_list): #paddle paddle = KineticBlock(Vector2(400,390), 80, 20, [234, 112, 90]) object_list.append(paddle) - + print(object_list) def main(): pygame.init()