life game 是一个著名的无参与者游戏。本程序用turtle模块实现了一下。
下面是部分代码预览:
"""
生命动画模拟turtle版.py
"""
__author__ = "lixingqiu"
__date__ = "2018/11/29"
from turtle import Turtle,Screen
from random import randint
def generate_cors(rows,cols,grid_width,grid_height):
"""产生每个格子中心点坐标列表"""
table_width = cols * grid_width
table_height = rows * grid_height
left = - table_width // 2 + grid_width //2 # 左上角格子中点x
top = table_height // 2 - grid_height //2 # 左上角格子中点y
def init_grids_value(rows,cols):
"""随机产生每个格子的值,0或1"""
grids = []
def print_dots(t,rows,cols):
"""根据格子的值打印黑点或白点,黑点不必打,因为背景是黑色的"""
def get_around_dots(rows,cols,x,y):
"""得到周围的活点数"""
counter = 0
if __name__ == "__main__":
screen = Screen()
screen.setup(480,320)
screen.bgcolor("black")
screen.tracer(0,0)
screen.title('生命模拟turtle版')
rows ,cols = 50,50
grid_width,grid_height = 5,5
grids_cors = generate_cors(rows,cols,grid_width,grid_height)
grids_value = init_grids_value(rows,cols) # 初始化点
t = Turtle(visible=False)
t.penup()
如需要查看完整源代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

