Advertisement
tengely03

Untitled

May 28th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. import pygame
  2. from copy import deepcopy
  3.  
  4.  
  5. def read_table_from_file(board_basic, file_name):
  6. with open(file_name, "r") as F:
  7. for i in range(len(board_basic)):
  8. for j in range(len(board_basic[i])):
  9. board_basic[i][j] = int(F.readline())
  10.  
  11.  
  12. def click(board, pos):
  13. """
  14. :param: pos
  15. :return: (row, col)
  16. """
  17. width = 270
  18. height = 270
  19. if pos[0] < width and pos[1] < height:
  20. gap = width / 9
  21. x = (pos[0] - 10) // gap
  22. y = (pos[1] - 10) // gap
  23. print(y, x)
  24. return (int(y), int(x))
  25. else:
  26. return None
  27.  
  28.  
  29. pygame.init()
  30.  
  31. BLACK = (0, 0, 0)
  32. WHITE = (255, 255, 255, 255)
  33. BLUE = (0, 0, 255)
  34. GREEN = (0, 255, 0, 100)
  35. RED = (255, 0, 0)
  36.  
  37. size2 = (30, 30)
  38. green_rect = pygame.Surface(size2, pygame.SRCALPHA)
  39. white_rect = pygame.Surface(size2, pygame.SRCALPHA)
  40.  
  41. pygame.draw.rect(green_rect, GREEN, green_rect.get_rect())
  42. pygame.draw.rect(white_rect, WHITE, white_rect.get_rect())
  43.  
  44.  
  45.  
  46. size = [600, 600]
  47. screen = pygame.display.set_mode(size)
  48. pygame.display.set_caption('$ sudo ku.py')
  49.  
  50. done = False
  51. clock = pygame.time.Clock()
  52. screen.fill(WHITE)
  53. font = pygame.font.SysFont("Arial", 20)
  54.  
  55. text = font.render("1", True, BLACK)
  56. textRect = text.get_rect()
  57. textRect.center = (25, 25)
  58. text2 = font.render("3", True, BLACK)
  59. textRect2 = text2.get_rect()
  60. textRect2.center = (55, 55)
  61. key = 0
  62. indices = None
  63. pos = (0, 0)
  64. rows = 9
  65. columns = 9
  66.  
  67. number_coordinates = [[0 for x in range(columns)] for y in range(rows)]
  68. grid = [[0 for x in range(columns)] for y in range(rows)]
  69.  
  70.  
  71.  
  72. read_table_from_file(grid, "sudoku_easy.txt")
  73. basic_grid = deepcopy(grid)
  74. #print(basic_grid)
  75.  
  76. x = -5
  77. y = 25
  78.  
  79. for j in range(columns):
  80. x += 30
  81. y = 25
  82. for i in range(rows):
  83. number_coordinates[i][j] = (x, y)
  84. y += 30
  85.  
  86. flag = False
  87. selected = False
  88.  
  89. while not done:
  90.  
  91. # This limits the while loop to a max of 10 times per second.
  92. # Leave this out and we will use all CPU we can.
  93. clock.tick(10)
  94. screen.fill(WHITE)
  95.  
  96.  
  97. for event in pygame.event.get(): # User did something
  98. if event.type == pygame.QUIT: # If user clicked close
  99. done = True # Flag that we are done so we exit this loop
  100. if event.type == pygame.KEYDOWN:
  101. if event.key == pygame.K_1:
  102. key = 1
  103. if event.key == pygame.K_2:
  104. key = 2
  105. if event.key == pygame.K_3:
  106. key = 3
  107. if event.key == pygame.K_4:
  108. key = 4
  109. if event.key == pygame.K_5:
  110. key = 5
  111. if event.key == pygame.K_6:
  112. key = 6
  113. if event.key == pygame.K_7:
  114. key = 7
  115. if event.key == pygame.K_8:
  116. key = 8
  117. if event.key == pygame.K_9:
  118. key = 9
  119. if event.key == pygame.K_DELETE:
  120. key = 0
  121. if event.type == pygame.MOUSEBUTTONDOWN:
  122. pos = pygame.mouse.get_pos()
  123. indices = click(grid, pos)
  124. # if grid[indices[0]][indices[1]] == 0:
  125. # screen.blit(white_rect, ((number_coordinates[indices[0]][indices[1]][0] - 15), (number_coordinates[indices[0]][indices[1]][1] - 15), 30, 30))
  126.  
  127. if event.button == 1 and basic_grid[indices[0]][indices[1]] == 0:
  128. selected = True
  129. key = None
  130. # elif event.button == 2:
  131. # key = 0
  132. #grid[indices[0]][indices[1]] = key
  133.  
  134. if indices and key != None:
  135. if basic_grid[indices[0]][indices[1]] == 0 :
  136. grid[indices[0]][indices[1]] = key
  137. selected = False
  138. #screen.blit(white_rect, ((number_coordinates[indices[0]][indices[1]][0] - 15), (number_coordinates[indices[0]][indices[1]][1] - 15), 30, 30))
  139. if grid[indices[0]][indices[1]] != 0:
  140. text = font.render(str(grid[indices[0]][indices[1]]), True, BLACK)
  141. textRect = text.get_rect()
  142. textRect.center = number_coordinates[indices[0]][indices[1]]
  143. screen.blit(text, textRect)
  144. selected = False
  145.  
  146. x = [10, 10]
  147. y = [10, 280]
  148. for i in range(10):
  149. if i % 3 == 0:
  150. depth = 3
  151. else:
  152. depth = 1
  153. pygame.draw.line(screen, BLACK, x, y, depth)
  154. x[0] = x[0] + 30
  155. y[0] = y[0] + 30
  156. x = [10, 10]
  157. y = [280, 10]
  158. for i in range(10):
  159. if i % 3 == 0:
  160. depth = 3
  161. else:
  162. depth = 1
  163. pygame.draw.line(screen, BLACK, x, y, depth)
  164. x[1] = x[1] + 30
  165. y[1] = y[1] + 30
  166. if flag == False:
  167. for i in range(columns):
  168. for j in range(rows):
  169. if grid[i][j] != 0:
  170. text = font.render(str(grid[i][j]), True, BLACK)
  171. textRect = text.get_rect()
  172. textRect.center = number_coordinates[i][j]
  173. screen.blit(text, textRect)
  174. if selected:
  175. screen.blit(green_rect, ((number_coordinates[indices[0]][indices[1]][0] - 15), (number_coordinates[indices[0]][indices[1]][1] - 15), 30, 30))
  176.  
  177. flag = False
  178. #screen.fill(WHITE)
  179. pygame.display.update()
  180. #pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement