когда я нажимаю одну из моих кнопок, вы видите, что кнопка подсвечена и вокруг нее появляется синий прямоугольник.Есть ли способ сделать так, чтобы кнопка оставалась такой, пока я не нажму на свою сетку.Я попытался использовать цикл 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()