我已经尝试了几天,将使用 pygame 模块的 python 程序转换为使用 pygbag 的网页,以便我可以将其集成到我的网站中。
但是,我不断收到此警告:
以下是我的程序的完整代码:
import pygame, asyncio
from random import randint
class timePlaying(pygame.sprite.Sprite):
def __init__(self):
Timer = "0"
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.SysFont("Arial", 12)
self.textSurf = self.font.render(Timer, 1, "white")
self.image = pygame.Surface((20, 10))
self.image.fill((0,255,0))
self.rect = self.image.get_rect(center=(20,10))
W = self.textSurf.get_width()
H = self.textSurf.get_height()
self.image.blit(self.textSurf, [20/2 - W/2, 10/2 - H/2])
def update(self, time):
Timer = str(time)
self.font = pygame.font.SysFont("Arial", 12)
self.textSurf = self.font.render(Timer, 1, "white")
self.image = pygame.Surface((20, 10))
self.image.fill((0,255,0))
self.rect = self.image.get_rect(center=(10,5))
W = self.textSurf.get_width()
H = self.textSurf.get_height()
self.image.blit(self.textSurf, [10/2 - W/2, 5/2 - H/2])
class Scoreboard1(pygame.sprite.Sprite):
def __init__(self, size, color, width, height, locationY):
CurrentScore1 = 0
score = "Score: "+str(CurrentScore1)
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.SysFont("Arial", size)
self.textSurf = self.font.render(score, 1, color)
self.image = pygame.Surface((width, height))
self.image.fill((0,0,255))
self.rect = self.image.get_rect(center=(400,locationY))
W = self.textSurf.get_width()
H = self.textSurf.get_height()
self.image.blit(self.textSurf, [width/2 - W/2, height/2 - H/2])
def update(self, CurrentScore1, size, color, width, height):
CurrentScore1 += 1
text1 = "Score: "+str(CurrentScore1)
self.font = pygame.font.SysFont("Arial", size)
self.textSurf = self.font.render(text1, 1, color)
self.image = pygame.Surface((width, height))
self.image.fill((0,0,255))
W = self.textSurf.get_width()
H = self.textSurf.get_height()
self.image.blit(self.textSurf, [width/2 - W/2, height/2 - H/2])
return CurrentScore1
class Scoreboard2(pygame.sprite.Sprite):
def __init__(self, size, color, width, height, locationY):
CurrentScore2 = 0
score = "Score: "+str(CurrentScore2)
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.SysFont("Arial", size)
self.textSurf = self.font.render(score, 1, color)
self.image = pygame.Surface((width, height))
self.image.fill((0,0,255))
self.rect = self.image.get_rect(center=(400,locationY))
W = self.textSurf.get_width()
H = self.textSurf.get_height()
self.image.blit(self.textSurf, [width/2 - W/2, height/2 - H/2])
def update(self, CurrentScore2, size, color, width, height):
CurrentScore2 += 1
text1 = "Score: "+str(CurrentScore2)
self.font = pygame.font.SysFont("Arial", size)
self.textSurf = self.font.render(text1, 1, color)
self.image = pygame.Surface((width, height))
self.image.fill((0,0,255))
W = self.textSurf.get_width()
H = self.textSurf.get_height()
self.image.blit(self.textSurf, [width/2 - W/2, height/2 - H/2])
return CurrentScore2
class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
class Player(pygame.sprite.Sprite):
def __init__(self, location):
pygame.sprite.Sprite.__init__(self)
self.surf = pygame.Surface((20, 50))
self.rect = self.surf.get_rect()
self.rect.left, self.rect.top = location
def moveUp(self, pixels):
self.rect.y -= pixels
if self.rect.y < 0:
self.rect.y = 0
def moveDown(self, pixels):
self.rect.y += pixels
if self.rect.y > 500:
self.rect.y = 500
class Goal(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self)
self.surf = pygame.Surface((50, 61))
self.surf.fill((255,255,255))
self.image = pygame.image.load(image_file)
self.rect = self.surf.get_rect(center=(location))
class StartOverlay(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self)
self.surf = pygame.image.load(image_file)
self.surf.fill((255, 255, 255, 200), None, pygame.BLEND_RGBA_MULT)
self.rect = self.surf.get_rect(center=(location))
class Ball(pygame.sprite.Sprite):
def __init__(self, image_file):
super(Ball, self).__init__()
self.surf = pygame.Surface((20, 20))
self.surf.fill((255,255,255))
self.image = pygame.image.load(image_file)
self.rect = self.surf.get_rect(center=(400,250,))
self.velocity = [randint(4,8),randint(-8,8)]
def update(self):
self.rect.x += self.velocity[0]
self.rect.y += self.velocity[1]
def bounce(self):
self.velocity[0] = -self.velocity[0]
self.velocity[1] = randint(-8,8)
def reset(self):
self.surf = pygame.Surface((20, 20))
self.surf.fill((255,255,255))
self.rect = self.surf.get_rect(center=(400,250,))
self.velocity = [randint(4,8),randint(-8,8)]
pygame.init()
win = pygame.display.set_mode((800, 500))
pygame.display.set_caption("Pong: Football edition")
clock = pygame.time.Clock()
scoreBoard1 = Scoreboard1(12,"white",100,50,25)
scoreBoard2 = Scoreboard2(12,"white",100,50,75)
timing = timePlaying()
startingOverlay = StartOverlay('start.png', [400,250])
player1 = Player(location =(50, 200))
player2 = Player(location =(720, 200))
all_sprites = pygame.sprite.Group()
players = pygame.sprite.Group()
goals = pygame.sprite.Group()
BackGround = Background('pitch.jpg', [0,0])
ball = Ball('football.png')
goal1 = Goal('goal.jpg', [25,250])
goal2 = Goal('goal2.jpg', [775,250])
all_sprites.add(scoreBoard1)
all_sprites.add(scoreBoard2)
all_sprites.add(timing)
all_sprites.add(player1)
all_sprites.add(player2)
all_sprites.add(ball)
all_sprites.add(BackGround)
players.add(player1)
players.add(player2)
goals.add(goal1)
goals.add(goal2)
async def main():
CurrentScore1 = 0
CurrentScore2 = 0
time = 0
gameStarted = False
run = True
while run:
clock.tick(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
pygame.time.set_timer(event, 1000)
time += 1
if not gameStarted:
gameStarted = True
startingOverlay.kill()
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
player1.moveUp(5)
if keys[pygame.K_s]:
player1.moveDown(5)
if keys[pygame.K_UP]:
player2.moveUp(5)
if keys[pygame.K_DOWN]:
player2.moveDown(5)
if ball.rect.x>=790:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.x<=0:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.y>490:
ball.velocity[1] = -ball.velocity[1]
if ball.rect.y<0:
ball.velocity[1] = -ball.velocity[1]
if pygame.sprite.spritecollideany(ball, players):
ball.bounce()
win.fill((0,0,0))
win.blit(BackGround.image, BackGround.rect)
if not gameStarted:
win.blit(startingOverlay.surf, startingOverlay.rect)
pygame.draw.rect(win, (255, 255, 255), player1)
pygame.draw.rect(win, (255, 255, 255), player2)
win.blit(ball.image, ball.rect)
win.blit(goal1.image, goal1.rect)
win.blit(goal2.image, goal2.rect)
win.blit(scoreBoard1.image,scoreBoard1)
win.blit(scoreBoard2.image,scoreBoard2)
win.blit(timing.image, timing)
timing.update(time)
ball.update()
pygame.display.update()
if pygame.sprite.spritecollideany(ball, players):
ball.update()
goal_scored = pygame.sprite.spritecollideany(ball, goals)
if goal_scored == goal2:
ball.kill()
CurrentScore1 = scoreBoard1.update(CurrentScore1,12,"white",100,50)
ball.reset()
if goal_scored == goal1:
ball.kill()
CurrentScore2 = scoreBoard2.update(CurrentScore2,12,"white",100,50)
ball.reset()
await asyncio.sleep(0)
asyncio.run(main())
任何人都可以帮我解决这个错误吗,我真的很想将此 pygame 程序集成到我的网站中。
我尝试通过转到我的链接来调试它:
http://localhost:8000/#debug
但是,当我这样做时,我收到了来自 python 的错误消息:
看起来加载文本字体时出现错误,但我不知道如何解决这个问题
这似乎是 platfrom_wasm
模块中的 bug pygame-web 项目.
如果我们看一下修补
pygame.time.set_timer
函数的代码:
def patch_set_timer(event: Union[int, pygame.event.Event], millis: int, loops: int = 0):
"""Patches the pygame.time.set_timer function to use gthreads"""
dlay = float(millis) / 1000
cevent = pygame.event.Event(event)
event_loop = asyncio.get_event_loop()
...
我们看到了线
cevent = pygame.event.Event(event)
。 pygame.event.Event
构造函数需要一个 int
值,但在您的情况下 event
已经是 pygame.event.Event
。
甚至类型提示 (
Union[int, pygame.event.Event]
) 也清楚地表明传递 pygame.event.Event
应该有效。
我不知道你想对定时事件做什么,所以我建议你暂时删除该行(
pygame.time.set_timer(event, 1000)
)。