生命游戏 - 覆盖当前一代而不是更新到下一代

2024-02-20

下面我添加了我的生命游戏代码。规则定义正确,并且运行顺利。然而,游戏并没有按预期进行。它不是更新到下一代,但它似乎正在覆盖当前一代。举个例子:三个水平点应该在下一代变成三个垂直点,但这并没有发生。

解决方案: 我有两代人,当代的和下一代的。它必须将规则应用于当前一代并在下一代中更新它们。然后它必须一次性用下一代覆盖当前一代,而不是逐个单元。我怎样才能解决这个问题?

import tkinter as tk
import itertools, os, platform, pygame, random

# Defining the grid dimensions.
GRID_SIZE = WIDTH, HEIGHT = 750, 1000

# Defining the cell size and the number of cells in the X and Y direction.
CELL_SIZE = 10
X_CELLS = int(WIDTH/CELL_SIZE)
Y_CELLS = int(HEIGHT/CELL_SIZE)

# Defining the number and color for dead and living cells.
COLOR_DEAD = 0
COLOR_ALIVE = 1
colors = []
colors.append((0, 0, 0))  # Black
colors.append((0, 128, 128))  # blue

# Defining two lists: current generation and next generation.
current_generation = [[COLOR_DEAD for y in range(Y_CELLS)] for x in range(X_CELLS)]
next_generation = [[COLOR_DEAD for y in range(Y_CELLS)] for x in range(X_CELLS)]

# Defining the max frames per second/speed of the game.
FPS_MAX = 10

