MEMBUAT GAMBAR LOKOMOTIF

Assalamualaikum wr wb, pada kesempatan yang baik ini saya akan menjelaskan program yang sudah saya buat. program ini adalah gambar lokomotif yang dapat: scalling, rotation, translasi, tombol mouse dan keyboard. program ini bisa dikatakan game sederhana atau program biasa.

Langkah pertama adalah menyiapkan modul.
modul yang harus di siapkan dalam pembuatan kodingan adalah:
  1. pip install OpenGLContext alternatif install manual => https://www.lfd.uci.edu/~gohlke/pythonlibs/ 
  2. pip install PyVRML97
  3. pip install pillow
seperti biasa Bahas Pemograman Yang digunakan:
  1. Python 3.8.2
  2. Apk Editor : Visual Code (Vs Code)
Modul yang Python yang dipakai:
  1. PyOpenGL-3.1.5-cp38-cp38-win32.whl
  2. PyOpenGL_accelerate-3.1.5-cp38-cp38-win32.whl
dan penambahan modul yang sudah di install, modul ini untuk membuat text:
  1. OpenGLContext-2.3.0-py2.py3-none-any.whl
  2. PyVRML97     2.3.1
  3. Pillow              7.1.2
Modul yang digunakan dalam program ini:
from OpenGLContext import testingcontext
from random import randrange
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *


Fungsi convert rgb untuk mengkonversi nilai kode rgb menjadi format warna
# Fungsi ini digunakan untuk konversi nilai kode warna rgb menjadi format warna pada PyopenGL
def convert_rgb(warna):
    return warna / 255.0

Fungsi membuat kotak
# Fungsi Untuk Membuat Kotak
def kotak():
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(kt_red),convert_rgb(kt_green),convert_rgb(kt_blue))
    glVertex2f(-50 + koordinat_x,-50 + koordinat_y)
    glVertex2f(50 + koordinat_x,-50 + koordinat_y)
    glVertex2f(50 + koordinat_x,50 + koordinat_y)
    glVertex2f(-50 + koordinat_x, 50 + koordinat_y)
    glEnd()

