Created
October 7, 2010 11:36
-
-
Save thvdburgt/614984 to your computer and use it in GitHub Desktop.
opgave3.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from OpenGL.GL import * | |
from OpenGL.GLUT import * | |
from OpenGL.GLU import * | |
import sys | |
# Constants | |
SPEED = 1.2 | |
WORLD_SIZE = 200.0 | |
MAZE_SIZE = 10 | |
MAZE_EXIT_Z = 1 | |
MAZE_EXIT_X = 9 | |
maze = [ | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], | |
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1], | |
[1, 0, 0, 0, 0, 0, 1, 1, 1, 1], | |
[1, 0, 1, 0, 0, 0, 1, 0, 0, 1], | |
[1, 0, 1, 0, 1, 0, 1, 0, 0, 1], | |
[1, 0, 1, 0, 1, 0, 1, 0, 0, 1], | |
[1, 0, 0, 0, 1, 0, 1, 0, 0, 1], | |
[1, 0, 0, 0, 1, 1, 1, 0, 0, 1], | |
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1], | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] | |
food = [ | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0], | |
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0], | |
[0, 1, 0, 1, 1, 1, 0, 1, 1, 0], | |
[0, 1, 0, 1, 0, 1, 0, 1, 1, 0], | |
[0, 1, 0, 1, 0, 1, 0, 1, 1, 0], | |
[0, 1, 1, 1, 0, 1, 0, 1, 1, 0], | |
[0, 1, 1, 1, 0, 0, 0, 1, 1, 0], | |
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] | |
def drawMaze(): | |
# draw the maze and the food. | |
offset = -(worldSize - wallSize) / 2.0 | |
for x in range(MAZE_SIZE): | |
for z in range(MAZE_SIZE): | |
glPushMatrix() | |
glTranslated(offset + x * wallSize, 0.0, offset + z * wallSize) | |
if maze[z][x]: | |
glutSolidCube(wallSize) | |
elif food[z][x]: | |
glPushAttrib(GL_ALL_ATTRIB_BITS) | |
rgb = [0.2, 0.7, 0.2, 1.0] | |
glMaterialfv(GL_FRONT, GL_DIFFUSE, rgb) | |
glutSolidSphere(wallSize / 40, 36, 18) | |
glPopAttrib() | |
glPopMatrix() | |
#draw the floor. | |
glBegin(GL_QUADS) | |
glVertex3d(-worldSize / 2.0, -wallSize / 2.0, worldSize / 2.0) | |
glVertex3d(worldSize / 2.0, -wallSize / 2.0, worldSize / 2.0) | |
glVertex3d(worldSize / 2.0, -wallSize / 2.0, -worldSize / 2.0) | |
glVertex3d(-worldSize / 2.0, -wallSize / 2.0, -worldSize / 2.0) | |
glEnd() | |
def display(): | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
# rotate the scene to match the viewing direction. | |
# if one looks to the left, the 'world' turns to the right and vice versa. | |
# the same goes for up and down. and for forward, backward, strafe left, | |
# strafe right. | |
glLoadIdentity() | |
glRotated(-verangle, 1, 0, 0) | |
glRotated(-horangle, 0, 1, 0) | |
glTranslated(-copx, -copy, -copz) | |
drawMaze() | |
glFlush() | |
glutSwapBuffers() | |
def reshape(width, height): | |
# set the new viewport. | |
screenWidth = width | |
screenHeight = height | |
glViewport(0, 0, width, height) | |
# set the new projection matrix. | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluPerspective(60, screenWidth / screenHeight, .1, 200) | |
glMatrixMode(GL_MODELVIEW) | |
def keyboardCallback(key, x, y): | |
if key == 'w': | |
zdelta = -math.cos(horangle/(180/math.pi)) * SPEED | |
xdelta = -math.sin(horangle/(180/math.pi)) * SPEED | |
elif key == 's': | |
zdelta = math.cos(horangle/(180/math.pi)) * SPEED | |
xdelta = math.sin(horangle/(180/math.pi)) * SPEED | |
elif key == 'a': | |
zdelta = math.sin(horangle/(180/math.pi)) * SPEED | |
xdelta = -math.cos(horangle/(180/math.pi)) * SPEED | |
elif key == 'd': | |
zdelta = -math.sin(horangle/(180/math.pi)) * SPEED | |
xdelta = math.cos(horangle/(180/math.pi)) * SPEED | |
global copx | |
global copz | |
ztile = int(math.floor(((copz + zdelta) + (worldSize/2)) / wallSize)) | |
xtile = int(math.floor(((copx + xdelta) + (worldSize/2)) / wallSize)) | |
global maze | |
# Don't move if there's a wall or nothing at all. | |
if (ztile >= MAZE_SIZE or xtile >= MAZE_SIZE or maze[ztile][xtile]): | |
return | |
# Collect any food on this tile. | |
if food[ztile][xtile]: | |
global foodCount | |
foodCount -= 1 | |
food[ztile][xtile] = 0 | |
print "food count: %d" % foodCount | |
if (foodCount == 0): # Open the exit if all food is collected. | |
maze[MAZE_EXIT_Z][MAZE_EXIT_X] = 0 | |
print "You hear a door open..." | |
if (foodCount == 0 and ztile == MAZE_EXIT_Z and xtile == MAZE_EXIT_X): | |
print "You are at the exit of the maze. Congratulations!" | |
copx += xdelta | |
copz += zdelta | |
glutPostRedisplay() | |
def mouseCallback(button, state, x, y): | |
global buttonPressed | |
if state == GLUT_DOWN: | |
buttonPressed = button | |
else: | |
buttonPressed = -1 | |
global lastX | |
global lastY | |
lastX = x | |
lastY = y | |
def motionCallback(x, y): | |
if buttonPressed != GLUT_LEFT_BUTTON: | |
return | |
global horangle | |
global verangle | |
global lastX | |
global lastY | |
horangle = math.fmod(horangle + (lastX - x)*0.5, 360) | |
verangle = math.fmod(verangle + (lastY - y)*0.5, 360) | |
lastX = x | |
lastY = y | |
glutPostRedisplay() | |
def main(): | |
global worldSize | |
global wallSize | |
worldSize = WORLD_SIZE | |
wallSize = worldSize / MAZE_SIZE | |
global screenWidth | |
global screenHeight | |
screenWidth = 400 | |
screenHeight = 400 | |
# We want to start in the middle of a square. | |
global copx | |
global copy | |
global copz | |
copx = wallSize / 2.0 | |
copy = 0.0 | |
copz = wallSize / 2.0 | |
# We're looking to the north. | |
global horangle | |
global verangle | |
horangle = 0.0 | |
verangle = 0.0 | |
# Set the foodCount. | |
global foodCount | |
foodCount = 0 | |
for z in range(MAZE_SIZE): | |
for x in range(MAZE_SIZE): | |
if(food[z][x]): | |
foodCount += 1 | |
# glut | |
glutInit(sys.argv) | |
glutInitWindowPosition(100, 100) | |
glutInitWindowSize(screenWidth, screenHeight) | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) | |
glutCreateWindow("After collecting all the food you can exit the maze") | |
# set viewpoint | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluPerspective(60, screenWidth / screenHeight, .1, 200) | |
glMatrixMode(GL_MODELVIEW) | |
# enable Backface Culling | |
glCullFace(GL_BACK) | |
glEnable(GL_CULL_FACE) | |
# enable Z-Buffering | |
glEnable(GL_DEPTH_TEST) | |
# use a white light! | |
lightPosition = [0.0, 50.0, 0.0, 1.0] | |
lightColour = [1.0, 1.0, 1.0, 0.0] | |
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition) | |
glLightfv(GL_LIGHT0, GL_AMBIENT, lightColour) | |
# enable Lighting in general and more specifically light 0 | |
glEnable(GL_LIGHTING) | |
glEnable(GL_LIGHT0) | |
# grey walls, only lit by GL_LIGHT0 | |
rgb = [0.7, 0.7, 0.7, 1.0] | |
glMaterialfv(GL_FRONT, GL_DIFFUSE, rgb) | |
# use smooth shading | |
glShadeModel(GL_SMOOTH) | |
# initialise call back functions | |
glutDisplayFunc(display) | |
glutReshapeFunc(reshape) | |
# Set callback functions | |
glutKeyboardFunc(keyboardCallback) | |
glutMouseFunc(mouseCallback) | |
glutMotionFunc(motionCallback) | |
# loop forever | |
glutMainLoop() | |
return 0 | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment