视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Python,OpenGL生命游戏
2020-11-27 14:16:44 责编:小采
文档
   初学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中文网!

下载本文
显示全文
专题