class GameOfLife:
    """
    describe what the method does.
    """
    def __init__(self):
        # Initializing the interpreter and creating a root window and title.
        self.root = tk.Tk()
        self.root.title("Game of Life - Created by - Have fun")
        # Defining the main frame, left-side frame and right-side frame.
        self.frame = tk.Frame(self.root , width=1000, height=1000, highlightbackground='red')
        self.menu = tk.Frame(self.frame, width=250, height=1000, highlightbackground='#595959', highlightthickness=10)
        self.game_border = tk.Frame(self.frame, width=750, height=1000, highlightbackground='green', highlightthickness=10)
        # Packing the windows.
        self.frame.pack()
        self.frame.pack_propagate(0)
        self.menu.pack(side="left")
        self.menu.pack_propagate(0)
        self.game_border.pack()

        # Defining the buttons.
        self.button_start = tk.Button(self.menu, text="Start", height=5, width=20, fg="black", activeforeground="red", background="grey80", activebackground="grey80", command=self.start_button)
        self.button_stop = tk.Button(self.menu, text="Stop", height=5, width=20, fg="black", activeforeground="red", background="grey80", activebackground="grey80", command=self.stop_button)
        self.button_iteration = tk.Button(self.menu, text="Next iteration", height=5, width=20, fg="black", activeforeground="red", background="grey80", activebackground="grey80", command=self.create_next_gen)
        self.button_random = tk.Button(self.menu, text="Random", height=5, width=20, fg="black", activeforeground="red", background="grey80", activebackground="grey80", command=self.random_grid)
        self.button_reset = tk.Button(self.menu, text="Reset", height=5, width=20, fg="black", activeforeground="red", background="grey80", activebackground="grey80", command=self.reset_button)
        self.button_quit = tk.Button(self.menu, text="Quit", height=5, width=20, fg="black", activeforeground="red", background="grey80", activebackground="grey80", command=self.quit_button)
        # Packing the buttons.
        self.button_start.pack()
        self.button_stop.pack()
        self.button_iteration.pack()
        self.button_random.pack()
        self.button_reset.pack()
        self.button_quit.pack()
        # Placing the buttons.
        self.button_start.place(x=40, y=50)
        self.button_stop.place(x=40, y=200)
        self.button_iteration.place(x=40, y=350)
        self.button_random.place(x=40, y=500)
        self.button_reset.place(x=40, y=650)
        self.button_quit.place(x=40, y=800)

        # Defining the slider.
        self.slider_random = tk.Scale(self.menu, from_=0, to=100, orient="horizontal", command=self.slider_value)
        self.slider_random.set(50)
        # Packing the slider.
        self.slider_random.pack()
        # Placing the slider.
        self.slider_random.place(x=62, y=590)

        # Defining a dropdown menu for the form and color.
        """
        self.options_figures = [
            "circles",
            "squares",
            "surprise"
        ]
        self.var_figure = tk.StringVar(self.root)
        self.dropdown_figure = tk.OptionMenu(self.menu, self.var_figure,
                                             self.options_figures[0], self.options_figures[1],
                                             self.options_figures[2])
        self.var_figure.set(self.options_figures[0])
        #self.var_color.trace("w", FUNCTIONNAME)
        self.dropdown_figure.pack()

        # Dropdown menu for the cell color
        self.options_colors = [
            "blue",
            "red",
            "white",
            "green",
            "yellow",
            "purple",
            "grey",
            "pink"
        ]
        self.var_color = tk.StringVar(self.root)
        self.dropdown_colors = tk.OptionMenu(self.menu, self.var_color,
                                             self.options_colors[0], self.options_colors[1],
                                             self.options_colors[2], self.options_colors[3],
                                             self.options_colors[4], self.options_colors[5],
                                             self.options_colors[6], self.options_colors[7])
        self.var_color.set(self.options_colors[0])
        #self.var_color.trace("w", FUNCTION NAME)
        self.dropdown_colors.pack()
        """

        # Defining the labels that count the dead and living cells.
        """
        self.label_alive = tk.Label(self.menu, text="Living cells:"+" 1000", height=5, width=20, fg="black", background="grey80")
        self.label_dead = tk.Label(self.menu, text="Dead cells"+" 1000", height=1, width=20, fg="black", background="grey80")
        Packing the labels
        self.label_alive.pack()
        self.label_dead.pack()
        self.label_alive.place(x=40, y=900)
        self.label_alive.place(x=40, y=900)
        """

        # This embeds the pygame window in the tkinter frame.
        os.environ['SDL_WINDOWID'] = str(self.game_border.winfo_id())
        system = platform.system()
        if system == "Windows":
            os.environ['SDL_VIDEODRIVER'] = 'windib'
        elif system == "Linux":
            os.environ['SDL_VIDEODRIVER'] = 'x11'

        # Initializing pygame.
        pygame.init()
        self.screen = pygame.display.set_mode(GRID_SIZE)
        # Initializing the generations.
        self.init_gen(current_generation, COLOR_DEAD)
        # Defining a clock to set the FPS.
        self.fps_clock = pygame.time.Clock()
        # Setting variables for later use.
        self.next_iteration = False
        self.game_over = False

    # Get the slider value to change the % of randomness.
    def slider_value(self, value):
        self.value = value

    # Button functions.
    def start_button(self):
        self.next_iteration = True
    def stop_button(self):
        self.next_iteration = False
    def reset_button(self):
        self.next_iteration = False
        self.init_gen(next_generation, COLOR_DEAD)
    def quit_button(self):
        self.game_over = True

    # Initializing all the cells.
    def init_gen(self, generation, c):
        for y in range(Y_CELLS):
            for x in range(X_CELLS):
                generation[x][y] = c

    # Creates a random grid based on the slider value.
    def random_grid(self):
        self.next_iteration = False
        self.init_gen(next_generation, COLOR_DEAD)
        self.percentage_zero = list(itertools.repeat(0,
                                                     (100 - self.slider_random.get())))
        self.percentage_one = list(itertools.repeat(1,
                                                    (self.slider_random.get())))
        # print(self.percentage_zero)
        # print(self.percentage_one)
        for row in range(X_CELLS):
            for col in range(Y_CELLS):
                next_generation[row][col] = random.choice(self.percentage_zero + self.percentage_one)
                # print(next_generation[row][col])

    # Drawing the cells, color black or blue at location (x,y).
    def draw_cell(self, x, y, c):
        pos = (int(x * CELL_SIZE + CELL_SIZE / 2),
               int(y * CELL_SIZE + CELL_SIZE / 2))
        # pygame.draw.rect(screen, colors[c], pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE-1, CELL_SIZE-1))
        # pygame.draw.circle(screen, colors[c], pos, CELL_SIZE, CELL_SIZE) #Weird form, can also be used instead of rectangles
        pygame.draw.circle(self.screen, colors[c], pos, 5, 0)

    # Updating the cells in the current generation.
    def update_gen(self):
        global current_generation
        for y in range(Y_CELLS):
            for x in range(X_CELLS):
                c = next_generation[x][y]
                self.draw_cell(x, y, c)
        current_generation = list(next_generation)

    # Activate a living cell.
    def activate_living_cell(self, x, y):
        global next_generation
        next_generation[x][y] = COLOR_ALIVE

    # Deactivate a living cell.
    def deactivate_living_cell(self, x, y):
        global next_generation
        next_generation[x][y] = COLOR_DEAD

    # Function to check neighbor cells.
    def check_cells(self, x, y):
        # Check the edges.
        if (x < 0) or (y < 0):
            return 0
        if (x >= X_CELLS) or (y >= Y_CELLS):
            return 0
        if current_generation[x][y] == COLOR_ALIVE:
            return 1
        else:
            return 0

    def check_cell_neighbors(self, row_index, col_index):
        # Get the number of alive cells surrounding the current cell.
        num_alive_neighbors = 0
        num_alive_neighbors += self.check_cells(row_index - 1, col_index - 1)
        num_alive_neighbors += self.check_cells(row_index - 1, col_index)
        num_alive_neighbors += self.check_cells(row_index - 1, col_index + 1)
        num_alive_neighbors += self.check_cells(row_index, col_index - 1)
        num_alive_neighbors += self.check_cells(row_index, col_index + 1)
        num_alive_neighbors += self.check_cells(row_index + 1, col_index - 1)
        num_alive_neighbors += self.check_cells(row_index + 1, col_index)
        num_alive_neighbors += self.check_cells(row_index + 1, col_index + 1)
        return num_alive_neighbors

    # Rules:
    # 1 Any live cell with fewer than two live neighbors dies, as if by underpopulation.
    # 2 Any live cell with two or three live neighbors lives on to the next generation.
    # 3 Any live cell with more than three live neighbors dies, as if by overpopulation.
    # 4 Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
    def create_next_gen(self):
        for y in range(Y_CELLS):
            for x in range(X_CELLS):
                n = self.check_cell_neighbors(x, y)  # Number of neighbors.
                c = current_generation[x][y]  # Current cell (either dead or alive).
                if c == COLOR_ALIVE:
                    if (n < 2):  # Rule number 1.
                        next_generation[x][y] = COLOR_DEAD
                    elif (n > 3):  # Rule number 3.
                        next_generation[x][y] = COLOR_DEAD
                    else:  # Rule number 2.
                        next_generation[x][y] = COLOR_ALIVE
                elif c == COLOR_DEAD:
                    if (n == 3):  # Rule number 4.
                        next_generation[x][y] = COLOR_ALIVE
                    else:
                        next_generation[x][y] = COLOR_DEAD
