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

Fix button #21

Merged
merged 9 commits into from
Mar 31, 2023
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
32 changes: 23 additions & 9 deletions components/ball.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
from element import Element
from pygame import Surface


class Ball(Element):
def __init__(self, surface: Surface, x: int, y: int, visible: bool = True) -> None:
"""Class for the ball.

Args:
surface (Surface): The window where the ball will be drawn.
x (int): the x coordinate of the ball.
y (int): the y coordinate of the ball.
visible (bool, optional): If the ball is visible . Defaults to True.
"""
super().__init__(surface, x, y, visible, False)
# this states whether the ball is released or not.
self.__is_released = False

def is_released(self) -> bool:
"""
:return: whether the ball is released or not.
"""Check if the ball is released or not.

Returns:
bool: whether the ball is released or not.
"""
return self.__is_released
def set_released(self, released:bool) -> None:
"""
Set the ball as released or not.
:param bool released: the release state of the ball.

def set_released(self, released: bool) -> None:
"""Set the ball as released or not.

Args:
released (bool): the release state of the ball.
"""
self.__is_released = released

def respawn(self) -> None:
"""
Reset the ball's position to it's initial location
"""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)
self.set_released(False)
121 changes: 102 additions & 19 deletions components/button.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,120 @@
import pygame
import time
# button class


#button class
class Button():
def __init__(self, x, y, image, image2, scale):
def __init__(self, x: int, y: int, image: str, image2: str, scale: int) -> None:
"""Class for the button.

Args:
x (int): The x coordinate of the button.
y (int): The y coordinate of the button.
image (str): The image of the button.
image2 (str): The image of the button when hovered.
scale (int): The scale of the button.
"""
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.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.action_music = False
self.hover_sound = pygame.mixer.Sound("./sound/blipshort1.mp3")
pygame.mixer.Sound.set_volume(self.hover_sound, 0.08)
self.enter_sound = pygame.mixer.Sound("./sound/blip2.mp3")
pygame.mixer.Sound.set_volume(self.enter_sound, 0.30)
def draw_and_clicked(self, surface: pygame.surface.Surface, event: pygame.event.Event) -> bool:
"""Draw the button on the screen and check if it is clicked.

self.hovered = False
self.hover_sound = pygame.mixer.Sound("./sound/blipshort1.wav")
pygame.mixer.Sound.set_volume(self.hover_sound, 0.008)
def draw_and_clicked(self, surface):
""" write button and return action"""
Args:
surface (pygame.surface.Surface): The window where the button will be drawn.
event (pygame.event.Event): The event that will be checked.

Returns:
bool: Whether the button is clicked or not.
"""
action = False
#get mouse position
# get mouse position
image = self.image
pos = pygame.mouse.get_pos()
#check mouseover and clicked conditions
# check mouseover and clicked conditions
if self.rect.collidepoint(pos):
if not self.hovered:
self.hovered = True
if self.hovered:
self.hover_sound.play()
if self.action_music == True:
self.hover_sound.play()
self.action_music = False
image = self.image2
if pygame.mouse.get_pressed()[0] == 1 :
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
action = True
self.enter_sound.play()
time.sleep(0.1)
elif pygame.mouse.get_pressed()[0] == 1:
action = True
self.enter_sound.play()
time.sleep(0.1)
else:
self.hovered = False
#draw button on screen
self.action_music = True

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

return action

