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 #12 from Basket-GO/feat/Interface
Feat/interface
- Loading branch information
Showing
15 changed files
with
265 additions
and
208 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
## Basket Go ! | ||
# BASKET GO! | ||
|
||
Basket Go ! est un jeu tournant sous pygame dont le but est de mettre la balle dans le panier afin de gagner des points. | ||
Basket Go ! est un jeu tournant sous la bibliothèque **Pygame**, comme son nom l'indique l'univers de ce jeu vidéo est tourner vers le basketball, comportant **trois catégories :** | ||
(Solo / 1vs1 'tour par tour' / Niveaux) Dont le but est de s'amusez en se challengeant à **marquez le plus de panier possible** dans un **temps imparti (45sec)**, le jeu étant composée d'une **boutique** et de sa propre **monnaie (Go ticket)** vous la gagnerait à chaque fin de partie selon le score obtenue. | ||
A vous de Jouez ! | ||
## Aspect Graphique | ||
[Charte Graphique](https://user-images.githubusercontent.com/97946104/206729155-c77702b8-d29a-4dad-ace3-678474b561ec.png) | ||
... | ||
... | ||
|
||
## Hierarchie code | ||
```mermaid | ||
graph LR | ||
A[main.py] --> B((button)) | ||
A --> C((game)) | ||
``` |
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,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 |
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,77 +1,78 @@ | ||
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 | ||
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,37 +1,37 @@ | ||
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) | ||
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) | ||
# get the initial x and y value. |
Oops, something went wrong.