以下是部分代码预览:
"""pygame像素级生命游戏模拟.py
Conway's Game of Life),又称康威生命棋,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机。
本程序用pygame模拟这种现象,作者:李兴球,风火轮少儿编程 www.scratch8.net
"""
import pygame
from pygame.locals import *
from random import randint
pygame.init()
life_color = (222,211,0,255) # 有颜色的代表活的,黑色代表死的
width,height = 200,200
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("生命游戏模拟_风火轮编程李兴球")
lives = {}
life_image = pygame.Surface((width,height))
# 初始化状态
for x in range(width):
for y in range(height):
if randint(1,10) == 1:
lives[(x,y)] = 1
else:
lives[(x,y)]=0
def get_numbers(pos):
"""得到周围活的数量"""
pass
running = True
while running:
for cell in lives:
pygame.event.poll()
f = lives[cell] # 生命状态 cell 就是x,y
cell_number = get_numbers(cell) # 得到周围活的细胞数量
pass
screen.fill((0,0,0))
life_image.fill((0,0,0))
[life_image.set_at(cell,life_color) for cell in lives if lives[cell] ]
screen.blit(life_image,(0,0)) # 把life_image渲染到screen上.
pygame.display.update() # 更新屏幕显示
"""
1、规则
生命游戏中,对于任意细胞,规则如下。每个细胞有两种状态:存活或死亡,每个细胞与以自身为中心的周围八格细胞产生互动。
当前细胞为存活状态时,当周围低于2个(不包含2个)存活细胞时, 该细胞变成死亡状态。(模拟生命数量稀少)
当前细胞为存活状态时,当周围有2个或3个存活细胞时, 该细胞保持原样。
当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态。(模拟生命数量过多)
当前细胞为死亡状态时,当周围有3个存活细胞时,该细胞变成存活状态。 (模拟繁殖)
---------------------
"""
pygame.image.save(life_image,"te.png")
如需要查看完整代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)
