Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

feat: ♻ refactor everything #9

Merged
merged 1 commit into from
Dec 8, 2022
Merged
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
16 changes: 8 additions & 8 deletions GUI.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import pygame
import button
import components.button as button

# init pygame.
pygame.init()
Expand All @@ -11,15 +11,15 @@
# set game name.
pygame.display.set_caption("ʙᴀsᴋᴇᴛ ɢᴏ !")
# define window's size.
window = pygame.display.set_mode((1024,640))
window = pygame.display.set_mode((1024, 640))
fond = pygame.image.load('./img/bg.png')
window.blit(fond, (0,0))
#load button images
window.blit(fond, (0, 0))
# load button images
start_img = pygame.image.load('./img/start_btn.png').convert_alpha()
exit_img = pygame.image.load('./img/exit_btn.png').convert_alpha()
start_img_hov = pygame.image.load('./img/start_btn_hover.png').convert_alpha()
exit_img_hov = pygame.image.load('./img/exit_btn_hover.png').convert_alpha()
#create button instances
# create button instances
start_button = button.Button(397, 390, start_img, 0.8)
start2_button = button.Button(397, 390, start_img_hov, 0.8)
exit_button = button.Button(412, 500, exit_img, 0.8)
Expand All @@ -31,14 +31,14 @@

while is_running:
if start_button.draw(window):
import main.py
import main
if exit_button.draw(window):
pygame.quit()
sys.exit()
MOUSE_POS = pygame.mouse.get_pos()
if 397 <= MOUSE_POS[0] < 622 and 390 <= MOUSE_POS[1] < 490 :
if 397 <= MOUSE_POS[0] < 622 and 390 <= MOUSE_POS[1] < 490:
start2_button.draw(window)
if 412 <= MOUSE_POS[0] < 607 and 500 <= MOUSE_POS[1] < 600 :
if 412 <= MOUSE_POS[0] < 607 and 500 <= MOUSE_POS[1] < 600:
exit2_button.draw(window)
for event in pygame.event.get():
if event.type == pygame.QUIT:
Expand Down
Empty file added components/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
70 changes: 35 additions & 35 deletions player.py → components/player.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import pygame
from element import Element
class Player():
def __init__(self, game, name:str) -> None:
# set player's game.
self.__game = game
# set our player's name.
self.__name = name
# init our score.
self.__score = 0
pass
def setup(self) -> None:
"""
Setup the player into the game.
"""
# game's default font.
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render(str(self.__score), True, (255, 255, 255), None)
# register the game element.
self.__game.get_window().register_element("player_" + self.__name + "_score", Element(text, 512, 10))
def get_name(self) -> str:
"""
:return: str: player's name.
"""
return self.__name
def get_score(self) -> int:
"""
:return: int: player's score.
"""
def set_score(self, score:int) -> None:
"""
Define player's score.
:param: int score: the score to set.
"""
import pygame
from element import Element

class Player():
def __init__(self, game, name:str) -> None:
# set player's game.
self.__game = game
# set our player's name.
self.__name = name
# init our score.
self.__score = 0
pass
def setup(self) -> None:
"""
Setup the player into the game.
"""
# game's default font.
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render(str(self.__score), True, (255, 255, 255), None)
# register the game element.
self.__game.get_window().register_element("player_" + self.__name + "_score", Element(text, 512, 10))
def get_name(self) -> str:
"""
:return: str: player's name.
"""
return self.__name
def get_score(self) -> int:
"""
:return: int: player's score.
"""
def set_score(self, score:int) -> None:
"""
Define player's score.
:param: int score: the score to set.
"""
self.__score = score
Empty file added events/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,74 +1,77 @@
from event_listener import EventListener
from stoppable_thread import StoppableThread
from vector import Vector
from math import (atan, cos, sin)
from time import time
import pygame

class BallReleaseEventListener(EventListener):
def __init__(self) -> None:
super().__init__()

def run(self, event, game):
# retrieve the ball.
ball = game.get_window().get_element("ball")
# retrieve the placeholder ball.
placeholder_ball = game.get_window().get_element("placeholder_ball")
# check if the ball is at the same position
if abs(ball.get_x() - ball.get_initial_x()) < 15 and abs(ball.get_y() - ball.get_initial_y()) < 15 or ball.is_released():
return
ball.set_released(True)
# disable ball.
placeholder_ball.set_visible(False)
# clear dots.
for i in range(20):
game.get_window().remove_element(("dot_",str(i)))
# launch new thread.
self.__t = StoppableThread(target=self.__move_ball, args=(game,))
# register the thread in order to be able to kill it.
game.register_thread(self.__t)
# start the thread execution.
self.__t.start()
def __move_ball(self, game):
"""
Makes the ball move.
"""
# retrieve the ball.
ball = game.get_window().get_element("ball")
# define a delta time.
delta_time = 0.01
# get the width and the height of the window.
w, h = pygame.display.get_surface().get_size()
# get the with and the height of the ball.
bw, bh = ball.get_surface().get_size()
# define the x and y values.
x, y = ball.get_x(), ball.get_y()
# define gravitation.
g = 9.81
vx, vy = 0.1 * (ball.get_initial_x() - ball.get_x()), 0.1 * (ball.get_initial_y() - ball.get_y())
# define our vector
v = Vector(vx, vy)
# define our reference time.
tr = time()
# update x and y position.
while True:
if self.__t.stopped():
break
ts = time() - tr
if ts >= delta_time:
v.set_y(v.get_y() + g * delta_time)
# update the ball's current coordinates.
x += v.get_x() * delta_time * 60
y += v.get_y() * delta_time * 60
# display the ball.
ball.set_x(x)
ball.set_y(y)
if y + bh >= h:
# calculate alpha.
alpha = atan(v.get_y() / v.get_x())
# re-calculate alpha.
alpha = -alpha
# update vector.
v.set_x(v.normalize() * cos(alpha) * 0.8)
v.set_y(v.normalize() * sin(alpha) * 0.8)
tr += delta_time
from events.event_listener import EventListener
from utils.stoppable_thread import StoppableThread
from utils.vector import Vector
from math import (atan, cos, sin)
from time import time
import pygame


class BallReleaseEventListener(EventListener):
def __init__(self) -> None:
super().__init__()

def run(self, event, game):
# retrieve the ball.
ball = game.get_window().get_element("ball")
# retrieve the placeholder ball.
placeholder_ball = game.get_window().get_element("placeholder_ball")
# check if the ball is at the same position
if abs(ball.get_x() - ball.get_initial_x()) < 15 and abs(ball.get_y() - ball.get_initial_y()) < 15 or ball.is_released():
return
ball.set_released(True)
# disable ball.
placeholder_ball.set_visible(False)
# clear dots.
for i in range(20):
game.get_window().remove_element(("dot_", str(i)))
# launch new thread.
self.__t = StoppableThread(target=self.__move_ball, args=(game,))
# register the thread in order to be able to kill it.
game.register_thread(self.__t)
# start the thread execution.
self.__t.start()

def __move_ball(self, game):
"""
Makes the ball move.
"""
# retrieve the ball.
ball = game.get_window().get_element("ball")
# define a delta time.
delta_time = 0.01
# get the width and the height of the window.
w, h = pygame.display.get_surface().get_size()
# get the with and the height of the ball.
bw, bh = ball.get_surface().get_size()
# define the x and y values.
x, y = ball.get_x(), ball.get_y()
# define gravitation.
g = 9.81
vx, vy = 0.1 * (ball.get_initial_x() - ball.get_x()
), 0.1 * (ball.get_initial_y() - ball.get_y())
# define our vector
v = Vector(vx, vy)
# define our reference time.
tr = time()
# update x and y position.
while True:
if self.__t.stopped():
break
ts = time() - tr
if ts >= delta_time:
v.set_y(v.get_y() + g * delta_time)
# update the ball's current coordinates.
x += v.get_x() * delta_time * 60
y += v.get_y() * delta_time * 60
# display the ball.
ball.set_x(x)
ball.set_y(y)
if y + bh >= h:
# calculate alpha.
alpha = atan(v.get_y() / v.get_x())
# re-calculate alpha.
alpha = -alpha
# update vector.
v.set_x(v.normalize() * cos(alpha) * 0.8)
v.set_y(v.normalize() * sin(alpha) * 0.8)
tr += delta_time
72 changes: 37 additions & 35 deletions drag_event_listener.py → events/drag_event_listener.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
from event_listener import EventListener
from element import Element
import pygame

