现代算法可视化(一)、python实现生命游戏(元胞自动机)

效果展示

在这里插入图片描述

规则介绍

基于二维元胞自动机的生命游戏可视化实现
规则:蓝色方块代表生命

  1. 有颜色的方块代表生命,无颜色的方块代表死亡
  2. 一个细胞周围有八个细胞,对于一个活细胞来说,如果它周围的八个细胞中:
    如果只有一个或没有一个是活的,那这个细胞就会死亡
    如果其中两个或者三个细胞是活的,那这个细胞就能维持生命
    如果超过3个细胞是活着的,那这个细胞就会因为过于拥挤而死亡
  3. 对于一个死细胞来说,如果这个细胞周围如果有三个细胞存活,该将获得新生。

附:所有规则都可以修改,种群初始密度也可以修改。本实验采用初始化人口随机分布,也可以自定义初始化人口分布以及网格大小得到滑翔机,播种机等复杂美观图形。

源码

'''
demoName:life simulation game
principle: cellular automata (two dimensional)
the rule of the game :
1. colored cell represents life, uncolored cell represents death
2. a cell has eight cells around it, to a live cell:
2.1 if only one or none of them are live, the cell will die
2.2 if two or three of them are live, the cell will sustain life
2.4 if more than three of them are live, the cell will die
3. to a dead cell, if there are three alive around it ,the cell will gain life
'''

import pygame
import sys 
import math 
import random
import copy

pygame.init()
chess_number=60					# 设置棋盘大小
LIVEDENSITY=0.3						#生命密度
TICK=10								#帧数
BG=(20,20,20)					#背景色
LINECOLOR=(52,53,46)				#网格色
LIFECOLOR=(31,97,189)				#活细胞的颜色
CELL_LENGTH=int(600/chess_number)	#每个格子的像素大小
LINE_WIDTH=4						#线的宽度
START_POSX=50
START_POSY=50
#239,60,57 红色		79,167,47绿色		188,69,229 紫色
#224,90,9 橙色		252,61,63大红
#188,69,229	紫色		239,60,57 红
#39,202,149 天绿	31,97,189蓝色		22,178,243 天蓝




# 设置背景框大小
size = width, height = 2*START_POSX+chess_number*CELL_LENGTH,2*START_POSY+chess_number*CELL_LENGTH
# 设置帧率,返回clock 类
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ace Cheney

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值