OpenGL pygame移动对象

时间:2018-04-29 07:34:26

标签: python opengl pygame

下面是我的OpenGL pygame代码。我需要移动物体 - 人类通过他的方式到达目的地。我努力使用翻译功能移动,但他并没有潜水。请帮帮我。

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from pygame.locals import *
import pygame
import sys

def text(s):
    map(glutBitmapCharacter,(GLUT_BITMAP_TIMES_ROMAN_24,)*len(s),map(ord, s)) #text font and size setting   
def text2(s):
    map(glutBitmapCharacter,(GLUT_BITMAP_HELVETICA_18,)*len(s),map(ord, s)) 
def text3(s):
    map(glutBitmapCharacter,(GLUT_BITMAP_9_BY_15,)*len(s),map(ord, s))
def text4(s):
    map(glutBitmapCharacter,(GLUT_BITMAP_HELVETICA_12,)*len(s),map(ord, s)) 


def display():      
    glClearColor(0.0,0.0,0.0,1.0)           
    glClear(GL_COLOR_BUFFER_BIT)
    gluOrtho2D(-25.0,25.0,-25.0,25.0)
    glColor3f(0.4,0.9,0.8)      #college name and project details
    glRasterPos2i(-5,22)
    text("SRINIVAS UNIVERSITY")
    glColor3f(0.5,0.2,0.4)
    glRasterPos2i(-11,20)
    text2("COLLEGE   OF   ENGINEERING   AND   TECHNOLOGY ,  MANGALURU")
    glColor3f(0.4,0.8,0.5)
    glRasterPos2i(-7,18)
    text4("[ DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ]")
    glColor3f(1.0,0.0,0.3)
    glRasterPos2i(-10,15)
    text2("COMPUTER GRAPHICS AND VISUALIZATION MINI PROJECT")      
    glPointSize(0.5)    
    glColor3f(0.9,0.8,0.0)
    glRasterPos2i(-5,12)
    text("   T H E  S U L O  -  G O")

    glColor3f(0.4,0.3,0.0)  #Team Members
    glRasterPos2i(-19,0)
    text2("TEAM MEMBERS ")
    glColor3f(0.4,0.9,0.8)
    glRasterPos2i(-20,-2)
    text2("CS2015006 - ASHIK TR")
    glRasterPos2i(-20,-4)
    text2("CS2015007 - DEEKSHA")

    glColor3f(0.4,0.9,0.8)  #project guide
    glRasterPos2i(13,0)
    text2("Mr SUNIL  C K")
    glColor3f(0.6,0.0,0.2)
    glRasterPos2i(12,-2)
    text2("ASSISTANT PROFESSOR")
    glColor3f(0.4,0.3,0.0)  
    glRasterPos2i(13,-4)
    text2("PROJECT   GUIDE")

    glColor3f(0.9,1.0,0.7)  # 'C|c' button to continue
    glRasterPos2i(-13,-14)  
    text2("[              PRESS      TO  CONTINUE.  PRESSING       BUTTON  EXITS  THE  WINDOW ]")   

    glColor3f(1.0,0.0,0.3)  
    glRasterPos2i(-13,-14)  
    text2("                             C                                                     E")   

    glColor3f(1.0,0.0,0.3)  
    glRasterPos2i(-13,-14)
    text2("  NOTE:   ")

    glColor3f(0.9,1.0,0.7)  
    glRasterPos2i(-13,-14)
    text2("[   ")   
    glFlush()

def page2():
    glClearColor(1.0,1.0,1.0,1.0)
    glClear(GL_COLOR_BUFFER_BIT)
    gluOrtho2D(0.0,1400.0,0.0,900.0)

    glColor3f(0.0,0.7,0.0)  #GRASS COLOR GREEN
    glBegin(GL_POLYGON)
    glVertex2f(0.0,0.0)
    glVertex2f(0.0,350.0)
    glVertex2f(1400.0,350.0)
    glVertex2f(1400.0,0.0)
    glVertex2f(0.0,0.0)
    glEnd()

    glColor3f(0.0,0.8,0.9)  #SKY COLOR BLUE
    glBegin(GL_POLYGON)
    glVertex2f(0.0,350.0)
    glVertex2f(0.0,900.0)
    glVertex2f(1400.0,900.0)
    glVertex2f(1400.0,350.0)
    glVertex2f(0.0,350.0)
    glEnd()

    glColor3f(0.1,0.1,0.1)  #stone1
    glBegin(GL_POLYGON)
    glVertex2f(200.0,350.0)
    glVertex2f(200.0,400.0)
    glVertex2f(350.0,400.0)
    glVertex2f(350.0,350.0)
    glVertex2f(200.0,350.0)
    glEnd()


    glColor3f(0.3,0.1,0.1)  #stone1 outline left side
    glBegin(GL_POLYGON)
    glVertex2f(190.0,350.0)
    glVertex2f(190.0,400.0)
    glVertex2f(200.0,400.0)
    glVertex2f(200.0,350.0)
    glVertex2f(190.0,350.0)
    glEnd()

    glColor3f(0.3,0.4,0.0)  #stone1 outline up side
    glBegin(GL_POLYGON)
    glVertex2f(185.0,400.0)
    glVertex2f(185.0,410.0)
    glVertex2f(300.0,410.0)
    glVertex2f(300.0,400.0)
    glVertex2f(185.0,400.0)
    glEnd()

    glColor3f(0.1,0.1,0.1)  #stone2
    glBegin(GL_POLYGON)
    glVertex2f(300.0,400.0)
    glVertex2f(300.0,450.0)
    glVertex2f(350.0,450.0)
    glVertex2f(350.0,400.0)
    glVertex2f(300.0,400.0)
    glEnd()

    glColor3f(0.3,0.1,0.1)  #stone2 outline left side
    glBegin(GL_POLYGON)
    glVertex2f(290.0,350.0)
    glVertex2f(290.0,450.0)
    glVertex2f(300.0,450.0)
    glVertex2f(300.0,350.0)
    glVertex2f(290.0,350.0)
    glEnd()

    glColor3f(0.3,0.4,0.0)  #stone2 outline up side
    glBegin(GL_POLYGON)
    glVertex2f(285.0,450.0)
    glVertex2f(285.0,460.0)
    glVertex2f(365.0,460.0)
    glVertex2f(365.0,450.0)
    glVertex2f(285.0,450.0)
    glEnd()

    glColor3f(0.3,0.1,0.1)  #stone2 right side
    glBegin(GL_POLYGON)
    glVertex2f(350.0,350.0)
    glVertex2f(350.0,450.0)
    glVertex2f(360.0,450.0)
    glVertex2f(360.0,350.0)
    glVertex2f(350.0,350.0)
    glEnd()

    glColor3f(0.0,1.0,0.0)  #GRASS COLOR GREEN
    glBegin(GL_POLYGON)
    glVertex2f(0.0,340.0)
    glVertex2f(0.0,350.0)
    glVertex2f(1400.0,350.0)
    glVertex2f(1400.0,340.0)
    glVertex2f(0.0,340.0)
    glEnd()

    glColor3f(0.3,0.1,0.1)  #stone 3
    glBegin(GL_POLYGON)
    glVertex2f(400.0,350.0)
    glVertex2f(400.0,500.0)
    glVertex2f(500.0,500.0)
    glVertex2f(500.0,350.0)
    glVertex2f(400.0,350.0)
    glEnd()

    glColor3f(0.1,0.1,0.1)  #stone 3 left
    glBegin(GL_POLYGON)
    glVertex2f(410.0,350.0)
    glVertex2f(410.0,490.0)
    glVertex2f(490.0,490.0)
    glVertex2f(490.0,350.0)
    glVertex2f(400.0,350.0)
    glEnd()

    glColor3f(0.3,0.4,0.1)  #stone 3 up
    glBegin(GL_POLYGON)
    glVertex2f(395.0,500.0)
    glVertex2f(395.0,510.0)
    glVertex2f(450.0,510.0)
    glVertex2f(450.0,500.0)
    glVertex2f(395.0,500.0)
    glEnd()

    glColor3f(0.1,0.1,0.1)  #stone 4
    glBegin(GL_POLYGON)
    glVertex2f(450.0,500.0)
    glVertex2f(450.0,550.0)
    glVertex2f(500.0,550.0)
    glVertex2f(500.0,500.0)
    glVertex2f(450.0,500.0)
    glEnd()

    glColor3f(0.3,0.1,0.1)  #stone 4 left
    glBegin(GL_POLYGON)
    glVertex2f(440.0,500.0)
    glVertex2f(440.0,550.0)
    glVertex2f(450.0,550.0)
    glVertex2f(450.0,500.0)
    glVertex2f(440.0,500.0)
    glEnd()

    glColor3f(0.3,0.1,0.1)  #stone 4 right
    glBegin(GL_POLYGON)
    glVertex2f(490.0,500.0)
    glVertex2f(490.0,550.0)
    glVertex2f(500.0,550.0)
    glVertex2f(500.0,500.0)
    glVertex2f(490.0,500.0)
    glEnd()


    glColor3f(0.3,0.4,0.0)  #stone 4 up
    glBegin(GL_POLYGON)
    glVertex2f(435.0,540.0)
    glVertex2f(435.0,550.0)
    glVertex2f(505.0,550.0)
    glVertex2f(505.0,540.0)
    glVertex2f(435.0,540.0)
    glEnd()


    glColor3f(0.9,0.0,0.0)  #fire 1
    glBegin(GL_POLYGON)
    glVertex2f(360.0,350.0)
    glVertex2f(360.0,375.0)
    glVertex2f(400.0,375.0)
    glVertex2f(400.0,350.0)
    glVertex2f(360.0,350.0)
    glEnd()

    glColor3f(1.0,0.8,0.2)  
    glBegin(GL_POLYGON)
    glVertex2f(360.0,370.0)
    glVertex2f(360.0,380.0)
    glVertex2f(400.0,380.0)
    glVertex2f(400.0,370.0)
    glVertex2f(360.0,370.0)
    glEnd()

    glColor3f(0.9,0.0,0.0)  
    glBegin(GL_POLYGON)
    glVertex2f(360.0,380.0)
    glVertex2f(360.0,390.0)
    glVertex2f(400.0,390.0)
    glVertex2f(400.0,380.0)
    glVertex2f(360.0,380.0)
    glEnd()

    glColor3f(0.9,0.0,0.0)  
    glBegin(GL_POLYGON)
    glVertex2f(500.0,350.0)
    glVertex2f(500.0,380.0)
    glVertex2f(600.0,380.0)
    glVertex2f(600.0,350.0)
    glVertex2f(500.0,350.0)
    glEnd()


    glColor3f(0.9,0.0,0.0)  #fire 2
    glBegin(GL_POLYGON)
    glVertex2f(500.0,350.0)
    glVertex2f(500.0,380.0)
    glVertex2f(600.0,380.0)
    glVertex2f(600.0,350.0)
    glVertex2f(500.0,350.0)
    glEnd()

    glColor3f(1.0,0.8,0.2)  
    glBegin(GL_POLYGON)
    glVertex2f(500.0,380.0)
    glVertex2f(500.0,390.0)
    glVertex2f(600.0,390.0)
    glVertex2f(600.0,380.0)
    glVertex2f(500.0,380.0)
    glEnd()

    glColor3f(0.9,0.0,0.0)  
    glBegin(GL_POLYGON)
    glVertex2f(500.0,390.0)
    glVertex2f(500.0,400.0)
    glVertex2f(600.0,400.0)
    glVertex2f(600.0,390.0)
    glVertex2f(500.0,390.0)
    glEnd()

    glColor3f(0.3,0.1,0.1)  #stone 5    
    glBegin(GL_POLYGON)
    glVertex2f(600.0,350.0)
    glVertex2f(600.0,400.0)
    glVertex2f(750.0,400.0)
    glVertex2f(750.0,350.0)
    glVertex2f(600.0,350.0)
    glEnd()


    glColor3f(0.1,0.1,0.1)      
    glBegin(GL_POLYGON)
    glVertex2f(610.0,350.0)
    glVertex2f(610.0,390.0)
    glVertex2f(740.0,390.0)
    glVertex2f(740.0,350.0)
    glVertex2f(610.0,350.0)
    glEnd()


    glColor3f(0.3,0.1,0.1)  #stone 6    
    glBegin(GL_POLYGON)
    glVertex2f(600.0,400.0)
    glVertex2f(600.0,450.0)
    glVertex2f(700.0,450.0)
    glVertex2f(700.0,400.0)
    glVertex2f(600.0,400.0)
    glEnd()


    glColor3f(0.1,0.1,0.1)  
    glBegin(GL_POLYGON)
    glVertex2f(610.0,400.0)
    glVertex2f(610.0,440.0)
    glVertex2f(690.0,440.0)
    glVertex2f(690.0,400.0)
    glVertex2f(690.0,400.0)
    glEnd()


    glColor3f(0.3,0.1,0.1)  #stone 7    
    glBegin(GL_POLYGON)
    glVertex2f(600.0,450.0)
    glVertex2f(600.0,500.0)
    glVertex2f(650.0,500.0)
    glVertex2f(650.0,450.0)
    glVertex2f(600.0,450.0)
    glEnd()

    glColor3f(0.1,0.1,0.1)      
    glBegin(GL_POLYGON)
    glVertex2f(610.0,450.0)
    glVertex2f(610.0,500.0)
    glVertex2f(640.0,500.0)
    glVertex2f(640.0,450.0)
    glVertex2f(610.0,450.0)
    glEnd()


    glColor3f(0.3,0.4,0.0)  #green mat on 7,6,5th stones    
    glBegin(GL_POLYGON)
    glVertex2f(595.0,500.0)
    glVertex2f(595.0,510.0)
    glVertex2f(655.0,510.0)
    glVertex2f(655.0,500.0)
    glVertex2f(595.0,500.0)
    glEnd()

    glColor3f(0.3,0.4,0.0)      
    glBegin(GL_POLYGON)
    glVertex2f(650.0,450.0)
    glVertex2f(650.0,460.0)
    glVertex2f(705.0,460.0)
    glVertex2f(705.0,450.0)
    glVertex2f(650.0,450.0)
    glEnd()



    glColor3f(0.3,0.4,0.0)      
    glBegin(GL_POLYGON)
    glVertex2f(700.0,400.0)
    glVertex2f(700.0,410.0)
    glVertex2f(755.0,410.0)
    glVertex2f(755.0,400.0)
    glVertex2f(700.0,400.0)
    glEnd()




    glColor3f(0.3,0.1,0.1)  #stone 8    
    glBegin(GL_POLYGON)
    glVertex2f(810.0,430.0)
    glVertex2f(810.0,440.0)
    glVertex2f(850.0,440.0)
    glVertex2f(850.0,430.0)
    glVertex2f(810.0,430.0)
    glEnd()


    glColor3f(0.3,0.4,0.0)      
    glBegin(GL_POLYGON)
    glVertex2f(805.0,440.0)
    glVertex2f(805.0,450.0)
    glVertex2f(855.0,450.0)
    glVertex2f(855.0,440.0)
    glVertex2f(805.0,440.0)
    glEnd()


    glColor3f(0.3,0.1,0.1)      
    glBegin(GL_POLYGON)
    glVertex2f(825.0,350.0)
    glVertex2f(825.0,440.0)
    glVertex2f(835.0,440.0)
    glVertex2f(835.0,350.0)
    glVertex2f(825.0,350.0)
    glEnd()




    glColor3f(0.3,0.1,0.1)  #stone 9    
    glBegin(GL_POLYGON)
    glVertex2f(910.0,440.0)
    glVertex2f(910.0,450.0)
    glVertex2f(950.0,450.0)
    glVertex2f(950.0,440.0)
    glVertex2f(910.0,440.0)
    glEnd()


    glColor3f(0.3,0.4,0.0)      
    glBegin(GL_POLYGON)
    glVertex2f(905.0,450.0)
    glVertex2f(905.0,460.0)
    glVertex2f(955.0,460.0)
    glVertex2f(955.0,450.0)
    glVertex2f(905.0,450.0)
    glEnd()


    glColor3f(0.3,0.1,0.1)      
    glBegin(GL_POLYGON)
    glVertex2f(925.0,350.0)
    glVertex2f(925.0,450.0)
    glVertex2f(935.0,450.0)
    glVertex2f(935.0,350.0)
    glVertex2f(925.0,350.0)
    glEnd()

    glColor3f(1.0,0.8,0.2)  #fire 3 
    glBegin(GL_POLYGON)
    glVertex2f(835.0,350.0)
    glVertex2f(835.0,375.0)
    glVertex2f(925.0,375.0)
    glVertex2f(925.0,350.0)
    glVertex2f(835.0,350.0)
    glEnd()


    glColor3f(0.9,0.0,0.0)      
    glBegin(GL_POLYGON)
    glVertex2f(835.0,350.0)
    glVertex2f(835.0,365.0)
    glVertex2f(925.0,365.0)
    glVertex2f(925.0,350.0)
    glVertex2f(835.0,350.0)
    glEnd()


    glColor3f(0.9,0.0,0.0)      
    glBegin(GL_POLYGON)
    glVertex2f(835.0,375.0)
    glVertex2f(835.0,385.0)
    glVertex2f(925.0,385.0)
    glVertex2f(925.0,375.0)
    glVertex2f(835.0,375.0)
    glEnd()

    glColor3f(0.1,0.1,0.1)  # stone 10 stage    
    glBegin(GL_POLYGON)
    glVertex2f(1000.0,350.0)
    glVertex2f(1000.0,375.0)
    glVertex2f(1050.0,375.0)
    glVertex2f(1050.0,350.0)
    glVertex2f(1000.0,350.0)
    glEnd()

    glColor3f(0.3,0.5,0.0)      
    glBegin(GL_POLYGON)
    glVertex2f(990.0,370.0)
    glVertex2f(990.0,385.0)
    glVertex2f(1060.0,385.0)
    glVertex2f(1060.0,370.0)
    glVertex2f(990.0,370.0)
    glEnd()

    glColor3f(0.3,0.4,0.0)      
    glBegin(GL_POLYGON)
    glVertex2f(985.0,385.0)
    glVertex2f(985.0,390.0)
    glVertex2f(1065.0,390.0)
    glVertex2f(1065.0,385.0)
    glVertex2f(985.0,385.0)
    glEnd()

    glColor3f(0.1,0.1,0.1)  #logo board 
    glBegin(GL_POLYGON)
    glVertex2f(1100.0,350.0)
    glVertex2f(1100.0,500.0)
    glVertex2f(1250.0,500.0)
    glVertex2f(1250.0,350.0)
    glVertex2f(1100.0,350.0)
    glEnd()

    glColor3f(1.0,0.8,0.2) 
    glBegin(GL_POLYGON)
    glVertex2f(1090.0,350.0)
    glVertex2f(1090.0,510.0)
    glVertex2f(1100.0,510.0)
    glVertex2f(1100.0,350.0)
    glVertex2f(1090.0,350.0)
    glEnd()

    glColor3f(1.0,0.8,0.2) 
    glBegin(GL_POLYGON)
    glVertex2f(1100.0,500.0)
    glVertex2f(1100.0,510.0)
    glVertex2f(1250.0,510.0)
    glVertex2f(1250.0,500.0)
    glVertex2f(1100.0,500.0)
    glEnd()

    glColor3f(1.0,0.8,0.2) 
    glBegin(GL_POLYGON)
    glVertex2f(1250.0,350.0)
    glVertex2f(1250.0,510.0)
    glVertex2f(1260.0,510.0)
    glVertex2f(1260.0,350.0)
    glVertex2f(1250.0,350.0)
    glEnd() 
    Human()  #calling human


