Wolfos

Evade the cube

Feb 19th, 2012
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <GLUT/glut.h>
  4.  
  5. float deltaTime = 0.0f;
  6. float playerX = 20.0f;
  7. float playerY = 20.0f;
  8. float enemyX = 500.0f;
  9. float enemyY = 10.0f;
  10. float prevtime = 0.0f;
  11. int lastFrameTime = 0;
  12. void makeQuad(float,float,float,float);
  13. //int wkey, akey, skey, dkey = 0;
  14. int keyDown[256];
  15. int playerspeed = 500;
  16. float gravity = 0.0f;
  17. /*
  18. Compile using:
  19. gcc -framework GLUT -framework OpenGL -framework Cocoa main.c -o game
  20. Then run using:
  21. ./game
  22.  
  23. Controls:
  24. WASD
  25.  
  26. Goal:
  27. Evade the small cube. If you hit the cube, your X will be reset to 20. Take that player!
  28. */
  29.  
  30. void keyPressed(unsigned char key, int x, int y) {
  31.     keyDown[key]=1;
  32. }  
  33.  
  34. void keyUp(unsigned char key, int x, int y){
  35.     keyDown[key]=0;
  36. }
  37.  
  38. void display(void){
  39.    
  40.     if(lastFrameTime == 0){
  41.         lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
  42.     }
  43.     int now = glutGet(GLUT_ELAPSED_TIME);
  44.     int elapsedMilliseconds = now - lastFrameTime;
  45.     float deltaTime = elapsedMilliseconds / 1000.0f;
  46.     lastFrameTime = now;
  47.    
  48.     //printf("\ndeltaTime is: %d", deltaTime);
  49.     int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
  50.     //playerX+=1000.0F*deltaTime;
  51.    
  52.    
  53.    
  54.     //--------------PLAYER CONTROLS---------
  55.    
  56.    
  57.     if(keyDown[100]==1){
  58.         playerX+=playerspeed*deltaTime;
  59.     }
  60.     if(keyDown[97]==1){
  61.         playerX-=playerspeed*deltaTime;
  62.     }
  63.     if(keyDown[119]==1){
  64.         playerY+=2000*deltaTime;
  65.     }
  66.    
  67.     if(playerX>windowWidth){
  68.         playerX-=windowWidth;
  69.         //printf("true");
  70.     }
  71.     if(playerX<0){
  72.         playerX+=windowWidth;
  73.     }
  74.     if(playerY>20){
  75.         gravity-=0.01;
  76.         playerY+=gravity;
  77.     }
  78.     else{
  79.         gravity=0;
  80.     }
  81.     if(playerX<enemyX+10&&playerX>enemyX-10&&playerY<enemyY+10&&playerY>enemyY-10){
  82.         playerX = 20;
  83.     }
  84.    
  85.    
  86.     //---------------ENEMY CONTROLS---------------
  87.     enemyX-=100*deltaTime;
  88.     if(enemyX<0){
  89.         enemyX+=windowWidth;
  90.     }
  91.    
  92.    
  93.    
  94.    
  95.     //--------------DRAW THINGS-------------------
  96.     //printf("\nwindowWidth is: %d", windowWidth);
  97.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  98.     glPushMatrix();
  99.     glTranslatef(playerX, playerY,0.0f);
  100.     makeQuad(0,0,20,40);
  101.     glPopMatrix();
  102.     glPushMatrix();
  103.     glTranslatef(enemyX, enemyY,0.0f);
  104.     makeQuad(0,0,20,20);
  105.     glPopMatrix();
  106.     glutSwapBuffers();
  107. }
  108.  
  109.  
  110.  
  111. void reshape(int width, int height){
  112.     glViewport(0,0,width,height);
  113.  
  114.     glMatrixMode(GL_PROJECTION);
  115.     glLoadIdentity();
  116.     gluOrtho2D(0, width, 0, height);
  117.     glMatrixMode(GL_MODELVIEW);
  118. }
  119.  
  120. void idle(void){
  121.     glutPostRedisplay();
  122. }
  123.  
  124. void makeQuad(float x, float y, float width, float height){
  125.     glBegin(GL_QUADS);
  126.     glVertex2f(x-(width/2), y-(height/2));
  127.     glVertex2f(x+(width/2), y-(height/2));
  128.     glVertex2f(x+(width/2), y+(height/2));
  129.     glVertex2f(x-(width/2), y+(height/2));
  130.     glEnd();
  131. }
  132.  
  133. int main(int argc, char** argv){
  134.     glutInit(&argc, argv);
  135.    
  136.  
  137.     //glutKeyboardFunc(keyPressed);
  138.  
  139.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  140.     glutInitWindowSize(800, 600);
  141.  
  142.     glutCreateWindow("Wolfos' epic game");
  143.    
  144.     glutDisplayFunc(display);
  145.     glutReshapeFunc(reshape);
  146.     glutIdleFunc(idle);
  147.    
  148.     glutKeyboardFunc(keyPressed);
  149.     glutKeyboardUpFunc(keyUp);
  150.  
  151.     glutMainLoop();
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment