pygame-web / pygbag

python and pygame wasm for everyone ( packager + test server + simulator )
https://github.com/pygame-web
MIT License
295 stars 34 forks source link

pygame window is just black #131

Closed Schnitzyy closed 8 months ago

Schnitzyy commented 8 months ago

image

pmp-p commented 8 months ago

most likely an infinite loop, that's what the simulator will help solving in next release.

Could you show whole project or at very least the game loop part

Schnitzyy commented 8 months ago
async def main():
    while True:
        global game_running, score, player_gravity
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if game_running:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        if player_rect.bottom == 300:
                            player_gravity = -20

                if event.type == pygame.MOUSEBUTTONDOWN:
                        if player_rect.collidepoint((event.pos)):
                            if player_rect.bottom == 300:
                                player_gravity = -20

            else:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        if not game_running:
                            game_running = True
                start_time = pygame.time.get_ticks()

        if game_running:

            score = display_score()

            # Draw a surface
            SCREEN.blit(sky_surf, (0,0))
            SCREEN.blit(ground_surf, ground_rect)

            # Obstacle Movement
            # obstacle_rect_list = obstacle_movement(obstacle_rect_list)

            # Player
            player_gravity += 1
            player_rect.y += player_gravity
            if player_rect.bottom > 300:
                player_rect.bottom = 300
            SCREEN.blit(player_surf, player_rect)

            display_score()
            # pygame.draw.rect(SCREEN,'#c0e8ec', score_rect)
            # pygame.draw.rect(SCREEN,'#c0e8ec', score_rect, 10)
            # SCREEN.blit(score_surf, score_rect)

            SCREEN.blit(snail_surf, snail_rect)
            snail_rect.left -= random.randint(3,4)
            if snail_rect.right <= 0:
                snail_rect.x = 800

            # Collisions
            if snail_rect.colliderect(player_rect):
                game_running = False
        else:
            SCREEN.fill((94,129,162))
            SCREEN.blit(player_stand, player_stand_rect)
            SCREEN.blit(start_text_surf1, start_text_surf1_rect)

            score_message = test_font.render(f'Your score: {score}', False, (111,196,169))
            score_message_rect = score_message.get_rect(center = (400, 330))

            if score == 0:
                SCREEN.blit(start_text_surf2, start_text_surf2_rect)
            else:
                SCREEN.blit(score_message, score_message_rect)

            snail_rect.left = 800

        if event.type == obstacle_timer and game_running:
            obstacle_rect_list.append(snail_surf.get_rect(bottomright = (random.randint(900, 1100), 300)))

        # end_music()

        # Update everything
        pygame.display.update()
        clock.tick(60)
    await asyncio.sleep(0)

asyncio.run(main())
pmp-p commented 8 months ago

try adding one indent level before await asyncio.sleep(0)

Schnitzyy commented 8 months ago

Oh okay that worked previously when I kept an indent there it gave me an error, but it works now. BTW does pygbag not support sound? image image

pmp-p commented 8 months ago

it does but some browsers will not tolerate .mp3, but all go along with .ogg, you should have a look to https://pygame-web.github.io "IMPORTANT ALL" section, other restrictions can apply

for background music, prefer the module pygame.mixer.music it has been optimized for web