代码实现EGE画面中有五个矩形,通过鼠标左键点击可以打勾,鼠标右键点击取消
时间: 2024-05-27 08:11:43 浏览: 93
以下是Python实现:
```python
import pygame
# 初始化pygame
pygame.init()
# 设置窗口大小和标题
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("EGE画面")
# 设置矩形的位置、大小和状态
rectangles = [
{"x": 50, "y": 50, "width": 100, "height": 100, "checked": False},
{"x": 200, "y": 50, "width": 100, "height": 100, "checked": False},
{"x": 350, "y": 50, "width": 100, "height": 100, "checked": False},
{"x": 500, "y": 50, "width": 100, "height": 100, "checked": False},
{"x": 650, "y": 50, "width": 100, "height": 100, "checked": False},
]
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 游戏循环
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
# 鼠标左键点击
if event.button == 1:
for rectangle in rectangles:
if rectangle["x"] < event.pos[0] < rectangle["x"] + rectangle["width"] and \
rectangle["y"] < event.pos[1] < rectangle["y"] + rectangle["height"]:
rectangle["checked"] = not rectangle["checked"]
# 鼠标右键点击
elif event.button == 3:
for rectangle in rectangles:
if rectangle["x"] < event.pos[0] < rectangle["x"] + rectangle["width"] and \
rectangle["y"] < event.pos[1] < rectangle["y"] + rectangle["height"]:
rectangle["checked"] = False
# 填充背景色
screen.fill(WHITE)
# 绘制矩形
for rectangle in rectangles:
pygame.draw.rect(screen, BLACK, (rectangle["x"], rectangle["y"], rectangle["width"], rectangle["height"]), 2)
if rectangle["checked"]:
pygame.draw.line(screen, GREEN, (rectangle["x"], rectangle["y"]), (rectangle["x"] + rectangle["width"], rectangle["y"] + rectangle["height"]), 5)
pygame.draw.line(screen, GREEN, (rectangle["x"] + rectangle["width"], rectangle["y"]), (rectangle["x"], rectangle["y"] + rectangle["height"]), 5)
# 更新屏幕
pygame.display.flip()
# 退出pygame
pygame.quit()
```
运行程序后,可以使用鼠标左键点击矩形打勾,使用鼠标右键点击矩形取消。效果如下图所示:
![EGE画面](https://cdn.luogu.com.cn/upload/image_hosting/zk4etd13.png)
阅读全文