多条告白如次剧本只需引入一次
1 证明:
1.1 运用python的pygame和OpenGL创造动静正方体cube。
1.2 安置python3.8和pygame(简略)。
1.3 安置OpenGL:
pip install PyOpenGL PyOpenGL_accelerate2 功效图:先看。
3 完备代码:
#---导出模块---import pygamefrom pygame.locals import *from OpenGL.GL import *from OpenGL.GLU import *#---初始化pygame和设置窗口巨细---pygame.init()#OPENGL|DOUBLEBUF=DOUBLEBUF|OPENGL#DOUBLEBUF:双缓冲形式(引荐和 HWSURFACE 或 OPENGL 一道运用)#创造一个 OPENGL 衬托的表露pygame.display.set_mode((640,480), OPENGL|DOUBLEBUF)#---元组设置---#设置正方体的xyz坐圈点CUBE_POINTS = ((0.5, -0.5, -0.5), (0.5, 0.5, -0.5),(-0.5, 0.5, -0.5), (-0.5, -0.5, -0.5),(0.5, -0.5, 0.5), (0.5, 0.5, 0.5),(-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5))#设置RGB脸色CUBE_COLORS = ((1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 0, 0),(1, 0, 1), (1, 1, 1), (0, 0, 1), (0, 1, 1))# 设置面,四个点形成一个面CUBE_QUAD_VERTS = ((0, 1, 2, 3), (3, 2, 7, 6), (6, 7, 5, 4),(4, 5, 1, 0), (1, 5, 7, 2), (4, 0, 3, 6))# 设置线,两个点形成一个线CUBE_EDGES = ((0,1), (0,3), (0,4), (2,1), (2,3), (2,7),(6,3), (6,4), (6,7), (5,1), (5,4), (5,7),)#---设置画正方体因变量---def drawcube(): # "绘制正方体",zip和list法 allpoints = list(zip(CUBE_POINTS, CUBE_COLORS)) #画表面积---发端---中断--- glBegin(GL_QUADS) for face in CUBE_QUAD_VERTS: for vert in face: pos, color = allpoints[vert] #在第2个for底下 glColor3fv(color) glVertex3fv(pos) #与第1个for对齐 glEnd() #边线脸色玄色 glColor3f(0, 0, 0) # 绘制线---发端---中断--- glBegin(GL_LINES) for line in CUBE_EDGES: for vert in line: pos, color = allpoints[vert] glVertex3fv(pos) glEnd()#---主因变量---def main(): glEnable(GL_DEPTH_TEST) #初始化 摄像头 glMatrixMode(GL_PROJECTION) gluPerspective(45.0,640/480.0,0.1,100.0) glTranslatef(0.0, 0.0, -3.0) glRotatef(25, 1, 0, 0) #启用轮回--- while True: #事变检验和测定 event = pygame.event.poll() #设置退出体制,在pygame的while轮回中,这一步必备树立 if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): break #废除屏幕 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #摄像机回旋 glRotatef(1,0,1,0) drawcube() #革新画面 pygame.display.flip()if __name__ == '__main__': main()4 元组的设置和推导式:
#---元组设置---#设置正方体的xyz坐圈点CUBE_POINTS = ((0.5, -0.5, -0.5), (0.5, 0.5, -0.5),(-0.5, 0.5, -0.5), (-0.5, -0.5, -0.5),(0.5, -0.5, 0.5), (0.5, 0.5, 0.5),(-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5))#设置RGB脸色CUBE_COLORS = ((1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 0, 0),(1, 0, 1), (1, 1, 1), (0, 0, 1), (0, 1, 1))# 设置面,四个点形成一个面CUBE_QUAD_VERTS = ((0, 1, 2, 3), (3, 2, 7, 6), (6, 7, 5, 4),(4, 5, 1, 0), (1, 5, 7, 2), (4, 0, 3, 6))# 设置线,两个点形成一个线CUBE_EDGES = ((0,1), (0,3), (0,4), (2,1), (2,3), (2,7),(6,3), (6,4), (6,7), (5,1), (5,4), (5,7),)# "绘制正方体"allpoints = list(zip(CUBE_POINTS, CUBE_COLORS))#print(allpoints)#print(allpoints[0])for face in CUBE_QUAD_VERTS: for vert in face: pos, color = allpoints[vert] #print(allpoints[vert]) print(pos) print(color)5 简化少许:如许很熟习少许。
#---元组设置---#设置正方体的xyz坐圈点a = ((0.5, -0.5, -0.5), (0.5, 0.5, -0.5),(-0.5, 0.5, -0.5), (-0.5, -0.5, -0.5),(0.5, -0.5, 0.5), (0.5, 0.5, 0.5),(-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5))#设置RGB脸色b = ((1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 0, 0),(1, 0, 1), (1, 1, 1), (0, 0, 1), (0, 1, 1))# 设置面,四个点形成一个面c = ((0, 1, 2, 3), (3, 2, 7, 6), (6, 7, 5, 4),(4, 5, 1, 0), (1, 5, 7, 2), (4, 0, 3, 6))# 设置线,两个点形成一个线d = ((0,1), (0,3), (0,4), (2,1), (2,3), (2,7),(6,3), (6,4), (6,7), (5,1), (5,4), (5,7),)# "绘制正方体"all = list(zip(a, b))#print(all)#print(all[0])for j in c: for i in j: pos, color = all[i] #print(all[i]) print(pos) print(color)5.1 元组的元组
5.2 zip因变量
5.3 list因变量:从来是列表的道理,创造仍旧元组,是元组的列表打包法。
5.4 推导式嵌套推导式和元组推导器?