-
-
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.
- Loading branch information
1 parent
a89ec1f
commit b680d26
Showing
3 changed files
with
190 additions
and
11 deletions.
There are no files selected for viewing
Binary file not shown.
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
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 |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import pygame | ||
|
||
|
||
class Slider: | ||
"""Slider class for creating interactive sliders in the game.""" | ||
|
||
def __init__( | ||
self, | ||
x, | ||
y, | ||
width, | ||
height, | ||
min_value, | ||
max_value, | ||
default_value, | ||
command=None, | ||
additional_data: list = None, | ||
color=(255, 255, 255), | ||
colortwo=(200, 200, 200), | ||
text="", | ||
text_position_below=True, | ||
): | ||
""" | ||
Initialize a slider. | ||
Args: | ||
x (int): The x-coordinate of the slider's top-left corner. | ||
y (int): The y-coordinate of the slider's top-left corner. | ||
width (int): The width of the slider. | ||
height (int): The height of the slider. | ||
min_value (float): The minimum value of the slider. | ||
max_value (float): The maximum value of the slider. | ||
default_value (float): The default value of the slider. | ||
command (function): The function to be executed when the slider is changed. | ||
additional_data (list): Arguments the slider's command needs to run. | ||
color (tuple): The color of the slider. | ||
text (str): The text to be displayed above or below the slider bar. | ||
text_position_below (bool): True if the text should be below the slider bar, False if above. | ||
""" | ||
self.x = x | ||
self.y = y | ||
self.width = width | ||
self.height = height | ||
self.min_value = min_value | ||
self.max_value = max_value | ||
self.value = default_value | ||
self.command = command | ||
self.additional_data = additional_data | ||
self.active = False | ||
self.color = color | ||
self.colortwo = colortwo | ||
self.text = text | ||
self.text_position_below = text_position_below | ||
|
||
def draw(self, screen): | ||
"""Draw the slider on the screen.""" | ||
pygame.draw.rect( | ||
screen, self.colortwo, (self.x, self.y, self.width, self.height) | ||
) | ||
slider_width = int( | ||
(self.value - self.min_value) | ||
/ (self.max_value - self.min_value) | ||
* self.width | ||
) | ||
pygame.draw.rect( | ||
screen, self.color, (self.x, self.y, slider_width, self.height) | ||
) | ||
|
||
if self.text is not None: | ||
font = pygame.font.Font(None, 36) | ||
text_surface = font.render(self.text, True, (255, 255, 255)) | ||
text_rect = text_surface.get_rect() | ||
|
||
if self.text_position_below: | ||
text_rect.midtop = (self.x + self.width / 2, self.y + self.height) | ||
else: | ||
text_rect.midbottom = (self.x + self.width / 2, self.y) | ||
|
||
screen.blit(text_surface, text_rect) | ||
|
||
def handle_event(self, event): | ||
""" | ||
Handle events related to the slider. | ||
Args: | ||
event: The Pygame event to be processed. | ||
""" | ||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: | ||
if ( | ||
self.x < event.pos[0] < self.x + self.width | ||
and self.y < event.pos[1] < self.y + self.height | ||
): | ||
self.active = True | ||
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1: | ||
self.active = False | ||
elif event.type == pygame.MOUSEMOTION and self.active: | ||
mouse_x = max(self.x, min(event.pos[0], self.x + self.width)) | ||
normalized_value = (mouse_x - self.x) / self.width | ||
self.value = self.min_value + normalized_value * ( | ||
self.max_value - self.min_value | ||
) | ||
self.value = round(self.value, 10) | ||
if self.command is not None: | ||
if self.additional_data is not None: | ||
self.command(self.value, *self.additional_data) | ||
else: | ||
self.command(self.value) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Initialize Pygame | ||
pygame.init() | ||
import pygame | ||
import sys | ||
from button import Button | ||
|
||
# Constants | ||
infoObject = pygame.display.Info() | ||
WIDTH, HEIGHT = infoObject.current_w, infoObject.current_h | ||
BACKGROUND_COLOR = (0, 0, 0) | ||
|
||
# Initialize the screen | ||
screen = pygame.display.set_mode((WIDTH, HEIGHT)) | ||
pygame.display.set_caption("Pygame Slider Example") | ||
|
||
# Define colors | ||
WHITE = (255, 255, 255) | ||
BUTTON_COLOR = (50, 50, 50) | ||
BUTTON_HOVER_COLOR = (100, 100, 100) | ||
|
||
# Create a button | ||
button = Button("Click me", 50, 50, 150, 50, lambda: print("Button Clicked")) | ||
|
||
# Create a slider | ||
def slider_callback(value): | ||
print("Slider Value:", value) | ||
|
||
slider = Slider(50, 150, 200, 20, 0, 100, 50, slider_callback) | ||
|
||
# Main game loop | ||
while True: | ||
screen.fill(BACKGROUND_COLOR) | ||
|
||
# Event handling | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
pygame.quit() | ||
sys.exit() | ||
|
||
# Handle events for the button and slider | ||
button.handle_event(event) | ||
slider.handle_event(event) | ||
|
||
# Draw the button and slider | ||
button.draw(screen) | ||
slider.draw(screen) | ||
|
||
# Update the display | ||
pygame.display.flip() |