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

Commit

Permalink
Merge pull request #14 from Basket-GO/docs/revole-conflict-readme
Browse files Browse the repository at this point in the history
docs: create readme
  • Loading branch information
l0u1sg authored Dec 19, 2022
2 parents dad8a6b + c3e29cf commit 3f6936b
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 120 deletions.
48 changes: 0 additions & 48 deletions GUI.py

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ python main.py

## How to play

To play, you have take the ball and score a basket. An Angry Birds-like indication allows you to preview the ball's trajectory.
To play, you have take the ball and score a basket. An Angry Birds-like indication allows you to preview the ball's trajectory.
10 changes: 9 additions & 1 deletion components/ball.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ def set_released(self, released:bool) -> None:
Set the ball as released or not.
:param bool released: the release state of the ball.
"""
self.__is_released = released
self.__is_released = released
def respawn(self) -> None:
"""
Reset the ball's position to it's initial location
and set the ball as not released.
"""
self.set_x(self.get_initial_x())
self.set_y(self.get_initial_y())
self.set_released(False)
64 changes: 34 additions & 30 deletions components/button.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import pygame

#button class
class Button():
def __init__(self, x, y, image, scale):
width = image.get_width()
height = image.get_height()
self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.clicked = False

def draw(self, surface):
action = False
#get mouse position
pos = pygame.mouse.get_pos()

#check mouseover and clicked conditions
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
self.clicked = True
action = True

if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False

#draw button on screen
surface.blit(self.image, (self.rect.x, self.rect.y))

return action
import pygame

#button class
class Button():
def __init__(self, x, y, image, image2, scale):
width = image.get_width()
height = image.get_height()
self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
self.image2 = pygame.transform.scale(image2, (int(width * scale), int(height * scale)))
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.clicked = False

def draw_and_clicked(self, surface):
""" write button and return action"""
press_up_or_down = True
action = False
#get mouse position
pos = pygame.mouse.get_pos()
image = self.image
#check mouseover and clicked conditions
if self.rect.collidepoint(pos) :
image = self.image2
if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
self.clicked = True
action = True

if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False

#draw button on screen
surface.blit(image, (self.rect.x, self.rect.y))

return action
34 changes: 27 additions & 7 deletions events/ball_release_event_listener.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from math import (atan, cos, sin, pi, copysign, tan)
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

Expand All @@ -13,17 +13,26 @@ def __init__(self) -> None:
def run(self, event, game):
# retrieve the ball.
ball = game.get_window().get_element("ball")
# check if the ball is already released.
if ball.is_released():
return
# retrieve the placeholder ball.
placeholder_ball = game.get_window().get_element("placeholder_ball")
# retrive game's window.
window = game.get_window()
# 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():
if abs(ball.get_x() - ball.get_initial_x()) < 60 and abs(ball.get_y() - ball.get_initial_y()) < 60 or ball.is_released():
ball.respawn()
# clear dots.
for i in range(20):
window.get_element(("dot_",str(i))).set_visible(False)
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)))
window.get_element(("dot_",str(i))).set_visible(False)
# launch new thread.
self.__t = StoppableThread(target=self.__move_ball, args=(game,))
# register the thread in order to be able to kill it.
Expand Down Expand Up @@ -55,23 +64,34 @@ def __move_ball(self, game):
tr = time()
# update x and y position.
while True:
if self.__t.paused():
pass
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
y = v.get_y() * delta_time * 60 + y if (v.get_y() * delta_time * 60) + y < h - bh else h - bh
# 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())
if v.get_x() == 0:
alpha = copysign(pi / 2, v.get_y())
elif v.get_x() > 0:
alpha = atan(v.get_y() / v.get_x())
elif v.get_x() < 0:
alpha = pi + 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
# check if the ball is out of the screen.
if x - bw >= w or x + bw <= 0 or (y >= 570 and v.get_y() > -0.05):
# reset ball's coordinates.
ball.respawn()
break
tr += delta_time
31 changes: 22 additions & 9 deletions events/drag_event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,35 @@


class DragEventListener(EventListener):
def __init__(self) -> None:
def __init__(self, game) -> 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))
for i in range(20):
game.get_window().register_element(("dot_",str(i)), Element(self.__white_dot, 0, 0, False, False))

def run(self, event, game):
# retrieve the ball.
ball = game.get_window().get_element("ball")
# get the with and the height of the ball.
bw, bh = ball.get_surface().get_size()
# get the width and the height of the window.
h = pygame.display.get_surface().get_size()[1]
# retrieve mouse position.
mouse_x, mouse_y = pygame.mouse.get_pos()
# check if the ball is out of limits on the x axis.
if pygame.mouse.get_pos()[0] - (bw / 2) < 0:
# still update the y axis.
mouse_x = bw / 2
# check if the ball is out of limits on the y axis.
if pygame.mouse.get_pos()[1] + (bh / 2) > h:
# still update the x axis.
mouse_y = h - bh / 2
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)
ball.set_x(mouse_x - 30)
ball.set_y(mouse_y - 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())
Expand All @@ -34,4 +47,4 @@ def run(self, event, game):
dot.set_x(x)
dot.set_y(y)
# set the dot visible.
dot.set_visible(True)
dot.set_visible(True)
34 changes: 25 additions & 9 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,33 @@ def __init__(self, screen: pygame.Surface, img_location: str, img_name: str, sou
self.__window = Window()
# game threads.
self.__threads = []
self.clock = pygame.time.Clock()
self.fps = 150
# get the actual basket ball field.
field = pygame.image.load(
img_location + "terrain_basket_sans_public.png")
field = pygame.image.load(img_location + "terrain_basket_public.png")
field = pygame.transform.scale(field, (1024, 640))
field2 = pygame.image.load("img/terrain_basket_public_2.png")
field2 = pygame.transform.scale(field2, (1024, 640))
self.field = field
self.field2 = field2
#get the cursor
cursor_init = pygame.image.load("img/cursor.png").convert_alpha()
self.cursor = cursor_init
# get the actual ball.
ball = pygame.image.load(img_location + img_name+".png")
# get the placeholder ball.
placeholder_ball = pygame.image.load(
img_location + img_name+"-placeholder.png")
# register the field without public.
self.get_window().register_element("field", Element(field, 0, 0))
self.get_window().register_element("field", Element(field2, 0, 0))
# register the ball.
self.get_window().register_element("ball", Ball(ball, 170, 450))
# register the placeholder ball.
self.get_window().register_element("placeholder_ball",
Element(placeholder_ball, 170, 450))
# listen to events.
self.listen(pygame.MOUSEMOTION,
DragEventListener())
self.listen(pygame.MOUSEBUTTONUP,
BallReleaseEventListener())

self.listen(pygame.MOUSEMOTION, DragEventListener(self))
self.listen(pygame.MOUSEBUTTONUP, BallReleaseEventListener())
def get_window(self) -> Window:
"""
:return: the game's window.
Expand All @@ -68,13 +73,21 @@ def register_thread(self, thread) -> None:
Register the given thread into a list.
"""
self.__threads.append(thread)

def display_fps(self):
"""Show the program's FPS in the window handle."""
caption = "{} - FPS: {:.2f}".format("ʙᴀsᴋᴇᴛ ɢᴏ !", self.clock.get_fps())
pygame.display.set_caption(caption)
def setup(self):
"""
Setup the ressources (background image, audio, etc.)
and listen to all the events built in the game.
"""
while True:
x,y = pygame.mouse.get_pos()
x -= self.cursor.get_width()/2
y -= self.cursor.get_height()/2
self.clock.tick(self.fps)
self.display_fps()
# loop through each elements.
for element in self.__window.get_elements():
# retrive the object.
Expand All @@ -94,6 +107,8 @@ def setup(self):
if event_pair[0] == event.type:
# run the event.
event_pair[1].run(event, self)
#cursor init
self.__screen.blit(self.cursor,(x,y))
pygame.display.update()

def listen(self, event_type: int, event_listener: EventListener) -> None:
Expand All @@ -113,3 +128,4 @@ def get_screen(self):
Return game's screen.
"""
return self.__screen

Binary file added img/cursor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/exit_btn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/exit_btn_hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/start_btn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/start_btn_hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/terrain_basket_public.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/terrain_basket_public_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed img/terrain_basket_sans_public.png
Binary file not shown.
Loading

0 comments on commit 3f6936b

Please sign in to comment.