pygbag pygame.error:参数“渲染器”无效

问题描述 投票:0回答:1

我正在尝试将 Pygame 程序转换为带有 pygbag 的网络应用程序。 按照所有说明,我更改了 main.py 文件以使用 asyncio 运行。使用 python 解释器运行主文件本身,一切正常。但是当我尝试使用 Pygbag 运行它并通过本地网络服务器运行游戏时,它无法正常工作。一般来说,浏览器“正在加载”,但什么也没发生。

所以我简化了程序,尝试看看它是否可以运行,并在浏览器上以调试模式运行 pygbag,然后出现错误: enter image description here

我在这里添加简化的代码:

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())

根据回溯,尝试创建圆形对象时发生错误。但我不知道为什么。

  1. 将代码简化为最基本的,仅创建 1 个对象并在屏幕上移动。
  2. 尝试将所有对象和组的启动都带入主函数中 - 尽管在 python 中运行不是必需的 - 没有帮助。
  3. 运行调试模式。
python pygame pygbag
1个回答
1
投票

渲染器错误似乎是 pygame.gfxdraw 的问题,根据 docs,这是一个“实验性”功能。

用 pygame.draw 函数替换 pygame.gfxdraw 函数为我解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.