From 33384c0bf8cee813077fd3d572f1a04d9286d81e Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Fri, 13 Jul 2018 08:24:20 -0700 Subject: [PATCH 1/7] initialized project and changed screen size to 400x800 --- .vscode/settings.json | 4 ++++ Pipfile | 12 +++++++++++ Pipfile.lock | 46 +++++++++++++++++++++++++++++++++++++++++++ src/draw.py | 2 +- 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 Pipfile create mode 100644 Pipfile.lock diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..4d1e04d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "python.pythonPath": "C:\\Users\\diveb\\.virtualenvs\\Python-OOP-Toy-Up2074oM\\Scripts\\python.exe", + "python.linting.enabled": false +} \ No newline at end of file diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..f1d417c --- /dev/null +++ b/Pipfile @@ -0,0 +1,12 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +pygame = "*" + +[dev-packages] + +[requires] +python_version = "3.6" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..93085e1 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,46 @@ +{ + "_meta": { + "hash": { + "sha256": "04d5136a2e3e1a7589c6313b58b439879c213afd077d2d6580213f4255dcef7d" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.6" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "pygame": { + "hashes": [ + "sha256:22d5195a72b9a300cb7e87d7bde1373930a02b1054e95bc431d26a321ae15d07", + "sha256:2a8292d0b5a67e0b96d2cae09bbd8252e99daffe95fd88a00166b92fb6497b40", + "sha256:2ee479b0120cc82f04f7c41a09d75fc85c9992ed1a26c9527fb2b7b0637e6739", + "sha256:4847550da37d3bc702e0201a035de005f184b19e86625a3c70ab1d318317ee91", + "sha256:4d4985d0e0e5470c5ef872d65a863f1722d85a136327eb3bb5dcafc2853f2e55", + "sha256:5cf670378c8aabdf1bdf283eeec690c93f95dfe8de77c66bc837c91274c8ca49", + "sha256:643de45518d11cb7784e3fe77aef43fe3cb7e0d6b495ad4361ade931f16c334b", + "sha256:66746c32d21d8193ab066fdf90da96ed993c4818adb13bf4eaa472f306f7f421", + "sha256:73c86fdbbfc5ae77d0271fb0964f4a3fc639cc2277ffeb60ee4e981fb0d27640", + "sha256:751021819bdc0cbe5cbd51904abb6ff9e9aee5b0e8955af02284d0e77d6c9ec2", + "sha256:792f8d1a455d5d1de9c8f2cc051c6dac2b90705afc4cf9fe4933aeb296b0ae3f", + "sha256:831c906d4a70aa58f9382e79671525805583b58ac8c895b4c4bd066e8858a1a1", + "sha256:834ee27388563f15e6c736aa4850e4cf95609f67056ee73ff646528bb658e79e", + "sha256:925e6be8e8c3f1cec016f5a68f369491ae34600e04633759e0e07780ce7bd083", + "sha256:96a8f61c729d82576ee3d5cfe3b0b91e7b52a4fbf3d618a3b659347eeb4fd937", + "sha256:97615fb075808b4709f6115e3a11a253bfac78614f430bededbc0ac76a5100af", + "sha256:c49ece0e4ba71be90ed4bf6a905c35e5c1a95c4c0b0b42eaff74ab69c5793847", + "sha256:e8626d4d4e7617ebf103d96a493b9cbe53f9b39cd46ecbff232e33ba4a082530", + "sha256:ea7da8dd7fbc12becccf691b4c76fbb381e369e124c30e21f9ed263b6e7e139a" + ], + "index": "pypi", + "version": "==1.9.3" + } + }, + "develop": {} +} diff --git a/src/draw.py b/src/draw.py index 14ff036..ceb352e 100644 --- a/src/draw.py +++ b/src/draw.py @@ -6,7 +6,7 @@ from ball import * from block import * -SCREEN_SIZE = [640, 480] +SCREEN_SIZE = [400, 800] BACKGROUND_COLOR = [255, 255, 255] def debug_create_objects(object_list): From 21ec2f0a5763253bfdae710dd315da75621fdc85 Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Fri, 13 Jul 2018 09:13:46 -0700 Subject: [PATCH 2/7] blocks removed from object_list when hit --- src/ball.py | 4 ++++ src/block.py | 10 ++++++++-- src/draw.py | 31 +++++++++++++++++++++++++------ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/ball.py b/src/ball.py index 20e2792..a4f9b04 100644 --- a/src/ball.py +++ b/src/ball.py @@ -134,9 +134,12 @@ def collide_with_rectangle(self, object): else: self.position.y = object.position.y + object.rectangle.height/2 + self.radius + 1 + self.object_list.remove(object) + elif test == 4: # TODO: Better error handling print('error: ball inside rectangle') + self.object_list.remove(object) elif test == 0: # We are at a corner. Either it narrowly missed, or it hit the corner @@ -156,6 +159,7 @@ def collide_with_rectangle(self, object): # the same speed stand_in = Ball(self.bounds, corner, Vector2(0, self.velocity.length()), [0,0,0], 0) self.collide_with_ball(stand_in, relative_vector) + self.object_list.remove(object) def check_collision(self): # Warning!: This is a primitive method of collision detection diff --git a/src/block.py b/src/block.py index f7966e2..1ea8145 100644 --- a/src/block.py +++ b/src/block.py @@ -21,7 +21,8 @@ def __init__(self, position, width, height, color): def update(self, **kwargs): - self.touched_by_ball = False + # self.touched_by_ball = False + pass def check_collision(self): pass @@ -32,6 +33,11 @@ def draw(self, screen, pygame): class KineticBlock(Block): # No custom code needed here, just want to be able to differentiate # KineticBall will handle the collison - pass + def __init__(self, position, width, height, color): + super().__init__(position, width, height, color) + + def draw(self, screen, pygame): + pygame.draw.rect(screen, self.color, self.rectangle) + diff --git a/src/draw.py b/src/draw.py index ceb352e..7571b4b 100644 --- a/src/draw.py +++ b/src/draw.py @@ -11,14 +11,33 @@ def debug_create_objects(object_list): 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(200 , 700), + Vector2(2.5 , -2.5), + [255, 10, 0], 10) object_list.append(kinetic) - block = KineticBlock(Vector2(200,200), 100, 100, [0, 0, 255]) - object_list.append(block) - + # block array + block1 = KineticBlock(Vector2(35,200), 50, 20, [0, 0, 255]) + object_list.append(block1) + + block2 = KineticBlock(Vector2(90,200), 50, 20, [0, 0, 255]) + object_list.append(block2) + + block3 = KineticBlock(Vector2(145,200), 50, 20, [0, 0, 255]) + object_list.append(block3) + + block4 = KineticBlock(Vector2(200,200), 50, 20, [0, 0, 255]) + object_list.append(block4) + + block5 = KineticBlock(Vector2(255,200), 50, 20, [0, 0, 255]) + object_list.append(block5) + + block6 = KineticBlock(Vector2(310,200), 50, 20, [0, 0, 255]) + object_list.append(block6) + + block7 = KineticBlock(Vector2(365,200), 50, 20, [0, 0, 255]) + object_list.append(block7) + def main(): pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE) From 481ba99e436a71735cf906efd7470d14f71b5ad3 Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Fri, 13 Jul 2018 09:36:01 -0700 Subject: [PATCH 3/7] made stationary paddle --- src/ball.py | 8 +++----- src/block.py | 17 +++++++++++------ src/draw.py | 21 ++++++++++++--------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/ball.py b/src/ball.py index a4f9b04..0f66ee3 100644 --- a/src/ball.py +++ b/src/ball.py @@ -117,7 +117,6 @@ def collide_with_rectangle(self, object): test = left + right + top + bottom if test == 1: - object.touched_by_ball = True # the ball has collided with an edge # TODO: # fix sticky edges if left or right: @@ -134,12 +133,11 @@ def collide_with_rectangle(self, object): else: self.position.y = object.position.y + object.rectangle.height/2 + self.radius + 1 - self.object_list.remove(object) + object.touched_by_ball = True elif test == 4: # TODO: Better error handling print('error: ball inside rectangle') - self.object_list.remove(object) elif test == 0: # We are at a corner. Either it narrowly missed, or it hit the corner @@ -153,13 +151,13 @@ def collide_with_rectangle(self, object): for corner in corners: relative_vector = self.position - corner if relative_vector.length() <= self.radius: - object.touched_by_ball = True # Create a dummy object to make use of ball to ball collision, because the math is the same # Give it a velocity of the same magnitude as the current ball to cause it to reflect at # the same speed stand_in = Ball(self.bounds, corner, Vector2(0, self.velocity.length()), [0,0,0], 0) self.collide_with_ball(stand_in, relative_vector) - self.object_list.remove(object) + + object.touched_by_ball = True def check_collision(self): # Warning!: This is a primitive method of collision detection diff --git a/src/block.py b/src/block.py index 1ea8145..15564df 100644 --- a/src/block.py +++ b/src/block.py @@ -21,8 +21,7 @@ def __init__(self, position, width, height, color): def update(self, **kwargs): - # self.touched_by_ball = False - pass + self.touched_by_ball = False def check_collision(self): pass @@ -33,11 +32,17 @@ def draw(self, screen, pygame): class KineticBlock(Block): # No custom code needed here, just want to be able to differentiate # KineticBall will handle the collison - def __init__(self, position, width, height, color): + def __init__(self, object_list, position, width, height, color): + self.object_list = object_list super().__init__(position, width, height, color) - def draw(self, screen, pygame): - pygame.draw.rect(screen, self.color, self.rectangle) - + def update(self): + if self.touched_by_ball == True: + self.object_list.remove(self) +class Paddle(KineticBlock): + def __init__(self, object_list, position, width, height, color): + super().__init__(object_list, position, width, height, color) + def update(self): + self.touched_by_ball = False \ No newline at end of file diff --git a/src/draw.py b/src/draw.py index 7571b4b..bfc7019 100644 --- a/src/draw.py +++ b/src/draw.py @@ -7,35 +7,38 @@ from block import * SCREEN_SIZE = [400, 800] -BACKGROUND_COLOR = [255, 255, 255] +BACKGROUND_COLOR = [0, 0, 0] def debug_create_objects(object_list): kinetic = GameBall(1, object_list, SCREEN_SIZE, Vector2(200 , 700), Vector2(2.5 , -2.5), - [255, 10, 0], 10) + [255, 0, 255], 5) object_list.append(kinetic) + paddle = Paddle(object_list, Vector2(200,720), 75, 10, [255, 0, 255]) + object_list.append(paddle) + # block array - block1 = KineticBlock(Vector2(35,200), 50, 20, [0, 0, 255]) + block1 = KineticBlock(object_list, Vector2(35,200), 50, 20, [0, 0, 255]) object_list.append(block1) - block2 = KineticBlock(Vector2(90,200), 50, 20, [0, 0, 255]) + block2 = KineticBlock(object_list, Vector2(90,200), 50, 20, [0, 0, 255]) object_list.append(block2) - block3 = KineticBlock(Vector2(145,200), 50, 20, [0, 0, 255]) + block3 = KineticBlock(object_list, Vector2(145,200), 50, 20, [0, 0, 255]) object_list.append(block3) - block4 = KineticBlock(Vector2(200,200), 50, 20, [0, 0, 255]) + block4 = KineticBlock(object_list, Vector2(200,200), 50, 20, [0, 0, 255]) object_list.append(block4) - block5 = KineticBlock(Vector2(255,200), 50, 20, [0, 0, 255]) + block5 = KineticBlock(object_list, Vector2(255,200), 50, 20, [0, 0, 255]) object_list.append(block5) - block6 = KineticBlock(Vector2(310,200), 50, 20, [0, 0, 255]) + block6 = KineticBlock(object_list, Vector2(310,200), 50, 20, [0, 0, 255]) object_list.append(block6) - block7 = KineticBlock(Vector2(365,200), 50, 20, [0, 0, 255]) + block7 = KineticBlock(object_list, Vector2(365,200), 50, 20, [0, 0, 255]) object_list.append(block7) def main(): From 85709406fe3d6a6d614f082ae1020090e35b929a Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Fri, 13 Jul 2018 10:55:09 -0700 Subject: [PATCH 4/7] paddle moves --- src/ball.py | 5 ++++- src/block.py | 44 ++++++++++++++++++++++++++++++++++++++------ src/draw.py | 2 +- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/ball.py b/src/ball.py index 0f66ee3..9db29e1 100644 --- a/src/ball.py +++ b/src/ball.py @@ -4,6 +4,7 @@ from pygame import Rect from block import KineticBlock +from block import Paddle class Ball: """ @@ -32,9 +33,11 @@ def update(self, **kwargs): if self.position.y <= 0 + self.radius: # screen height self.position.y = self.radius + 1 self.velocity.y *= -1 + sys.exit() #Win Condition if self.position.y >= self.bounds[1] - self.radius: self.position.y = self.bounds[1] - self.radius - 1 self.velocity.y *= -1 + # sys.exit() #Lose Condition self.position += self.velocity self.collision_rectangle = self.update_rectangle() @@ -165,7 +168,7 @@ def check_collision(self): index = self.object_list.index(self) for object in self.object_list[index+1:]: # TODO: Check effeciency # Balls colliding with blocks - if issubclass(type(object), KineticBlock) and object != self: + if (issubclass(type(object), Paddle) or issubclass(type(object), KineticBlock)) and object != self: # Do a first round pass for collision (we know object is a KineticBlock) if self.collision_rectangle.colliderect(object.rectangle): self.collide_with_rectangle(object) diff --git a/src/block.py b/src/block.py index 15564df..1c59995 100644 --- a/src/block.py +++ b/src/block.py @@ -12,8 +12,8 @@ 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), + self.position.x - (width/2), + self.position.y - (height/2), width, height) self.color = color @@ -40,9 +40,41 @@ def update(self): if self.touched_by_ball == True: self.object_list.remove(self) -class Paddle(KineticBlock): - def __init__(self, object_list, position, width, height, color): - super().__init__(object_list, position, width, height, color) +class Paddle(): + def __init__(self, position, width, height, color): + # Create a rectangle centered around the x and y + self.position = position + self.width = width + self.height = height + self.rectangle = pygame.Rect( + self.position.x - (self.width/2), + self.position.y - (self.height/2), + self.width, + self.height) + self.color = color + self.touched_by_ball = False def update(self): - self.touched_by_ball = False \ No newline at end of file + self.touched_by_ball = False + if pygame.key.get_pressed()[pygame.K_LEFT] == True: + print("left pressed") + self.position.x += -3 + self.rectangle = pygame.Rect( + self.position.x - 3 - (self.width/2), + self.position.y - (self.height/2), + self.width, + self.height) + if pygame.key.get_pressed()[pygame.K_RIGHT] == True: + print("right pressed") + self.position.x += 3 + self.rectangle = pygame.Rect( + self.position.x + 3 - (self.width/2), + self.position.y - (self.height/2), + self.width, + self.height) + + def check_collision(self): + pass + + def draw(self, screen, pygame): + pygame.draw.rect(screen, self.color, self.rectangle) \ No newline at end of file diff --git a/src/draw.py b/src/draw.py index bfc7019..3693793 100644 --- a/src/draw.py +++ b/src/draw.py @@ -16,7 +16,7 @@ def debug_create_objects(object_list): [255, 0, 255], 5) object_list.append(kinetic) - paddle = Paddle(object_list, Vector2(200,720), 75, 10, [255, 0, 255]) + paddle = Paddle(Vector2(200,720), 75, 10, [255, 0, 255]) object_list.append(paddle) # block array From ba1681ae996bd11142da6428b8f76e1597a79484 Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Fri, 13 Jul 2018 10:57:16 -0700 Subject: [PATCH 5/7] win and lose conditions added --- src/ball.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ball.py b/src/ball.py index 9db29e1..43f6091 100644 --- a/src/ball.py +++ b/src/ball.py @@ -37,7 +37,7 @@ def update(self, **kwargs): if self.position.y >= self.bounds[1] - self.radius: self.position.y = self.bounds[1] - self.radius - 1 self.velocity.y *= -1 - # sys.exit() #Lose Condition + sys.exit() #Lose Condition self.position += self.velocity self.collision_rectangle = self.update_rectangle() From e85e577a6847ea7e188203d55ab2baf848bbb193 Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Fri, 13 Jul 2018 11:08:12 -0700 Subject: [PATCH 6/7] stronger blocks added --- src/block.py | 42 ++++++++++++++++++++++++++++++------------ src/draw.py | 2 +- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/block.py b/src/block.py index 1c59995..c88c76d 100644 --- a/src/block.py +++ b/src/block.py @@ -29,17 +29,6 @@ def check_collision(self): def draw(self, screen, pygame): pygame.draw.rect(screen, self.color, self.rectangle) -class KineticBlock(Block): - # No custom code needed here, just want to be able to differentiate - # KineticBall will handle the collison - def __init__(self, object_list, position, width, height, color): - self.object_list = object_list - super().__init__(position, width, height, color) - - def update(self): - if self.touched_by_ball == True: - self.object_list.remove(self) - class Paddle(): def __init__(self, position, width, height, color): # Create a rectangle centered around the x and y @@ -77,4 +66,33 @@ def check_collision(self): pass def draw(self, screen, pygame): - pygame.draw.rect(screen, self.color, self.rectangle) \ No newline at end of file + pygame.draw.rect(screen, self.color, self.rectangle) + +class KineticBlock(Block): + # No custom code needed here, just want to be able to differentiate + # KineticBall will handle the collison + def __init__(self, object_list, position, width, height, color): + self.object_list = object_list + super().__init__(position, width, height, color) + + def update(self): + if self.touched_by_ball == True: + self.object_list.remove(self) + +class StrongKineticBlock(KineticBlock): + def __init__(self, object_list, position, width, height, color, strength): + self.strength = strength + super().__init__(object_list, position, width, height, color) + + def update(self): + if self.touched_by_ball == True: + if self.strength == 0: + self.object_list.remove(self) + else: + self.strength -= 1 + self.color[0] = (self.color[0] + 100) % 256 + self.color[1] = (self.color[1] - 100) % 256 + self.color[2] = (self.color[2] + 50) % 256 + self.touched_by_ball = False + + diff --git a/src/draw.py b/src/draw.py index 3693793..966e1a7 100644 --- a/src/draw.py +++ b/src/draw.py @@ -23,7 +23,7 @@ def debug_create_objects(object_list): block1 = KineticBlock(object_list, Vector2(35,200), 50, 20, [0, 0, 255]) object_list.append(block1) - block2 = KineticBlock(object_list, Vector2(90,200), 50, 20, [0, 0, 255]) + block2 = StrongKineticBlock(object_list, Vector2(90,200), 50, 20, [0, 0, 255], 2) object_list.append(block2) block3 = KineticBlock(object_list, Vector2(145,200), 50, 20, [0, 0, 255]) From d197ccb0b2b1e033627ea80d2e143dc63586c298 Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Fri, 13 Jul 2018 11:18:04 -0700 Subject: [PATCH 7/7] paddle cannot leave screen --- src/block.py | 36 +++++++++++++++++++++--------------- src/draw.py | 2 +- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/block.py b/src/block.py index c88c76d..26c7cf3 100644 --- a/src/block.py +++ b/src/block.py @@ -30,8 +30,9 @@ def draw(self, screen, pygame): pygame.draw.rect(screen, self.color, self.rectangle) class Paddle(): - def __init__(self, position, width, height, color): + def __init__(self, bounds, position, width, height, color): # Create a rectangle centered around the x and y + self.bounds = bounds self.position = position self.width = width self.height = height @@ -46,21 +47,26 @@ def __init__(self, position, width, height, color): def update(self): self.touched_by_ball = False if pygame.key.get_pressed()[pygame.K_LEFT] == True: - print("left pressed") - self.position.x += -3 - self.rectangle = pygame.Rect( - self.position.x - 3 - (self.width/2), - self.position.y - (self.height/2), - self.width, - self.height) + if self.position.x <= 0 + self.width/2: + self.position.x += 1 + else: + self.position.x += -3 + self.rectangle = pygame.Rect( + self.position.x - 3 - (self.width/2), + self.position.y - (self.height/2), + self.width, + self.height) + if pygame.key.get_pressed()[pygame.K_RIGHT] == True: - print("right pressed") - self.position.x += 3 - self.rectangle = pygame.Rect( - self.position.x + 3 - (self.width/2), - self.position.y - (self.height/2), - self.width, - self.height) + if self.position.x >= self.bounds[0] - self.width/2: + self.position.x += -1 + else: + self.position.x += 3 + self.rectangle = pygame.Rect( + self.position.x + 3 - (self.width/2), + self.position.y - (self.height/2), + self.width, + self.height) def check_collision(self): pass diff --git a/src/draw.py b/src/draw.py index 966e1a7..46d6902 100644 --- a/src/draw.py +++ b/src/draw.py @@ -16,7 +16,7 @@ def debug_create_objects(object_list): [255, 0, 255], 5) object_list.append(kinetic) - paddle = Paddle(Vector2(200,720), 75, 10, [255, 0, 255]) + paddle = Paddle(SCREEN_SIZE, Vector2(200,720), 75, 10, [255, 0, 255]) object_list.append(paddle) # block array