我创建了一个滑块,用于更新网格大小的值。当我更新此值时,我希望网格大小更改为新的更新值,但当前它仍保持与以前相同。反正有什么可以改变的。
以下是滑块和游戏循环:
def slider_loop(s,grid_width,grid_height,graph):
s.move()
grid_size = int(slider_value.val)
return grid_size
def game_loop():
done = False # is our program finished running?
global grid_size
current_colour = white # which colour is active
load = False
start = button(screen,75,12.5,100,50,green,bright_green,bright_green, "Start", white)
finish = button(screen,75,87.5,100,50,red,bright_red,bright_red,"Finished",white)
blocked = button(screen,75,162.5,100,50,black,grey,grey,"Blocked",white)
remove = button(screen,75,237.5,100,50,yellow,grey,grey,"Remove",white)
Diagonal = diagonal_button(screen,75,312.5,100,50,white,"Diagonal","Y","N")
cheesy = button(screen,75,387.5,100,50,yellow,grey,grey,"NY Grid size",white)
begin = begin_button(75,537.5,100,50,green, bright_green, "Begin",white)
diagonal_path = False
#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
start.clicked = start.button_on(mpos_x, mpos_y)
finish.clicked = finish.button_on(mpos_x, mpos_y)
blocked.clicked = blocked.button_on(mpos_x, mpos_y)
remove.clicked = remove.button_on(mpos_x,mpos_y)
if event.button == 1:
begin.click(lambda:dijkstra(grid, grid_dictionary, start_node,finish_node))
#check if diagonal-yes was clicked
button_x_min, button_y_min, button_width, button_height = 97.5,342.5,15,15
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:
Diagonal.clicked1 = True
Diagonal.clicked2 = False
diagonal_path = True
print("click")
#check if diagonal-no is clicked
button_x_min, button_y_min, button_width, button_height = 137.5,342.5,15,15
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:
Diagonal.clicked2 = True
Diagonal.clicked1 = False
diagonal_path = False
print("click2")
#check if finish was clicked
button_x_min, button_y_min, button_width, button_height = 75,87.5,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,12.5,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,162.5,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
# check if remove WAS CLICKED
button_x_min, button_y_min, button_width, button_height = 75,237.5,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 = white
# 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
pos = pygame.mouse.get_pos()
for s in slides:
if s.button_rect.collidepoint(pos):
s.hit = True
elif event.type == pygame.MOUSEBUTTONUP:
for s in slides:
s.hit = False
# Move slides
for s in slides:
if s.hit:
grid_size = slider_loop(s,grid_width,grid_height,grid)
node_num = -1
for row in grid:
for column in row:
node_num += 1
if column[2] == green:
start_node = node_num
if column[2] == red:
finish_node = node_num
if column[2] == grey:
blocked_node = node_num
blocked_nodes.append(blocked_node)
# grid_dictionary = child_nodes(grid,diagonal_path)
# drawing
screen.fill(black)
menu = pygame.draw.rect(screen, white, [0,0,250,750])
start.draw_button()
finish.draw_button()
blocked.draw_button()
remove.draw_button()
Diagonal.draw_button()
cheesy.draw_button()
begin.draw()
load = action_button(75,462.5,100,50,green, bright_green, "load",white,load_menu)
reset = action_button(75,612.5,100,50,green, bright_green, "reset",white,lambda:reset_grid(grid))
quit_game = action_button(75,687.5,100,50, black, grey,"Quit", white, quitgame)
slider_value.draw()
print(grid_size)
print(slider_value.val)
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)
此部分开始循环并调用原始游戏循环(在更新任何值之前)
slider_value = slider("Grid Size",20,50,1,75,387.5)
slides = [slider_value]
grid_size = int(slider_value.val)
grid_margin = 5 # number of pixels between each cell
grid_width = int(700/grid_size)-grid_margin
grid_height = int(700/grid_size)-grid_margin
cell = (grid_width, grid_height)
distance_from_left = 275 # number of pixels between the grid and the left and right of the screen
distance_from_top = 25 # number of pixels between the grid and the top and bottom of the screen
# create the screen and clock
screen_size = [1000,750]
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Djikstra's and A*")
clock = pygame.time.Clock()
# logic goes here
start_node = None
finish_node = None
blocked_nodes = []
# 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(grid_size):
row = []
for x in range(grid_size):
row.append([x * (grid_width + grid_margin) + distance_from_left, y * (grid_height + grid_margin) + distance_from_top, white])
grid.append(row)
game_loop()
pygame.quit()