Fungsi membuat lokomotif
# Fungsi Untuk Membuat sektsa lokomotif
def shape():
    # Untuk Mengatur Scalling gambar / Zooming LOKOMOTIF
    glScalef(zoom_x,zoom_y, 0.0)
    # Untuk Mengatur Translasi gambar / Pergerakan LOKOMOTIF
    glTranslatef(pos_x,pos_y,0)
    # Untuk Mengatur Rotasi pada gambar lokomotif
    glRotatef(rotation, 0.00.0,1.0)
    
    # Body Lokomotif
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(169),convert_rgb(169),convert_rgb(169))
    # Bagian Atas
    glVertex2f(-200  ,100  )
    glVertex2f(400  ,100  )
    # Bagian Kanan
    glVertex2f(400  ,0  )
    glVertex2f(400  ,-100  )
    # Bagian Bawah
    glVertex2f(-300  ,-100  )
    # Bagian Kiri
    glVertex2f(-300  ,0  )
    glVertex2f(-200  ,100  )
    
    glEnd()

   
    # Kaca Depan
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(225),convert_rgb(225),convert_rgb(225))
    glVertex2f(-170  ,5  )
    glVertex2f(-220  ,5  )
    glVertex2f(-220  ,60  )
    glVertex2f(-170 ,60  )
    glVertex2f(-170  ,5  )
    glEnd()    

    # Kaca belakang
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(225),convert_rgb(225),convert_rgb(225))
    glVertex2f(-100  ,5  )
    glVertex2f(-150  ,5  )
    glVertex2f(-150 ,60  )
    glVertex2f(-100  ,60  )
    glVertex2f(-100  ,5  )
    glEnd()

    # batas kabin lokomotif
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(0),convert_rgb(0),convert_rgb(0))
    glVertex2f(-85  ,100  )
    glVertex2f(-80  ,100  )
    glVertex2f(-80 ,-100  )
    glVertex2f(-85  ,-100  )
    glVertex2f(-85  ,100  )
    glEnd()

    # kap mesin 1
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(112),convert_rgb(128),convert_rgb(144))
    glVertex2f(200  ,80  )
    glVertex2f(60  ,80  )
    glVertex2f(60 ,-80  )
    glVertex2f(200  ,-80  )
    glVertex2f(200  ,80  )
    glEnd()

    # kap mesin 2
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(112),convert_rgb(128),convert_rgb(144))
    glVertex2f(370  ,-10  )
    glVertex2f(250  ,-10  )
    glVertex2f(250 ,-80  )
    glVertex2f(370  ,-80  )
    glVertex2f(370  ,80  )
    glEnd()

    # gandengan 1
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(139),convert_rgb(0),convert_rgb(0))
    glVertex2f(-290  ,-155  )
    glVertex2f(-300  ,-155  )
    glVertex2f(-300  ,-100  )
    glVertex2f(-290  ,-100  )
    glVertex2f(-290  ,-155  )
    glEnd()

    # gandengan 2
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(139),convert_rgb(0),convert_rgb(0))
    glVertex2f(390  ,-155  )
    glVertex2f(400  ,-155  )
    glVertex2f(400  ,-100  )
    glVertex2f(390  ,-100  )
    glVertex2f(390  ,-155  )
    glEnd()

    # roda1
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(128),convert_rgb(128),convert_rgb(128))
    glVertex2f(-60  ,-155  )
    glVertex2f(-270  ,-155  )
    glVertex2f(-270  ,-100  )
    glVertex2f(-60  ,-100  )
    glVertex2f(-60  ,-155 )
    glEnd()

    # roda2
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(128),convert_rgb(128),convert_rgb(128))
    glVertex2f(150  ,-155  )
    glVertex2f(370  ,-155  )
    glVertex2f(370  ,-100  )
    glVertex2f(150 ,-100  )
    glVertex2f(150  ,-155  )
    glEnd()

    # tangki atas
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(211),convert_rgb(211),convert_rgb(211))
    glVertex2f(-45  ,-130  )
    glVertex2f(135  ,-130  )
    glVertex2f(135  ,-100  )
    glVertex2f(-45 ,-100  )
    glVertex2f(-45  ,-130  )
    glEnd()

    # tangki bawah
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(245),convert_rgb(245),convert_rgb(245))
    glVertex2f(-25  ,-150  )
    glVertex2f(118 ,-150  )
    glVertex2f(118  ,-130  )
    glVertex2f(-25 ,-130  )
    glVertex2f(-25  ,-150  )
    glEnd()
   

    # tempat Lampu
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(125),convert_rgb(252),convert_rgb(0))
    glVertex2f(-110  ,120  )
    glVertex2f(-200  ,120  )
    glVertex2f(-200  ,100  )
    glVertex2f(-110  ,100  )
    glVertex2f(-110  ,120 )
    glEnd()

    # Cahaya Lampu 1
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(light_red),convert_rgb(light_green), convert_rgb(light_blue))
    glVertex2f(-200  ,110  )
    glVertex2f(-460  ,30)
    glVertex2f(-460  ,200)
    glVertex2f(-200  ,110)
    
    glEnd()

    # Cahaya Lampu2
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(light_red),convert_rgb(light_green), convert_rgb(light_blue))
    glVertex2f(-300  ,-62  )
    glVertex2f(-450  ,-50)
    glVertex2f(-450  ,-110)
    glVertex2f(-300  ,-72)
    
    glEnd()
    

# Fungsi ini membuat text
def drawBitmapText(string,x,y,z) :
    glRasterPos3f(x,y,z)
    for text in string :
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,ord(text))

def reshape(wh):
    glViewport(0,0,w,h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(0,w,h,0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()


# Fungsi untuk mendapatkan callback event handling Pada Mouse
def input_mouse(buttonstatexy):
    global merah, hijau, biru
    global teks_lampu
    global bg_red, bg_green, bg_blue
    global light_red, light_green, light_blue
    global kt_red, kt_green, kt_blue

    # Jika Klik Kanan ditekan maka lampu akan mati dan warna background dan kotak akan berubah
    if button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN:
        light_red = 255
        light_green = 69
        light_blue = 0
        # Warna Background
        bg_red = 255
        bg_green = 69
        bg_blue = 0
        # Warna Kotak
        kt_red = 24
        kt_green = 101
        kt_blue = 164    

        glClearColor(convert_rgb(bg_red), convert_rgb(bg_green), convert_rgb(bg_blue), 1.0)

        teks_lampu = "MATI"
        print("Klik Kanan ditekan ""(", x, ",", y, ")")
    
    # Jika Klik Kiri ditekan maka lampu akan menyala, warna background dan kotak akan berubah
    elif button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
        light_red = 255.0
        light_green = 255.0
        light_blue = 0.0
        # Warna Background
        bg_red = 255
        bg_green = 165
        bg_blue = 0
        # Warna Kotak
        kt_red = 255
        kt_green = 255
        kt_blue = 255

        glClearColor(convert_rgb(bg_red), convert_rgb(bg_green), convert_rgb(bg_blue), 1.0)
        teks_lampu = "NYALA"
        print("Klik Kiri ditekan ""(", x, ",", y, ")")

# Fungsi untuk mendapatkan callback event handling Pada Keyboard Tombol khusus
def input_keyboard_special_func(key,x,y):
    global pos_x, pos_y
    global warna_background
    global teks_merah, teks_hijau, teks_biru
 
    # Untuk mengubah posisi LOKOMOTIF dengan menekan
    # Tombol Key UP, Key DOWN, Key RIGHT, Key LEFT
    # saat ditekan nilai pada variable pos_x dan pos_y
    # akan berubah secara increment dan decremant dengan pertambahan 20
    if key == GLUT_KEY_UP:
        pos_y += 20
        print("Tombol Atas ditekan ""x : ", pos_x, " y : ", pos_y)
        print("Koor ""x : ", koordinat_x, " y : ", koordinat_y)
    elif key == GLUT_KEY_DOWN:
        pos_y -= 20
        print("Tombol Bawah ditekan ""x : ", pos_x, " y : ", pos_y)
        print("Koor ""x : ", koordinat_x, " y : ", koordinat_y)
    elif key == GLUT_KEY_RIGHT:
        pos_x += 20
        print("Tombol Kanan ditekan ""x : ", pos_x, " y : ", pos_y)
        print("Koor ""x : ", koordinat_x, " y : ", koordinat_y)
    elif key == GLUT_KEY_LEFT:
        pos_x -= 20
        print("Tombol Kiri ditekan ""x : ", pos_x, " y : ", pos_y)
        print("Koor ""x : ", koordinat_x, " y : ", koordinat_y)


# Fungsi untuk mendapatkan callback event handling Pada Keyboard Tombol yang akan mengembalikan nilai ascii
def input_keyboard_func(keyxy):

    global zoom_x, zoom_y
    global rotation

    dk = key.decode("utf-8")

    # Untuk mengatur Zoom In & Out pada gambar Tekan tombol a / s
    # Maka variable zoom_x dan zoom_y
    # akan berubah secara increment dan decrement
    # dengan pertamban 0.1
    if dk == 'a' or dk == 'A':
        zoom_x += 0.1
        zoom_y += 0.1
    elif dk == 's' or dk == 'S':
        zoom_x -= 0.1
        zoom_y -= 0.1

    # Untuk mengatur Rotasi ke kanan / kiri pada gambar Tekan tombol z / x
    # Maka variable rotation akan berubah secara increment dan decrement
    # dengan pertamban 10
    elif dk == 'z' or dk == 'Z':
        rotation -= 10
        print(rotation)
    elif dk == 'x' or dk == 'X':
        rotation += 10
        print(rotation)

def update(value):
    global koordinat_x, koordinat_y, pos_x, pos_y
    global angka
    global kt
    global kt_red, kt_green, kt_blue
    
    # Untuk mengatur posisi kotak
    # Secara Randaom
    koordinat_x = randrange(-800,100)
    koordinat_y = randrange(-100,800)

    # Untuk mendapatkan Poin
    # Ketika teks_lampu = ON
    # atau nilai koor_x, koor_y sama dengan pos_x, pos_y
    if  teks_lampu == "NYALA":
        if koordinat_x == pos_x or koordinat_y == pos_x:
            angka += 1
        
    glutPostRedisplay()
    glutTimerFunc(10,update,0)

# Fungsi untuk menampilkan Teks Pada Window
def drawText():
    glColor3f(teks_merah,teks_hijau,teks_biru)
    # Petunjuk Pemakaia
    drawBitmapText("GAMBAR : ",-460,440,0)
    drawBitmapText("SEKTSA LOKOMOTIF INDONESIA",-460,400,0)
    # Kontrol
    drawBitmapText("KONTROL :",-460,-200,0)
    drawBitmapText("- Untuk Scaling Tekan tombol => a/s a = zoom in, s = zoom out",-460,-240,0)
    drawBitmapText("- Untuk rotasi tekan tombol => z/x z = searah jarum jam, x = berlawanan",-460,-280,0)
    #INFORMASI
    drawBitmapText("INFORMASI : " ,-460,-320,0)
    drawBitmapText("-> Ganti Warna Klik Mouse Kanan dan Kiri",-460,-360,0)
    drawBitmapText("-> Untuk Menjalankan Objek Tekan Keyboard => KEY UP, KEY DOWN, KEY RIGHT, KEY LEFT " ,-460,-400,0)
    drawBitmapText("-> Angka => " + str(angka),-460,-440,0)
    drawBitmapText("-> Lampu => " + teks_lampu,-460,-480,0)

Source Code Full:
# NAMA : WIWIT FARIANTO
# NIM : 17102072
# KELAS : MM 03

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

# Variable Kooridinat Posisi 
pos_x = 0
pos_y = 0

# Variable Koordinat Posisi Kotak
koordinat_x = 0
koordinat_y = 0

# Warna Kotak
kt_red = 225
kt_green = 0
kt_blue = 255

# Warna Background
bg_red = 225
bg_green = 0
bg_blue = 0

# Warna Teks
teks_merah = 1
teks_hijau = 1
teks_biru = 1

# Teks Warna Objek
teks_lampu = "MATI"

# Rotation
rotation = 1.0

# Zoom
zoom_x = 1.0
zoom_y = 1.0

# Cahaya Lampu
light_red = 225
light_green = 255
light_blue = 0

# Poin
angka = 0


def init():
    glClearColor(convert_rgb(bg_red), convert_rgb(bg_green), convert_rgb(bg_blue), 1.0)
    gluOrtho2D(-500.0500.0, -500.0500.0)

# Fungsi ini digunakan untuk konversi nilai kode warna rgb menjadi format warna pada PyopenGL
def convert_rgb(warna):
    return warna / 255.0

# Fungsi Untuk Membuat Kotak
def kotak():
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(kt_red),convert_rgb(kt_green),convert_rgb(kt_blue))
    glVertex2f(-50 + koordinat_x,-50 + koordinat_y)
    glVertex2f(50 + koordinat_x,-50 + koordinat_y)
    glVertex2f(50 + koordinat_x,50 + koordinat_y)
    glVertex2f(-50 + koordinat_x, 50 + koordinat_y)
    glEnd()

# Fungsi Untuk Membuat sektsa lokomotif
def shape():
    # Untuk Mengatur Scalling gambar / Zooming LOKOMOTIF
    glScalef(zoom_x,zoom_y, 0.0)
    # Untuk Mengatur Translasi gambar / Pergerakan LOKOMOTIF
    glTranslatef(pos_x,pos_y,0)
    # Untuk Mengatur Rotasi pada gambar lokomotif
    glRotatef(rotation, 0.00.0,1.0)
    
    # Body Lokomotif
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(169),convert_rgb(169),convert_rgb(169))
    # Bagian Atas
    glVertex2f(-200  ,100  )
    glVertex2f(400  ,100  )
    # Bagian Kanan
    glVertex2f(400  ,0  )
    glVertex2f(400  ,-100  )
    # Bagian Bawah
    glVertex2f(-300  ,-100  )
    # Bagian Kiri
    glVertex2f(-300  ,0  )
    glVertex2f(-200  ,100  )
    
    glEnd()

   
    # Kaca Depan
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(225),convert_rgb(225),convert_rgb(225))
    glVertex2f(-170  ,5  )
    glVertex2f(-220  ,5  )
    glVertex2f(-220  ,60  )
    glVertex2f(-170 ,60  )
    glVertex2f(-170  ,5  )
    glEnd()    

    # Kaca belakang
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(225),convert_rgb(225),convert_rgb(225))
    glVertex2f(-100  ,5  )
    glVertex2f(-150  ,5  )
    glVertex2f(-150 ,60  )
    glVertex2f(-100  ,60  )
    glVertex2f(-100  ,5  )
    glEnd()

    # batas kabin lokomotif
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(0),convert_rgb(0),convert_rgb(0))
    glVertex2f(-85  ,100  )
    glVertex2f(-80  ,100  )
    glVertex2f(-80 ,-100  )
    glVertex2f(-85  ,-100  )
    glVertex2f(-85  ,100  )
    glEnd()

    # kap mesin 1
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(112),convert_rgb(128),convert_rgb(144))
    glVertex2f(200  ,80  )
    glVertex2f(60  ,80  )
    glVertex2f(60 ,-80  )
    glVertex2f(200  ,-80  )
    glVertex2f(200  ,80  )
    glEnd()

    # kap mesin 2
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(112),convert_rgb(128),convert_rgb(144))
    glVertex2f(370  ,-10  )
    glVertex2f(250  ,-10  )
    glVertex2f(250 ,-80  )
    glVertex2f(370  ,-80  )
    glVertex2f(370  ,80  )
    glEnd()

    # gandengan 1
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(139),convert_rgb(0),convert_rgb(0))
    glVertex2f(-290  ,-155  )
    glVertex2f(-300  ,-155  )
    glVertex2f(-300  ,-100  )
    glVertex2f(-290  ,-100  )
    glVertex2f(-290  ,-155  )
    glEnd()

    # gandengan 2
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(139),convert_rgb(0),convert_rgb(0))
    glVertex2f(390  ,-155  )
    glVertex2f(400  ,-155  )
    glVertex2f(400  ,-100  )
    glVertex2f(390  ,-100  )
    glVertex2f(390  ,-155  )
    glEnd()

    # roda1
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(128),convert_rgb(128),convert_rgb(128))
    glVertex2f(-60  ,-155  )
    glVertex2f(-270  ,-155  )
    glVertex2f(-270  ,-100  )
    glVertex2f(-60  ,-100  )
    glVertex2f(-60  ,-155 )
    glEnd()

    # roda2
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(128),convert_rgb(128),convert_rgb(128))
    glVertex2f(150  ,-155  )
    glVertex2f(370  ,-155  )
    glVertex2f(370  ,-100  )
    glVertex2f(150 ,-100  )
    glVertex2f(150  ,-155  )
    glEnd()

    # tangki atas
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(211),convert_rgb(211),convert_rgb(211))
    glVertex2f(-45  ,-130  )
    glVertex2f(135  ,-130  )
    glVertex2f(135  ,-100  )
    glVertex2f(-45 ,-100  )
    glVertex2f(-45  ,-130  )
    glEnd()

    # tangki bawah
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(245),convert_rgb(245),convert_rgb(245))
    glVertex2f(-25  ,-150  )
    glVertex2f(118 ,-150  )
    glVertex2f(118  ,-130  )
    glVertex2f(-25 ,-130  )
    glVertex2f(-25  ,-150  )
    glEnd()
   

    # tempat Lampu
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(125),convert_rgb(252),convert_rgb(0))
    glVertex2f(-110  ,120  )
    glVertex2f(-200  ,120  )
    glVertex2f(-200  ,100  )
    glVertex2f(-110  ,100  )
    glVertex2f(-110  ,120 )
    glEnd()

    # Cahaya Lampu 1
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(light_red),convert_rgb(light_green), convert_rgb(light_blue))
    glVertex2f(-200  ,110  )
    glVertex2f(-460  ,30)
    glVertex2f(-460  ,200)
    glVertex2f(-200  ,110)
    
    glEnd()

    # Cahaya Lampu2
    glBegin(GL_POLYGON)
    glColor3f(convert_rgb(light_red),convert_rgb(light_green), convert_rgb(light_blue))
    glVertex2f(-300  ,-62  )
    glVertex2f(-450  ,-50)
    glVertex2f(-450  ,-110)
    glVertex2f(-300  ,-72)
    
    glEnd()
       
