Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from copy import deepcopy
- def read_table_from_file(board_basic, file_name):
- with open(file_name, "r") as F:
- for i in range(len(board_basic)):
- for j in range(len(board_basic[i])):
- board_basic[i][j] = int(F.readline())
- def click(board, pos):
- """
- :param: pos
- :return: (row, col)
- """
- width = 270
- height = 270
- if pos[0] < width and pos[1] < height:
- gap = width / 9
- x = (pos[0] - 10) // gap
- y = (pos[1] - 10) // gap
- print(y, x)
- return (int(y), int(x))
- else:
- return None
- pygame.init()
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255, 255)
- BLUE = (0, 0, 255)
- GREEN = (0, 255, 0, 100)
- RED = (255, 0, 0)
- size2 = (30, 30)
- green_rect = pygame.Surface(size2, pygame.SRCALPHA)
- white_rect = pygame.Surface(size2, pygame.SRCALPHA)
- pygame.draw.rect(green_rect, GREEN, green_rect.get_rect())
- pygame.draw.rect(white_rect, WHITE, white_rect.get_rect())
- size = [600, 600]
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('$ sudo ku.py')
- done = False
- clock = pygame.time.Clock()
- screen.fill(WHITE)
- font = pygame.font.SysFont("Arial", 20)
- text = font.render("1", True, BLACK)
- textRect = text.get_rect()
- textRect.center = (25, 25)
- text2 = font.render("3", True, BLACK)
- textRect2 = text2.get_rect()
- textRect2.center = (55, 55)
- key = 0
- indices = None
- pos = (0, 0)
- rows = 9
- columns = 9
- number_coordinates = [[0 for x in range(columns)] for y in range(rows)]
- grid = [[0 for x in range(columns)] for y in range(rows)]
- read_table_from_file(grid, "sudoku_easy.txt")
- basic_grid = deepcopy(grid)
- #print(basic_grid)
- x = -5
- y = 25
- for j in range(columns):
- x += 30
- y = 25
- for i in range(rows):
- number_coordinates[i][j] = (x, y)
- y += 30
- flag = False
- selected = False
- while not done:
- # This limits the while loop to a max of 10 times per second.
- # Leave this out and we will use all CPU we can.
- clock.tick(10)
- screen.fill(WHITE)
- for event in pygame.event.get(): # User did something
- if event.type == pygame.QUIT: # If user clicked close
- done = True # Flag that we are done so we exit this loop
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_1:
- key = 1
- if event.key == pygame.K_2:
- key = 2
- if event.key == pygame.K_3:
- key = 3
- if event.key == pygame.K_4:
- key = 4
- if event.key == pygame.K_5:
- key = 5
- if event.key == pygame.K_6:
- key = 6
- if event.key == pygame.K_7:
- key = 7
- if event.key == pygame.K_8:
- key = 8
- if event.key == pygame.K_9:
- key = 9
- if event.key == pygame.K_DELETE:
- key = 0
- if event.type == pygame.MOUSEBUTTONDOWN:
- pos = pygame.mouse.get_pos()
- indices = click(grid, pos)
- # if grid[indices[0]][indices[1]] == 0:
- # screen.blit(white_rect, ((number_coordinates[indices[0]][indices[1]][0] - 15), (number_coordinates[indices[0]][indices[1]][1] - 15), 30, 30))
- if event.button == 1 and basic_grid[indices[0]][indices[1]] == 0:
- selected = True
- key = None
- # elif event.button == 2:
- # key = 0
- #grid[indices[0]][indices[1]] = key
- if indices and key != None:
- if basic_grid[indices[0]][indices[1]] == 0 :
- grid[indices[0]][indices[1]] = key
- selected = False
- #screen.blit(white_rect, ((number_coordinates[indices[0]][indices[1]][0] - 15), (number_coordinates[indices[0]][indices[1]][1] - 15), 30, 30))
- if grid[indices[0]][indices[1]] != 0:
- text = font.render(str(grid[indices[0]][indices[1]]), True, BLACK)
- textRect = text.get_rect()
- textRect.center = number_coordinates[indices[0]][indices[1]]
- screen.blit(text, textRect)
- selected = False
- x = [10, 10]
- y = [10, 280]
- for i in range(10):
- if i % 3 == 0:
- depth = 3
- else:
- depth = 1
- pygame.draw.line(screen, BLACK, x, y, depth)
- x[0] = x[0] + 30
- y[0] = y[0] + 30
- x = [10, 10]
- y = [280, 10]
- for i in range(10):
- if i % 3 == 0:
- depth = 3
- else:
- depth = 1
- pygame.draw.line(screen, BLACK, x, y, depth)
- x[1] = x[1] + 30
- y[1] = y[1] + 30
- if flag == False:
- for i in range(columns):
- for j in range(rows):
- if grid[i][j] != 0:
- text = font.render(str(grid[i][j]), True, BLACK)
- textRect = text.get_rect()
- textRect.center = number_coordinates[i][j]
- screen.blit(text, textRect)
- if selected:
- screen.blit(green_rect, ((number_coordinates[indices[0]][indices[1]][0] - 15), (number_coordinates[indices[0]][indices[1]][1] - 15), 30, 30))
- flag = False
- #screen.fill(WHITE)
- pygame.display.update()
- #pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement