初学Python和OpenGL,练手的第一个小程序life.py,这个小程序在日后会不断调整,增加类、优化判断及操作
执行效果:
在开局时间内,随机生成绿色、红色两种会逐渐老化死亡的生物。然后开始生存游戏,周围本物种生物大于竞争下限且小于竞争上限时生存并繁殖,红绿两物种间会互相对抗
from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * import cv2 as cv import random ‘‘‘全局参数开始‘‘‘ life_down_p = 1 #竞争下限 life_up_p = 3 #竞争上限 life_die_time = 5 #死亡时间 life_begin = 100 #开局生成时间 ‘‘‘全局参数结束‘‘‘ num = 0 #golbal life_map = [0]*100*100 #golbal life_time = [0]*100*100 #golbal def draw_point(color,p) : #画点 w = 2/100 #width pre h = 2/100 #height pre x = int(p%100) y = int(p/100) 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,life_time,num if num < life_begin : #初始生成开始 num += 1 x = random.randint(1,98)*100+random.randint(1,98) if random.randint(0,1) : ‘‘‘绿色生物‘‘‘ life_map[x] = 1 draw_point([0,1,0],x) else : ‘‘‘红色生物‘‘‘ life_map[x] = 9 draw_point([1,0,0],x) else :#初始生成结束,开始繁殖 i = random.randint(1,98)*100+random.randint(1,98) #获取各方位信息 here = life_map[i] d = [0]*8 d[0] = life_map[i-100] #north d[1] = life_map[i+100] #south d[2] = life_map[i- 1] #west d[3] = life_map[i+ 1] #east d[4] = life_map[i-101] #west-north d[5] = life_map[i- 99] #east-north d[6] = life_map[i+ 99] #west-south d[7] = life_map[i+101] #east-south green_d,red_d,all_d = 0,0,0 for dd in d : if dd == 1 : green_d += 1 elif dd == 9 : red_d += 1 else : all_d += 1 all_d = 8 - all_d if here == 1 : ‘‘‘绿色生物‘‘‘ if green_d < life_down_p or green_d > life_up_p or green_d < red_d or life_time[i] > life_die_time : #挂了 life_map[i] = 0 draw_point([0,0,0],i) elif green_d == red_d : #势均力敌 if random.randint(0,1) : life_time[i] += 1 draw_point([1,1-life_time[i]/life_die_time,0],i) else : #转变阵营 life_map[i] = 9 draw_point([1-life_time[i]/life_die_time,1,0],i) else : #继续存活 life_time[i] += 1 draw_point([0,1-life_time[i]/life_die_time,0],i) ‘‘‘绿色生物‘‘‘ elif here == 9 : ‘‘‘红色生物‘‘‘ if red_d < life_down_p or red_d > life_up_p or red_d < green_d or life_time[i] > life_die_time : #挂了 life_map[i] = 0 draw_point([0,0,0],i) elif green_d == red_d : #势均力敌 if random.randint(0,1) : life_time[i] += 1 draw_point([1-life_time[i]/life_die_time,1,0],i) else : #转变阵营 life_map[i] = 1 draw_point([1,1-life_time[i]/life_die_time,0],i) else : #继续存活 life_time[i] += 1 draw_point([1-life_time[i]/life_die_time,0,0],i) ‘‘‘红色生物‘‘‘ elif here == 0 : #无生物 if green_d > red_d and green_d > life_down_p and green_d < life_up_p : life_map[i] = 1 draw_point([0,1,0],i) elif red_d > green_d and red_d > life_down_p and red_d < life_up_p : life_map[i] = 9 draw_point([1,0,0],i) elif red_d == green_d and all_d > life_down_p and all_d <life_up_p : if random.randint(0,1) : life_map[i] = 1 draw_point([0,1,0],i) else : life_map[i] = 9 draw_point([1,0,0],i) ‘‘‘情况判断结束‘‘‘ def drawFunc() : god() glFlush() glutInit() glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA) glutInitWindowSize(800,800) glutCreateWindow(b"life") glutDisplayFunc(drawFunc) glutIdleFunc(drawFunc) glutMainLoop()
执行截图:
执行效果还算满意,用到的都是最最基本的api
原文:http://www.cnblogs.com/lclblack/p/6386847.html