#Problem: first counting, then next iteration.

    # Defines button and mouse clicks.
    def handle_events(self):
        for event in pygame.event.get():
            # Turns the mouse position into a position in the grid.
            posn = pygame.mouse.get_pos()
            x = int(posn[0] / CELL_SIZE)
            y = int(posn[1] / CELL_SIZE)
            # Pressing quit --> quit the game.
            if event.type == pygame.QUIT:
                self.game_over = True
            # Pressing the left mouse button to activate or deactivate a cell.
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if next_generation[x][y] == COLOR_DEAD:
                        self.activate_living_cell(x, y)
                    else:
                        self.deactivate_living_cell(x, y)
            # Keeping the right mouse button pressed activates drawing mode.
            if event.type == pygame.MOUSEMOTION and event.buttons[2]:
                self.activate_living_cell(x, y)

            # Define the keyboard key presses for q, space, a, s, r.
            if event.type == pygame.KEYDOWN:
                # Quit the game.
                if event.unicode == 'q':
                    self.game_over = True
                    print("q")
                # Next iteration - manually.
                elif event.key == pygame.K_SPACE:
                    self.create_next_gen()
                    print("keypress")
                # Next iteration - automated.
                elif event.unicode == 'a':  # a to automate the iterations.
                    self.next_iteration = True
                    print("a")
                # Stop the automated iterations.
                elif event.unicode == 's':
                    self.next_iteration = False
                    print("s")
                # Empty the grid.
                elif event.unicode == 'r':
                    self.next_iteration = False
                    self.init_gen(next_generation, COLOR_DEAD)
                    print("r")

    # Runs the game loop
    def run(self):
        while not self.game_over:
            self.handle_events()
            if self.next_iteration:
                self.create_next_gen()
            self.update_gen()
            pygame.display.flip()
            self.fps_clock.tick(FPS_MAX)
            self.root.update()

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

