首页 > 编程语言 >python pygame 生命的游戏

python pygame 生命的游戏

时间:2022-10-03 13:36:27浏览次数:52  
标签:box 游戏 python Cells window pygame screen 255

import sys
import pygame
import random

# 参数设置
box_w, box_h = 10, 10 # 盒子宽高
window_w, window_h = 400, 400
x, y = 0,0
#使用pygame之前必须初始化
pygame.init()

#设置主屏窗口
screen = pygame.display.set_mode((400,400))

#设置窗口标题
pygame.display.set_caption('')
# 如果没有下列主循环代码,运行结果会一闪而过

def checks(x, y):
n = 0 # 周围细胞个数
L = x+box_w,y
R = x-box_w,y
U = x,y-box_h
D = x,y+box_h
ul = x+box_w,y-box_h
ur = x-box_w,y-box_h
dl = x+box_w,y+box_h
dr = x+box_w,y+box_h
d = [L, R, U, D, ul, ur, dl, dr]
for i in d:
if i in Cells:
n += 1
# 过于拥挤 n >= 4
if n >= 4:
Cells.remove([x,y])
# 过于孤独 n <= 1
elif n <= 1:
Cells.remove([x,y])
# 保持存活 2 <= n < 3
elif 2 <= n < 3:
pass
# 细胞繁衍 n = 3
elif n == 3:
while 1:
x,y = random.choice(d)
if [x,y] not in Cells:
print(x,y)
Cells.append([x,y])
break

# 细胞群
Cells = []
for t in range(100):
i = random.randint(0,int(window_w/box_w))
j = random.randint(0,int(window_h/box_h))
x, y = i*box_w,j*box_h
Cells.append([x, y])

# 更新屏幕内容
pygame.display.flip()
while True:
# #填充主窗口的背景颜色,参数值RGB(颜色元组)
screen.fill((255, 255, 255))
# 循环获取事件,监听事件
for event in pygame.event.get():
# 判断用户是否点了关闭按钮
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序
sys.exit()

# 矩形
for x,y in Cells:
pygame.draw.rect(screen, (255, 0, 255), (x, y, box_w, box_h),width=0)
checks(x,y)

# 竖向
for i in range(int(window_h/box_w)):
pygame.draw.line(screen, (1, 1, 1), (box_w * i, 0), (box_w * i, window_h), 2)
# 横向
for j in range(int(window_w/box_h)):
pygame.draw.line(screen, (1, 1, 1), (0, box_h * j), (window_w, box_h * j), 2)

screen.blit(screen, (0, 0))
# 定义频率
clock = pygame.time.Clock()
# 设定刷新帧率
clock.tick(1) # 越大刷新的越快
pygame.display.update()

标签:box,游戏,python,Cells,window,pygame,screen,255
From: https://www.cnblogs.com/lld76/p/16750389.html

相关文章

  • 学习python遇到的问题
    python重定向输入:io.UnsupportedOperation:notreadable两处错误一、用open打开一个文件,此时调用的是w写入模式,下面使用read是没有权限的,得使用w+读写模式二、使用wri......
  • python学习:multiprocessing多进程-Pool进程池模块
    Multiprocessing.Pool可以提供指定数量的进程供用户调用,当有新的请求提交到pool中时,如果池还没有满,那么就会创建一个新的进程用来执行该请求;但如果池中的进程数已经达到规定......
  • 【推荐收藏】时间序列分析全面指南(附Python代码)
    大家好,时间序列是在规律性时间间隔上记录的观测值序列。本文我将带你了解在Python中分析给定时间序列的特征的21个全过程。内容较长,建议收藏、点赞、关注。内容​​1.什......
  • TransBigData:一款基于 Python 的超酷炫交通时空大数据工具包
    今天分享一次Python交通数据分析与可视化的实战!其中主要是使用TransBigData库快速高效地处理、分析、挖掘出租车GPS数据。所介绍的相关技术开发了Python开源库TransBigData,......
  • 解决conda安装虚拟Python环境联网失败问题
    使用condacreate-ncarlapython=3.7命令安装虚拟环境出错,说网络连接异常,经久查询未果:在终端输入下面这些:condaconfig--addchannelshttps://mirrors.tuna......
  • python 如何快速配置pip加速
    相关地址PyPI镜像使用帮助https://mirrors.tuna.tsinghua.edu.cn/help/pypi/使用清华镜像源加速0.打开cmd1.输入pipconfigsetglobal.index-urlhttps://pypi.tun......
  • 使用bt面板中Python项目管理部署Django项目找不到static采坑记
    工作需要,准备在用django做一个小项目,本地测试没有问题,但是使用bt的工具“python项目管理器”部署到服务器上,找不到static文件(python项目管理器应用可以参考https://www.bt.......
  • pushgateway python脚本
    vimget_network.pyimportprometheus_clientfromprometheus_clientimportCounterfromprometheus_clientimportGaugefromprometheus_client.coreimportCollec......
  • 数字游戏
    题目:        这道题目是一点数学成分+一点DFS主要介绍DFS以及其调用的变量DFS:dfs(x,y,dir,rem,d_t)1.DFS变量介绍(x,y):出发x,y坐标dir:向对于整个地图来......
  • 【C语言】经典猜数字游戏
    ​​#include<stdio.h>​​​​#include<stdlib.h>​​​​#include<time.h>​​​​voidmenu()​​​​{​​​​printf("**********\n");​​​​printf("*****......