OpenGL with PyOpenGL Python and PyGame p.4-Moving the player automatically towards the cube
来源:互联网 发布:淘宝号信用点数查询 编辑:程序博客网 时间:2024/05/16 05:57
Moving the player automatically towards the cube
Since we're trying to make a 3D PyOpenGL version of our previous game, we want the cube to just start coming at us. We of course will soon want many cubes, but let's just start with one for now.
This tutorial is fairly simplistic, we just need to make the cube come at us, and we need to be able to know where we are in reference to the camera.
Now the beginning of our main function should look like this (Note the commented areas as areas of change that you might not notice)
def main(): pygame.init() display = (800,600) pygame.display.set_mode(display, DOUBLEBUF|OPENGL) gluPerspective(45, (display[0]/display[1]), 0.1, 50.0) #start further back glTranslatef(random.randrange(-5,5),0, -30) # no more rotate #glRotatef(25, 2, 1, 0) object_passed = False
So we've commented out the rotation, and we've started back a bit further. We also have set a variable to false that asks if the object has passed us yet.
Next:
while not object_passed: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: glTranslatef(-0.5,0,0) if event.key == pygame.K_RIGHT: glTranslatef(0.5,0,0) if event.key == pygame.K_UP: glTranslatef(0,1,0) if event.key == pygame.K_DOWN: glTranslatef(0,-1,0) ''' if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 4: glTranslatef(0,0,1.0) if event.button == 5: glTranslatef(0,0,-1.0) '''
Note that we've commented out the zooming ability, since that's not quite what we want, though we could allow the user later to speed up/slow down slightly. This would make a lot of sense is coverage space and time is somehow made important in our game.
Also note that our movement according to key-presses has changed.
That code basically handles our navigation, but we also know we want to know once the cube has passed us. To do this, we need to know where we are. We are the ones moving (remember my question? Did you get it right?). We can store where we have placed the cubes, now we want to know how to acquire our own location:
x = glGetDoublev(GL_MODELVIEW_MATRIX)This will give us our modelview matrix, which contains our X, Y, and Z coordinates! The following are our actual coordinates:
camera_x = x[3][0] camera_y = x[3][1] camera_z = x[3][2]
Now, we can wrap up our code with:
glTranslatef(0,0,0.5) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) Cube() pygame.display.flip() if camera_z <= 0: object_passed = True for x in range(10): main()
Here, we're slowly moving with the glTranslate, and then performing the typical code, followed by a final if statement asking if we've passed the point of the cube. If we have, then we basically are just restarting the instance.
#!/usr/bin/python# -*- coding:utf-8 -*- import pygamefrom pygame.locals import *import randomfrom OpenGL.GL import *from OpenGL.GLU import *verticies = ( (1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, -1), (1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1) )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) )surfaces = ( (0,1,5,4), #创建平面时平面的顺序不一定要相临,但是每个面的顶点一定要按顺时针或者逆时针写 (5,4,6,7), (6,7,2,3), (2,7,5,1), (0,1,2,3), (0,3,6,4))colors = ( (1,0,0), (0,1,0), (0,0,1), (1,1,0), (1,0,1), (1,1,1), (1,0,0), (0,1,0), (0,0,1), (1,0,0), (1,1,1), (0,1,1), )def Cube(): glBegin(GL_QUADS) x = 0 for surface in surfaces: x+=1 for vertex in surface: glColor3fv(colors[x]) glVertex3fv(verticies[vertex]) glEnd() glBegin(GL_LINES) for edge in edges: for vertex in edge: glVertex3fv(verticies[vertex]) glEnd()def main(): pygame.init() #pygame的一些初始化不用管 display = (800,600) pygame.display.set_mode(display, DOUBLEBUF|OPENGL) gluPerspective(45,(display[0]/display[1]), 0.5, 50.0) #参数1是我们看显示物体的远近 #参数2是物体显示的长宽比,和窗口长宽比相同就行 #参数3和4是z轴近和远的裁剪面的距离,但是还是不太明白要这干啥 #glTranslatef(0.0,0.0, -5) #Z轴就是我们眼睛到屏幕方向的轴,负是远,正是近,其实就是让物体相对与屏幕在XYZ各方向移动几个距离 #start further back glTranslatef(0,0, -30) #glRotatef(360, 1, 1, 1) #360是让他转360度,它最终回到原来初始位置 object_passed = False while not object_passed: #如果camera_z 的距离小于某个值则跳出循环 for event in pygame.event.get(): if event.type == pygame.QUIT: #退出事件响应 pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: glTranslatef(-0.5,0,0) if event.key == pygame.K_RIGHT: glTranslatef(0.5,0,0) if event.key == pygame.K_UP: glTranslatef(0,1,0) if event.key == pygame.K_DOWN: glTranslatef(0,-1,0) x = glGetDoublev(GL_MODELVIEW_MATRIX) #X=[[a,0,0,0],[0,a,0,0],[0,0,a,0],[X,Y,Z,a]] camera_x = x[3][0] camera_y = x[3][1] camera_z = x[3][2] #这里是camera 视角,z表示物体距离屏幕有多远是正值 #print x[3] glTranslatef(0,0,0.01) if camera_z <= 2: #如果camera_z 的距离小于某个值则跳出循环 object_passed = True #glRotatef(10, 0, 0, 1) #参数1是每次旋转的度数是多少, #参数2是x, y and z的一个坐标,表示从(0,0,0)点到(x,y,z)这条线为轴进行旋转 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #用来删除就得画面,清空画布 Cube() #创建模型 pygame.display.flip() #显示画面 pygame.time.wait(10) #10ms刷新一次 main()
- OpenGL with PyOpenGL Python and PyGame p.4-Moving the player automatically towards the cube
- OpenGL with PyOpenGL tutorial Python and PyGame p.1 - Making a rotating Cube Example
- OpenGL with PyOpenGL Python and PyGame p.2 - Coloring Surfaces
- OpenGL with PyOpenGL Python and PyGame p.5 Adding a ground in OpenGL
- OpenGL with PyOpenGL Python and PyGame p.3 - Movement and Navigation
- 【Space Shoot Project】moving the player
- Startup the web server and instance automatically.
- Hexo-blogs-to-add-search-and-the-python-scripts-record-automatically
- Moving a drawn line with the mouse
- The nvidia opengl driver lost connection with the display driver and is unable to continue
- The Graphics Pipeline and OpenGL
- Make Games with Python & Pygame (4)
- C++ Qt Game Tutorial 2 - Moving With the Arrow Keys
- Accessing the Raspberry Pi Camera with OpenCV and Python
- Moving the Origin
- Moving the Search Box
- hdu_4717 The Moving Points
- hdu4717 The Moving Points
- 高性能平台配置环境变量
- Tomcat源码解析(五):Connector连接器的初始化和启动流程
- 图论面试题
- Android设计模式之代理模式 Proxy
- Samba服务器的安装及Crontab调度命令的使用
- OpenGL with PyOpenGL Python and PyGame p.4-Moving the player automatically towards the cube
- android.mk编译apk在64位机器内运行so库报错
- Cannot reload AVD list
- RAM之程序运行时数据和代码加载
- 1.ARM基础概念及芯片平台
- 51nod--1242 斐波那契数列第N项 (矩阵乘法优化)
- Poj 2892 Tunnel Warfare
- 反射和晚期绑定
- 基于Face++实现的人脸检测(年龄检测)Android程序