def initialize_pos(self): self.squares = [] # List of Square objects self.length = 2 self.direction = 1 self.dead = False self.squares.append(Square(1, 1, (pg.Color('green')))) for i in range(1, self.length): self.squares.append( Square(self.squares[i - 1].x - self.squares[i - 1].side_length, 1, pg.Color('green')))
def _initialized_sprites(self): """Päivittää pelin spritet peliruudukon tietojen mukaan. """ self._sprites.empty() for y in range(self._difficulty.height()): for x in range(self._difficulty.width()): square_type = self._grid.coordinates(y, x) self._sprites.add(Square(y, x, square_type, self._square_size))
def check_win_condition(self): # The number of squares on the screen cell = Square(0, 0, pg.Color('black')) squares = int(self.display_width / cell.side_length) * int( self.display_height / cell.side_length) # Win condition: the snake fills the entire screen if self.snake.length >= squares: self.end_screen('YOU WIN')
def grow(self, n): # Grow the snake by n squares # Put new squares on the end of the snake's tail # This way, the snake gets longer only when it moves, and the end of the tail does not move for i in range(n): self.squares.append( Square(self.squares[-1].x, self.squares[-1].y, pg.Color('green'))) self.length += 1
def __init__(self): display_width, display_height = pg.display.get_surface().get_size() node = Square(0, 0, pg.Color('black')) self.row_size = int(display_width / node.side_length) self.column_size = int(display_height / node.side_length) # Initialize adjacency matrix to all 0s, indicating no connections self.graph = [[0 for x in range(self.row_size * self.row_size)] for y in range(self.row_size * self.row_size)] # Initialize cycle to bogus values, and add an extra element to make it repeatable self.cycle = [-1 for i in range(self.row_size * self.column_size + 1)]
def draw_gridlines(self): pg.display.get_surface().fill(pg.Color('white')) cell = Square(0, 0, pg.Color('black')) squares = int(self.display_width / cell.side_length) for x in range(squares): for y in range(squares): cell.x = x * (cell.side_length + 1) + 1 cell.y = y * (cell.side_length + 1) + 1 cell.draw()
def init_game(self): pg.display.get_surface().fill(pg.Color('white')) self.current_input = 0 self.snake.initialize_pos() self.apple.spawn() # Make one thread to display a loading screen, one to generate a cycle, and poll for events on the main thread load = threading.Thread(target=self.load_screen) load.start() # Find a Hamiltonian cycle around the screen (starting at the snake's starting position) and generate an input list to allow the snake to follow it self.graph.init_graph() start_node = Square(0, 0, pg.Color('black')) graph_start = int(start_node.x + start_node.y / start_node.side_length) cycle = threading.Thread(target=self.graph.hamiltonian_cycle, args=(graph_start, ), daemon=True) cycle.start() # Keep event polling on the main thread, rather than the loading screen, to keep the window responsive while cycle.is_alive(): # Because the loading function depends on pygame, that thread must be terminated before exiting for event in pg.event.get(): if event.type == pg.QUIT: self.finished_loading = True load.join() pg.quit() sys.exit(0) cycle.join() # The input list should be generated quickly enough that the window remains responsive self.generate_input_list() self.finished_loading = True load.join()
def _build_board(self, board_size): self._squares = [Square(name=i, board=self) for i in range(board_size)] self.start_square = self._squares[0]