class DragEventListener(EventListener):
def __init__(self) -> None:
super().__init__()
self.__white_dot = pygame.image.load("img/white_dot.png")
self.__white_dot = pygame.transform.scale(self.__white_dot, (20, 20))

def run(self, event, game):
# pre-register the dots.
for i in range(0, 20):
game.get_window().register_element(("dot_",str(i)), Element(self.__white_dot, 0, 0, False, False))
# retrieve the ball.
ball = game.get_window().get_element("ball")
if pygame.mouse.get_pressed()[0] == True and not ball.is_released():
# update x and y position.
ball.set_x(pygame.mouse.get_pos()[0] - 30)
ball.set_y(pygame.mouse.get_pos()[1] - 30)
# get the initial x and y value.
ix, iy = ball.get_initial_x(), ball.get_initial_y()
vx, vy = 1.1 * (ix - ball.get_x()), 1.1 * (iy - ball.get_y())
for t in range(20):
# get the inital vector values.
x = (ball.get_x() + 30 + vx * t)
y = (9.81 * t**2 + vy * t + ball.get_y() + 30)
if t != 0:
# retrieve the dot.
dot = game.get_window().get_element(("dot_",str(t)))
# update x and y coordinates.
dot.set_x(x)
dot.set_y(y)
# set the dot visible.
dot.set_visible(True)
from events.event_listener import EventListener
from element import Element
import pygame


class DragEventListener(EventListener):
def __init__(self) -> None:
super().__init__()
self.__white_dot = pygame.image.load("img/white_dot.png")
self.__white_dot = pygame.transform.scale(self.__white_dot, (20, 20))

def run(self, event, game):
# pre-register the dots.
for i in range(0, 20):
game.get_window().register_element(("dot_", str(i)),
Element(self.__white_dot, 0, 0, False, False))
# retrieve the ball.
ball = game.get_window().get_element("ball")
if pygame.mouse.get_pressed()[0] == True and not ball.is_released():
# update x and y position.
ball.set_x(pygame.mouse.get_pos()[0] - 30)
ball.set_y(pygame.mouse.get_pos()[1] - 30)
# get the initial x and y value.
ix, iy = ball.get_initial_x(), ball.get_initial_y()
vx, vy = 1.1 * (ix - ball.get_x()), 1.1 * (iy - ball.get_y())
for t in range(20):
# get the inital vector values.
x = (ball.get_x() + 30 + vx * t)
y = (9.81 * t**2 + vy * t + ball.get_y() + 30)
if t != 0:
# retrieve the dot.
dot = game.get_window().get_element(("dot_", str(t)))
# update x and y coordinates.
dot.set_x(x)
dot.set_y(y)
# set the dot visible.
dot.set_visible(True)
18 changes: 9 additions & 9 deletions event_listener.py → events/event_listener.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class EventListener():
def __init__(self) -> None:
pass
def run(self, event, game):
"""
Oriented Object Observer
:param Event event: the triggered event.
:param Game game: the game instance.
"""
class EventListener():
def __init__(self) -> None:
pass
def run(self, event, game):
"""
Oriented Object Observer
:param Event event: the triggered event.
:param Game game: the game instance.
"""
pass
Loading