def parcourir(dict_pos: dict, location: str, MOUSE_POS: tuple, event: pygame.event.Event) -> tuple:
"""Function that takes as parameter a dictionary and a location (where it is in the interface),
returns the new position of the mouse.

Args:
dict_pos (dict): the dictionary of the positions of the mouse.
location (str): the location of the button.
MOUSE_POS (tuple): the position of the mouse.
event (pygame.event.Event): the event that will be checked.

Returns:
tuple: the new position of the mouse.
"""
if location == "main1":
# button path
if event.scancode == 82:
if MOUSE_POS != dict_pos["main1"][0]:
pygame.mouse.set_pos(dict_pos["main1"][0])
else:
pygame.mouse.set_pos(dict_pos["main1"][1])
if event.scancode == 81:
if MOUSE_POS != dict_pos["main1"][1]:
pygame.mouse.set_pos(dict_pos["main1"][1])
else:
pygame.mouse.set_pos(dict_pos["main1"][0])
if event.scancode == 80:
if MOUSE_POS != dict_pos["main2"][0]:
pygame.mouse.set_pos(dict_pos["main2"][0])
if MOUSE_POS == dict_pos["main2"][0]:
pygame.mouse.set_pos(dict_pos["main2"][2])
if MOUSE_POS == dict_pos["main2"][2]:
pygame.mouse.set_pos(dict_pos["main2"][1])
if event.scancode == 79:
if MOUSE_POS != dict_pos["main2"][2]:
pygame.mouse.set_pos(dict_pos["main2"][2])
if MOUSE_POS == dict_pos["main2"][2]:
pygame.mouse.set_pos(dict_pos["main2"][0])
if MOUSE_POS == dict_pos["main2"][0]:
pygame.mouse.set_pos(dict_pos["main2"][1])
if location == "option":
if event.scancode == 80:
if MOUSE_POS != dict_pos["option"][0]:
pygame.mouse.set_pos(dict_pos["option"][0])
else:
pygame.mouse.set_pos(dict_pos["option"][1])
if event.scancode == 79:
if MOUSE_POS != dict_pos["option"][1]:
pygame.mouse.set_pos(dict_pos["option"][1])
else:
pygame.mouse.set_pos(dict_pos["option"][0])
if location == "leaderboard":
if event.scancode == 80 or event.scancode == 79:
if MOUSE_POS != dict_pos["leaderboard"][0]:
pygame.mouse.set_pos(dict_pos["leaderboard"][0])
43 changes: 30 additions & 13 deletions components/player.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
import pygame
from element import Element


class Player():
def __init__(self, game, name:str) -> None:
def __init__(self, game: str, name: str) -> None:
"""Player class.

Args:
game (str): The game where the player is playing.
name (str): The name of the player.
"""

# 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.
"""Setup the player's score.
"""
# 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))
self.__game.get_window().register_element(
"player_" + self.__name + "_score", Element(text, 512, 10))

def get_name(self) -> str:
"""
:return: str: player's name.
"""Get the player's name.

Returns:
str: the player's name.
"""
return self.__name

def get_score(self) -> int:
"""Get the player's score.

Returns:
int: the player's score.
"""
:return: int: player's score.
"""
def set_score(self, score:int) -> None:
"""
Define player's score.
:param: int score: the score to set.

def set_score(self, score: int) -> None:
"""Set the player's score.

Args:
score (int): the player's score.
"""
self.__score = score
self.__score = score
32 changes: 27 additions & 5 deletions element.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
from pygame import Surface


class Element():
def __init__(self, surface:Surface, x:int, y:int, visible:bool=True, allow_override:bool=False) -> None:
def __init__(self, surface: Surface, x: int, y: int, visible: bool = True, allow_override: bool = False) -> None:
"""The base class for all the elements in the game.

Args:
surface (Surface): The surface where the element will be drawn.
x (int): The x coordinate of the element.
y (int): The y coordinate of the element.
visible (bool, optional): If the element is visble or not . Defaults to True.
allow_override (bool, optional): If the element can be override. Defaults to False.
"""
self.__surface = surface
# those are the inital x and y coordinates. (those coordinates are immutable)
self.__ix, self.__iy = x, y
Expand All @@ -10,67 +20,79 @@ def __init__(self, surface:Surface, x:int, y:int, visible:bool=True, allow_overr
self.__visible = visible
self.__allow_override = allow_override
pass

def get(self) -> tuple:
"""
:return: the whole element.
"""
return (self.__surface, (self.__x, self.__y))

def get_surface(self) -> Surface:
"""
:return: the surface.
"""
return self.__surface

def get_initial_x(self):
"""
:return: the initial immutable x coordinate of the element.
"""
return self.__ix

def get_initial_y(self):
"""
:return: the initial immutable x coordinate of the element.
"""
return self.__iy

def get_x(self):
"""
:return: the live x position of the element.
"""
return self.__x

def get_y(self):
"""
:return: the live y position of the element.
"""
return self.__y

def get_surface(self):
"""
:return: the element surface.
"""
return self.__surface

def allow_override(self):
"""
:return: whether the element can be overrident or not.
(another element can erase this one.)
"""
return self.__allow_override

def is_visible(self):
"""
:return: whether the element is visible or not. (Note it can still interact.)
"""
return self.__visible
def set_visible(self, visible:bool):

def set_visible(self, visible: bool):
"""
Set the visibility of the element.
:param: bool visible: whether the element is visible or not.
"""
self.__visible = visible
def set_x(self, x:int):

def set_x(self, x: int):
"""
Set the x position of the element.
:param int x: the x position of the element.
"""
self.__x = x
def set_y(self, y:int):

def set_y(self, y: int):
"""
Set the y position of the element.
:param int y: the y position of the element.
"""
self.__y = y
self.__y = y
Loading