我可以在游戏中添加一个链接吗?就像在HTML中一样,一旦点击了它,我们就会被重定向到URL。我有一个游戏‘基于回合的战斗’-> https://www.pygame.org/project/5492/7939,我想添加一个文本在屏幕上的某个地方,他们可以按下的链接。如果您想查看代码,您可以从创建者查看Github页面,这基本上和我现在的游戏一样。-> https://github.com/russs123/Battle
发布于 2021-08-12 17:11:17
您所要做的就是检查鼠标按下事件是否发生在文本的rect内。然后可以使用webbrowser.open()在浏览器中打开链接。
样本代码:
import pygame
import webbrowser
pygame.init()
screen = pygame.display.set_mode((1000, 800))
link_font = pygame.font.SysFont('Consolas', 50)
link_color = (0, 0, 0)
running = True
while running:
screen.fill((255, 255, 255))
rect = screen.blit(link_font.render("Sample Link", True, link_color), (50, 50))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = event.pos
if rect.collidepoint(pos):
webbrowser.open(r"https://stackoverflow.com/")
if rect.collidepoint(pygame.mouse.get_pos()):
link_color = (70, 29, 219)
else:
link_color = (0, 0, 0)
pygame.display.update()https://stackoverflow.com/questions/68761290
复制相似问题