Skip to content
This repository was archived by the owner on Aug 24, 2018. It is now read-only.
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
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python.pythonPath": "C:\\Users\\diveb\\.virtualenvs\\Python-OOP-Toy-Up2074oM\\Scripts\\python.exe",
"python.linting.enabled": false
}
12 changes: 12 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pygame = "*"

[dev-packages]

[requires]
python_version = "3.6"
46 changes: 46 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/ball.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pygame import Rect

from block import KineticBlock
from block import Paddle

class Ball:
"""
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -117,7 +120,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:
Expand All @@ -134,6 +136,8 @@ def collide_with_rectangle(self, object):
else:
self.position.y = object.position.y + object.rectangle.height/2 + self.radius + 1

object.touched_by_ball = True

elif test == 4:
# TODO: Better error handling
print('error: ball inside rectangle')
Expand All @@ -150,20 +154,21 @@ 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)

object.touched_by_ball = True

def check_collision(self):
# Warning!: This is a primitive method of collision detection
# Consider time complexity when adding more of this type
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)
73 changes: 70 additions & 3 deletions src/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,9 +29,76 @@ def check_collision(self):
def draw(self, screen, pygame):
pygame.draw.rect(screen, self.color, self.rectangle)

class Paddle():
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
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
if pygame.key.get_pressed()[pygame.K_LEFT] == True:
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:
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

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
pass
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


38 changes: 30 additions & 8 deletions src/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,41 @@
from ball import *
from block import *

SCREEN_SIZE = [640, 480]
BACKGROUND_COLOR = [255, 255, 255]
SCREEN_SIZE = [400, 800]
BACKGROUND_COLOR = [0, 0, 0]

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, 0, 255], 5)
object_list.append(kinetic)

block = KineticBlock(Vector2(200,200), 100, 100, [0, 0, 255])
object_list.append(block)

paddle = Paddle(SCREEN_SIZE, Vector2(200,720), 75, 10, [255, 0, 255])
object_list.append(paddle)

# block array
block1 = KineticBlock(object_list, Vector2(35,200), 50, 20, [0, 0, 255])
object_list.append(block1)

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])
object_list.append(block3)

block4 = KineticBlock(object_list, Vector2(200,200), 50, 20, [0, 0, 255])
object_list.append(block4)

block5 = KineticBlock(object_list, Vector2(255,200), 50, 20, [0, 0, 255])
object_list.append(block5)

block6 = KineticBlock(object_list, Vector2(310,200), 50, 20, [0, 0, 255])
object_list.append(block6)

block7 = KineticBlock(object_list, Vector2(365,200), 50, 20, [0, 0, 255])
object_list.append(block7)

def main():
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
Expand Down