你必须深复制next_generation to current_generation. But

current_generation = list(next_generation)

没有做你期望它做的事情,因为next_generation也是一个列表。

要深度复制每个元素都是数字列表的列表(不会复制对象),您必须:

current_generation = [list(e) for e in next_generation]

or

current_generation = [[i for i in j] for j in next_generation]

or

current_generation = [e[:] for e in next_generation]

由于方法中存在嵌套循环GameOfLife.update_gen,这个问题也可以通过简单的赋值来解决:

class GameOfLife:

    # [...]

    # Updating the cells in the current generation.
    def update_gen(self):
        global current_generation
        for y in range(Y_CELLS):
            for x in range(X_CELLS):
                c = next_generation[x][y]
                self.draw_cell(x, y, c)
                current_generation[x][y] = next_generation[x][y] # assign element by element


还有一个问题是,当动画运行并且游戏由鼠标操作时。当鼠标被按下时,然后next_generation被更改为.activate_living_cell or .deactivate_living_cell.
但在那之后next_generation由数据重新计算current_generation.

while not self.game_over:
     self.handle_events()       # change "next_generation" by click
     if self.next_iteration:
         self.create_next_gen() # compute "next_generation" from "current_generation"
     self.update_gen()          # copy "current_generation" from "next_generation"

这个问题就可以轻松解决。考虑一下当游戏由鼠标操作时,current_generation and next_generation是平等的。

要么改变current_generation代替next_generation或更新current_generation操纵后:

def handle_events(self):
    for event in pygame.event.get():

        # [...]        

        # Pressing the left mouse button to activate or deactivate a cell.
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if next_generation[x][y] == COLOR_DEAD:
                    self.activate_living_cell(x, y)
                else:
                   self.deactivate_living_cell(x, y)
                self.update_gen() # <----------------------------
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

生命游戏 - 覆盖当前一代而不是更新到下一代 的相关文章

