-
-
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
71bea8f
commit c8139f9
Showing
23 changed files
with
1,357 additions
and
31 deletions.
There are no files selected for viewing
Binary file not shown.
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,37 @@ | ||
|
||
import pygame | ||
from PIL import Image, ImageDraw, ImageOps | ||
|
||
|
||
class ImageElement: | ||
def __init__(self, x, y, image, crop_mode="None"): | ||
self.x = x | ||
self.y = y | ||
self.image = image | ||
self.crop_mode = crop_mode # "None" or "Circle" | ||
self.active = False | ||
self.width, self.height = self.image.size | ||
self.widtho, self.heighto = self.image.size | ||
self.text = "" | ||
self.bold = False | ||
self.italics = False | ||
self.underlined = False | ||
self.font_name = "Recources\Fonts\PixelifySans-Regular.ttf" | ||
self.color = (0, 0, 0) | ||
self.size = self.width * self.height | ||
|
||
def draw(self, screen): | ||
# Optionally apply cropping based on the selected cropping mode | ||
if self.crop_mode == "Circle": | ||
# Crop the image to a circle using PIL | ||
mask = Image.new("L", (self.widtho, self.heighto), 0) | ||
draw = ImageDraw.Draw(mask) | ||
draw.ellipse((0, 0, self.widtho, self.heighto), fill=255) | ||
self.image.putalpha(mask) | ||
|
||
# Draw the image on the screen | ||
pygame_image = pygame.image.fromstring( | ||
self.image.tobytes(), self.image.size, self.image.mode | ||
) | ||
pygame_image = pygame.transform.scale(pygame_image, (self.width, self.height)) | ||
screen.blit(pygame_image, (self.x, self.y)) |
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,77 @@ | ||
import pygame | ||
|
||
|
||
class TextElement: | ||
def __init__(self, x, y, text, font_size): | ||
""" | ||
Initialize a button object | ||
Args: | ||
x: x-coordinate of the button | ||
y: y-coordinate of the button | ||
text: Text to display on the button | ||
font_size: Font size of the text | ||
Returns: | ||
self: Button object | ||
- Sets the x and y coordinates | ||
- Sets the text and font size | ||
- Sets default color, size and other attributes | ||
- Initializes other properties like hover, width, height etc. | ||
""" | ||
self.x = x | ||
self.y = y | ||
self.text = text | ||
self.font_size = font_size | ||
self.color = (255, 255, 255) # Default text color | ||
self.size = font_size | ||
self.hovered = False | ||
self.width = 50 | ||
self.height = 25 | ||
self.bold = False | ||
self.italics = False | ||
self.underlined = False | ||
self.font_name = None | ||
|
||
def draw(self, screen): | ||
""" | ||
Renders text on a screen surface. | ||
Args: | ||
screen: The screen surface to render text on | ||
Returns: | ||
None: Does not return anything | ||
- Loads the font based on properties of the Text object | ||
- Renders the text surface using the font and text properties | ||
- Blits/draws the text surface onto the screen surface at the x,y position""" | ||
font = pygame.font.Font(self.font_name, self.size) | ||
font.set_bold(self.bold) | ||
font.set_italic(self.italics) | ||
font.set_underline(self.underlined) | ||
text_surface = font.render(self.text, True, self.color) | ||
screen.blit(text_surface, (self.x, self.y)) | ||
|
||
def change_text(self, event): | ||
""" | ||
Change text on mouse events | ||
Args: | ||
event: pygame event object | ||
Returns: | ||
None | ||
Processing Logic: | ||
- Check if mouse is hovering over button on MOUSEMOTION | ||
- Set button to active if mouse is pressed on button on MOUSEBUTTONDOWN | ||
- Change text if a key is pressed while button is active on KEYDOWN | ||
""" | ||
if event.type == pygame.MOUSEMOTION: | ||
self.hovered = ( | ||
self.x < event.pos[0] < self.x + self.width | ||
and self.y < event.pos[1] < self.y + self.height | ||
) | ||
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: | ||
if self.hovered: | ||
self.active = True | ||
else: | ||
self.active = False | ||
if event.type == pygame.KEYDOWN and self.active: | ||
if event.key == pygame.K_BACKSPACE: | ||
self.text = self.text[:-1] | ||
else: | ||
self.text += event.unicode |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import pygame | ||
|
||
|
||
if __name__ == "__main__": | ||
# Constants | ||
infoObject: object = pygame.display.Info() | ||
WIDTH, HEIGHT = infoObject.current_w, infoObject.current_h | ||
BACKGROUND_COLOR = (0, 0, 0) | ||
font = pygame.font.Font("Recources\Fonts\PixelifySans-Regular.ttf", 36) | ||
# Define colors | ||
WHITE = (255, 255, 255) | ||
BUTTON_COLOR = (50, 50, 50) | ||
BUTTON_HOVER_COLOR = (100, 100, 100) | ||
white = (255, 255, 255) | ||
|
||
|
||
class Button: | ||
"""Button class for creating interactive buttons in the game.""" | ||
|
||
def __init__( | ||
self, | ||
text, | ||
x, | ||
y, | ||
width, | ||
height, | ||
command, | ||
additional_data: list = None, | ||
color=(255, 255, 255), | ||
): | ||
""" | ||
Initialize a button. | ||
Args: | ||
text (str): The text displayed on the button. | ||
x (int): The x-coordinate of the button's top-left corner. | ||
y (int): The y-coordinate of the button's top-left corner. | ||
width (int): The width of the button. | ||
height (int): The height of the button. | ||
command (function): The function to be executed when the button is clicked. | ||
aditional data (list): arguments the buttons command needs to run | ||
""" | ||
self.text = text | ||
self.x = x | ||
self.y = y | ||
self.width = width | ||
self.height = height | ||
self.command = command | ||
self.additional_data = additional_data | ||
self.hovered = False | ||
self.size = 36 | ||
self.active = False | ||
self.bold = False | ||
self.italics = False | ||
self.underlined = False | ||
self.font_name = "Recources\Fonts\PixelifySans-Regular.ttf" | ||
self.color = color | ||
|
||
def draw(self, screen): | ||
"""Draw the button on the screen.""" | ||
font = pygame.font.Font(self.font_name, self.size) | ||
font.set_bold(self.bold) | ||
font.set_italic(self.italics) | ||
font.set_underline(self.underlined) | ||
color = BUTTON_HOVER_COLOR if self.hovered else BUTTON_COLOR | ||
pygame.draw.rect(screen, color, (self.x, self.y, self.width, self.height)) | ||
text = font.render(self.text, True, self.color) | ||
screen.blit( | ||
text, | ||
( | ||
self.x + self.width // 2 - text.get_width() // 2, | ||
self.y + self.height // 2 - text.get_height() // 2, | ||
), | ||
) | ||
|
||
def handle_event(self, event): | ||
""" | ||
Handle events related to the button. | ||
Args: | ||
event: The Pygame event to be processed. | ||
""" | ||
if event.type == pygame.MOUSEMOTION: | ||
self.hovered = ( | ||
self.x < event.pos[0] < self.x + self.width | ||
and self.y < event.pos[1] < self.y + self.height | ||
) | ||
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: | ||
if self.hovered: | ||
self.active = True | ||
if self.additional_data != None: | ||
a = self.command(*self.additional_data) | ||
else: | ||
a = self.command() | ||
if a: | ||
return a | ||
else: | ||
self.active = False | ||
|
||
def selected(self): | ||
self.hovered = True | ||
|
||
def change_text(self, event): | ||
if event.type == pygame.MOUSEMOTION: | ||
self.hovered = ( | ||
self.x < event.pos[0] < self.x + self.width | ||
and self.y < event.pos[1] < self.y + self.height | ||
) | ||
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: | ||
if self.hovered: | ||
self.active = True | ||
else: | ||
self.active = False | ||
if event.type == pygame.KEYDOWN and self.active: | ||
if event.key == pygame.K_BACKSPACE: | ||
self.text = self.text[:-1] | ||
else: | ||
self.text += event.unicode |
Oops, something went wrong.