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.

The next tutorial:





  • OpenGL with PyOpenGL introduction and creation of Rotating Cube
  • Coloring Surfaces as well as understand some of the basic OpenGL code
  • Understanding navigation within the 3D environment via OpenGL
  • Moving the player automatically towards the cube
  • Random Cube Position
  • Adding Many Cubes to our Game
  • Adding a ground in OpenGL
  • Infinite flying cubes
  • Optimizing the processing for infinite cubes