def Human():
    glColor3f(0.3,0.1,0.1)   #human left leg 
    glBegin(GL_POLYGON)
    glVertex2f(50.0,350.0)
    glVertex2f(50.0,375.0)
    glVertex2f(60.0,375.0)
    glVertex2f(60.0,350.0)
    glVertex2f(50.0,350.0)
    glEnd()

    glColor3f(0.3,0.1,0.1)  #right leg
    glBegin(GL_POLYGON)
    glVertex2f(65.0,350.0)
    glVertex2f(65.0,375.0)
    glVertex2f(75.0,375.0)
    glVertex2f(75.0,350.0)
    glVertex2f(65.0,350.0)
    glEnd()


    glColor3f(0.2,0.2,0.8)  #body
    glBegin(GL_POLYGON)
    glVertex2f(50.0,375.0)
    glVertex2f(50.0,400.0)
    glVertex2f(75.0,400.0)
    glVertex2f(75.0,375.0)
    glVertex2f(50.0,375.0)
    glEnd()

    glColor3f(0.9,0.8,0.5) #hand
    glBegin(GL_POLYGON)
    glVertex2f(45.0,380.0)
    glVertex2f(45.0,400.0)
    glVertex2f(50.0,400.0)
    glVertex2f(50.0,380.0)
    glVertex2f(45.0,380.0)
    glEnd()


    glColor3f(0.9,0.8,0.5) #hand
    glBegin(GL_POLYGON)
    glVertex2f(75.0,380.0)
    glVertex2f(75.0,400.0)
    glVertex2f(80.0,400.0)
    glVertex2f(80.0,380.0)
    glVertex2f(75.0,380.0)
    glEnd()


    glColor3f(1.0,0.8,0.5) #face
    glBegin(GL_POLYGON)
    glVertex2f(55.0,400.0)
    glVertex2f(55.0,415.0)
    glVertex2f(70.0,415.0)
    glVertex2f(70.0,400.0)
    glVertex2f(55.0,400.0)
    glEnd()


    glColor3f(0.8,0.0,0.0) #hat
    glBegin(GL_POLYGON)
    glVertex2f(45.0,415.0)
    glVertex2f(45.0,420.0)
    glVertex2f(80.0,420.0)
    glVertex2f(80.0,415.0)
    glVertex2f(45.0,415.0)
    glEnd()

    glColor3f(0.8,0.1,0.1) 
    glBegin(GL_POLYGON)
    glVertex2f(55.0,420.0)
    glVertex2f(55.0,430.0)
    glVertex2f(70.0,430.0)
    glVertex2f(70.0,420.0)
    glVertex2f(55.0,420.0)


    glEnd()
    glFlush()

def ctocontinue(*args):             
    if args[0] == 'C':          # if C MOVE TO PAGE 2 
        glClearColor(1.0,1.0,1.0,1.0) 
        glClear(GL_COLOR_BUFFER_BIT)
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
        glutInitWindowSize(1600,1200)
        glutInitWindowPosition(0,0)
        glutCreateWindow("THE SULO-GO MINI PROJECT")
        glutDisplayFunc(page2)

    elif args[0] == 'c':            # if C MOVE TO PAGE 2 
        glClearColor(1.0,1.0,1.0,1.0) 
        glClear(GL_COLOR_BUFFER_BIT)
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
        glutInitWindowSize(1600,1200)
        glutInitWindowPosition(0,0)
        glutCreateWindow("THE SULO-GO MINI PROJECT")
        glutDisplayFunc(page2)  

    elif args[0] == 'E':            #if E exit
        exit()
    elif args[0] == 'e':            #if E exit
        exit()


def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
    glutInitWindowSize(1400,900)
    glutInitWindowPosition(0,0)
    glutCreateWindow("THE SULO-GO MINI PROJECT")
    glutDisplayFunc(display)
    glutKeyboardFunc(ctocontinue)
    glutMainLoop()

main()

1 个答案:

答案 0 :(得分:1)

使用OpenGL固定功能管道矩阵堆栈,请参阅OpenGL Programming Guide Chapter 3 Viewing

