2

GLUTグラフィックを含むウィンドウが表示されたら、ターミナルに入力を入力します。

user@computer: python woop.py
# Now displaying a beautiful landscape
(cmd): season winter
# Now changing season to winter
(cmd): event meteor
# Now meteoring otherwise peaceful landscape
(cmd): season summer
# Now changing season to summer
(cmd): exit
#bye ^_^
user@computer:

理想的には、 PythoncmdをGLUTのglutKeyboardFuncと統合したいと思います。私の試みは失敗しました(両方ではなく、どちらか一方を同時に許可します。また、ウィンドウまたは端末にフォーカスがあるかどうかの問題もあります)。

回転するティーポットを表示するサンプルコードを次に示します。現在、「m」を押すとメテオラの良さ(スタブ)が呼び出さますが、たとえば「メテオ500」と入力できることが望ましいでしょう。

#! /usr/bin/env python
''' 
Code is a reduced version of http://www.seethroughskin.com/blog/?p=771
'''
import OpenGL 
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

import time, sys

class dizzyTea:

    global rotY

    def __init__(self):
        self.main()

    def InitGL(self,Width, Height):
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glClearDepth(1.0)
        glShadeModel(GL_SMOOTH)  
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()       
        gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)     

    # The main drawing function. 
    def DrawGLScene(self):
        global rotY
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()                    # Reset The View 
        glTranslatef(-0.5, 0.0, -6.0)
        glRotatef(rotY,0.0,1.0,0.0)
        glutWireTeapot(1.0)
        glScalef(0.3,0.3,0.3)
        glutSwapBuffers()
        rotY += 1.0

    # The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
    def keyPressed(self,*args):

        # If escape is pressed, kill everything.
        if args[0] == '\x1b':
            sys.exit()
        elif args[0] == 'm':
            print "Now meteoring otherwise peaceful teapot"
            # meteor shenanigans

    def main(self):
        global window
        global rotY
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
        glutInitWindowSize(640, 480)
        glutInitWindowPosition(0, 0)
        window = glutCreateWindow("Jeff Molofee's desecrated GL Code Tutorial")
        glutDisplayFunc(self.DrawGLScene)
        glutIdleFunc(self.DrawGLScene)
        glutKeyboardFunc(self.keyPressed)
        self.InitGL(800, 600)
        rotY = 0.0
        glutMainLoop()

if __name__ == "__main__":
    x = dizzyTea()

glutKeyboardFuncを使用して文字をグローバル文字列に収集し、同じ機能効果を与えることができますが、ユーザーはブラインドと入力します。「printsomestring」を使用すると、同じ行に印刷できますが、コンマは、入力中に出力が表示されないことを意味します。また、「print'\ b'」(バックスペース)は普遍的に機能しません...

基本的に私は持ちたくない:

user@computer: python woop.py
# Now displaying a beautiful landscape
(cmd): s
(cmd): se
(cmd): sea
(cmd): seas
...etc

1つのコマンドを入力します

使用制限:

  • pyopengl
  • GLUT

(他の答えは気まぐれな未来のために歓迎されていますが-別の問題を解決しようとしている人々)

4

2 に答える 2

2
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
def keyPressed(self,*args):
    if args[0] == '\x08':
        self.keyCache = self.keyCache[:-1]
    elif args[0] == '\x1b':
        sys.exit()
    elif args[0] == 'm':
        print "Now meteoring otherwise peaceful teapot"
        # meteor shenanigans
    else:
        self.keyCache += args[0]
    sys.stdout.write(self.keyCache +"                                                  \r")#print "keypress: <",self.keyCache,">"
    sys.stdout.flush()

そして、新しいクラス変数'keyCache'を追加します。

次に、通常の印刷フラッシュを使用して、同じ行にデータを書き込みます。唯一のハッキーな点は、キャッシュされたキーストロークの後にいくつかの空白スペースを書き込む必要があることです。そうしないと、バックスペースを使用しても、削除された要素が画面に表示されたままになります。

もう1つの方法は、コマンドラインからのキーストロークを処理するための並列キーボードスレッドです。そこにある大きな問題は、ウィンドウが閉じたときに過剰が適切なコールバックを提供しないため、スレッドを強制終了する別の方法を考え出す必要があることです。

于 2011-11-30T03:17:15.187 に答える
1

cmd [0]フレームワークを使用してインタープリターを構築し、別のスレッドでcmd.cmdloopを起動する必要があります。0. http://docs.python.org/3/library/cmd.html

于 2013-07-03T08:09:56.843 に答える