Skip to content

Commit

Permalink
[ADD] assets/fonts
Browse files Browse the repository at this point in the history
[ADD] assets/fonts/pixel.ttf
[FEATURE] Implemented custom font selection
  • Loading branch information
nickname2002 committed Jul 3, 2023
1 parent 2d203f8 commit c439495
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .idea/jorcademy_python_template.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified __pycache__/game.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/jorcademy.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/primitives.cpython-311.pyc
Binary file not shown.
Binary file added assets/fonts/pixel.ttf
Binary file not shown.
2 changes: 1 addition & 1 deletion game.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ def setup() -> None:


def update() -> None:
pass
pass
12 changes: 9 additions & 3 deletions jorcademy.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,16 @@ def rect(c: color, x: float, y: float, w: float, h: float, rotation=0) -> None:


# Draw a string of text
def text(content: str, c: color, x: float, y: float, rotation=0) -> None:
font = pygame.font.Font(None, 48)
def text(content: str, size: int, c: color, x: float, y: float, font="Nunito") -> None:
# Fetch font
try:
font = pygame.font.Font("./assets/" + font, size)
except:
font = pygame.font.SysFont(font, size)

# Draw font
text_surface = font.render(content, True, c)
t = Text(content, text_surface, c, x, y, None, None, rotation)
t = Text(content, text_surface, c, x, y, None, None, size, font)
draw_buffer.append(t)


Expand Down
17 changes: 8 additions & 9 deletions primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,20 @@ def draw(self, context: pygame.display):

# Derived class - representing a text object
class Text(DrawableObject):
def __init__(self, content, surface, color, x, y, w, h, rotation=0):
super().__init__(x, y, w, h, rotation)
def __init__(self, content, surface, color, x, y, w, h, size, font):
super().__init__(x, y, w, h)
self.object_name = "Text"
self.color = color
self.contents = content
self.surface = surface
self.size = size
self.font = font

def draw(self, context: pygame.display):
# Rotate surface
self.surface = pygame.transform.rotate(self.surface, self.rotation)
rect = self.surface.get_rect()
rect.center = (self.x, self.y)

# Draw surface
context.blit(self.surface, rect)
# Set the position of the text
text_position = self.surface.get_rect()
text_position.center = (self.x, self.y) # Centered on the screen
context.blit(self.surface, text_position)


# Derived class - representing an image object
Expand Down

0 comments on commit c439495

Please sign in to comment.