Python 如何使我的按钮保持高亮显示,直到我在网格上再次单击

Python 如何使我的按钮保持高亮显示,直到我在网格上再次单击,python,pygame,Python,Pygame,当我单击其中一个按钮时,您可以看到该按钮高亮显示,并在其周围显示一个蓝色矩形。有没有办法使按钮保持这样直到我点击我的网格。我试着使用一个while循环,使它保持那种颜色,直到第二次点击被注册,但这只是导致我的程序崩溃。谢谢 import pygame from pygame.locals import * pygame.init() def text_objects(text, font): textSurface = font.render(text, True, white)

当我单击其中一个按钮时,您可以看到该按钮高亮显示,并在其周围显示一个蓝色矩形。有没有办法使按钮保持这样直到我点击我的网格。我试着使用一个while循环,使它保持那种颜色,直到第二次点击被注册,但这只是导致我的程序崩溃。谢谢

import pygame
from pygame.locals import *

pygame.init()

def text_objects(text, font):
    textSurface = font.render(text, True, white)
    return textSurface, textSurface.get_rect()

class button():
    def __init__(self,screen,x,y,w,h,ic,ac,cc,text,text_colour):
        self.screen = screen
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.ic = ic
        self.colour = ic
        self.ac = ac
        self.cc = cc
        self.text = text
        self.text_colour = text_colour
        self.clicked = False
    def draw_button(self):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if self.x+self.w > mouse[0] > self.x and self.y+self.h > mouse[1] > self.y:
            pygame.draw.rect(self.screen, self.ac,(self.x,self.y,self.w,self.h))
            if click[0]:
                self.clicked = True
            if self.clicked == True:
                pygame.draw.rect(self.screen, self.cc,(self.x,self.y,self.w,self.h))
                pygame.draw.rect(self.screen, blue,(self.x,self.y,self.w,self.h), 3)
        else:
            pygame.draw.rect(self.screen, self.ic,(self.x,self.y,self.w,self.h))

        font = pygame.font.SysFont("arial black",20)
        text = font.render(self.text,True,(self.text_colour))
    #this code ensures it will be placed central in the button
        screen.blit(text,[self.x+self.w/2-(text.get_rect().w/2),self.y+self.h/2-(text.get_rect().h/2)])


# first we define some constants
# doing this will reduce the amount of 'magic' numbers throughout the code
black = (0, 0, 0)
white = (255, 255, 255)
grey = (125,125,125)
green = (0, 200, 0)
red = (200, 0, 0)
bright_red = (255,0,0)
bright_green = (0,255,0)
blue = (0,0,200)

grid_width = 40
grid_height = 40
cell = (grid_width, grid_height)

grid_margin = 5 # number of pixels between each cell

distance_from_left = 500 # number of pixels between the grid and the left and right of the screen
distance_from_top = 100 # number of pixels between the grid and the top and bottom of the screen

done = False # is our program finished running?


# create the screen and clock
screen_size = [1000,1000]
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Djikstra's and A*")
clock = pygame.time.Clock()

current_colour = white # which colour is active

# setting up the grid
# cells can be accessed by grid[row][col] ie. grid[3][4] is the 3rd row and 4th column
# each cell contains [x, y, colour]
# where x is the x position on the screen
#       y is the y position on the screen
#       colour is the current colour of the cell
grid = []
for y in range(10):
     row = []
     for x in range(10):
         row.append([x * (grid_width + grid_margin) + distance_from_left, y * (grid_height + grid_margin) + distance_from_top, white])
     grid.append(row)

# main loop
while not done:

    # process all events
    for event in pygame.event.get():
        if event.type == QUIT: # did the user click the 'x' to close the screen
            done = True

        if event.type == MOUSEBUTTONDOWN:
            # get the position of the mouse
            mpos_x, mpos_y = event.pos

            # check if finish was clicked
            button_x_min, button_y_min, button_width, button_height = 75,250,100,50
            button_x_max, button_y_max = button_x_min + button_width, button_y_min + button_height
            if button_x_min <= mpos_x <= button_x_max and button_y_min <= mpos_y <= button_y_max:
                current_colour = red

            # check if start WAS CLICKED
            button_x_min, button_y_min, button_width, button_height = 75,150,100,50
            button_x_max, button_y_max = button_x_min + button_width, button_y_min + button_height
            if button_x_min <= mpos_x <= button_x_max and button_y_min <= mpos_y <= button_y_max:
                current_colour = green

            # check if blocked WAS CLICKED
            button_x_min, button_y_min, button_width, button_height = 75,350,100,50
            button_x_max, button_y_max = button_x_min + button_width, button_y_min + button_height
            if button_x_min <= mpos_x <= button_x_max and button_y_min <= mpos_y <= button_y_max:
                current_colour = grey

            # calculations for clicking cells

            mpos_x -= distance_from_left # mouse position relative to the upper left cell
            mpos_y -= distance_from_top # ^ same

            col = mpos_x // (grid_width + grid_margin) # which cell is the mouse clicking
            row = mpos_y // (grid_height + grid_margin) # ^ same

            # make sure the user clicked on the grid area
            if row >= 0 and col >= 0:
                try:
                    # calculate the boundaries of the cell
                    cell_x_min, cell_y_min =  col * (grid_height + grid_margin), row * (grid_width + grid_margin)
                    cell_x_max = cell_x_min + grid_width
                    cell_y_max = cell_y_min + grid_height
                    # now we will see if the user clicked the cell or the margin
                    if cell_x_min <= mpos_x <= cell_x_max and cell_y_min <= mpos_y <= cell_y_max:
                        grid[row][col][2] = current_colour if event.button == 1 else white
                    else:
                        # the user has clicked the margin, so we do nothing
                        pass
                except IndexError: # clicked outside of the grid
                    pass           # we will do nothing

    # logic goes here


    # drawing
    screen.fill(black)

    menu = pygame.draw.rect(screen, white, [0,0,300,1000])

    start = button(screen,75,150,100,50,green,bright_green,grey, "Start", white)
    start.draw_button()
    finish = button(screen,75,250,100,50,red,bright_red,grey,"Finished",white)
    finish.draw_button()
    blocked = button(screen,75,350,100,50,black,grey,green,"Blocked",white)
    blocked.draw_button()

    for row in grid:
        for x, y, colour in row:
            pygame.draw.rect(screen, colour, (x, y, grid_width, grid_height))

    pygame.display.flip()

    clock.tick(60)

