pip install https://github.com/aratakileo/pygex/archive/refs/tags/v0.2.zip
from typing import Sequence
import pygex.image
import pygex.text
import pygame
import pygex
def render_text(_header_text: str, _text: str, size: Sequence[int], _agent_rect: pygame.Rect, _agent_moved: bool):
output_surface = pygex.image.gradient(size, (0xffc107, 0xfff3e0), is_vertical=True)
pygex.text.get_pygame_font(50).bold = True
_header_text_surface = pygex.text.render_aligned_text(
_header_text,
0xffb300,
size[0],
50,
pygex.text.ALIGN_CENTER
)
pygex.text.get_pygame_font(35).italic = True
_text_surface = pygex.text.render_aligned_text(
_text,
0xffffff,
size[0],
35,
pygex.text.ALIGN_CENTER
)
text_height = _header_text_surface.get_height() + _text_surface.get_height()
header_text_y = max((size[1] - text_height) / 2 - _header_text_surface.get_height() * 2, 0)
text_y = header_text_y + _header_text_surface.get_height()
if not _agent_moved:
_agent_text_surface = pygex.text.render_text('Hold, press [w,a,s,d] or drag by mouse to move « »', 0x80deea, 35)
_agent_text_surface_y = text_y + _text_surface.get_height() + _agent_text_surface.get_height() * 2
_agent_text_surface_x = (size[0] - _agent_text_surface.get_width()) / 2
output_surface.blit(_agent_text_surface, (_agent_text_surface_x, _agent_text_surface_y))
_agent_rect.x = _agent_text_surface_x + _agent_text_surface.get_width() - _agent_rect.width - 20
_agent_rect.y = _agent_text_surface_y
output_surface.blit(_header_text_surface, (0, header_text_y))
output_surface.blit(_text_surface, (0, text_y))
return output_surface, pygex.image.blur(output_surface, 10)
window = pygex.Window()
window.bg_color = 0xffffff
window.fps_limit = 120
agent_moved = False
agent_rect = pygame.Rect(0, 0, 25, 25)
agent_dragging = False
fullscreen_toast = pygex.Toast('To exit full screen press [F11]')
header_text = 'Welcome to pygex example!'
text = '''- press [f11] to enter full screen -
- press [f1] to take screenshot -
- hold [f2] to blur text -'''
text_surface, blured_text_surface = render_text(header_text, text, window.size, agent_rect, agent_moved)
old_window_size = window.size
def rerender_screen_bg():
global text_surface, blured_text_surface
text_surface, blured_text_surface = render_text(header_text, text, window.size, agent_rect, agent_moved)
while True:
if old_window_size != window.size:
old_window_size = window.size
rerender_screen_bg()
window.surface.blit(blured_text_surface if window.input.is_hold(pygame.K_F2) else text_surface, (0, 0))
debug_text_info = pygex.text.render_text(f'{window.fps:.3f}/120fps, pygex: v0.2', 0)
window.surface.blit(
debug_text_info,
(window.width - debug_text_info.get_width(), window.height - debug_text_info.get_height())
)
pygame.draw.rect(window.surface, 0x80deea, agent_rect)
if window.input.any_is_applying(pygame.K_w, pygame.K_UP):
agent_rect.y -= 10
if not agent_moved:
agent_moved = True
rerender_screen_bg()
elif window.input.any_is_applying(pygame.K_s, pygame.K_DOWN):
agent_rect.y += 10
if not agent_moved:
agent_moved = True
rerender_screen_bg()
if window.input.any_is_applying(pygame.K_a, pygame.K_LEFT):
agent_rect.x -= 10
if not agent_moved:
agent_moved = True
rerender_screen_bg()
elif window.input.any_is_applying(pygame.K_d, pygame.K_RIGHT):
agent_rect.x += 10
if not agent_moved:
agent_moved = True
rerender_screen_bg()
if window.mouse.left_is_down and agent_rect.collidepoint(window.mouse.pos):
agent_dragging = True
elif window.mouse.left_is_hold and agent_dragging:
agent_rect.center = window.mouse.pos
if not agent_moved:
agent_moved = True
rerender_screen_bg()
else:
agent_dragging = False
if agent_rect.x < 0:
agent_rect.x = 0
elif agent_rect.right > window.width:
agent_rect.right = window.width
if agent_rect.y < 0:
agent_rect.y = 0
elif agent_rect.bottom > window.height:
agent_rect.bottom = window.height
if window.input.is_up(pygame.K_F1):
window.take_screenshot()
elif window.input.is_up(pygame.K_F11):
window.fullscreen = not window.fullscreen
if window.fullscreen:
fullscreen_toast.show()
else:
fullscreen_toast.cancel()
window.flip()