Library Yang Digunakan
- PyOpenGL_accelerate-3.1.5
- OpenGLContext 2.3.0
- PyVRML97 2.3.1
- Tambahkan Library OpenGLContext
- Tambahkan variable global untuk mengatur warna pada teks serta tulisan
- Tambahkan fungsi drawBitmap Text(), reshape(), dan drawText untuk memproyeksikan teks pada window
- Membuat fungsi untuk membuat lingkaran
- Membuat fungsi display untuk melakukan perulangan lingkaran sehingga membentuk lingkaran tak berarturan
- Membuat fungsi input mouse untuk mengubah objek sesuai dengan klik yang ditekan
- Membuat fungsi untuk input keyboard
- Code untuk membuat posisi kotak berubah
- Membuat warna background window berbeda tiap kotak
- Membuat fungsi update
- membuat fungsi main
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT 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"
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)
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)
# Membuat bentuk lingkaran
def segitiga():
global pos_x, pos_y
glColor3f(red,green,blue)
glBegin(GL_LINE_LOOP)
# 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 segitiga():
global pos_x, pos_y
glColor3f(red,green,blue)
glBegin(GL_LINE_LOOP)
# 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 display():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1.0,0.7,0.0)
for i in range(0,19):
s = 20
glRotatef(s, 0.0, 0.0,1.0)
segitiga()
drawText();
glFlush()
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1.0,0.7,0.0)
for i in range(0,19):
s = 20
glRotatef(s, 0.0, 0.0,1.0)
segitiga()
drawText();
glFlush()
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, ")")
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
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)
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
# 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)
glutPostRedisplay()
glutTimerFunc(10,update,0)
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
glutInitWindowSize(500,500)
glutInitWindowPosition(100,100)
glutCreateWindow("Quiz_17102036")
glutDisplayFunc(display)
glutSpecialFunc(input_keyboard)
glutMouseFunc(input_mouse)
glutTimerFunc(50, update, 0)
init()
glutMainLoop()
main()
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
glutInitWindowSize(500,500)
glutInitWindowPosition(100,100)
glutCreateWindow("Quiz_17102036")
glutDisplayFunc(display)
glutSpecialFunc(input_keyboard)
glutMouseFunc(input_mouse)
glutTimerFunc(50, update, 0)
init()
glutMainLoop()
main()
Comments
Post a Comment