Hai teman-teman, saya akan memberikan contoh lain untuk event handling pada keyboard. Kali ini dengan objek roket yang saya buat dengan sangat sederhana. Langsung saja menuju source code dan hasilnya.
Source Code lengkap :
Source Code lengkap :
from OpenGLContext import testingcontext
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
# Koordinat x dan y untuk posisi roket
pos_x = 0
pos_y = 0
# 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 reshape(w, h):
glViewport(0,0,w,h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0,w,h,0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def drawText():
glColor3f(1.0,1.0,0.0)
drawBitmapText("Warna",-460,-350,0)
drawBitmapText("Backgorund : " + warna_background ,-460,-450,0)
def convert_rgb(c):
return c / 255.0
# Membuat bentuk roket
def roket():
glBegin(GL_POLYGON)
glColor3f(convert_rgb(50),convert_rgb(50),convert_rgb(150))
glVertex2f(-40 + pos_x,100 + pos_y)
glVertex2f(40 + pos_x,100 + pos_y)
glVertex2f(-40 + pos_x,-50 + pos_y)
glVertex2f(40 + pos_x,-50 + pos_y)
glVertex2f(40 + pos_x,100 + pos_y)
glEnd()
#kerucut
glBegin(GL_POLYGON)
glColor3f(convert_rgb(255),convert_rgb(0),convert_rgb(255))
glVertex2f(-40 + pos_x,100 + pos_y)
glVertex2f(0 + pos_x,125 + pos_y)
glVertex2f(40 + pos_x,100 + pos_y)
glEnd()
#ekor
glBegin(GL_POLYGON)
glColor3f(convert_rgb(255),convert_rgb(0),convert_rgb(255))
glVertex2f(-30 + pos_x,-60 + pos_y)
glVertex2f(30 + pos_x,-60 + pos_y)
glVertex2f(-30 + pos_x,-50 + pos_y)
glVertex2f(30 + pos_x,-50 + pos_y)
glVertex2f(30 + pos_x,-60 + pos_y)
glEnd()
#jendela
glBegin(GL_POLYGON)
glColor3f(convert_rgb(210),convert_rgb(220),convert_rgb(50))
glVertex2f(-30 + pos_x,80 + pos_y)
glVertex2f(-5 + pos_x,80 + pos_y)
glVertex2f(-30 + pos_x,50 + pos_y)
glVertex2f(-5 + pos_x,50 + pos_y)
glVertex2f(-5 + pos_x,80 + pos_y)
glEnd()
glBegin(GL_POLYGON)
glColor3f(convert_rgb(210),convert_rgb(220),convert_rgb(50))
glVertex2f(30 + pos_x,80 + pos_y)
glVertex2f(5 + pos_x,80 + pos_y)
glVertex2f(30 + pos_x,50 + pos_y)
glVertex2f(5 + pos_x,50 + pos_y)
glVertex2f(5 + pos_x,80 + pos_y)
glEnd()
#turbo
glBegin(GL_POLYGON)
glColor3f(convert_rgb(255),convert_rgb(0),convert_rgb(0))
glVertex2f(-25 + pos_x,-60 + pos_y)
glVertex2f(-20 + pos_x,-60 + pos_y)
glVertex2f(-25 + pos_x,-80 + pos_y)
glVertex2f(-20 + pos_x,-80 + pos_y)
glVertex2f(-20 + pos_x,-60 + pos_y)
glEnd()
glBegin(GL_POLYGON)
glColor3f(convert_rgb(255),convert_rgb(0),convert_rgb(0))
glVertex2f(-15 + pos_x,-60 + pos_y)
glVertex2f(-10 + pos_x,-60 + pos_y)
glVertex2f(-15 + pos_x,-80 + pos_y)
glVertex2f(-10 + pos_x,-80 + pos_y)
glVertex2f(-10 + pos_x,-60 + pos_y)
glEnd()
glBegin(GL_POLYGON)
glColor3f(convert_rgb(255),convert_rgb(0),convert_rgb(0))
glVertex2f(-5 + pos_x,-60 + pos_y)
glVertex2f(5 + pos_x,-60 + pos_y)
glVertex2f(-5 + pos_x,-80 + pos_y)
glVertex2f(5 + pos_x,-80 + pos_y)
glVertex2f(5 + pos_x,-60 + pos_y)
glEnd()
glBegin(GL_POLYGON)
glColor3f(convert_rgb(255),convert_rgb(0),convert_rgb(0))
glVertex2f(10 + pos_x,-60 + pos_y)
glVertex2f(15 + pos_x,-60 + pos_y)
glVertex2f(10 + pos_x,-80 + pos_y)
glVertex2f(15 + pos_x,-80 + pos_y)
glVertex2f(15 + pos_x,-60 + pos_y)
glEnd()
glBegin(GL_POLYGON)
glColor3f(convert_rgb(255),convert_rgb(0),convert_rgb(0))
glVertex2f(20 + pos_x,-60 + pos_y)
glVertex2f(25 + pos_x,-60 + pos_y)
glVertex2f(20 + pos_x,-80 + pos_y)
glVertex2f(25 + pos_x,-80 + pos_y)
glVertex2f(25 + pos_x,-60 + pos_y)
glEnd()
def display():
glClear(GL_COLOR_BUFFER_BIT)
drawText()
glColor3f(1.0,1.0,1.0)
glBegin(GL_LINES)
glVertex2f(-500.0, 0.0)
glVertex2f(500.0, 0.0)
glVertex2f(0.0, 500.0)
glVertex2f(0.0, -500.0)
glEnd()
glPushMatrix()
roket()
glPopMatrix()
glFlush()
def input_keyboard(key,x,y):
global pos_x, pos_y
global warna_background
# Untuk mengubah posisi roket
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)
# Untuk Mengubah Warna backgorund
# 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"
# Background Kanan Atas berubah warna menjadi Hijau
if pos_x > 0 and pos_y > 0:
glClearColor(1.0, 1.0, 0.0, 0.0)
warna_background = "Kuning"
# Background Kanan Bawah berubah warna menjadi Biru
if pos_x > 0 and pos_y < 0:
glClearColor(0.0,1.0,1.0,1.0)
warna_background = "cyan"
# 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"
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("ROKET")
glutDisplayFunc(display)
glutSpecialFunc(input_keyboard)
glutTimerFunc(50, update, 0)
init()
glutMainLoop()
main()
Output :
Sekian dan selamat mencoba!
Komentar
Posting Komentar