,可以通过以下步骤实现:
import pygame
from pygame.locals import *
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
background_color = (255, 255, 255) # 设置背景颜色为白色
clicked_color = (255, 0, 0) # 设置点击后的颜色为红色
def handle_click(pos):
x, y = pos
if x < screen_width / 2 and y < screen_height / 2:
pygame.draw.rect(screen, clicked_color, (0, 0, screen_width / 2, screen_height / 2))
elif x >= screen_width / 2 and y < screen_height / 2:
pygame.draw.rect(screen, clicked_color, (screen_width / 2, 0, screen_width / 2, screen_height / 2))
elif x < screen_width / 2 and y >= screen_height / 2:
pygame.draw.rect(screen, clicked_color, (0, screen_height / 2, screen_width / 2, screen_height / 2))
else:
pygame.draw.rect(screen, clicked_color, (screen_width / 2, screen_height / 2, screen_width / 2, screen_height / 2))
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == MOUSEBUTTONDOWN:
handle_click(pygame.mouse.get_pos())
screen.fill(background_color)
pygame.display.flip()
pygame.quit()
这样,当鼠标点击屏幕的不同区域时,被点击的区域将会变为红色,并在发布Pygame后保持不变。
关于Pygame的更多信息和使用方法,可以参考腾讯云的相关产品介绍链接地址:Pygame产品介绍
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云