用pygame写一段代码,鼠标在一个坐标范围点击会出现一个弹窗
时间: 2024-05-09 15:17:37 浏览: 156
pygame 连连看程序源代码
好的,以下是使用pygame编写鼠标点击弹窗的代码:
```python
import pygame
# 初始化 Pygame
pygame.init()
# 定义屏幕大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# 定义弹窗大小和位置
popup_width = 200
popup_height = 100
popup_x = (screen_width - popup_width) // 2
popup_y = (screen_height - popup_height) // 2
# 定义弹窗颜色
popup_color = (255, 255, 255)
# 定义字体
font = pygame.font.SysFont(None, 24)
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# 获取鼠标位置
mouse_x, mouse_y = pygame.mouse.get_pos()
# 判断鼠标是否在弹窗范围内
if popup_x <= mouse_x <= popup_x + popup_width and popup_y <= mouse_y <= popup_y + popup_height:
# 绘制弹窗
popup = pygame.Surface((popup_width, popup_height))
popup.fill(popup_color)
screen.blit(popup, (popup_x, popup_y))
# 绘制文字
text = font.render("Hello, World!", True, (0, 0, 0))
text_rect = text.get_rect(center=(popup_width // 2, popup_height // 2))
popup.blit(text, text_rect)
# 绘制屏幕
screen.fill((255, 255, 255))
pygame.display.update()
```
请问您还有其他问题吗?
阅读全文