Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- When you start the game, it starts out in a random state.
- 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.
- """
- import pygame
- import time
- import random
- screen = pygame.display.set_mode((640, 480))
- dimensions = 4
- x_start = 120
- y_start = 40
- board_width = 400
- block_width = (board_width - dimensions - 1) / dimensions
- def toggle(grid, row, column):
- def toggle_cell(grid, row, column):
- grid[row][column] = not grid[row][column]
- return grid[row][column]
- def toggle_surroundings(grid, row, column):
- surroundings = [
- [row - 1, column],
- [row + 1, column],
- [row, column - 1],
- [row, column + 1],
- ]
- for coord in surroundings:
- if coord[0] != -1 and coord[1] != -1:
- if coord[0] < len(grid) and coord[1] < len(grid):
- toggle_cell(grid, coord[0], coord[1])
- toggle_surroundings(grid, row, column)
- toggle_cell(grid, row, column)
- def check_solved(grid):
- solved = True
- row = 0
- while row < len(grid):
- column = 0
- while column < len(grid):
- if grid[row][column]:
- solved = False
- column += 1
- row += 1
- return solved
- def setup_board(dimensions):
- #create grid in solveable state
- grid = []
- for each in range(dimensions):
- grid.append([False]*len(range(dimensions)))
- grid[0][0] = True
- grid[0][2] = True
- grid[1][0] = True
- #iterate to create puzzle state - not sure if this is crash-proof or not
- i = 0
- while i < 20:
- if check_solved(grid) == True:
- setup_board(dimensions)
- list_of_Trues = []
- row = 0
- while row < len(grid):
- column = 0
- while column < len(grid[0]):
- if grid[row][column]:
- list_of_Trues.append([row, column])
- column += 1
- row += 1
- pair = random.choice(list_of_Trues)
- toggle(grid, pair[0], pair[1])
- i += 1
- return grid
- def draw_board(dimensions, x_start, y_start, board_width):
- #board frame
- pygame.draw.rect(screen, (123,123,123), pygame.Rect(x_start, y_start, board_width, board_width), 3)
- #inside lines
- i = 1
- while i <= dimensions:
- vertical_x1 = i + x_start + i * block_width + 2
- vertical_y1 = y_start
- vertical_x2 = i + x_start + i * block_width + 2
- vertical_y2 = y_start + board_width
- horizontal_x1 = x_start
- horizontal_y1 = i + y_start + i * block_width + 2
- horizontal_x2 = x_start + board_width
- horizontal_y2 = i + y_start + i * block_width + 2
- pygame.draw.line(screen, (123,123,123), (vertical_x1, vertical_y1), (vertical_x2, vertical_y2))
- pygame.draw.line(screen, (123,123,123), (horizontal_x1, horizontal_y1), (horizontal_x2, horizontal_y2))
- i += 1
- def light_on(grid, x_start, y_start, block_width): #double check arg names
- row = 0
- while row < len(grid):
- column = 0
- while column < len(grid[0]):
- x_coord = row + x_start + row * block_width + 2 - 80
- y_coord = column + y_start + column * block_width + 2 + 80
- if grid[row][column]:
- 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
- else:
- 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
- column += 1
- row += 1
- #==================================================
- def playgame():
- pygame.init()
- done = False
- fps = 30
- #set up function(s)
- original_grid = setup_board(dimensions)
- current_grid = original_grid
- while not done:
- start = time.time()
- # Event Phase (plus Update Phase - not sure how to separate out)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_Q:
- done = True
- if event.key == pygame.K_N:
- done = True
- if event.type == pygame.MOUSEBUTTONDOWN:
- mouse_x, mouse_y = pygame.mouse.get_pos()
- cell_x = (mouse_x - x_start - 2) / (1+ block_width)
- cell_y = (mouse_y - y_start - 2) / (1+ block_width)
- if -1 < cell_x < dimensions and -1 < cell_y < dimensions:
- toggle(current_grid, cell_y, cell_x) #note: cell_x corresponds to column; cell_y corresponds to row
- # Render Phase
- screen.fill((0,0,0))
- draw_board(dimensions, x_start, y_start, board_width)
- light_on(current_grid, x_start, y_start, block_width)
- pygame.display.flip()
- # Sleep Phase
- end = time.time()
- diff = end - start
- if diff < 1.0 / fps:
- delay = 1.0 / fps - diff
- time.sleep(delay)
- playgame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement