我想要在鼠标悬停时突出显示此矩形,并在单击时更改其颜色。我设法让它保持突出显示但点击后的颜色变化只是暂时的。我如何让它保持这种状态?
这是我的代码:
import pygame, sys
from pygame.locals import *
FPS = 30
BGCOLOR = (3, 115, 46)
BEFORECLICK = (22, 22, 106)
AFTERCLICK = (200, 200, 200)
boardWidth = 500
boardHeight = 500
rectX = 150
rectY = 150
rectWidth = 200
rectHeight = 200
myRectangle = pygame.Rect(rectX, rectY, rectWidth, rectHeight)
def main():
global FPSCLOCK, DISPLAYSURF
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((boardWidth, boardHeight))
pygame.display.set_caption("Klikni, kar klikni.")
mousex = 0
mousey = 0
while True:
mouseClicked = False
DISPLAYSURF.fill(BGCOLOR)
pygame.draw.rect(DISPLAYSURF, BEFORECLICK, myRectangle)
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == MOUSEMOTION:
mousex, mousey = event.pos
elif event.type == MOUSEBUTTONUP:
mousex, mousey = event.pos
mouseClicked = True
mouseOver = determine_mouseOver(mousex, mousey)
if mouseOver == True and mouseClicked == True:
pygame.draw.rect(DISPLAYSURF, AFTERCLICK, myRectangle)
elif mouseOver == True and mouseClicked == False:
pygame.draw.rect(DISPLAYSURF, AFTERCLICK, myRectangle, 3)
pygame.display.update()
FPSCLOCK.tick(30)
def determine_mouseOver(valx, valy):
if myRectangle.collidepoint(valx, valy):
return True
else:
return False
main()
非常感谢任何帮助。谢谢!
答案 0 :(得分:0)
我可能会这样做:定义一个引用当前所选按钮颜色的变量(例如button_color = BEFORECLICK
),如果用户按下该按钮,只需将其更改为AFTERCLICK
即可。然后你可以绘制矩形并在主循环pygame.draw.rect(DISPLAYSURF, button_color, myRectangle)
中传递当前颜色。
# Current color of the button.
button_color = BEFORECLICK
mouseOver = False
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == MOUSEMOTION:
mousex, mousey = event.pos
elif event.type == MOUSEBUTTONUP:
if mouseOver:
# Change the current color if button was clicked.
button_color = AFTERCLICK
mouseOver = determine_mouseOver(mousex, mousey)
DISPLAYSURF.fill(BGCOLOR)
# Just draw the rect with the current button color.
pygame.draw.rect(DISPLAYSURF, button_color, myRectangle)
if mouseOver:
pygame.draw.rect(DISPLAYSURF, AFTERCLICK, myRectangle, 3)
pygame.display.update()
FPSCLOCK.tick(30)