Pygame - игра жизни, выбирающая несколько ячеек в сетке - PullRequest
0 голосов
/ 25 мая 2019

Я работаю над игрой в пигмейм из жизни. Я могу вручную активировать или деактивировать ячейки, щелкая по ним, но я хочу добавить функцию для рисования на сетке (активировать несколько ячеек, щелкнув один раз, а затем переместив курсор мыши). Теперь, щелкнув левой кнопкой мыши, вы можете вручную выбрать ячейки, и я хочу назначить функцию рисования правой кнопке мыши. Я разделил щелчки мыши, установив кнопку события на 1 или 3. Я не могу понять, как он может активировать несколько ячеек, наводя на них курсор после щелчка правой кнопкой мыши. Я думал, что мне нужен цикл while (пока нажата кнопка, сделайте это ..), но он активировал только первую ячейку (ту, на которую вы нажимаете). Любые предложения о том, как это исправить? Часть кода добавлена ​​ниже.

    # Runs the game loop
    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.game_over = True  
            if event.type == pygame.MOUSEBUTTONDOWN:  
                posn = pygame.mouse.get_pos()
                x = int(posn[0] / CELL_SIZE)
                y = int(posn[1] / CELL_SIZE)
                print(x,y)
                if event.button == 1: #Left click is 1, right click is 3.
                    if next_generation[x][y] == COLOR_DEAD:
                        self.activate_living_cell(x, y)
                    else:
                        self.deactivate_living_cell(x, y)
                elif event.button == 3:
                        self.activate_living_cell(x, y)

            if event.type == pygame.KEYDOWN:  
                if event.unicode == 'q':  # Press q to quit.
                    self.game_over = True
                    print("q")

                elif event.key == pygame.K_SPACE:  # Space for the next iteration manually.
                    self.create_next_gen()
                    print("keypress")
                elif event.unicode == 'a':  # a to automate the iterations.
                    self.next_iteration = True
                    print("a")
                elif event.unicode == 's':  # s to stop the automated iterations.
                    self.next_iteration = False
                    print("s")
                elif event.unicode == 'r':  # r to reset the grid.
                    self.next_iteration = False
                    self.init_gen(next_generation, COLOR_DEAD)
                    print("r")

    def run(self):
        while not self.game_over:
            # Set the frames per second.
            self.handle_events()
            if self.next_iteration:  # if next iteration is true, the next gen is created according to the rules.
                self.create_next_gen()
            # Updating
            self.update_gen()
            pygame.display.flip()
            self.FPSCLOCK.tick(fps_max)
            self.root.update()

if __name__ == "__main__":
    game = GameOfLife()
    game.run()
...