Skip to content
This repository has been archived by the owner on Aug 24, 2018. It is now read-only.

Thuy Pham #77

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 84 additions & 22 deletions src/ball.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,104 @@ def __init__(self, bounds, position, velocity, color, radius):
self.bounds = bounds
self.color = color
self.radius = radius
self.maxHeight = self.radius + 2
self.reduce = self.bounds[1]/3

def update(self):
# bounce at edges. TODO: Fix sticky edges
if self.position.x < 0 + self.radius or self.position.x > self.bounds[0] - self.radius: # screen width
if self.position.x < 0 + self.radius or self.position.x > self.bounds[0] - self.radius/2: # screen width
self.velocity.x *= -1
if self.position.y < 0 + self.radius or self.position.y > self.bounds[1] - self.radius: # screen height
if self.position.y < 0 + self.radius or self.position.y > self.bounds[1] - self.radius/2: # screen height
self.velocity.y *= -1
self.position += self.velocity

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 effected by gravity
# """
# # TODO:
class BouncingBall(Ball):
"""
ball effected by gravity
"""
def update(self):
# super().update()
print(self.velocity.y, self.position.y, self.bounds[1] - self.radius)
self.velocity.x = 0

# class RainbowBall(???):
# """
# Ball that changes colors
# """
# # TODO:
if (self.velocity.y > 0-self.radius/2 and self.velocity.y < self.radius/2 and self.position.y >= self.bounds[1] - self.radius):
self.velocity.y = 0
else:
self.velocity.y += 0.2

if self.position.y > self.bounds[1]: # screen height
self.velocity.y -= 0.8
self.velocity.y *= -1


# if self.velocity.y < 0:


# class BouncingRainbow(???):
# """
# Ball that changes color and is affected by gravity
# """
# # TODO:
self.position += self.velocity
# if self.maxHeight >= (self.bounds[1] - 0.001):
# self.velocity.y = 0
# else:
# if (self.velocity.y < 0 and self.position.y < self.maxHeight) or self.position.y >= self.bounds[1] - self.radius:
# self.velocity.y *= -0.7
# self.reduce = self.maxHeight
# self.maxHeight += (self.bounds[1] - self.reduce)/3
# if self.velocity.y > 0:
# self.velocity.y += 0.01

# self.position += self.velocity


class RainbowBall(Ball):
"""
Ball that changes colors
"""
def update(self):
r = (self.color[0]+3) % 256
g = (self.color[1]+2) % 256
b = (self.color[2]-1) % 256

self.color = [r,g,b]

super().update()

class BouncingRainbow(RainbowBall):
"""
Ball that changes color and is affected by gravity
"""
def update(self):
self.velocity.x = 0
super().update()

class KineticBall(Ball):
"""
A ball that collides with other collidable balls using simple elastic circle collision
"""
def __init__(self, objects_list, bounds, position, velocity, color, radius):
self.objects_list = objects_list
super().__init__(bounds, position, velocity, color, radius)

def update(self):
super().update()
for obj in self.objects_list:
if not issubclass(type(obj), KineticBall):
continue

if obj == self:
continue

distance = obj.position.distance_to(self.position)

sumr = self.radius + obj.radius

if distance < sumr:
print("Collision!")
obj.velocity.x *= -1
obj.velocity.y *= -1

# class KineticBall(???):
# """
# A ball that collides with other collidable balls using simple elastic circle collision
# """
# # TODO:

# class KineticBouncing(???):
# """
Expand Down
10 changes: 8 additions & 2 deletions src/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
BACKGROUND_COLOR = [255, 255, 255]

def debug_create_balls(object_list):
ball = Ball(SCREEN_SIZE, Vector2(50, 50), Vector2(3, 3), [255, 0, 0], 10)
object_list.append(ball)
# ball1 = Ball(SCREEN_SIZE, Vector2(50, 50), Vector2(3, 3), [255, 0, 0], 10)
# ball2 = BouncingBall(SCREEN_SIZE, Vector2(100, 100), Vector2(3, -3), [255, 0, 0], 10)
# ball3 = RainbowBall(SCREEN_SIZE, Vector2(20, 50), Vector2(8, 8), [255, 0, 0], 10)
# ball4 = BouncingRainbow(SCREEN_SIZE, Vector2(70, 10), Vector2(4, 4), [255, 0, 0], 10)
ball1 = KineticBall(object_list, SCREEN_SIZE, Vector2(90, 60), Vector2(2, 4), [0, 0, 255], 20)
ball2 = KineticBall(object_list, SCREEN_SIZE, Vector2(200, 80), Vector2(-4, 4), [0, 255, 0], 20)
ball5 = KineticBall(object_list, SCREEN_SIZE, Vector2(400, 30), Vector2(4, -2), [255, 0, 0], 20)
object_list.extend([ball1, ball2, ball5])

# TODO: Create other ball types for testing

Expand Down