python,opengl生命游戏

  初学python和opengl,练手的第一个小程序life.py,这个小程序在日后会不断调整,增加类、优化判断及操作

  执行效果:

    按正规生命游戏的规则:

      1、周围生命等于3时产生生命

      2、周围生命等于2时保持不变

      3、红绿两种互相侵蚀(新增)

      4、其他情况死亡

  新增了边界循环(2017/2/13)

from opengl.gl import *from opengl.glu import *from opengl.glut import *import random”’全局参数开始”’life_down_p = 2 #竞争参数下限
life_up_p = 3 #竞争参数上限
life_die_time = 5 #死亡时间
life_begin = 1000 #开局生成时间
map_size = 100”’全局参数结束”’num = 0 #golbal
life_map = [0]*map_size*map_size #golbal
life_new = [0]*map_size*map_size #golbal
all_c = [0]*map_size*map_size
green_c = [0]*map_size*map_size
red_c = [0]*map_size*map_size
w = 2/map_size #width pre
h = 2/map_size #height pre
red = 1green = 2def draw_point(color,p) : #画点
x = int(p%map_size)
y = int(p/map_size)
glcolor3f(color[0],color[1],color[2])
glbegin(gl_quads)
glvertex2f(x*w-1,y*h-1)
glvertex2f((x+1)*w-1,y*h-1)
glvertex2f((x+1)*w-1,(y+1)*h-1)
glvertex2f(x*w-1,(y+1)*h-1)
glend()
def god() : global life_map,num,font_map,all_c,green_c,red_c if num < life_begin : #初始生成开始 num += 1 x = random.randint(1,map_size-2)*map_size+random.randint(1,map_size-2) if random.randint(0,1) : #绿色生物 life_map[x] = green draw_point([0,1,0],x) else : #红色生物 life_map[x] = red draw_point([1,0,0],x) else : #初始生成结束,开始繁殖 '''情况判断开始''' for x in range(0,map_size) : for y in range(0,map_size) : i = y*map_size+x '''获取周边信息''' c = [(y-1)%map_size*map_size+(x-1)%map_size, (y-1)%map_size*map_size+ x , (y-1)%map_size*map_size+(x+1)%map_size, y *map_size+(x-1)%map_size, y *map_size+(x+1)%map_size, (y+1)%map_size*map_size+(x-1)%map_size, (y+1)%map_size*map_size+ x , (y+1)%map_size*map_size+(x+1)%map_size,] red_c[i],green_c[i],all_c[i] = 0,0,0 for cc in c : if life_map[cc] == green : green_c[i] += 1 elif life_map[cc] == red : red_c[i] += 1 all_c[i] = green_c[i] + red_c[i] '''判断''' for i in range(0,map_size*map_size) : if all_c[i] == life_up_p : #生存 if green_c[i] > red_c[i] :
life_map[i] = green
draw_point([0,1,0],i)
elif green_c[i] < red_c[i] : life_map[i] = red draw_point([1,0,0],i) else : if random.randint(0,1) : life_map[i] = green draw_point([0,1,0],i) else : life_map[i] = red draw_point([1,0,0],i) elif all_c[i] > life_up_p or all_c[i] < life_down_p : #死亡 life_map[i] = 0 draw_point([0,0,0],i) #else : 保持def drawfunc() : god() glflush() glutinit() glutinitdisplaymode(glut_single | glut_rgba) glutinitwindowsize(800,800) glutcreatewindow(b"life-forver") glutdisplayfunc(drawfunc) glutidlefunc(drawfunc) glutmainloop()

执行截图:

更多python,opengl生命游戏相关文章请关注php中文网!

Posted in 未分类

发表评论