# Fungsi ini membuat text
def drawBitmapText(string,x,y,z) :
    glRasterPos3f(x,y,z)
    for text in string :
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,ord(text))

def reshape(wh):
    glViewport(0,0,w,h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(0,w,h,0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

# Fungsi untuk mendapatkan callback event handling Pada Mouse
def input_mouse(buttonstatexy):
    global merah, hijau, biru
    global teks_lampu
    global bg_red, bg_green, bg_blue
    global light_red, light_green, light_blue
    global kt_red, kt_green, kt_blue

    # Jika Klik Kanan ditekan maka lampu akan mati dan warna background dan kotak akan berubah
    if button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN:
        light_red = 255
        light_green = 69
        light_blue = 0
        # Warna Background
        bg_red = 255
        bg_green = 69
        bg_blue = 0
        # Warna Kotak
        kt_red = 24
        kt_green = 101
        kt_blue = 164    

        glClearColor(convert_rgb(bg_red), convert_rgb(bg_green), convert_rgb(bg_blue), 1.0)

        teks_lampu = "MATI"
        print("Klik Kanan ditekan ""(", x, ",", y, ")")
    
    # Jika Klik Kiri ditekan maka lampu akan menyala, warna background dan kotak akan berubah
    elif button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
        light_red = 255.0
        light_green = 255.0
        light_blue = 0.0
        # Warna Background
        bg_red = 255
        bg_green = 165
        bg_blue = 0
        # Warna Kotak
        kt_red = 255
        kt_green = 255
        kt_blue = 255

        glClearColor(convert_rgb(bg_red), convert_rgb(bg_green), convert_rgb(bg_blue), 1.0)
        teks_lampu = "NYALA"
        print("Klik Kiri ditekan ""(", x, ",", y, ")")
    
    
# Fungsi untuk mendapatkan callback event handling Pada Keyboard Tombol khusus
def input_keyboard_special_func(key,x,y):
    global pos_x, pos_y
    global warna_background
    global teks_merah, teks_hijau, teks_biru
 
    # Untuk mengubah posisi LOKOMOTIF dengan menekan
    # Tombol Key UP, Key DOWN, Key RIGHT, Key LEFT
    # saat ditekan nilai pada variable pos_x dan pos_y
    # akan berubah secara increment dan decremant dengan pertambahan 20
    if key == GLUT_KEY_UP:
        pos_y += 20
        print("Tombol Atas ditekan ""x : ", pos_x, " y : ", pos_y)
        print("Koor ""x : ", koordinat_x, " y : ", koordinat_y)
    elif key == GLUT_KEY_DOWN:
        pos_y -= 20
        print("Tombol Bawah ditekan ""x : ", pos_x, " y : ", pos_y)
        print("Koor ""x : ", koordinat_x, " y : ", koordinat_y)
    elif key == GLUT_KEY_RIGHT:
        pos_x += 20
        print("Tombol Kanan ditekan ""x : ", pos_x, " y : ", pos_y)
        print("Koor ""x : ", koordinat_x, " y : ", koordinat_y)
    elif key == GLUT_KEY_LEFT:
        pos_x -= 20
        print("Tombol Kiri ditekan ""x : ", pos_x, " y : ", pos_y)
        print("Koor ""x : ", koordinat_x, " y : ", koordinat_y)

# Fungsi untuk mendapatkan callback event handling Pada Keyboard Tombol yang akan mengembalikan nilai ascii
def input_keyboard_func(keyxy):

    global zoom_x, zoom_y
    global rotation

    dk = key.decode("utf-8")

    # Untuk mengatur Zoom In & Out pada gambar Tekan tombol a / s
    # Maka variable zoom_x dan zoom_y
    # akan berubah secara increment dan decrement
    # dengan pertamban 0.1
    if dk == 'a' or dk == 'A':
        zoom_x += 0.1
        zoom_y += 0.1
    elif dk == 's' or dk == 'S':
        zoom_x -= 0.1
        zoom_y -= 0.1

    # Untuk mengatur Rotasi ke kanan / kiri pada gambar Tekan tombol z / x
    # Maka variable rotation akan berubah secara increment dan decrement
    # dengan pertamban 10
    elif dk == 'z' or dk == 'Z':
        rotation -= 10
        print(rotation)
    elif dk == 'x' or dk == 'X':
        rotation += 10
        print(rotation)
    

def update(value):
    global koordinat_x, koordinat_y, pos_x, pos_y
    global angka
    global kt
    global kt_red, kt_green, kt_blue
    
    # Untuk mengatur posisi kotak
    # Secara Randaom
    koordinat_x = randrange(-800,100)
    koordinat_y = randrange(-100,800)

    # Untuk mendapatkan Poin
    # Ketika teks_lampu = ON
    # atau nilai koor_x, koor_y sama dengan pos_x, pos_y
    if  teks_lampu == "NYALA":
        if koordinat_x == pos_x or koordinat_y == pos_x:
            angka += 1
        
    glutPostRedisplay()
    glutTimerFunc(10,update,0)
  

# Fungsi untuk menampilkan Teks Pada Window
def drawText():
    glColor3f(teks_merah,teks_hijau,teks_biru)
    # Petunjuk Pemakaia
    drawBitmapText("GAMBAR : ",-460,440,0)
    drawBitmapText("SEKTSA LOKOMOTIF INDONESIA",-460,400,0)
    # Kontrol
    drawBitmapText("KONTROL :",-460,-200,0)
    drawBitmapText("- Untuk Scaling Tekan tombol => a/s a = zoom in, s = zoom out",-460,-240,0)
    drawBitmapText("- Untuk rotasi tekan tombol => z/x z = searah jarum jam, x = berlawanan",-460,-280,0)
    #INFORMASI
    drawBitmapText("INFORMASI : " ,-460,-320,0)
    drawBitmapText("-> Ganti Warna Klik Mouse Kanan dan Kiri",-460,-360,0)
    drawBitmapText("-> Untuk Menjalankan Objek Tekan Keyboard => KEY UP, KEY DOWN, KEY RIGHT, KEY LEFT " ,-460,-400,0)
    drawBitmapText("-> Angka => " + str(angka),-460,-440,0)
    drawBitmapText("-> Lampu => " + teks_lampu,-460,-480,0)
   

# Fungsi untuk menampilkan semua bentuk atau objek pada Window
def display():
    glClear(GL_COLOR_BUFFER_BIT)
    # Deklarasi Sektsa Lokomotif
    glPushMatrix()
    shape()
    glPopMatrix()
    # Deklarasi Bentuk Kotak
    kotak()
    # Deklarasi untuk Teks
    drawText()

    glFlush()


def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
    glutInitWindowSize(900,600)
    glutInitWindowPosition(100,100)
    glutCreateWindow("LOKOMOTIF INDONESIA")

    glutDisplayFunc(display)

    glutSpecialFunc(input_keyboard_special_func)
    glutKeyboardFunc(input_keyboard_func)
    glutMouseFunc(input_mouse)

    glutTimerFunc(50, update, 0)

    init()
    glutMainLoop()
    
main()


#LOKOMOTIF

note:
  1. untuk mengatur zoom in dan zoom out maka mengunakan glScalef()
  2. untuk mengatur pergerakan maka glTranslatef()
  3. untuk memutar Rotasi maka gunakan glRotatef()
Kontrol :
  1. untuk pergerakan lokomotif  mengunakan keyboard => keyUp, keyDown, keyRight, keyLeft
  2. untuk scaling atau zoom, tekan tombol a/s.
  3. untuk  rotasi, tekan tombol z/s.
  4. untuk ganti warna dan  menyalakan lampu, klik mouse kanan dan kiri.

gambar dan video:




Komentar