Home > Article > Backend Development > Python, OpenGL Game of Life
Beginner to learn Python and OpenGL, life.py is the first small program to practice. This small program will be continuously adjusted in the future to add classes, optimize judgment and operations
Execution effect:
According to the rules of the regular life game:
1. Life is generated when the surrounding life is equal to 3
2. It remains unchanged when the surrounding life is 2
3. Red and green Mutual erosion (new)
4. Death in other circumstances
Added boundary cycle (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 red_c[i] : life_map[i] = GREEN draw_point([0,1,0],i) elif green_c[i] life_up_p or all_c[i] <p></p><p>Execution screenshots:</p><p><img src="https://img.php.cn/upload/article/000/000/013/c279f9a8ca8d2f1e78c7adf5c6c1ce00-0.png" alt="Python, OpenGL Game of Life" style="max-width:90%" style="max-width:90%" title="Python, OpenGL Game of Life"></p><p> For more articles related to Python and OpenGL Game of Life, please pay attention to the PHP Chinese website! </p>