Search code examples
pythonopenglpyopengl

PyOpenGL Taking keyboard input


I'm trying to make a raycaster in Python with PyOpenGL and i accomplished to make a little yellow square. Main thing is that i need keyboard input to move my square and i made something but it is not working. Here is the code:

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
import math

def drawPlayer():
    glColor3f(1,1,0)
    glPointSize(8)
    glBegin(GL_POINTS)
    glVertex2i(px,py)
    glEnd()

def display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    drawPlayer()
    glutSwapBuffers()

# Here is my keyboard input code
def buttons(key,x,y):
    if key == 'a':
        px -= 5
    if key == 'd':
        px += 5
    if key == 'w':
        py -= 5
    if key == 's':
        py += 5

def init():
    glClearColor(0.3,0.3,0.3,0)
    gluOrtho2D(0,1024,512,0)
    global px, py
    px = 300; py = 300

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
    glutInitWindowSize(1024, 512)
    window = glutCreateWindow("Raycaster in python")
    init()
    glutDisplayFunc(display)
    glutKeyboardFunc(buttons)
    glutMainLoop()

if __name__ == "__main__":
    main()

Why this code is not working?


Solution

  • Define px and py in global namespace:

    px = 300; py = 300
    
    def drawPlayer():
        glColor3f(1,1,0)
        glPointSize(8)
        glBegin(GL_POINTS)
        glVertex2i(px,py)
        glEnd()
    

    Use the global statement to change the variables in the global namespace within the button callback. Use the bytesprefix to compare the key to a letter (e.g. key == b'a'). Invoke glutPostRedisplay to mark the current window as needing to be redisplayed:

    def buttons(key,x,y):
        global px, py
        if key == b'a':
            px -= 5
        if key == b'd':
            px += 5
        if key == b'w':
            py -= 5
        if key == b's':
            py += 5
        glutPostRedisplay()
    

    Complete example

    from OpenGL.GL import *
    from OpenGL.GLU import *
    from OpenGL.GLUT import *
    import sys
    import math
    
    px = 300; py = 300
    
    def drawPlayer():
        glColor3f(1,1,0)
        glPointSize(8)
        glBegin(GL_POINTS)
        glVertex2i(px,py)
        glEnd()
    
    def display():
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        drawPlayer()
        glutSwapBuffers()
    
    # Here is my keyboard input code
    def buttons(key,x,y):
        global px, py
        if key == b'a':
            px -= 5
        if key == b'd':
            px += 5
        if key == b'w':
            py -= 5
        if key == b's':
            py += 5
        glutPostRedisplay()
    
    def init():
        glClearColor(0.3,0.3,0.3,0)
        gluOrtho2D(0,1024,512,0)    
    
    def main():
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
        glutInitWindowSize(1024, 512)
        window = glutCreateWindow("Raycaster in python")
        init()
        glutDisplayFunc(display)
        glutKeyboardFunc(buttons)
        glutMainLoop()
    
    if __name__ == "__main__":
        main()