From 73eb6526cf985a39175e7f40c7701deed2e254a0 Mon Sep 17 00:00:00 2001 From: april Date: Fri, 13 Jul 2018 07:40:05 -0400 Subject: [PATCH 1/5] pulling gravity in --- src/ball.py | 8 +++++++- src/draw.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/ball.py b/src/ball.py index 20e2792..32c142c 100644 --- a/src/ball.py +++ b/src/ball.py @@ -46,9 +46,15 @@ def check_collision(self): def draw(self, screen, pygame): # cast x and y to int for drawing pygame.draw.circle(screen, self.color, [int(self.position.x), int(self.position.y)], self.radius) +class BouncingBall(Ball): + GRAVITY = .1 + def update(self): + self.velocity.y += self.Gravity + super().update() + class GameBall(Ball): - """ + """ A ball that collides with blocks """ def __init__(self, mass, object_list, bounds, position, velocity, color, radius): diff --git a/src/draw.py b/src/draw.py index 14ff036..17c921f 100644 --- a/src/draw.py +++ b/src/draw.py @@ -6,33 +6,79 @@ from ball import * from block import * -SCREEN_SIZE = [640, 480] +SCREEN_SIZE = [640, 480] BACKGROUND_COLOR = [255, 255, 255] def debug_create_objects(object_list): + #ball = Ball(SCREEN_SIZE, Vector2(50, 50), Vector2(3, 3), [255, 0, 0], 10) + # object_list.append(ball) + + # bouncing_ball = BouncingBall(SCREEN_SIZE, Vector2(100, 100), Vector2(3, 0), + # [0, 0, 255], 8) + + # object_list.append(bouncing_ball) + + # rainbow_ball = RainbowBall(SCREEN_SIZE, Vector2(400, 300), Vector2(3, -2) + # [0, 255, 0], 8) + + # object_list.append(rainbow_ball) + # bouncing_rainbow_ball = BouncingRainbow(SCREEN_SIZE, Vector2(300, 150), + # Vector2(-4, 0), [0, 255, 0], 8) + # object_list.append(bouncing_rainbow_ball) + + for i in range(0, 10): 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) object_list.append(kinetic) + # for i in range(0, 5): + # kinetic = AllTheThings(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), + # [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)], + # random.randint(3, 20)) + # object_list.append(kinetic) block = KineticBlock(Vector2(200,200), 100, 100, [0, 0, 255]) object_list.append(block) + # kinetic1 = KineticBall(1, object_list, SCREEN_SIZE, + # Vector2(100, 200), + # Vector2(2, 0), + # [10, 255, 0], 20) + + # kinetic2 = KineticBall(1, object_list, SCREEN_SIZE, + # Vector2(400, 180), + # Vector2(-1, 0), + # [10, 128, 255], 20) + + # kinetic1 = KineticBall(1, object_list, SCREEN_SIZE, + # Vector2(200, 100), + # Vector2(0, 1), + # [10, 255, 0], 20) + + # kinetic2 = KineticBall(1, object_list, SCREEN_SIZE, + # Vector2(210, 400), + # Vector2(0, -2), + # [10, 128, 255], 20) + + # object_list.extend((kinetic1, kinetic2)) + def main(): pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE) # Used to manage how fast the screen updates - clock = pygame.time.Clock() + clock = pygame.time.Clock(60) 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 - left = False - right = False + for event in pygame.event.get(): + if event.type == pygame.QUIT: sys.exit() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() @@ -43,6 +89,8 @@ def main(): left = True if keys[pygame.K_RIGHT]: right = True + + h9 for object in object_list: object.update() object.check_collision() From c28b0ef8564b34ad311de438ad6c5e3dd528161d Mon Sep 17 00:00:00 2001 From: april Date: Fri, 13 Jul 2018 08:05:18 -0400 Subject: [PATCH 2/5] making thing happen --- src/ball.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/ball.py b/src/ball.py index 32c142c..0d3f195 100644 --- a/src/ball.py +++ b/src/ball.py @@ -52,7 +52,19 @@ class BouncingBall(Ball): def update(self): self.velocity.y += self.Gravity super().update() - + + +class RainbowBall(Ball): + def update(self): + r = (self.color[0] + 4) % 256 + g = (self.color[1] + 12) % 256 + b = (self.color[2]) - 6) % 256 + self.color = [r, g, b] + super().update() + +class BouncingRainbow(BouncingBall, RainbowBall): + pass + class GameBall(Ball): """ A ball that collides with blocks @@ -170,6 +182,18 @@ def check_collision(self): for object in self.object_list[index+1:]: # TODO: Check effeciency # Balls colliding with blocks if issubclass(type(object), KineticBlock) and object != self: + relative_vector = self.position - object.position + if relative_vector.length() <= self.radius + object.radius: + # Objects are in collision range, so collide + self.collide_with_ball(object, relative_vector) + # Balls colliding with rectangles + elif 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) + +class KineticBouncing(BouncingBall, KineticBall): + pass + +class AllTheThings(BouncingBall, KineticBall, RainbowBall): + pass From 42a2dc14d25253fe29930cb1c8aa56d7afbfcccf Mon Sep 17 00:00:00 2001 From: april Date: Fri, 13 Jul 2018 13:31:38 -0400 Subject: [PATCH 3/5] got thing to work --- .vscode/settings.json | 3 ++ Pipfile | 12 ++++++++ Pipfile.lock | 46 ++++++++++++++++++++++++++++ src/ball.py | 32 +------------------ src/block.py | 56 ++++++++++++++++++++++++++++++++++ src/draw.py | 71 +++++++++---------------------------------- 6 files changed, 132 insertions(+), 88 deletions(-) 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..018d413 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\Users\\User\\.virtualenvs\\Python-OOP-Toy-fSTVGyeN\\Scripts\\python.exe" +} \ 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/ball.py b/src/ball.py index 0d3f195..20e2792 100644 --- a/src/ball.py +++ b/src/ball.py @@ -46,27 +46,9 @@ def check_collision(self): def draw(self, screen, pygame): # cast x and y to int for drawing pygame.draw.circle(screen, self.color, [int(self.position.x), int(self.position.y)], self.radius) -class BouncingBall(Ball): - GRAVITY = .1 - - def update(self): - self.velocity.y += self.Gravity - super().update() - - -class RainbowBall(Ball): - def update(self): - r = (self.color[0] + 4) % 256 - g = (self.color[1] + 12) % 256 - b = (self.color[2]) - 6) % 256 - self.color = [r, g, b] - super().update() - -class BouncingRainbow(BouncingBall, RainbowBall): - pass class GameBall(Ball): - """ + """ A ball that collides with blocks """ def __init__(self, mass, object_list, bounds, position, velocity, color, radius): @@ -182,18 +164,6 @@ def check_collision(self): for object in self.object_list[index+1:]: # TODO: Check effeciency # Balls colliding with blocks if issubclass(type(object), KineticBlock) and object != self: - relative_vector = self.position - object.position - if relative_vector.length() <= self.radius + object.radius: - # Objects are in collision range, so collide - self.collide_with_ball(object, relative_vector) - # Balls colliding with rectangles - elif 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) - -class KineticBouncing(BouncingBall, KineticBall): - pass - -class AllTheThings(BouncingBall, KineticBall, RainbowBall): - pass diff --git a/src/block.py b/src/block.py index f7966e2..d9c2144 100644 --- a/src/block.py +++ b/src/block.py @@ -21,6 +21,9 @@ def __init__(self, position, width, height, color): def update(self, **kwargs): + if self.touched_by_ball == True: + print(1) + self.touched_by_ball = False def check_collision(self): @@ -34,4 +37,57 @@ class KineticBlock(Block): # KineticBall will handle the collison pass +class Paddle(KineticBlock): + def __init__(self, position, width, height, color): + self.left = False + self.right = False + self.height = height + self.width = width + super().__init__(position, width, height, color) + + + def update(self): + if self.left == True: + self.position.x = (self.position.x - 3) if self.position.x > 30 else 30 #TODO: dynamic min + self.rectangle = pygame.Rect( + self.position.x - (self.width/2), + self.position.y - (self.height/2), + self.width, + self.height) + if self.right == True: + self.position.x = (self.position.x + 3) if self.position.x < 370 else 370 #TODO: dynamic max + self.rectangle = pygame.Rect( + self.position.x - (self.width/2), + self.position.y - (self.height/2), + self.width, + self.height) + self.left = False + self.right = False + super().update() + + def move_left(self): + self.left = True + + def move_right(self): + self.right = True + +class Weak_Block(KineticBlock): + def __init__(self, position, width, height, color): + self.hp = 1 + super().__init__(position, width, height, color) + + def update(self): + if self.touched_by_ball == True: + print(self.hp) + super().update() + + +class Strong_Block(KineticBlock): + def __init__(self, position, width, height, color): + self.hp = 3 + super().__init__(position, width, height, color) + def update(self): + if self.touched_by_ball == True: + print(self.hp) + super().update() diff --git a/src/draw.py b/src/draw.py index 17c921f..349551e 100644 --- a/src/draw.py +++ b/src/draw.py @@ -6,71 +6,31 @@ 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): - #ball = Ball(SCREEN_SIZE, Vector2(50, 50), Vector2(3, 3), [255, 0, 0], 10) - # object_list.append(ball) - - # bouncing_ball = BouncingBall(SCREEN_SIZE, Vector2(100, 100), Vector2(3, 0), - # [0, 0, 255], 8) - - # object_list.append(bouncing_ball) - - # rainbow_ball = RainbowBall(SCREEN_SIZE, Vector2(400, 300), Vector2(3, -2) - # [0, 255, 0], 8) - - # object_list.append(rainbow_ball) - # bouncing_rainbow_ball = BouncingRainbow(SCREEN_SIZE, Vector2(300, 150), - # Vector2(-4, 0), [0, 255, 0], 8) - # object_list.append(bouncing_rainbow_ball) - - for i in range(0, 10): kinetic = GameBall(1, object_list, SCREEN_SIZE, - Vector2(random.randint(20, SCREEN_SIZE[0] - 20), random.randint(20, SCREEN_SIZE[1] - 20)), + Vector2(random.randint(20, SCREEN_SIZE[0] - 20), random.randint(300, SCREEN_SIZE[1] - 20)), Vector2(4*random.random() - 2, 4*random.random() - 2), [255, 10, 0], 20) object_list.append(kinetic) - # for i in range(0, 5): - # kinetic = AllTheThings(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), - # [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)], - # random.randint(3, 20)) - # object_list.append(kinetic) - - block = KineticBlock(Vector2(200,200), 100, 100, [0, 0, 255]) - object_list.append(block) - # kinetic1 = KineticBall(1, object_list, SCREEN_SIZE, - # Vector2(100, 200), - # Vector2(2, 0), - # [10, 255, 0], 20) - # kinetic2 = KineticBall(1, object_list, SCREEN_SIZE, - # Vector2(400, 180), - # Vector2(-1, 0), - # [10, 128, 255], 20) + paddle = Paddle(Vector2(SCREEN_SIZE[0] // 2, SCREEN_SIZE[1] - 50), 70, 15, [0, 0, 0]) + object_list.append(paddle) - # kinetic1 = KineticBall(1, object_list, SCREEN_SIZE, - # Vector2(200, 100), - # Vector2(0, 1), - # [10, 255, 0], 20) - - # kinetic2 = KineticBall(1, object_list, SCREEN_SIZE, - # Vector2(210, 400), - # Vector2(0, -2), - # [10, 128, 255], 20) - - # object_list.extend((kinetic1, kinetic2)) - + for i in range(5): + color = [random.randint(100, 250), random.randint(100, 250), random.randint(100, 250)] + for j in range(3): + block = KineticBlock(Vector2(52 + (i*74),100 + (j* 40)), 70, 30, color) + object_list.append(block) def main(): pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE) # Used to manage how fast the screen updates - clock = pygame.time.Clock(60) + clock = pygame.time.Clock() object_list = [] # list of objects of all types in the toy @@ -80,17 +40,14 @@ def main(): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() - for event in pygame.event.get(): - if event.type == pygame.QUIT: sys.exit() - - #TODO: Feed input variables into update for objects that need it. keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: - left = True + object_list[1].move_left() + pass if keys[pygame.K_RIGHT]: - right = True + object_list[1].move_right() + pass - h9 for object in object_list: object.update() object.check_collision() From 295ea87da81e0e7baa9f5de8be62717c714ea65c Mon Sep 17 00:00:00 2001 From: april Date: Fri, 13 Jul 2018 13:42:06 -0400 Subject: [PATCH 4/5] maling things work --- src/draw.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/draw.py b/src/draw.py index 349551e..23220a8 100644 --- a/src/draw.py +++ b/src/draw.py @@ -13,14 +13,14 @@ def debug_create_objects(object_list): kinetic = GameBall(1, object_list, SCREEN_SIZE, Vector2(random.randint(20, SCREEN_SIZE[0] - 20), random.randint(300, SCREEN_SIZE[1] - 20)), Vector2(4*random.random() - 2, 4*random.random() - 2), - [255, 10, 0], 20) + [200, 10, 0], 20) object_list.append(kinetic) paddle = Paddle(Vector2(SCREEN_SIZE[0] // 2, SCREEN_SIZE[1] - 50), 70, 15, [0, 0, 0]) object_list.append(paddle) for i in range(5): - color = [random.randint(100, 250), random.randint(100, 250), random.randint(100, 250)] + color = [random.randint(10, 250), random.randint(10, 250), random.randint(10, 250)] for j in range(3): block = KineticBlock(Vector2(52 + (i*74),100 + (j* 40)), 70, 30, color) object_list.append(block) From 8c8f84f14d14b371df50d73cf6d77e4f2a70d927 Mon Sep 17 00:00:00 2001 From: april Date: Fri, 13 Jul 2018 13:44:31 -0400 Subject: [PATCH 5/5] making things --- src/block.py | 2 +- src/draw.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/block.py b/src/block.py index d9c2144..9998af2 100644 --- a/src/block.py +++ b/src/block.py @@ -45,7 +45,7 @@ def __init__(self, position, width, height, color): self.width = width super().__init__(position, width, height, color) - +this is too make blocks def update(self): if self.left == True: self.position.x = (self.position.x - 3) if self.position.x > 30 else 30 #TODO: dynamic min diff --git a/src/draw.py b/src/draw.py index 23220a8..a154f78 100644 --- a/src/draw.py +++ b/src/draw.py @@ -39,7 +39,7 @@ def main(): while True: # TODO: Create more elegant condition for loop for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() - +#this is too make the paddle move left to right keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: object_list[1].move_left()