This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Basket-GO/feat/refactor
feat: ♻ refactor everything
- Loading branch information
Showing
14 changed files
with
261 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
151 changes: 77 additions & 74 deletions
151
ball_release_event_listener.py → events/ball_release_event_listener.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.