Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- from color_gradients import RGB_to_hex, polylinear_gradient
- square_size = 8
- board_dimension = 80
- pygame.init()
- screen_width = screen_height = board_dimension * square_size
- red = (225, 0, 0)
- purple = (55, 0, 55)
- yellow = (255, 255, 0)
- green = (0, 255, 0)
- blue = (0, 0, 255)
- magenta = (255, 0, 255)
- tan = (210, 180, 140)
- black = (0, 0, 0)
- white = (255, 255, 255)
- colorPoints = [black, magenta, blue, green, yellow, red, white]
- colorPoints.reverse()
- color_discrete_RGB_list = [RGB_to_hex(RGB) for RGB in colorPoints]
- intermediate_list = (polylinear_gradient(color_discrete_RGB_list, 36))
- colors_long_list = []
- for color_number in range(len(intermediate_list["r"])):
- RGB = (intermediate_list["r"][color_number], intermediate_list["g"][color_number], intermediate_list["b"][color_number])
- colors_long_list.append(RGB)
- print(colors_long_list)
- depth = len(colors_long_list)
- clock = pygame.time.Clock()
- crashed = False
- screen = pygame.display.set_mode((screen_width, screen_height)) # a surface object
- screen.fill(white)
- pygame.display.set_caption("Chess Knights!")
- knight_move_list = [
- [2, 1],
- [1, 2],
- [-2, 1],
- [-1, 2],
- [2, -1],
- [1, -2],
- [-2, -1],
- [-1, -2]
- ]
- def knight_moves_distance(input_x = 1, input_y = 1):
- steps_to_position = 0
- moves_explored = [[input_x, input_y]]
- squares_distance = [[[input_x, input_y], 0]]
- while steps_to_position < depth: ## Keep going until we have reach every square x moves awway
- temp_moves_list = []
- temp_distance_list = []
- steps_to_position = steps_to_position + 1
- for move in moves_explored: ## go through every square already visited
- for translation in knight_move_list: ## go through every knight move possible
- inter_varX = move[0] + translation[0] ##perform a knight move to a position, and store this new position
- inter_varY = move[1] + translation[1]
- if ([inter_varX, inter_varY] not in moves_explored):## if this position hasn't been explored, add to temp list
- temp_moves_list.append([inter_varX, inter_varY])
- temp_distance_list.append([[inter_varX, inter_varY], steps_to_position])
- for element in temp_moves_list: ## go through the temp list, and add each position if it isn't already on the list, to avoid redundancy
- if element not in moves_explored:
- moves_explored.append(element)
- for element in temp_distance_list:
- if element not in squares_distance:
- squares_distance.append(element)
- return squares_distance
- def board_draw():
- for x in range(board_dimension):
- for y in range(board_dimension):
- square_rect = (x * square_size, y * square_size, square_size, square_size)
- if (x + y) % 2 == 0:
- screen.fill(tan, square_rect)
- else:
- screen.fill(black, square_rect)
- pygame.display.update()
- def re_color_board(list):
- temp_dict = {}
- for positions in list:
- temp_dict[(positions[0][0], positions[0][1])] = positions[1] #create a dict where each key is a position and each value is the number of moves it takes to get to that position.
- print(positions)
- for x in range(board_dimension):
- for y in range(board_dimension):
- square_rect = (x * square_size, y * square_size, square_size, square_size)
- screen.fill(colors_long_list[temp_dict[(x+1, y+1)]], square_rect)
- board_draw()
- while not crashed:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- crashed = True
- if event.type == pygame.MOUSEBUTTONDOWN:
- print(event.pos)
- knight_list = knight_moves_distance(math.ceil(event.pos[0]/square_size), math.ceil(event.pos[1]/square_size))
- re_color_board(knight_list)
- distance_sum = 0
- for position in knight_list:
- distance_sum += position[1]
- print("average distance per square: {:02.4}".format(distance_sum/board_dimension**2))
- print("mobility is: {:06f}".format(1/(distance_sum/board_dimension**2)))
- pygame.display.update()
- clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement