【问题标题】:How can I get my game of life code to animate in python?如何让我的生命游戏代码在 python 中制作动画?
【发布时间】:2020-08-28 19:40:51
【问题描述】:

我目前正在用 python 编写 John Conway 的生命游戏,但在让细胞根据规则进行动画处理时我被卡住了。目前我的代码正在编译,没有任何错误,但我只是不知道如何让代码制作动画。我的代码是:

代码编辑

from tkinter import *
from random import *
import time
import numpy as np

PIXEL_SIZE = 10
ROW = 910
COLUMN = 700
grid = []
updated_grid = [[]]

def create_grid():
    for row in range(0, ROW):
        grid2 = []
        for column in range(0, COLUMN):
            grid2.append(randint(0, 1))
        grid.append(grid2)


def draw_grid():
    for row in range(0, ROW):
        for column in range(0, COLUMN):
            if grid[row][column] == 1:
                x0 = row*PIXEL_SIZE
                y0 = column*PIXEL_SIZE
                x1 = x0+PIXEL_SIZE
                y1 = y0+PIXEL_SIZE
                canvas.create_rectangle(x0, y0, x1, y1, fill='red')


def apply_rules():
    for row in range(1, ROW - 1):
        for column in range(1, COLUMN - 1):
            neighbours_count = 0
            # will count the neighbours for each cell
            neighbours_count += grid[row-1][column-1] # top left
            neighbours_count += grid[row][column-1] # top center
            neighbours_count += grid[row+1][column-1] # top right

            neighbours_count += grid[row-1][column] # middle left
            neighbours_count += grid[row+1][column] # middle right

            neighbours_count += grid[row-1][column+1] # bottom left
            neighbours_count += grid[row][column+1] # bottom center
            neighbours_count += grid[row+1][column+1] # bottom right

            # Game Of Life rules:

            # alive cell rules
            if grid[row][column] == 1:
                if neighbours_count < 2: # rule 1 any live cell with fewer than two live neighbours dies, as if by underpopulation
                    grid[row][column] = 0
                elif neighbours_count == 2 | neighbours_count == 3: # rule 2 any live cell with two or three live neighbours lives on to the next generation
                    grid[row][column] = 1
                elif neighbours_count > 3 & neighbours_count <= 8: # rule 3 any live cell with more than three live neighbours dies, as if by overpopulation
                    grid[row][column] = 0
                else:
                    grid[row][column] = 0
            elif grid[row][column] == 0: # dead cells rule 4 any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction
                if neighbours_count == 3:
                    grid[row][column] = 1
                else:
                    grid[row][column] = 0


def one_cycle():
    apply_rules()
    draw_grid()
    window.after(1, one_cycle)


window = Tk() # creates the window for the game
window.title('Game Of Life Python') # is the game title written on the window
canvas_frame = Frame(window) # creates a frame on the window to hold the canvas
game_title = Frame(window) # creates a frame on the window to display the game title (which will be a label)
start_button = Button(window, text='Start Game', command=one_cycle) # creates a button which will be used to start the game
canvas = Canvas(canvas_frame, width=ROW, height=COLUMN, background='black') # creates the canvas used to the draw the game of life
game_title_label = Label(game_title, text='Game Of Life', font='Helvetica 20 bold', fg='grey') # creates the label for the game title which will be placed in a frame

canvas.grid(row=0, column=0) # places the canvas onto the canvas_frame
canvas_frame.grid(row=1, column=1) # places the canvas_frame onto the window
game_title_label.grid(rowspan=2, column=0) # places the title of the game onto the game_title frame
game_title.grid(row=0, columnspan=2) # places the frame for the game title onto the window
start_button.grid(rowspan=2, column=1) # places the start onto the window


create_grid()
window.mainloop()

我是 python 新手,所以如果您发现我遗漏的任何错误,请原谅我,并感谢您能给我的任何帮助。

【问题讨论】:

    标签: python python-3.x animation canvas tkinter


    【解决方案1】:

    我认为您需要进行一些修改:

    1. 更改您的Button 开始绘图:start_button = Button(window, text='Start Game', command=apply_rules)
    2. 修改def apply_rules():以在末尾添加:window.after(0, apply_rules)
    3. 不要打电话给apply_rules()你自己:apply_rules()

    运行程序时,您需要按下Start Game 按钮。

    =================

    更新:

    您可能需要添加另一个函数来封装运行规则并连续显示结果的想法:

    保留 def apply_rules(): 的原样并添加新功能:

    def one_cycle():
        apply_rules()
        draw_grid()
        window.after(1, one_cycle)   # use 1 to ease performance as Bryan suggests
    

    Button命令改为:command=one_cycle

    现在你只需要在底部:

    create_grid()
    window.mainloop()
    

    【讨论】:

    • 感谢您的帮助,我进行了更改,但按下开始按钮后程序似乎冻结了
    • after(0, ...) 是个坏主意。该数字应该大于零,否则您可能会遇到性能问题。
    • @BryanOakley 和 @quamrana 感谢帮助我添加了更改,但没有运气让程序可视化我将网格打印到终端,它似乎正在运行,但只是没有在我的出于某种原因的 GUI。也许我把window.after(1, apply_rules) 放在了错误的地方?我已经更新了问题中的代码,以表明我已经提出了您的建议。再次感谢
    • 您忘记删除apply_rules()末尾的after(0, apply_rules)
    • @quamrana 再次感谢它帮助它显示但不正确我想我可能需要更新画布因为在我看来屏幕没有被重绘但它被绘制在顶部已经显示的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    相关资源
    最近更新 更多