我希望这个矩形突出显示时,鼠标在它上面,并改变其颜色时,点击。我设法让它保持突出显示,但点击后颜色的变化只是短暂的。我怎么才能让它保持这种状态?在
我的代码是:
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()
非常感谢任何帮助。谢谢!在
我可能会这样做:定义一个引用当前所选按钮颜色的变量(例如
button_color = BEFORECLICK
),如果用户按下按钮,只需将其更改为AFTERCLICK
。然后您可以绘制rect并在主循环pygame.draw.rect(DISPLAYSURF, button_color, myRectangle)
中传递当前颜色。在相关问题 更多 >
编程相关推荐