#*************************# # CamelCrusaders v 2.0.0 # # Sept. 21, 2007 # #*************************# #*************************# import sys, math, random, time from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from copy import deepcopy gamepause = False debugmode = -1 #-1-off, 0-all, 1-input, 2-offsets, 3-system, 3.5-spf, 4-temp, 5-track, 6-loc, 7-exactloc 11-all, 12-off #Camera Vars camerax = 0.0 cameray = 0.0 cameraz = 1.0 anglea = 0.0 angleb = 0.0 mouseact = False mousepause = True #System Vars fps = 1 spf = 0.1 spfmax = 0.3 lastsec = int(time.time()) lastinstance = time.time() framecount = 0 #EnviroVars landhts=[[0]] landsize = 25#50 landhtsnearx = [[0]] landhtsneary = [[0]] landhtsnearxy = [[0]] nearx = -1 neary = -1 xoffset = 0 yoffset = 0 #Movement Vars pxa = 0 pya = 0 pxv = 0 pyv = 0 pacc = 5 gravity = -32 jumpvel = 8*math.sqrt(5) #v0 = 8*sqrt(ht) za = gravity zv = 0.0 tx = 0.0 ty = 0.0 tangle = 0.0 dtangle = 0.0 tred = 0.0 tgreen = 0.0 tblue = 0.0 lastmousex = 0.0 lastmousey = 0.0 angle = 0.0 def main(toDEBUG): DEBUG = Debugger(toDEBUG) glutInit(sys.argv) #Build Window glutInitWindowPosition(5,30) glutInitWindowSize(1260,925) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) #Depth Double Buffering with RGBA color mode glutCreateWindow('3D Camel Crusaders v. 2.0') # Set the display callback. You can set other callbacks for keyboard and # mouse events. glutDisplayFunc(display) glutIdleFunc(display) glutReshapeFunc(reshape) glutKeyboardFunc(normalkeys) glutSpecialFunc(specialkeys) glutMouseFunc(mouseRead) glutMotionFunc(mousemotionRead) glutPassiveMotionFunc(mousemotionRead) glutEntryFunc(mouseentryRead) glClearColor(1.0, 1.0, 1.0, 1.0) glEnable(GL_DEPTH_TEST)#WORKWORKWORKWORKWORKWORKWORKWORKWORKWORK glDepthFunc(GL_LESS) glClearDepth(1.0) global landhts,landhtsnearx,landhtsneary,landhtsnearxy landhts = buildterrain(landhts) landhtsnearx = buildterrain(landhtsnearx) landhtsneary = buildterrain(landhtsneary) landhtsnearxy = buildterrain(landhtsnearxy) # Run the GLUT main loop until the user closes the window. glutMainLoop() # The display() method does all the work; it has to call the appropriate # OpenGL functions to actually display something. def display(): global fps, lastsec, framecount, spf, lastinstance, pxa, pya, pxv, pyv, za, zv, camerax, cameray, cameraz debug(6, ("x:", int(pxa), int(pxv), int(camerax), "y:", int(pya), int(pyv), int(cameray), "z:", int(za), int(zv), int(cameraz))) temp = time.time() spf = temp-lastinstance debug(5,"DISPLAYSTART") debug(3.5, spf) lastinstance=temp if int(time.time()) > lastsec: if framecount>0: fps = framecount/(int(time.time())-lastsec) framecount = 0 lastsec = int(time.time()) debug(3,(lastsec, fps, spf)) framecount += 1 global gamepause if gamepause: debug(5, "PAUSE*DISPLAYEND") return debug(7, (pxa, pya, za)) debug(7, (pxv, pyv, zv)) debug(7, (camerax, cameray, cameraz)) global anglea, angleb glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glClearColor(0.0, 0.0, 0.0, 1.0) #glEnable(GL_DEPTH_TEST) ##glDepthFunc(GL_LEQUAL) ##glClearDepth(1.0) ##glLoadIdentity() #glPushMatrix() global tangle, dtangle, tred, tgreen, tblue, angle, tx, ty from random import choice #rtemp = choice([1,2,3,4,5,6]) #if rtemp == 1: # tx += 0.1 #elif rtemp == 2: # tx -= 0.1 #elif rtemp == 3: # ty += 0.1 #elif rtemp == 4: # ty -= 0.1 #tx += 0.1 #ty = 0 tangle += dtangle if camerax-tx > 1: tx += 0.7 elif camerax - tx <-1: tx -= 0.7 elif cameray > ty: ty += 0.7 elif cameray 0: pxv += pxa/fps camerax += pxv/fps pyv += pya/fps cameray += pyv/fps zv += za/fps cameraz += zv/fps else: pxv += pxa*spf camerax += pxv*spf pyv += pya*spf cameray += pyv*spf zv += za*spf cameraz += zv*spf if not(cameraz > getheight(camerax, cameray)+1): cameraz = getheight(camerax,cameray)+1 zv = 0 pxa = 0 pya = 0 #draw axes glBegin(GL_LINES) glColor3f (1.0, 0.0, 0.0) glVertex3f(0,0,0) glVertex3f(10,0,0) glColor3f (0.0, 1.0, 0.0) glVertex3f(0,0,0) glVertex3f(0,10,0) glColor3f (0.0, 0.0, 1.0) glVertex3f(0,0,0) glVertex3f(0,0,10) glEnd() #drawtriangle glRotatef(tangle,1.0+tx,1.0+ty,1.0) glColor3f (0.0, 0.0, 0.0) glBegin(GL_TRIANGLES) glVertex3f(0.0+tx,-0.5+ty,-0.5) glVertex3f(0.0+tx,0.0+ty,0.5) glVertex3f(0.0+tx,0.5+ty,0.0) glEnd() glRotatef(-tangle,1.0+tx,1.0+ty,1.0) #glPopMatrix() glFlush() glFinish() glutSwapBuffers() #glMatrixMode(GL_MODELVIEW) glLoadIdentity() rada = (anglea*math.pi)/180.0 radb = (angleb*math.pi)/180.0 gluLookAt(camerax,cameray,cameraz,camerax+ math.cos(radb)*math.cos(rada),cameray+math.cos(radb)*math.sin(rada),cameraz+math.sin(radb),0,0,1) """# Clear the color and depth buffers""" glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # ... render stuff in here ... # It will go to an off-screen frame buffer. glClearColor (0.0, 0.0, 0.0, 0.0) glClear (GL_COLOR_BUFFER_BIT) #glFlush(); # Copy the off-screen buffer to the screen. #glutSwapBuffers() debug(5,"DISPLAYEND") def reshape(width, height): global camerax, cameray, cameraz, anglea, angleb if height == 0: height = 1 proportion = 1.0 * width / height glMatrixMode(GL_PROJECTION) glLoadIdentity() glViewport(0, 0, width, height) gluPerspective(45, proportion, 0, 10) glMatrixMode(GL_MODELVIEW) glLoadIdentity() rada = (anglea*math.pi)/180.0 radb = (angleb*math.pi)/180.0 gluLookAt(camerax,cameray,cameraz,camerax+ math.cos(radb)*math.cos(rada),cameray+math.cos(radb)*math.sin(rada),cameraz+math.sin(radb),0,0,1) def normalkeys(key, x, y): global gamepause if gamepause: return global tangle,dtangle, camerax, cameray, cameraz, anglea, angleb, zv, jumpvel, gravity,pxa,pya,pxv,pyv,pacc rada = (anglea*math.pi)/180.0 radb = (angleb*math.pi)/180.0 dx = pacc*math.cos(radb)*math.cos(rada) dy = pacc*math.cos(radb)*math.sin(rada) dz = 0#math.sin(radb) if key == 'w': pxa = dx pya = dy if key == 's': pxa = -dx pya = -dy """camerax -= dx cameray -= dy cameraz -= dz""" rada = (findPerp(anglea)*math.pi)/180.0 radb = (angleb*math.pi)/180.0 dx = pacc*math.cos(radb)*math.cos(rada) dy = pacc*math.cos(radb)*math.sin(rada) dz = 0#math.sin(radb) if key == 'a': pxa = dx pya = dy if key == 'd': pxa = -dx pya = -dy if not(key=='a' or key=='d' or key=='w' or key=='s'): pya = 0 pxa = 0 pxv = 0 pyv = 0 debug(1,(pxa, pya)) debug(1, (pxv, pyv)) if key =='Q': exit(0) if key =='e': dtangle += 1 if key == 'c': dtangle -= 1 if key == ' ': zv+=jumpvel if dx != 0 or dy != 0: terrainmove(camerax, cameray) if key == 'q': quit() debug(1,key) debug(1,(camerax, cameray, cameraz)) def specialkeys(key, x, y): global gamepause, debugmode if gamepause: return debug(1,key) if key>0 and key<13: debugmode = key def mouseRead(button, state, x, y): global gamepause if gamepause: return global mouseact, mousepause debug(1,button) debug(1, state) global lastmousex,lastmousey, angle if button == 0 and state == 1: mousepause = True mouseact = False if button == 0 and state == 0: mousepause = False debug(1,("mousepause",mousepause)) debug(1,("mouseact",mouseact)) #dx = x-lastmousex #dy = y-lastmousey #anglea += dx #oldmousex = x #oldmousey = y def activemotionRead(x,y): global gamepause if gamepause: return debug(1,x) def mousemotionRead(x,y): global gamepause if gamepause: return debug(1,x) global lastmousex,lastmousey, anglea, angleb, mouseact dx = lastmousex-x dy = lastmousey-y if mouseact==True and mousepause == False: anglea += dx*0.5 angleb += dy*0.3 elif mousepause == False: mouseact = True lastmousex = x lastmousey = y while anglea > 180: anglea -= 360.0 while anglea <= -180: anglea += 360.0 if angleb > 90: angleb = 90.0 if angleb < -90: angleb = -90.0 debug(1,(anglea, angleb)) debug(1,("mousepause",mousepause)) debug(1,("mouseact",mouseact)) def mouseentryRead(state): debug(1,state) global mouseact, gamepause if state == 0: mouseact = False if state == 0: gamepause = True if state == 1: gamepause = False debug(1,gamepause) def findPerp(angle): angle +=90 while angle >180: angle -= 360.0 while angle <= 180: angle += 360.0 return angle def buildterrain(heights): debug(5,"BUILDTTSTART") global landsize tempx = 0 tempy = 0 while tempx < landsize: if tempx > 0: heights.append([0]) while tempy < landsize: if tempy > 0: #heights[tempx].append(0)#math.sin(tempx)+math.sin(tempy))#random.randint(0,0)) if tempy >landsize/2: heights[tempx].append(0) else: heights[tempx].append(0.5) tempy += 1 tempy = 0 tempx+=1 heights[landsize/2][landsize/2]=4 heights[landsize/2-1][landsize/2]=2 debug(5,"BUILDTTEND") return heights def drawterrain(heights, txoffset, tyoffset, border): global spf, spfmax if(spf>spfmax): debug(3, "+++FRAMESKIP+++") debug(5, "+++FRAMESKIPTTDRAW+++") return if(spf>2.5): print spf debug(5,"DRAWTTSTART") tempx = -1 tempy = -1 if not(border): tempx = 0 tempy = 0 lmax = landsize debug(2, (txoffset,tyoffset)) while tempx < (landsize): while tempy < (landsize): if(framesec()>spfmax): return x = tempx+txoffset*landsize y = tempy+tyoffset*landsize if not(getheight(x,y) == -10) and not(getheight(x+1,y) == -10) and not(getheight(x, y+1) == -10) and not(framesec()>spfmax): glColor3f(1.0,1.0,0.0) #print "STARTDRAW" glBegin(GL_TRIANGLES) glVertex3f(x,y,getheight(x,y))#heights[tempx][tempy]) glVertex3f(x+1,y,getheight(x+1,y))#heights[tempx+1][tempy]) glVertex3f(x,y+1,getheight(x, y+1))#heights[tempx][tempy+1]) glEnd() #print "ENDDRAW" glColor3f(0.0,1.0,0.0) glBegin(GL_TRIANGLES) glVertex3f(x+1,y,getheight(x+1,y))#heights[tempx][tempy]) glVertex3f(x,y+1,getheight(x,y+1))#heights[tempx+1][tempy]) glVertex3f(x+1,y+1,getheight(x+1, y+1))#heights[tempx][tempy+1]) glEnd() """glBegin(GL_LINES) glColor3f(0.0,1.0,0.0) glVertex3f(x,y,getheight(x,y)) glVertex3f(x+1,y,getheight(x+1,y)) glColor3f(1.0,0.0,0.0) glVertex3f(x,y,getheight(x,y)) glVertex3f(x,y+1,getheight(x, y+1)) glColor3f(0.0,0.0,0.0) glVertex3f(x+1,y,getheight(x+1,y)) glVertex3f(x,y+1,getheight(x, y+1)) glEnd()""" #if not(txoffset==0): #debug(2, (tempx,txoffset,landsize)) #debug(2, (tempy,tyoffset,landsize)) tempy += 1 tempy = -1 if not(border): tempy = 0 tempx += 1 debug(5,"DRAWTTEND") def getheight(x, y): #debug(5,"GETHTSTART") global landsize, landhts, landhtsnearx, landhtsneary, landhtsnearxy global nearx, neary, camerax, cameray, cameraz, xoffset, yoffset txoffset = math.floor(int(x) / landsize) tyoffset = math.floor(int(y) / landsize) localx = int(x - txoffset*landsize) localy = int(y - tyoffset*landsize) #debug(5,"GETHTALMOSTEND") if localx < 0 or localy < 0 or localx >= landsize or localy >= landsize: print ("ERROR: OOBHT",localx,localy, txoffset, tyoffset, x, y) if (xoffset == txoffset and yoffset == tyoffset): return landhts[localx][localy] elif (txoffset == xoffset + nearx and yoffset == tyoffset): return landhtsnearx[localx][localy] elif (xoffset == txoffset and tyoffset == yoffset+neary): return landhtsneary[localx][localy] elif (txoffset == xoffset + nearx and tyoffset == yoffset + neary): return landhtsnearxy[localx][localy] else: return -10 def terrainmove(x, y): global fps, spf, spfmax if(spf>spfmax): debug(3, "+++FRAMESKIP+++") debug(5,"+++FRAMESKIPTTMOVE+++") return debug(5,"MOVETTSTART") global landsize, landhts, landhtsnearx, landhtsneary, landhtsnearxy global nearx, neary, camerax, cameray, cameraz, xoffset, yoffset txoffset = math.floor(camerax / landsize) tyoffset = math.floor(cameray / landsize) localx = camerax - xoffset*landsize localy = cameray - yoffset*landsize nearchange = False if (localx < landsize/2.0) and (nearx==1): landhtsnearx = [[0]] landhtsnearx = buildterrain(landhtsnearx) nearx = -1 nearchange = True if (localx >= landsize/2.0) and (nearx==-1): landhtsnearx = [[0]] landhtsnearx = buildterrain(landhtsnearx) nearx = 1 nearchange = True if (localy < landsize/2.0) and (neary==1): landhtsneary = [[0]] landhtsneary = buildterrain(landhtsneary) neary= -1 nearchange = True if (localy >= landsize/2.0) and (neary==-1): landhtsneary = [[0]] landhtsneary = buildterrain(landhtsneary) neary = 1 nearchange = True if (nearchange): landhtsnearxy = [[0]] landhtsnearxy = buildterrain(landhtsnearxy) if (localx < 0) or (localx >= landsize): temp1 = deepcopy(landhts) landhts = deepcopy(landhtsnearx) landhtsnearx = deepcopy(temp1) temp2 = deepcopy(landhtsneary) landhtsneary = deepcopy(landhtsnearxy) landhtsnearxy = deepcopy(temp2) nearx *= -1 xoffset += math.floor(localx/landsize) if (localy < 0) or (localy >= landsize): temp1 = deepcopy(landhts) landhts = deepcopy(landhtsneary) landhtsneary = deepcopy(temp1) temp2 = deepcopy(landhtsnearx) landhtsnearx = deepcopy(landhtsnearxy) landhtsnearxy = deepcopy(temp2) neary *= -1 yoffset += math.floor(localy/landsize) debug(5,"MOVETTEND") def framesec(): global lastinstance, spfmax secs = time.time()-lastinstance if(secs > spfmax): print ("+++OVERLOAD+++", secs) return secs def debug(mode, dstring): global debugmode if ((mode == debugmode) or (debugmode == 11) or (debugmode == 0)) and not(debugmode == -1): print dstring class GLOBVARS: def __init__(ang): self.angle=ang class Camel: def __init__(self, health, height): self.h = health self.ht = height #************** #Debugger Class #************** class Debugger: def __init__(self): self.active = False def __init__(self, act): self.active = act def dprint(self, pntstr): if self.active: print(pntstr), return self.active def dprintln(self, pntstr): if self.active: print(pntstr), return self.active #end class Debugger #****************** #****************** #Start program from main() DEBUG = True main(DEBUG) #*************** #END APPLICATION #*************** #***************