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

feat: 🌟initial commit #3

Merged
merged 1 commit into from
Dec 2, 2022
Merged
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
6 changes: 3 additions & 3 deletions ball_release_event_listener.py
Original file line number Diff line number Diff line change
@@ -11,9 +11,9 @@ def __init__(self) -> None:

def run(self, event, game:Game):
# retrieve the ball.
ball = game.get_element("ball")
ball = game.get_window().get_element("ball")
# retrieve the placeholder ball.
placeholder_ball = game.get_element("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:
return
@@ -30,7 +30,7 @@ def __move_ball(self, game:Game):
Makes the ball move.
"""
# retrieve the ball.
ball = game.get_element("ball")
ball = game.get_window().get_element("ball")
# define a delta time.
delta_time = 0.1
# get the width and the height of the window.
15 changes: 13 additions & 2 deletions drag_event_listener.py
Original file line number Diff line number Diff line change
@@ -5,11 +5,22 @@
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:Game):
if pygame.mouse.get_pressed()[0] == True:
# retrieve the ball.
ball = game.get_element("ball")
ball = game.get_window().get_element("ball")
# update x and y position.
ball.set_x(pygame.mouse.get_pos()[0] - 30)
ball.set_y(pygame.mouse.get_pos()[1] - 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()
for t in range(0, 150):
t = t
# get the inital vector values.
vx, vy = ix - ball.get_x(), iy - ball.get_y()
x = ball.get_x() + 30 + vx * t
y = -(1/2) * -9.81 * t**2 + vy * t + ball.get_y() + 30
game.get_screen().blit(self.__white_dot, (x, y))
40 changes: 13 additions & 27 deletions game.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
from event_listener import EventListener
from player import Player
from element import Element
from window import Window

class Game():
def __init__(self, screen:pygame.Surface, img_location:str, sound_location:str) -> None:
@@ -13,8 +14,8 @@ def __init__(self, screen:pygame.Surface, img_location:str, sound_location:str)
self.__players = []
# set up the events pair.
self.__events = []
# game elements (objects, background etc.)
self.__elements = []
# set up our window.
self.__window = Window()
# game threads.
self.__threads = []
# get the actual basket ball field.
@@ -30,13 +31,18 @@ def __init__(self, screen:pygame.Surface, img_location:str, sound_location:str)
placeholder_ball = pygame.image.load(img_location + "basket-ball-placeholder.png")
placeholder_ball = pygame.transform.scale(placeholder_ball, (70, 70))
# register the field without public.
self.register_element("field", Element(field, 0, 250))
self.__window.register_element("field", Element(field, 0, 250))
# register the crow with arms down.
self.register_element("crow_arms_down", Element(crow_arms_down, 0, 0))
self.__window.register_element("crow_arms_down", Element(crow_arms_down, 0, 0))
# register the ball.
self.register_element("ball", Element(ball, 200, 400))
self.__window.register_element("ball", Element(ball, 200, 400))
# register the placeholder ball.
self.register_element("placeholder_ball", Element(placeholder_ball, 200, 400))
self.__window.register_element("placeholder_ball", Element(placeholder_ball, 200, 400))
def get_window(self) -> Window:
"""
:return: the game's window.
"""
return self.__window
def register_player(self, player_name:str) -> None:
"""
Register a player by its name.
@@ -53,34 +59,14 @@ def register_thread(self, thread) -> None:
Register the given thread into a list.
"""
self.__threads.append(thread)
def register_element(self, key:str, element):
"""
Register a game element into the cache.
:param str key: the key to pair to the given element.
:param object element: the element to store.
"""
self.__elements.append([key, element])

def get_element(self, key:str):
"""
If present returns the element paired with the given key
else it raises RuntimeError.
:param str key: the key paired to the game element.
:return: the game element.
"""
for element in self.__elements:
if element[0] == key:
return element[1]
raise RuntimeError("element not found for key: ", key)

def setup(self):
"""
Setup the ressources (background image, audio, etc.)
and listen to all the events built in the game.
"""
while True:
# loop through each elements.
for element in self.__elements:
for element in self.__window.get_elements():
# retrive the object.
obj = element[1]
if(obj.is_visible()):
2 changes: 1 addition & 1 deletion player.py
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ def setup(self) -> None:
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render(str(self.__score), True, (255, 255, 255), None)
# register the game element.
self.__game.register_element("player_" + self.__name + "_score", Element(text, 512, 0))
self.__game.get_window().register_element("player_" + self.__name + "_score", Element(text, 512, 10))
def get_name(self) -> str:
"""
:return: str: player's name.
27 changes: 27 additions & 0 deletions window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Window():
def __init__(self) -> None:
# game elements (objects, background etc.)
self.__elements = []
def register_element(self, key:str, element):
"""
Register a game element into the cache.
:param str key: the key to pair to the given element.
:param object element: the element to store.
"""
self.__elements.append([key, element])
def get_element(self, key:str):
"""
If present returns the element paired with the given key
else it raises RuntimeError.
:param str key: the key paired to the game element.
:return: the game element.
"""
for element in self.__elements:
if element[0] == key:
return element[1]
raise RuntimeError("element not found for key: ", key)
def get_elements(self):
"""
:return: the game elements.
"""
return self.__elements