Mouse Key Board Text


Haiii sobat gacor pada kesempatan kali ini saya ingin membagikan sedikit ilmu yang sedang dipelajari yaitu tentang Menggabungkan Event Handling pada Mouse & Keyboard dengan Font & Text pada PyOpenGL

Contoh code :

from OpenGLContext import testingcontext
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

# Koordinat x dan y untuk posisi kotak
pos_x = 0
pos_y = 0

# Warna Kotak
red = 0
green = 0
blue = 0

# Warna Teks
txtred = 1
txtgreen = 1
txtblue = 1

# Teks Warna Objek
warna_objek = "Hitam"

# Teks Warna Background
warna_background = "Hitam"

def init():
    glClearColor(0.0, 0.0, 0.0, 1.0)
    gluOrtho2D(-500.0, 500.0, -500.0, 500.0)

def drawBitmapText(string,x,y,z) :
    glRasterPos3f(x,y,z)
    for c in string :
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,ord(c))

def drawText():
    glColor3f(txtred,txtgreen,txtblue)
    drawBitmapText("Warna",-460,-350,0)
    drawBitmapText("Objek : " + warna_objek,-460,-400,0)
    drawBitmapText("Background : " + warna_background ,-460,-450,0)
    
def display():
    glClear(GL_COLOR_BUFFER_BIT)

    glColor3f(1.0,0.7,0.0)
    for i in range(0,30):
        s = 100
        glRotatef(s, 1.0, 0.0, 0.0)
        segitiga() 
        drawText(); 
          
    glFlush()

# Membuat bentuk segitiga
def segitiga():
    global pos_x, pos_y
    glColor3f(red,green,blue)

    glBegin(GL_TRIANGLES)
    # Kiri Atas
    glVertex2f(-50 + pos_x,-50 + pos_y)
    # Kanan Atas
    glVertex2f(50 + pos_x,-50 + pos_y)
    # Kanan Bawah
    glVertex2f(50 + pos_x,50 + pos_y)
    # Kiri Bawah
    glVertex2f(-50 + pos_x,50 + pos_y)
    glEnd()

def input_mouse(button, state, x, y):
    global red, green, blue
    global warna_objek
    
    # Klik kanan = objek merah hijau/biru
    if button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN:
        if green < 1:
            red = 0
            green = 1
            blue = 0
            warna_objek = "Hijau"
        elif blue < 1:
            red = 0
            green = 0
            blue = 1
            warna_objek = "Biru"
        print("Klik Kanan ditekan ", "(", x, ",", y, ")")
    # Klik kiri = objek merah hitam/merah
    elif button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
        if red < 1:
            red = 1
            green = 0
            blue = 0
            warna_objek = "Merah"
        else:
            red = 0
            green = 0
            blue = 0
            warna_objek = "Hitam"
        print("Klik Kiri ditekan ", "(", x, ",", y, ")")

def input_keyboard(key,x,y):
    global pos_x, pos_y
    global warna_background
    global txtred, txtgreen, txtblue

    # Untuk mengubah posisi objek
    if key == GLUT_KEY_UP:
        pos_y += 5
        print("Tombol Atas ditekan ", "x : ", pos_x, " y : ", pos_y)
    elif key == GLUT_KEY_DOWN:
        pos_y -= 5
        print("Tombol Bawah ditekan ", "x : ", pos_x, " y : ", pos_y)
    elif key == GLUT_KEY_RIGHT:
        pos_x += 5
        print("Tombol Kanan ditekan ", "x : ", pos_x, " y : ", pos_y)
    elif key == GLUT_KEY_LEFT:
        pos_x -= 5
        print("Tombol Kiri ditekan ", "x : ", pos_x, " y : ", pos_y)

    # Ubah warna bg window
    # Background Kiri Atas berubah warna menjadi Merah
    if pos_x < 0 and pos_y > 0:
        glClearColor(1.0, 0.0, 0.0, 1.0)
        warna_background = "Merah"
        txtred = 0
        txtgreen = 0
        txtblue = 0

    # Background Kiri Bawah berubah warna menjadi Hitam
    if pos_x < 0 and pos_y < 0:
        glClearColor(0.0,0.0,0.0,1.0)
        warna_background = "Hitam"
        txtred = 1
        txtgreen = 1
        txtblue = 1

    # Background Kanan Atas berubah warna menjadi Hijau
    if pos_x > 0 and pos_y > 0:
        glClearColor(0.0, 1.0, 0.0, 1.0)
        warna_background = "Hijau"
        txtred = 0
        txtgreen = 0
        txtblue = 0

    # Background Kanan Bawah berubah warna menjadi Biru
    if pos_x > 0 and pos_y < 0:
        glClearColor(0.0,0.0,1.0,1.0)
        warna_background = "Biru"
        txtred = 1
        txtgreen = 1
        txtblue = 1

def update(value):
    glutPostRedisplay()
    glutTimerFunc(10,update,0)

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
    glutInitWindowSize(500,500)
    glutInitWindowPosition(100,100)
    glutCreateWindow("mousekeyboardtext")
    glutDisplayFunc(display)
    glutSpecialFunc(input_keyboard)
    glutMouseFunc(input_mouse)
    glutTimerFunc(50, update, 0)
    init()
    glutMainLoop()

main()


Tidak ada komentar:

Diberdayakan oleh Blogger.