Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
annguyen18 authored Dec 28, 2022
1 parent 277c000 commit 235437f
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 0 deletions.
Binary file added background2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import pygame
from random import randint
from random import randint
from time import sleep
ball_size = 40

# self.rect.x = 450
# self.rect.y = 400
s = []


class Ball(pygame.sprite.Sprite):
def __init__(self, image):
# Call the parent class (Sprite) constructor
super().__init__()

self.image = pygame.image.load(image).convert_alpha()
self.image = pygame.transform.scale(self.image, (ball_size, ball_size))
self.rect = self.image.get_rect()
# velocity of x and y axis
# a = randint(-3, 6)
# b = randint(3, 8)
# self.velocity = []
# self.velocity.append(a)
# self.velocity.append(b)
self.velocity = [7, 9]
s.append(7)
s.append(9)

def spawn(self):
self.rect.x = 375
self.rect.y = 615-25
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
self.rect.x += s[0]
self.rect.y += s[1]

def update(self, paddle):

collide = pygame.Rect.colliderect(self.rect, paddle)
if collide:
self.velocity[1] *= -1

# if the ball touches left/right border
if self.rect.left < 0 or self.rect.right >= 900:
self.velocity[0] *= -1

# if the ball touches top border
if self.rect.top < 0:
self.velocity[1] *= -1

# if the ball go over bottom border
# if self.rect.bottom >= 700:
# spawn()

# self.rect.move_ip(self.velocity)

# def update(self):
# self.rect.x += self.velocity[0]
# self.rect.y += self.velocity[1]
# print("UPDATE: {} {}".format(self.rect.x, self.rect.y))

# def bounce(self):
# self.velocity[0] = -self.velocity[0]
# self.velocity[1] = randint(-1, 3)
# print("BOUNCE: {} {}".format(self.velocity[0], self.velocity[1]))

def draw(self, window):
window.blit(self.image, (self.rect.x, self.rect.y))
Binary file added bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions brick.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pygame
from PIL import Image

brick_size = 50
size = []


class Brick(pygame.sprite.Sprite):
def __init__(self, image):
# Call the parent class (Sprite) constructor
super().__init__()
ratio = Image.open(image)
w, h = ratio.size
size.append(w)
size.append(h)
self.image = pygame.image.load(image).convert_alpha()
self.image = pygame.transform.scale(
self.image, (brick_size*(w/h), brick_size))
self.rect = self.image.get_rect()

def draw(self, window, x, y):
window.blit(self.image, (x, y))

def getWidth(self):
return brick_size*(size[0]/size[1])

def getHeight(self):
return brick_size
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions levels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
level_1 = [
'1625485731735',
'8274615238564',
'4626852712643',
'7264852618257'
]

animal_dict = {
'1': 'bear',
'2': 'snake',
'3': 'owl',
'4': 'rhino',
'5': 'monkey',
'6': 'parrot',
'7': 'zebra',
'8': 'elephant'
}
72 changes: 72 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pygame
import sys
import os
from random import choice
from paddle import Paddle
from ball import Ball
from brick import Brick
from levels import *

pygame.init()

WIDTH = 1000
HEIGHT = 700
clock = pygame.time.Clock()
window = pygame.display.set_mode((WIDTH, HEIGHT)) # set a window of 900 x 00
pygame.display.set_caption("Brick Breaker by")
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
bg = pygame.image.load('background2.png')
bg = pygame.transform.scale(bg, (1260, 700))


paddle = Paddle()
ball = Ball("assets/PNG/Round (outline)/chick.png")

animal_list = os.listdir('assets/PNG/Square')

random_animal = choice(animal_list)
# brick = Brick("assets/PNG/Square (outline)/" + (random_animal))


def setup_level():
x_gap = 50

for row_index, row in enumerate(level_1):
y_gap = 30
for animal in row:
brick = Brick("assets/PNG/Square (outline)/" +
animal_dict[str(animal)] + ".png")
# print("row {}: {}".format(row_index, i))
brick.draw(window, y_gap, row_index+x_gap)
y_gap += brick.getWidth()+10
x_gap += brick.getHeight() + 10


def setup():
window.blit(bg, (0, 0))
paddle.paddle_input(10)
paddle.draw(window)
# ball.update(paddle)
# ball.draw(window)
setup_level()
clock.tick(60)
pygame.display.update()


def start_game():
running = True

while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False

setup()
pygame.quit()


start_game()
Binary file added paddle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions paddle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pygame
from pygame.locals import *

paddleX = 375
paddleY = 615
paddleWidth = 170
paddleHeight = 35


class Paddle(pygame.sprite.Sprite):
def __init__(self):
# init sprite class
super().__init__()

# image is the surface to be displayed
self.image = pygame.image.load(
'paddle.png').convert_alpha()
self.image = pygame.transform.scale(
self.image, (170, 35)) # 1438 x 500
# rect is the rectangle that surrounds the object
self.rect = self.image.get_rect()
self.rect.x = paddleX
self.rect.y = paddleY

def paddle_input(self, vel):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rect.x -= vel
if self.rect.x < 0:
self.rect.x = 0
elif keys[pygame.K_RIGHT]:
self.rect.x += vel
if self.rect.x > (900-paddleWidth):
self.rect.x = (900-paddleWidth)
else:
pygame.mouse.set_visible(False)
for event in pygame.event.get():
mouse_pos = pygame.mouse.get_pos()[0]
if mouse_pos < paddleWidth/2:
self.rect.x = 0
if self.rect.x > (900-paddleWidth):
self.rect.x = (900-paddleWidth)+paddleWidth/2
self.rect.x = mouse_pos-paddleWidth/2

def draw(self, window):
window.blit(self.image, (self.rect.x, self.rect.y))
6 changes: 6 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os
from random import choice

file_list = os.listdir('assets/PNG/Square')

print(choice(file_list))

0 comments on commit 235437f

Please sign in to comment.