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

David Loveday #86

Open
wants to merge 4 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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\Users\\Aquila\\.virtualenvs\\Python-OOP-Toy-XlVGbrZH\\scripts\\python.exe"
}
13 changes: 13 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pygame = "*"

[dev-packages]
pylint = "*"

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

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

9 changes: 6 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
import pygame

class Ball:
"""
Expand Down Expand Up @@ -33,8 +34,10 @@ def update(self, **kwargs):
self.position.y = self.radius + 1
self.velocity.y *= -1
if self.position.y >= self.bounds[1] - self.radius:
self.position.y = self.bounds[1] - self.radius - 1
self.velocity.y *= -1
print("Game Over")
pygame.quit()
# self.position.y = self.bounds[1] - self.radius - 1
# self.velocity.y *= -1

self.position += self.velocity
self.collision_rectangle = self.update_rectangle()
Expand Down Expand Up @@ -115,7 +118,7 @@ def collide_with_rectangle(self, object):
bottom = True

test = left + right + top + bottom

if test == 1:
object.touched_by_ball = True
# the ball has collided with an edge
Expand Down
54 changes: 54 additions & 0 deletions src/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,57 @@ class KineticBlock(Block):
pass


class BreakableBlock(KineticBlock):
def __init__(self, object_list, bounds, position, width, height, color):
self.object_list = object_list
super().__init__(position, width, height, color)
self.breakable = True

def update(self, **kwargs):
if self.touched_by_ball:
self.object_list.remove(self)



# class LessBrittleBlock(BreakableBlock):
# def __init__(self, position, width, height, color):
# super().__init__(position, width, height, color)
# self.hits = 0


# def update(self, **kwargs):
# # if self.touched_by_ball and self.hits < 1:
# self.hits += 1
# self.color = [5, 22, 20]
# self.touched_by_ball = False
# if self.touched_by_ball:
# self.rectangle.width = 0
# self.rectangle.height = 0



class Paddle(KineticBlock):
def __init__(self, bounds, position, width, height, color):
super().__init__(position, width, height, color)
self.bounds = bounds

def update(self, **kwargs):
left = kwargs['left']
right = kwargs['right']
speed = 11

if left:
self.position.x -= speed
if self.position.x < 0 + self.rectangle.width / 2:
self.position.x = 0 + self.rectangle.width / 2
elif right:
self.position.x += speed
if self.position.x > self.bounds[0] - self.rectangle.width / 2:
self.position.x = self.bounds[0] - self.rectangle.width / 2
self.rectangle = pygame.Rect(
self.position.x - (self.rectangle.width/2),
self.position.y - (self.rectangle.height/2),
self.rectangle.width,
self.rectangle.height,
)
super().update()
46 changes: 32 additions & 14 deletions src/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,63 @@
from ball import *
from block import *

SCREEN_SIZE = [640, 480]
SCREEN_SIZE = [400, 700]
BACKGROUND_COLOR = [255, 255, 255]
STANDARD_BLOCK = 40

def debug_create_objects(object_list):
kinetic = GameBall(1, object_list, SCREEN_SIZE,
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(20*random.random() - 2, 20*random.random() - 2),
[255, 10, 0], 15)
object_list.append(kinetic)

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

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


breakable = lambda i, J, k: BreakableBlock(object_list, SCREEN_SIZE, Vector2(20+(i*60), j * 50),60,30,[125,0,10])
breakableblocks = []

for i in range(0, 10):
for j in range(1, 10):
for k in range(2, 10):
if i % 2 == 0:
breakableblocks.append(breakable(i, j, k))

object_list.extend(breakableblocks)

paddle = Paddle(SCREEN_SIZE, Vector2(200,600), 150, 20, [25, 55, 0])
object_list.append(paddle)



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

# Used to manage how fast the screen updates
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
left = False
right = False

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
if keys[pygame.K_RIGHT]:
right = True
for object in object_list:
object.update()
object.update(left=left, right=right)
object.check_collision()

# Draw Updates
Expand Down