# How can I add 3D animation to my Pygame window?

I've developed a quiz game using Pygame but now I'm being asked to implement a 3D animation element (a game show host) to the game.

I used Pygame as I was told it would be an easy enough language to learn and make something simple, however I see now that it seems very limited in terms of 3D animation.

I've seen OpenGL being used with Pygame and what I need is for my game to run exactly as it is, but with the 3D element (game show host) added on the corner of the window for example.

How can I use OpenGL in Pygame in order to do this?

• Why not store the animation as a gif? – Bálint Mar 13 '18 at 12:03
• It has to be a dynamic 3D animation, so it responds to things happening in-game, I don't think gif would be a feasible way to do that unfortunately – João Oliveira Mar 13 '18 at 12:37
• I think the gif approach would be easiest. You can still have dynamism if you create a bunch of "pseudo 3D" animations and play them depending on what's happening on screen. Trying to learn and then work with Pygame's OpenGL support in addition to learning OpenGL simultaneously would be a large task. (I know because I abandoned this task.) – GrimGrom Mar 13 '18 at 15:13
• Yeah I see what you're saying, and yeah I imagine that would be hell to learn in a short-ish time frame.. Thanks for the suggestions – João Oliveira Mar 15 '18 at 11:24

import pygame
from pygame.locals import *

from 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)
)

def Cube():
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verticies[vertex])
glEnd()

def main():
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

glTranslatef(0.0,0.0, -5)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cube()
pygame.display.flip()
pygame.time.wait(10)

main()


This is some example code taken from https://pythonprogramming.net that creates a simple cube and draws it on screen. It has and accompanying tutorial playlist featured here.

You will need to have the seperate PyOpenGL installed which can be found here.

In the example code he first creates a list of vertices and a list of edges that references the index of each vertex. He then creates a simple Cube class which loops through each vertex and connects them with lines.

In the main function when pygame.display.set_mode() is called, it should have the DOUBLEBUF tag to create a double buffer, and the OPENGL tag to have pygame create an OpenGL-renderable display. You can find more info on the Pygame display module at the Pygame docs here.

He then creates the OpenGL perspective with gluPerspective with the FOV, Aspect-ratio, and near and far clipping planes glTranslatef translates the display (in this case -5 on the z axis.

In the main loop, among the normal Pygame code, glClear clears the display with the flags being what is meant to be cleared, and the Cube() class which creates the wireframe cube that was mentioned earlier.

Hopefully this helps you on the track learning PyOpenGL in Pygame.