pygame更改颜色鼠标悬停

2022-08-06

以下示例是关于Python中包含pygame更改颜色鼠标悬停用法的示例代码,想了解pygame更改颜色鼠标悬停的具体用法?pygame更改颜色鼠标悬停怎么用?pygame更改颜色鼠标悬停使用的例子?那么可以参考以下相关源代码片段来学习它的具体使用方法。

[英]:pygame change color mouse hover源码类型:Python
# Imports
import sys
import pygame

# Configuration
pygame.init()
fps = 60
fpsClock = pygame.time.Clock()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))

# Before Main loop
rect = pygame.Rect(10, 10, 100, 60)
print(rect.width)
btn_surface = pygame.Surface(
    (rect.width, rect.height)
)

normal_color = (200, 100, 100)
hover_color = (100, 200, 100)

# Game loop.
while  True:
    screen.fill((20, 20, 20))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # In the Main loop
    if rect.collidepoint(pygame.mouse.get_pos()):
        btn_surface.fill(hover_color)
    else:
        btn_surface.fill(normal_color)

    screen.blit(btn_surface, rect)

    pygame.display.flip()
    fpsClock.tick(fps)

本文地址:https://www.itbaoku.cn/snippets/795560.html