【问题标题】:How to insert url link inside a pygame [duplicate]如何在pygame中插入url链接[重复]
【发布时间】:2021-10-16 01:16:27
【问题描述】:
【问题讨论】:
标签:
python
hyperlink
pygame
【解决方案1】:
您所要做的就是检查鼠标按下事件是否发生在文本的矩形内。然后你可以使用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()