Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'bradly.tillmann'
- from OpenGL.GL import *
- from OpenGL.GLUT import *
- from OpenGL.GLU import *
- import sys, random
- #import serial
- import os
- import threading
- ESCAPE = '\033'
- window = 0
- #rotation
- X_AXIS = 0.0
- Y_AXIS = 0.0
- Z_AXIS = 0.0
- global loc_x
- global loc_y
- global loc_z
- global mouse_x
- global mouse_y
- loc_x = 0
- loc_y = 0
- loc_z = 0
- mouse_x = 0
- mouse_y = 0
- player_y = 0
- world = []
- DIRECTION = 1
- file = open("key.txt", "a")
- def InitGL(Width, Height):
- glClearColor(0.0, 0.0, 0.0, 0.0)
- glClearDepth(1.0)
- glDepthFunc(GL_LESS)
- glEnable(GL_DEPTH_TEST)
- glShadeModel(GL_SMOOTH)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
- glMatrixMode(GL_MODELVIEW)
- def world_generator(noise, max_x, max_y, max_z):
- for a in range(max_x):
- for b in range(max_y):
- for c in range(max_z):
- world.append([a, b, c, random.randrange(0, 2), random.randrange(0, 2), random.randrange(0, 2)])
- chance = random.randrange(0, noise)
- if chance < noise/2 and b == max_y:
- world.append([a, b+1, c, random.randrange(0, 2), random.randrange(0, 2), random.randrange(0, 2)])
- elif b == max_y:
- world.append([a, b+1, c, random.randrange(0, 2), random.randrange(0, 2), random.randrange(0, 2)])
- world.append([a, b+2, c, random.randrange(0, 2), random.randrange(0, 2), random.randrange(0, 2)])
- def render_world():
- for a in world:
- cube(a[0], a[1], a[2], a[3], a[4], a[5])
- def keyPressed(key, x, y):
- if key == 'w':
- global loc_x
- loc_x += 1
- if key == 's':
- global loc_x
- loc_x -= 1
- if key == 'a':
- global loc_y
- loc_y += 1
- if key == 'd':
- global loc_y
- loc_y -= 1
- if key == 'e':
- global loc_z
- loc_z += 0.1
- if key == 'q':
- global loc_z
- loc_z -= 0.1
- if key == ' ':
- global player_y
- player_y -= 0.1
- def cube(x, y, z, red, green, blue):
- glBegin(GL_QUADS)
- glColor3f(red, green, blue)
- glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
- glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
- glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
- glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
- glColor3f(red, green, blue)
- glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
- glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
- glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
- glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
- glColor3f(red, green, blue)
- glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
- glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
- glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
- glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
- glColor3f(red, green, blue)
- glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
- glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
- glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
- glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
- glColor3f(red, green, blue)
- glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
- glVertex3f(-1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
- glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
- glVertex3f(-1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
- glColor3f(red, green, blue)
- glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2),-1.0 + (z * 2))
- glVertex3f( 1.0 + (x * 2), 1.0 + (y * 2), 1.0 + (z * 2))
- glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2), 1.0 + (z * 2))
- glVertex3f( 1.0 + (x * 2),-1.0 + (y * 2),-1.0 + (z * 2))
- glEnd()
- def DrawGLScene():
- global X_AXIS,Y_AXIS,Z_AXIS
- global DIRECTION
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- glLoadIdentity()
- glTranslatef(0.0,0.0,-6.0)
- glTranslatef(0.0,float(player_y),0.0)
- glRotatef(X_AXIS,1.0,0.0,0.0)
- glRotatef(Y_AXIS,0.0,1.0,0.0)
- glTranslatef(0.0, 0.0, float(Z_AXIS))
- # Draw Cube (multiple quads)
- render_world()
- X_AXIS = loc_x
- Z_AXIS = loc_z
- Y_AXIS = loc_y
- glutSwapBuffers()
- def main():
- global window
- glutInit(sys.argv)
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
- world_generator(3, 10, 10, 10)
- glutInitWindowSize(1024,1024)
- glutInitWindowPosition(200,200)
- window = glutCreateWindow('OpenGL Python Cube')
- glutDisplayFunc(DrawGLScene)
- glutIdleFunc(DrawGLScene)
- glutKeyboardFunc(keyPressed)
- glutKeyboardFunc(keyPressed)
- InitGL(640, 480)
- glutMainLoop()
- def display():
- glClear(GL_COLOR_BUFFER_BIT)
- glBegin(GL_LINES)
- glVertex2f(1.0, 1.0)
- glVertex2f(-1.0, -1.0)
- glEnd()
- glutSwapBuffers()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement