Beginner python and OpenGL, practiced hand's first small program life.py, this small program will continue to adjust in the future, add class, optimize judgment and operation
Execution effect:
Rules according to normal life game:
1, the surrounding life is equal to 3 to produce life
2, the surrounding life is equal to 2 o'clock remains unchanged
3, red and green two kinds of mutual erosion (new)
4. Death in other cases
Added boundary loop (2017/2/13)
From OPENGL.GL import *from Opengl.glu import *from opengl.glut import *import random ' global parameter start ' life_down_p = 2 #竞争参数下限li Fe_up_p = 3 #竞争参数上限life_die_time = 5 #死亡时间life_begin = #开局生成时间map_size = 100 "Global parameter End" num = 0 #golballife_map = [0] *map_size*map_size #golballife_new = [0]*map_size*map_size #golbalall_c = [0]*map_size*map_sizegreen_c = [0]*map_size* Map_sizered_c = [0]*map_size*map_sizew = 2/map_size #width Preh = 2/map_size #height prered = 1GREEN = 2def Draw_point (col OR,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) Els E: #初始生成结束, start breeding "The case begins" "For X in Range (0,map_size): for y in range (0,map_size): i = y*map_size+x ' Get peripheral information ' 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[c c] = = RED: Red_c[i] + = 1 All_c[i] = Green_c[i] + red_c[i] "' judgment ' ' 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: Keep Def drawfunc (): God () Glflush () Glutinit () Glutinitdisplaymode (Glut_single | Glut_rgba) glutinitwindowsize (800,800) Glutcreatewindow (b "life-fOrver ") Glutdisplayfunc (Drawfunc) glutidlefunc (Drawfunc) Glutmainloop ()
Perform:
More Python,opengl Life Games related articles please pay attention to topic.alibabacloud.com!