pygame.quit()
导入pygame
从pygame.locals导入*
pygame.init()
def text_对象(文本、字体):
textSurface=font.render(文本、真、白)
返回textSurface,textSurface.get_rect()
类按钮():
定义初始(自身、屏幕、x、y、w、h、ic、ac、cc、文本、文本颜色):
self.screen=屏幕
self.x=x
self.y=y
self.w=w
self.h=h
self.ic=ic
self.color=ic
self.ac=ac
self.cc=cc
self.text=文本
self.text\u color=文本颜色
self.clicked=False
def draw_按钮(自身):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
如果self.x+self.w>鼠标[0]>self.x和self.y+self.h>鼠标[1]>self.y:
pygame.draw.rect(self.screen,self.ac,(self.x,self.y,self.w,self.h))
如果单击[0]:
self.clicked=True
如果self.clicked==True:
pygame.draw.rect(self.screen,self.cc,(self.x,self.y,self.w,self.h))
pygame.draw.rect(self.screen,蓝色,(self.x,self.y,self.w,self.h),3)
其他:
pygame.draw.rect(self.screen,self.ic,(self.x,self.y,self.w,self.h))
font=pygame.font.SysFont(“arial黑色”,20)
text=font.render(self.text,True,(self.text\u颜色))
#此代码确保将其置于按钮的中央
blit(text[self.x+self.w/2-(text.get\rect().w/2),self.y+self.h/2-(text.get\rect().h/2)])
#首先我们定义一些常数
#这样做将减少整个代码中“神奇”数字的数量
黑色=(0,0,0)
白色=(255,255,255)
灰色=(125125)
绿色=(0,200,0)
红色=(200,0,0)
鲜红色=(255,0,0)
亮绿色=(0255,0)
蓝色=(0,0200)
网格宽度=40
网格高度=40
单元格=(网格宽度、网格高度)
网格_边距=5#每个单元格之间的像素数
距离_from _left=500#网格与屏幕左右之间的像素数
距屏幕顶部的距离=100#网格与屏幕顶部和底部之间的像素数
done=False#我们的程序运行完毕了吗?
#创建屏幕和时钟
屏幕大小=[10001000]
screen=pygame.display.set_模式(屏幕大小)
pygame.display.set_标题(“Djikstra's and A*”)
clock=pygame.time.clock()
当前颜色=白色#哪种颜色处于活动状态
#设置网格
#单元格可以通过网格[行][col]访问,即网格[3][4]是第3行和第4列
#每个单元格包含[x,y,颜色]
#其中x是屏幕上的x位置
#y是屏幕上的y位置
#颜色是单元格的当前颜色
网格=[]
对于范围(10)内的y:
行=[]
对于范围(10)内的x:
行。追加([x*(网格宽度+网格边距)+距离左侧,y*(网格高度+网格边距)+距离顶部,白色])
grid.append(行)
#主回路
虽然没有这样做:
#处理所有事件
对于pygame.event.get()中的事件:
如果event.type==QUIT:#用户是否单击“x”关闭屏幕
完成=正确
如果event.type==MOUSEBUTTONDOWN:
#获取鼠标的位置
mpos_x,mpos_y=event.pos
#检查是否单击了“完成”
按钮x最小值、按钮y最小值、按钮宽度、按钮高度=75250100,50
按钮x最大值,按钮y最大值=按钮x最小值+按钮宽度,按钮y最小值+按钮高度
如果按钮\u x\u min简化事情:

将方法(
isOn
)添加到
类按钮
,如果鼠标位于该按钮上,则会选中该方法:

def-isOn(self、mx、my):
返回self.x