I'm trying to convert a Pygame program to work as a web app with pygbag. following all instructions, I changed the main.py file to run with asyncio. running the main file itself with python interpreter, and all works fine. but when I'm trying to run it with Pygbag and run the game via the local webserver, it's not working properly. In general, the browser "loading", but than nothing happened.
So i simplified the program, tried to see if it's can run, and run the pygbag on browser in debug mode, than i got the error: enter image description here
I'm adding here the simplified code:
import pygame, pygame.gfxdraw, random, asyncio
from pygame.locals import (
RLEACCEL,
K_UP,
K_DOWN,
K_RIGHT,
K_LEFT,
KEYDOWN,
K_ESCAPE,
QUIT
)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# =============== Change direction ===============
def change_direction(direction, collision):
if direction == 'up-right' and collision == 'right':
direction = 'up-left'
if direction == 'down-right' and collision == 'right':
direction = 'down-left'
if direction == 'up-left' and collision == 'left':
direction = 'up-right'
if direction == 'down-left' and collision == 'left':
direction = 'down-right'
if direction == 'up-right' and collision == 'top':
direction = 'down-right'
if direction == 'up-left' and collision == 'top':
direction = 'down-left'
if direction == 'down-right' and collision == 'bottom':
direction = 'up-right'
if direction == 'down-left' and collision == 'bottom':
direction = 'up-left'
return direction
# =============== Ball ===============
class Ball(pygame.sprite.Sprite):
def __init__(self):
super(Ball, self).__init__()
# make round surface
circle = pygame.Surface((30, 30), pygame.SRCALPHA)
pygame.gfxdraw.aacircle(circle, 15, 15, 7, (255, 255, 255))
pygame.gfxdraw.filled_circle(circle, 15, 15, 7, (255, 255, 255))
self.surf = circle
self.rect = self.surf.get_rect(center = (random.randrange(5,790), 550))
def update(self, direction):
if self.rect.right > SCREEN_WIDTH:
direction = change_direction(direction, 'right')
if self.rect.left < 0:
direction = change_direction(direction, 'left')
if self.rect.top < 0:
direction = change_direction(direction, 'top')
if self.rect.top > SCREEN_HEIGHT:
direction = change_direction(direction, 'bottom')
if direction == 'up-right':
self.rect.move_ip(1, -1)
if direction == 'up-left':
self.rect.move_ip(-1, -1)
if direction == 'down-right':
self.rect.move_ip(1, 1)
if direction == 'down-left':
self.rect.move_ip(-1, 1)
return direction
# =============== Run the game ===============
async def main():
pygame.init()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption("BreakBricks")
ball = Ball()
all_sprites = pygame.sprite.Group(ball)
clock = pygame.time.Clock()
running = True
direction = 'up-right'
while running:
for x in pygame.event.get():
if x.type == KEYDOWN:
if x.key == K_ESCAPE:
running = False
elif x.type == QUIT:
running = False
direction = ball.update(direction)
screen.fill((0, 0, 0))
for spr in all_sprites:
screen.blit(spr.surf, spr.rect)
pygame.display.flip()
clock.tick(30)
await asyncio.sleep(0)
asyncio.run(main())
According to the traceback, the error occurred while trying to create the circle object. but i don't know why.
The renderer error seems to be an issue with pygame.gfxdraw, which is an "experimental" feature according to the docs.
Replacing the pygame.gfxdraw functions with pygame.draw functions fixed this for me.