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..ba8c1d1 100644 --- a/src/ball.py +++ b/src/ball.py @@ -1,9 +1,10 @@ import math +import pygame from pygame.math import Vector2 from pygame import Rect -from block import KineticBlock +from block import KineticBlock, TopBlock class Ball: """ @@ -14,7 +15,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): @@ -30,9 +31,13 @@ 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: + 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 diff --git a/src/block.py b/src/block.py index f7966e2..d427fb0 100644 --- a/src/block.py +++ b/src/block.py @@ -20,12 +20,21 @@ def __init__(self, position, width, height, color): self.touched_by_ball = False - def update(self, **kwargs): + def update(self, *args): self.touched_by_ball = False + 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): pass + def draw(self, screen, pygame): pygame.draw.rect(screen, self.color, self.rectangle) @@ -35,3 +44,26 @@ class KineticBlock(Block): pass +class TopBlock(KineticBlock): + 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), + position.y - (height/2), + width, + height) + self.color = color + self.touched_by_ball = False + + def update(self, *args): + if self.touched_by_ball: + for object in self.object_list: + + if object == self: + self.object_list.remove(object) + else: + continue + + \ No newline at end of file diff --git a/src/draw.py b/src/draw.py index 14ff036..162e2ba 100644 --- a/src/draw.py +++ b/src/draw.py @@ -1,24 +1,57 @@ 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) + Vector2(2, 2), + [240, 197, 202], 20) object_list.append(kinetic) + print(object_list) + + #blocks at top of screen + + #top row of blocks + block1 = TopBlock(Vector2(50,20), 90, 30, [156, 214, 184], object_list) + object_list.append(block1) + block2 = TopBlock(Vector2(150,20), 90, 30, [156, 214, 184], object_list) + object_list.append(block2) + block3 = TopBlock(Vector2(250,20), 90, 30, [156, 214, 184], object_list) + object_list.append(block3) + block4 = TopBlock(Vector2(350,20), 90, 30, [156, 214, 184], object_list) + object_list.append(block4) + block5 = TopBlock(Vector2(450,20), 90, 30, [156, 214, 184], object_list) + object_list.append(block5) + 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]) + object_list.append(paddle) + print(object_list) - block = KineticBlock(Vector2(200,200), 100, 100, [0, 0, 255]) - object_list.append(block) - def main(): pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE) @@ -27,7 +60,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 +74,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)