随机推荐

  • 如何清除Docker任务历史记录

    当以 Swarm 模式运行 docker 时 过去任务的历史记录会随着 docker 服务的更新而累积 跑步docker node ps显示任务日志 如何在不删除服务的情况下清除此日志 您可以通过运行以下命令来调整 swarm 中的历史记录
  • IE9拒绝加载自定义字体?

    我正在尝试让 IE9 显示自定义字体 应该很容易 研究了大量的谷歌网站 甚至 stackoverflow 问题 这就是我所拥有的 font face font family BrushstrokePlain src url fonts BR
  • 了解 git rev-list

    在寻找 git hook 示例时 我遇到了以下帖子 https github com Movidone git hooks blob master pre receive https github com Movidone git hook
  • Stackdriver 监控图表的算术运算

    我正在尝试为我的服务提供的自定义指标设置 Stackdriver 仪表板 特别是我从一般开始custom grpc time ms指标是一个量规并且有status上面有标签 我希望能够设置一个图表并针对指标的成功率发出警报 类似于count
  • Azure AppInsights - Http 结果代码故障

    我们已经在Azure中配置了API WebApp 然后连接了App Insights Log以获取失败时的详细信息 我们正在 APIM 上进行负载测试 有一次 我们开始收到 500 错误代码 这意味着应用程序级别存在问题 当我们查看详细信息
  • 何时使用 C++forward_list

    我对 C 有点陌生 正在阅读 C 编程语言 第 4 版 一书 在阅读 STL Containers 章节时 书中对forward list有介绍 forward list 单链表 基本上是一个优化的列表 对于空的和非常短的列表 空的forw
  • 优化整数系数列表与其长整数表示之间的转换

    我正在尝试优化我的多项式实现 特别是我正在处理系数模的多项式n 可能 gt 2 64 并对以下形式的多项式取模x r 1 r is lt 2 64 目前 我将系数表示为整数列表 并且我已经以最直接的方式实现了所有基本操作 我希望求幂和乘法尽
  • Chrome 扩展程序 MAX_WRITE 配额未补充

    我最近超出了 chrome 扩展中每小时允许的写入操作数 现在当我尝试使用 chrome storage 时收到此消息 Unchecked runtime lastError while running storage set This r
  • Spring Security JavaConfig:配置所需的通道(安全、不安全、任意)

    我试图通过任何渠道提供所有静态资源 css javascript 和图像 但无法让它与 anyRequest requiresInsecure Configuration EnableWebMvcSecurity PropertySourc
  • 通过滚动查找所有元素

    查找页面上所有具有滚动的元素的最可靠和最有效的方法是什么 目前 我正在考虑使用element all http angular github io protractor api view ElementArrayFinder with fi
  • 组合框在所选项目上绘制图像

    当选择该项目时 我尝试从组合框中的图像列表中绘制图像 我能够绘制图像 但是当onSelctedIndexChanged活动结束 我失去了我的形象 我的组合框已经有DrawMode OwnerDrawFixed 我有一个ListImage名为
  • 管理员权限被意外删除

    在尝试清除 Sonar 实例中不需要的用户 权限时 管理员的权限似乎已被撤回 我们再也看不到settings菜单项 通常出现在管理员登录链接旁边 无法再更改警报 我现在花了几个小时试图找出这些权限在数据库中的位置 并希望使用数据库中的一些插
  • 主干推送状态和错误 404

    我正在尝试实施 pushState true 但它仅适用于基本路线 不适用于其他继续给我错误 404 的路线 在 Chrome 中 如果我访问 http example app http example app OK 控制台消息显示 htt
  • 在 Elixir 中将映射转换为关键字列表

    我有一张以下形式的地图 browser name gt Chrome platform gt linux 我需要将其转换为关键字列表 browser name Chrome platform linux 实现这一目标的最佳方法是什么 这行不
  • 在这里疯了,不明白为什么 rename()、copy() 函数不起作用

    这是我所拥有的 name image jpeg to var www vhosts site com httpdocs termination files personal photos original name from var www
  • 按变量名迭代结构体

    更新 6个月后 我刚刚发现了这个答案 索引结构是否合法 Slava 的回答 https stackoverflow com a 40590471 2066079 我认为这是一个比这里提供的任何解决方案都要好得多的解决方案 因为绝对没有未定义
  • UIImagePickerController 在 iOS 9 上不请求权限

    我有一个 iOS 应用程序 我在其中展示了一个图像选择器 self picker UIImagePickerController alloc init self picker delegate self self picker source
  • 在 N 球面上生成均匀分布的随机点的算法

    我还没有在Python上找到这种算法的实现 像这样的事情 有两个输入参数 n 空间维度 m n 1 球面上的点数 我需要将它们大致均匀地排列在 n 球体的表面上 坐标轴位于n 1个球体的中心 例如 在 3d 中 可以在规则球体上定位点像这样
  • 如何使用关键原子逻辑安全地中断线程?

    我有一个线程在循环中执行两件事 等待 BlockingQueue 中的对象 然后处理它们 处理逻辑具有可在 JVM 外部观察到的效果 并且必须以原子方式完成 目前 该线程由一个易失性布尔变量控制 如中讨论的https stackoverfl
  • 生命游戏 - 覆盖当前一代而不是更新到下一代

    下面我添加了我的生命游戏代码 规则定义正确 并且运行顺利 然而 游戏并没有按预期进行 它不是更新到下一代 但它似乎正在覆盖当前一代 举个例子 三个水平点应该在下一代变成三个垂直点 但这并没有发生 解决方案 我有两代人 当代的和下一代的 它必