将正交投影矩阵放在投影矩阵(GL_PROJECTION)堆栈上(参见glMatrixMode)和init 模型视图矩阵堆栈(GL_MODELVIEW)。进一步强制窗口连续重新显示(glutPostRedisplay):

def page2():

    glClearColor(1.0,1.0,1.0,1.0)
    glClear(GL_COLOR_BUFFER_BIT)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(0.0,1400.0,0.0,900.0)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    .....

    Human()

    glFlush()
    glutPostRedisplay()

使用glTranslatef来设置"人类"的位置:

pos_x = 0
pos_y = 0

def Human():

    global pos_x
    global pos_y

    glMatrixMode(GL_MODELVIEW)
    glPushMatrix()
    glTranslatef( pos_x, pos_y, 0 )

    ....

    glPopMatrix()
    return

修改"人类"的位置通过键,但切换到第二页时不要创建新窗口,只需调整现有OpenGL窗口的大小(glutReshapeWindow):

def ctocontinue(*args): 

    global pos_x
    global pos_y

    if args[0] == 'C' or args[0] == 'c':
        glutReshapeWindow(1600,1200)
        glutDisplayFunc(page2)
        glutMainLoop()

    elif args[0] == 'A' or args[0] == 'a':
        pos_x = pos_x - 10
    elif args[0] == 'D' or args[0] == 'd':
        pos_x = pos_x + 10

    elif args[0] == 'E' or args[0] == 'e':
        exit()

如果要创建新窗口,则必须将键盘回调设置为新窗口(glutKeyboardFunc):

def ctocontinue(*args):             
    global pos_x
    global pos_y

    if args[0] == 'C' or args[0] == 'c':
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
        glutInitWindowSize(1600,1200)
        glutInitWindowPosition(0,0)
        glutCreateWindow("THE SULO-GO MINI PROJECT")
        glutKeyboardFunc(ctocontinue)
        glutDisplayFunc(page2)
        glutMainLoop()

    elif args[0] == 'A' or args[0] == 'a':
        pos_x = pos_x - 10
    elif args[0] == 'D' or args[0] == 'd':
        pos_x = pos_x + 10

    elif args[0] == 'E' or args[0] == 'e':
        exit()