Skip to content

Commit

Permalink
feat: Adding enum class to make the directions easier to use
Browse files Browse the repository at this point in the history
  • Loading branch information
kerodekroma committed Aug 19, 2024
1 parent 0923f0d commit aeb19d7
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 92 deletions.
39 changes: 21 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,34 @@ git clone https://github.com/kerodekroma/py-thumbpad.git
Here's a basic example of how to use PyThumbPad in your Pygame project:

```py
import pygame
from py_thumbpad import PyThumbPad
from py_thumbpad import PyThumbPad, PY_THUMBPAD_Directions

# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))

# Create a PyThumbPad instance
thumb_pad = PyThumbPad((400, 300), {"quadrants": 8})
# Initialize the thumbpad at position (400, 300)
thumbpad = PyThumbPad((400, 300), {})

# In your game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Listen for events on the thumb pad
thumb_pad.listen_events(event)

# Update thumb pad state
thumb_pad.update()

# Render everything
screen.fill((0, 0, 0))
thumb_pad.render(screen)

thumbpad.listen_events(event)

# Check the direction the thumbpad is pointing to
if PY_THUMBPAD_Directions.TOP in thumbpad.directions:
print("Moving UP!")
elif PY_THUMBPAD_Directions.BOTTOM in thumbpad.directions:
print("Moving DOWN!")
elif PY_THUMBPAD_Directions.LEFT in thumbpad.directions:
print("Moving LEFT!")
elif PY_THUMBPAD_Directions.RIGHT in thumbpad.directions:
print("Moving RIGHT!")

# Update and render the thumbpad
thumbpad.update()
thumbpad.render(screen)

pygame.display.flip()

pygame.quit()
Expand Down
21 changes: 16 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pygame
import sys
from single_square import SingleSquare
from py_thumbpad import PyThumbPad
from py_thumbpad import PyThumbPad, PY_THUMBPAD_Directions

WIDTH, HEIGHT = 800, 600

Expand All @@ -29,17 +29,17 @@ def __init__(self):
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Demo of ThumbPad")
self.thumb_pad = PyThumbPad((150, HEIGHT - 150), {
"quadrants": 8,
"button_color": PALETTE[2],
"donut_color": PALETTE[5],
"donut_bg_color": PALETTE[3]
"donut_bg_color": PALETTE[3],
})
#font
self.font = pygame.font.Font('assets/font/PixelSimpel.otf', 32)

#single square
square_size = 50
self.single_square = SingleSquare(( ( WIDTH - square_size )//2, (HEIGHT - square_size)//2 ), square_size, PALETTE[7])

self.single_square = SingleSquare(( ( WIDTH - square_size )//2, (HEIGHT - square_size)//2 ), square_size, PALETTE[7], (WIDTH, HEIGHT))
async def render(self):
while True:
for event in pygame.event.get():
Expand All @@ -51,6 +51,17 @@ async def render(self):

self.screen.fill(PALETTE[1])


# Check the direction the thumbpad is pointing to
if PY_THUMBPAD_Directions.TOP in self.thumb_pad.directions:
print("Moving UP!")
elif PY_THUMBPAD_Directions.BOTTOM in self.thumb_pad.directions:
print("Moving DOWN!")
elif PY_THUMBPAD_Directions.LEFT in self.thumb_pad.directions:
print("Moving LEFT!")
elif PY_THUMBPAD_Directions.RIGHT in self.thumb_pad.directions:
print("Moving RIGHT!")

self.single_square.move(self.thumb_pad.directions)
# bg to highlight the slider with theme 'one'
pygame.draw.rect(self.screen, PALETTE[0], (0, 0, self.screen.get_width(), 120))
Expand All @@ -60,7 +71,7 @@ async def render(self):
text_rect = text_surface.get_rect(center=(400, 50))
self.screen.blit(text_surface, text_rect)

text_surface = self.font.render(f'Direction: {", ".join( self.thumb_pad.directions )}', True, PALETTE[5])
text_surface = self.font.render(f'Direction: {", ".join([direction.name for direction in self.thumb_pad.directions])}', True, PALETTE[5])
text_rect = text_surface.get_rect(center=(400, 100))
self.screen.blit(text_surface, text_rect)

Expand Down
6 changes: 5 additions & 1 deletion py_thumbpad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
from .donut import Donut
from .button_pad import ButtonPad
from .utils import calculate_angle, get_direction, get_direction_expanded
from .events import PY_THUMBPAD_Directions

# to export the enum of the custom events
__all__ = ['PY_THUMBPAD_Directions']

class PyThumbPad:
def __init__(self, position, options):
Expand Down Expand Up @@ -38,7 +42,7 @@ def listen_events(self, event):
mouse_x, mouse_y = pygame.mouse.get_pos()
self.current_angle = calculate_angle(self.position[0], self.position[1], mouse_x, mouse_y )
self.directions = self.get_directions(self.current_angle)

def get_directions(self, current_angle):
if self.quadrants == 4:
return get_direction(current_angle)
Expand Down
7 changes: 7 additions & 0 deletions py_thumbpad/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from enum import Enum, auto

class PY_THUMBPAD_Directions(Enum):
TOP = auto()
BOTTOM = auto()
LEFT = auto()
RIGHT = auto()
45 changes: 0 additions & 45 deletions py_thumbpad/py_thumbpad.py

This file was deleted.

25 changes: 13 additions & 12 deletions py_thumbpad/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
from .events import PY_THUMBPAD_Directions

# Function to calculate angle in degrees from reference_coord to mouse position
def calculate_angle(x1, y1, x2, y2):
Expand All @@ -13,16 +14,16 @@ def get_direction(angle):
return []

if -135 <= angle < -45:
return [ 'top' ]
return [ PY_THUMBPAD_Directions.TOP ]

if angle >= 135 or angle < -135:
return [ "left" ]
return [ PY_THUMBPAD_Directions.LEFT ]

if -45 <= angle < 45:
return [ "right" ]
return [ PY_THUMBPAD_Directions.RIGHT ]

if 45 <= angle < 135:
return [ "bottom" ]
return [ PY_THUMBPAD_Directions.BOTTOM ]

return []

Expand All @@ -31,27 +32,27 @@ def get_direction_expanded(angle):
return []

if -112.5 <= angle < -67.5:
return [ 'top' ]
return [ PY_THUMBPAD_Directions.TOP ]

if -157.5 <= angle < -112.5:
return [ 'top', 'left', 'top-left' ]
return [ PY_THUMBPAD_Directions.TOP, PY_THUMBPAD_Directions.LEFT ]

if -67.5 <= angle < -22.5:
return [ 'top', 'right', 'top-right' ]
return [ PY_THUMBPAD_Directions.TOP, PY_THUMBPAD_Directions.RIGHT ]

if 67.5 <= angle < 112.5:
return [ 'bottom' ]
return [ PY_THUMBPAD_Directions.BOTTOM ]

if 112.5 <= angle < 157.5:
return [ 'bottom', 'left', 'bottom-left' ]
return [ PY_THUMBPAD_Directions.BOTTOM, PY_THUMBPAD_Directions.LEFT ]

if 22.5 <= angle < 67.5:
return [ 'bottom', 'right', 'bottom-right' ]
return [ PY_THUMBPAD_Directions.BOTTOM, PY_THUMBPAD_Directions.RIGHT ]

if angle >= 157.5 or angle < -157.5:
return [ 'left']
return [ PY_THUMBPAD_Directions.LEFT ]

if -22.5 <= angle < 22.5:
return [ 'right']
return [ PY_THUMBPAD_Directions.RIGHT ]

return []
35 changes: 24 additions & 11 deletions single_square.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import pygame
from py_thumbpad import PY_THUMBPAD_Directions

class SingleSquare:
def __init__(self, position, size, color) -> None:
def __init__(self, position, size, color, bounds) -> None:
self.x = position[0]
self.y = position[1]
self.size = size
self.bounds_x = bounds[0]
self.bounds_y = bounds[1]
self.rect = pygame.Rect(self.x, self.y, size, size )
self.color = color
self.speed = 5
self.size = 50

def move(self, directions):
if "left" in directions:
self.x -= self.speed
if "right" in directions:
self.x += self.speed
if "top" in directions:
self.y -= self.speed
if "bottom" in directions:
self.y += self.speed
if PY_THUMBPAD_Directions.LEFT in directions:
self.rect.x -= self.speed
if PY_THUMBPAD_Directions.RIGHT in directions:
self.rect.x += self.speed
if PY_THUMBPAD_Directions.TOP in directions:
self.rect.y -= self.speed
if PY_THUMBPAD_Directions.BOTTOM in directions:
self.rect.y += self.speed

if self.rect.x < 0:
self.rect.x = 0

if self.rect.y < 0:
self.rect.y = 0

# Apply bounds checks
self.rect.x = max(0, min(self.rect.x, self.bounds_x - self.size))
self.rect.y = max(0, min(self.rect.y, self.bounds_y - self.size))

def render(self, screen):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.size, self.size))
pygame.draw.rect(screen, self.color, self.rect)

0 comments on commit aeb19d7

Please sign in to comment.