Install pygbag to run the code in the browswer

This project should bring to make something like a e-reader.
To run it here, I first put the folder with the apk file into formazione.github.io, my repo, and then I used the iframe tag here:

- remember to import asyncyo
- Put async before the def main()
- Put the await asyncio.sleep(0) under the clock tick and the asyncio.run(main()) to run the code
Once you saved the main.py file, go out of the folder containing the main.py file and write in the cmd (from that path) pygbag folder, where folder is the name of the folder where the main.py file is.

How to create the apk file
import pygame
import sys
import asyncio
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BG_COLOR = (30, 30, 30)
TEXT_COLOR = (255, 255, 255)
BUTTON_COLOR = (70, 70, 70)
BUTTON_HOVER_COLOR = (100, 100, 100)
FONT_SIZE = 48
BUTTON_FONT_SIZE = 24
PADDING = 20
# Setup screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Amleto Chapter 1')
# Fonts
font = pygame.font.Font(None, FONT_SIZE)
button_font = pygame.font.Font(None, BUTTON_FONT_SIZE)
# Text data
amleto_chapter1 = """Amleto - Chapter 1:
To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles
And by opposing end them. To die: to sleep;
No more; and by a sleep to say we end
The heart-ache and the thousand natural shocks
That flesh is heir to, 'tis a consummation
Devoutly to be wish'd. To die, to sleep;
To sleep: perchance to dream: ay, there's the rub;
For in that sleep of death what dreams may come
When we have shuffled off this mortal coil,
Must give us pause: there's the respect
That makes calamity of so long life;
For who would bear the whips and scorns of time,
The oppressor's wrong, the proud man's contumely,
The pangs of despised love, the law's delay,
The insolence of office and the spurns
That patient merit of the unworthy takes,
When he himself might his quietus make
With a bare bodkin? who would fardels bear,
To grunt and sweat under a weary life,
But that the dread of something after death,
The undiscover'd country from whose bourn
No traveller returns, puzzles the will
And makes us rather bear those ills we have
Than fly to others that we know not of?
Thus conscience does make cowards of us all;
And thus the native hue of resolution
Is sicklied o'er with the pale cast of thought,
And enterprises of great pith and moment
With this regard their currents turn awry,
And lose the name of action.--Soft you now!
The fair Ophelia! Nymph, in thy orisons
Be all my sins remember'd.""".splitlines()
# Button settings
button_text = "Next"
button_rect = pygame.Rect((SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 70), (100, 50))# Button settings
# button previous
button_text1 = "Previous"
button_rect1 = pygame.Rect((SCREEN_WIDTH // 2 - 151, SCREEN_HEIGHT - 70), (100, 50))
# State
current_line = 0
def render_text(text, font, color, surface, x, y):
lines = text.split('\n')
for i, line in enumerate(lines):
text_surface = font.render(line, True, color)
surface.blit(text_surface, (x, y + i * FONT_SIZE))
def draw_button(surface, rect, color, text, font, text_color):
pygame.draw.rect(surface, color, rect)
text_surface = font.render(text, True, text_color)
text_rect = text_surface.get_rect(center=rect.center)
surface.blit(text_surface, text_rect)
async def main():
global current_line
clock = pygame.time.Clock()
while True:
screen.fill(BG_COLOR)
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if button_rect.collidepoint(event.pos):
if current_line < len(amleto_chapter1) - 1:
current_line += 1
if button_rect1.collidepoint(event.pos):
if current_line > 0:
current_line -= 1
# Render current text
if current_line < len(amleto_chapter1):
render_text(amleto_chapter1[current_line], font, TEXT_COLOR, screen, PADDING, PADDING)
# Draw button
mouse_pos = pygame.mouse.get_pos()
if button_rect.collidepoint(mouse_pos):
button_color = BUTTON_HOVER_COLOR
else:
button_color = BUTTON_COLOR
if button_rect1.collidepoint(mouse_pos):
button_color1 = BUTTON_HOVER_COLOR
else:
button_color1 = BUTTON_COLOR
draw_button(screen, button_rect, button_color, button_text, button_font, TEXT_COLOR)
draw_button(screen, button_rect1, button_color1, button_text1, button_font, TEXT_COLOR)
# Update display
pygame.display.flip()
# Cap the frame rate
clock.tick(30)
await asyncio.sleep(0)
if __name__ == "__main__":
asyncio.run(main())
Subscribe to the newsletter for updates
Tkinter templatesTwitter: @pythonprogrammi - python_pygame
Claude's Games
1. Memory gameVideos
Speech recognition gamePygame's Platform Game