Advertisement
Guest User

Untitled

a guest
Apr 6th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. """
  2. When you start the game, it starts out in a random state.
  3.  
  4. When you click on a button with the mouse, the button you click and all the surrounding buttons toggle on or off, opposite of their current state.
  5.  
  6. """
  7. import pygame
  8. import time
  9. import random
  10.  
  11. screen = pygame.display.set_mode((640, 480))
  12.  
  13. dimensions = 4
  14. x_start = 120
  15. y_start = 40
  16. board_width = 400
  17. block_width = (board_width - dimensions - 1) / dimensions
  18.  
  19. def toggle(grid, row, column):
  20. def toggle_cell(grid, row, column):
  21. grid[row][column] = not grid[row][column]
  22. return grid[row][column]
  23. def toggle_surroundings(grid, row, column):
  24. surroundings = [
  25. [row - 1, column],
  26. [row + 1, column],
  27. [row, column - 1],
  28. [row, column + 1],
  29. ]
  30. for coord in surroundings:
  31. if coord[0] != -1 and coord[1] != -1:
  32. if coord[0] < len(grid) and coord[1] < len(grid):
  33. toggle_cell(grid, coord[0], coord[1])
  34. toggle_surroundings(grid, row, column)
  35. toggle_cell(grid, row, column)
  36.  
  37. def check_solved(grid):
  38. solved = True
  39. row = 0
  40. while row < len(grid):
  41. column = 0
  42. while column < len(grid):
  43. if grid[row][column]:
  44. solved = False
  45. column += 1
  46. row += 1
  47. return solved
  48.  
  49. def setup_board(dimensions):
  50. #create grid in solveable state
  51. grid = []
  52. for each in range(dimensions):
  53. grid.append([False]*len(range(dimensions)))
  54. grid[0][0] = True
  55. grid[0][2] = True
  56. grid[1][0] = True
  57.  
  58. #iterate to create puzzle state - not sure if this is crash-proof or not
  59. i = 0
  60. while i < 20:
  61. if check_solved(grid) == True:
  62. setup_board(dimensions)
  63. list_of_Trues = []
  64. row = 0
  65. while row < len(grid):
  66. column = 0
  67. while column < len(grid[0]):
  68. if grid[row][column]:
  69. list_of_Trues.append([row, column])
  70. column += 1
  71. row += 1
  72. pair = random.choice(list_of_Trues)
  73. toggle(grid, pair[0], pair[1])
  74. i += 1
  75. return grid
  76.  
  77. def draw_board(dimensions, x_start, y_start, board_width):
  78. #board frame
  79. pygame.draw.rect(screen, (123,123,123), pygame.Rect(x_start, y_start, board_width, board_width), 3)
  80.  
  81. #inside lines
  82. i = 1
  83. while i <= dimensions:
  84. vertical_x1 = i + x_start + i * block_width + 2
  85. vertical_y1 = y_start
  86. vertical_x2 = i + x_start + i * block_width + 2
  87. vertical_y2 = y_start + board_width
  88. horizontal_x1 = x_start
  89. horizontal_y1 = i + y_start + i * block_width + 2
  90. horizontal_x2 = x_start + board_width
  91. horizontal_y2 = i + y_start + i * block_width + 2
  92. pygame.draw.line(screen, (123,123,123), (vertical_x1, vertical_y1), (vertical_x2, vertical_y2))
  93. pygame.draw.line(screen, (123,123,123), (horizontal_x1, horizontal_y1), (horizontal_x2, horizontal_y2))
  94. i += 1
  95.  
  96. def light_on(grid, x_start, y_start, block_width): #double check arg names
  97. row = 0
  98. while row < len(grid):
  99. column = 0
  100. while column < len(grid[0]):
  101. x_coord = row + x_start + row * block_width + 2 - 80
  102. y_coord = column + y_start + column * block_width + 2 + 80
  103. if grid[row][column]:
  104. pygame.draw.rect(screen, (0, 255, 0), pygame.Rect(y_coord, x_coord, block_width, block_width)) # not sure why y_coord and x_coord seem backwards
  105. else:
  106. pygame.draw.rect(screen, (0, 80, 0), pygame.Rect(y_coord, x_coord, block_width, block_width)) # not sure why y_coord and x_coord seem backwards
  107. column += 1
  108. row += 1
  109.  
  110. #==================================================
  111. def playgame():
  112. pygame.init()
  113.  
  114. done = False
  115. fps = 30
  116.  
  117. #set up function(s)
  118. original_grid = setup_board(dimensions)
  119. current_grid = original_grid
  120.  
  121. while not done:
  122.  
  123. start = time.time()
  124.  
  125. # Event Phase (plus Update Phase - not sure how to separate out)
  126.  
  127. for event in pygame.event.get():
  128. if event.type == pygame.QUIT:
  129. done = True
  130. if event.type == pygame.KEYDOWN:
  131. if event.key == pygame.K_Q:
  132. done = True
  133. if event.key == pygame.K_N:
  134. done = True
  135. if event.type == pygame.MOUSEBUTTONDOWN:
  136. mouse_x, mouse_y = pygame.mouse.get_pos()
  137. cell_x = (mouse_x - x_start - 2) / (1+ block_width)
  138. cell_y = (mouse_y - y_start - 2) / (1+ block_width)
  139. if -1 < cell_x < dimensions and -1 < cell_y < dimensions:
  140. toggle(current_grid, cell_y, cell_x) #note: cell_x corresponds to column; cell_y corresponds to row
  141.  
  142. # Render Phase
  143. screen.fill((0,0,0))
  144.  
  145. draw_board(dimensions, x_start, y_start, board_width)
  146. light_on(current_grid, x_start, y_start, block_width)
  147.  
  148. pygame.display.flip()
  149.  
  150. # Sleep Phase
  151.  
  152. end = time.time()
  153.  
  154. diff = end - start
  155. if diff < 1.0 / fps:
  156. delay = 1.0 / fps - diff
  157. time.sleep(delay)
  158.  
  159. playgame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement