Advertisement
fr1sk

Untitled

Aug 26th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.78 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <time.h>
  5. #include <stdio.h>
  6. #include <time.h>
  7. #include <string.h>
  8. #include <GLUT/glut.h> // *** DISABLE THIS LINE FOR LINUX ***
  9. //#include <GL/glut.h> *** ENABLE THIS LINE FOR LINUX ***
  10. #include <stdio.h>
  11. #include <math.h>
  12.  
  13. //#include <freeglut.h>
  14. //#include <GL/gl.h>
  15. //#include <GL/glut.h>
  16.  
  17. //#include <OpenGL/gl.h>
  18. //#include <OpenGL/glu.h>
  19. //#include <GLUT/GLUT.h>
  20. //
  21. //#include <OpenGL/gl.h>
  22. //#include <OpenGL/glu.h>
  23.  
  24.  
  25. #define TIMER_ID 0
  26. #define TIMER_INTERVAL 25
  27. #define MOVEMENTSPEED 0.4
  28. #define NUMOFBLOCKS 7
  29. #define STARTSCROLLING 1
  30. #define BLUE 3
  31. #define YELLOW 2
  32. #define GREEN 0
  33. #define RED 1
  34. #define HEIGHT 740
  35. #define WIDTH 640
  36.  
  37.  
  38. struct point{
  39. int x,y;
  40. };
  41.  
  42.  
  43. static float currentX, currentY; //current object coordinates
  44. static float currentFloorCoord = -6; //current floor Y coordinate
  45. static float vectorSpeedX, vectorSpeedY; //speed vector coords
  46. static int animation_ongoing; //animation flag
  47. static bool left = false;
  48. static bool right = false;
  49. static float border = 8.4;
  50. static float borderTrough = 8.39;
  51. int currBlocks = 0;
  52. bool haveFloor = true;
  53. char textScore[15];
  54. float worldMovementSpeed = 0;
  55. bool moveWorld = false;
  56. int globalScore = 0;
  57. bool bonusActivated = false;
  58. int bonusJumps = 0;
  59. int rotation = 0;
  60. bool isGameOver = false;
  61. int maxHeightIndex = NUMOFBLOCKS-1;
  62. float density = 0.6;
  63.  
  64. static void onKeyboard(unsigned char key, int x, int y);
  65. static void onKeyboard2(unsigned char key, int x, int y);
  66.  
  67. static void on_timer(int value);
  68. static void on_display(void);
  69.  
  70. typedef struct {
  71. float currX;
  72. float length;
  73. float currY;
  74. int blockMode; //0 - normal | 1 - breakable | 2 - bonus | 3 - boost
  75. float yOffset;
  76. int bonus; //increase score by BONUS when jump on this block
  77. } Block;
  78.  
  79. typedef struct {
  80. float r;
  81. float g;
  82. float b;
  83. } RGB;
  84.  
  85.  
  86.  
  87. Block blocks[NUMOFBLOCKS];
  88. void pickColor(bool x);
  89. void drawAllTheBlocks();
  90. void blockInit();
  91.  
  92.  
  93. int main(int argc, char **argv)
  94. {
  95. //glut init
  96. glutInit(&argc, argv);
  97. glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  98.  
  99. //window create
  100. glutInitWindowSize(WIDTH, HEIGHT);
  101. glutInitWindowPosition(350, 100);
  102. glutCreateWindow(argv[0]);
  103.  
  104. glutKeyboardFunc(onKeyboard);
  105. glutKeyboardUpFunc(onKeyboard2);
  106. glutDisplayFunc(on_display);
  107.  
  108. srand(time(NULL));
  109. animation_ongoing = 0;
  110.  
  111. //glClearColor(0, 0.6, 0.544, 0);
  112. pickColor(true);
  113. glEnable(GL_DEPTH_TEST);
  114. printf("Input debug:\n");
  115.  
  116. //main loop
  117. blockInit();
  118. glutMainLoop();
  119.  
  120. return 0;
  121. }
  122.  
  123.  
  124.  
  125.  
  126. float randBetween(float min, float max){
  127. //r = (rand() % (max + 1 - min)) + min
  128. float r = (rand() % (int)(max+1-min)) + min;
  129. //printf("RAND %lf", r);
  130. return r;
  131. }
  132.  
  133. int getRandomBlock(){
  134. int x = randBetween(0, 100);
  135. //printf("RANDOM: %d\n", x);
  136.  
  137. if(x<40){
  138. return 0;
  139. } else if(x>=40 && x<75){
  140. return 1;
  141. } else if(x>=75 && x<90){
  142. return 3;
  143. } else {
  144. return 2;
  145. }
  146. }
  147.  
  148. int myRandom(int m) {
  149. return rand()%m;
  150.  
  151. }
  152. // void test(){
  153. // currentX = -(1 - size / 2) + (2 - size);
  154. // currentY = -(1 - size / 2) + (2 - size);
  155. //
  156. // vectorSpeedX = -size / 2 + size * MOVEMENTSPEED;
  157. // vectorSpeedY = -size / 2 + size * MOVEMENTSPEED;
  158. // }
  159.  
  160. void moveLeft(){
  161. currentX -= MOVEMENTSPEED;
  162. //printf("\ta - move left\n");
  163. }
  164.  
  165. void moveRight(){
  166. currentX += MOVEMENTSPEED;
  167. //printf("\td - move right\n");
  168. }
  169.  
  170. void startMovingWorld(){
  171. worldMovementSpeed = 0.1;
  172. moveWorld = true;
  173. }
  174.  
  175. void jump(){
  176. if(currentY >= 1){
  177. startMovingWorld();
  178. }
  179.  
  180. if (currentY <= currentFloorCoord) {
  181. currentY = currentFloorCoord;
  182. //upspeed
  183. printf("rot: %d\n", rotation);
  184. if(rotation==31)
  185. vectorSpeedY = 1.6;
  186. else
  187. vectorSpeedY = 1.2;
  188. }
  189. else{
  190. //downspeed
  191. if(vectorSpeedY>=-1){
  192. vectorSpeedY -= 0.1;
  193. }
  194. }
  195. if(currentY>=5 && vectorSpeedY>0){
  196. return;
  197. }
  198. currentY += vectorSpeedY;
  199. }
  200.  
  201. void throughWall(){
  202. if(currentX < -borderTrough){
  203. currentX = borderTrough;
  204. }
  205.  
  206. if(currentX > borderTrough){
  207. currentX = -borderTrough;
  208. }
  209. }
  210.  
  211. //void drawPlatform(){
  212. //#define glutCube glutWireCube
  213. // glPushMatrix();
  214. // glScalef(0.2, 1, 1);
  215. // glutCube(1);
  216. // glPopMatrix();
  217. //#undef glutCube
  218. //}
  219.  
  220. void generateRandBlockCoord(int x){
  221. float blockY = randBetween(3.2,7.1);
  222. float blockX = randBetween(-8.0, 8.0);
  223. }
  224.  
  225.  
  226. void setNewBlockCoords(){
  227. int minHeight = (maxHeightIndex+1)%NUMOFBLOCKS;
  228. //printf("min height: %d\n", minHeight);
  229. if(blocks[minHeight].currY<=-9){
  230. blocks[minHeight].currY = blocks[maxHeightIndex].currY + randBetween(5, 7) * density;
  231. blocks[minHeight].length = randBetween(2, 7);
  232. blocks[minHeight].currX = randBetween(-8.3, 8.3);
  233. blocks[minHeight].blockMode = getRandomBlock();
  234. blocks[minHeight].yOffset = 0;
  235. if(blocks[minHeight].blockMode==BLUE){
  236. blocks[minHeight].bonus = 5;
  237. } else {
  238. blocks[minHeight].bonus = 1;
  239. }
  240. maxHeightIndex = minHeight;
  241. }
  242.  
  243. }
  244.  
  245.  
  246. void blockInit(){
  247. for(int i=0; i<NUMOFBLOCKS; i++){
  248. switch (i) {
  249. case 0:
  250. blocks[i].currX = -5;
  251. blocks[i].currY = -3;
  252. blocks[i].length = 3;
  253. blocks[i].blockMode = RED;
  254. blocks[i].bonus = 1;
  255. break;
  256. case 1:
  257. blocks[i].currX = 0;
  258. blocks[i].currY = -8;
  259. blocks[i].length = 11;
  260. blocks[i].blockMode = 0;
  261. blocks[i].bonus = 0;
  262. break;
  263. case 2:
  264. blocks[i].currX = 5;
  265. blocks[i].currY = 2;
  266. blocks[i].length = 5;
  267. blocks[i].blockMode = GREEN;
  268. blocks[i].bonus = 1;
  269.  
  270.  
  271. }
  272. if(i>2){
  273. if(i == NUMOFBLOCKS - 1){
  274. blocks[i].currX = 3;
  275. blocks[i].currY = 7;
  276. blocks[i].length = 3;
  277. blocks[i].blockMode = RED;
  278. blocks[i].bonus = 1;
  279. } else {
  280. blocks[i].currY = randBetween(9, 20);
  281. blocks[i].currX = randBetween(-8.3, 8.3);
  282. blocks[i].blockMode = getRandomBlock();
  283. blocks[i].length = randBetween(2, 7);
  284. blocks[i].yOffset = 0;
  285. if(blocks[i].blockMode==BLUE){
  286. blocks[i].bonus = 5;
  287. } else {
  288. blocks[i].bonus = 1;
  289. }
  290. }
  291. }
  292. blocks[i].yOffset=0;
  293.  
  294. }
  295.  
  296. }
  297.  
  298. void drawBlock(int index){
  299. GLfloat diffuse_coeffs[4];
  300. if(blocks[index].blockMode==1){
  301. diffuse_coeffs[0] = 1;
  302. diffuse_coeffs[1] = .2;
  303. diffuse_coeffs[2] = .2;
  304. diffuse_coeffs[3] = 1;
  305. } else if(blocks[index].blockMode==2){
  306. diffuse_coeffs[0] = 1;
  307. diffuse_coeffs[1] = .8;
  308. diffuse_coeffs[2] = 0;
  309. diffuse_coeffs[3] = 1;
  310. } else if(blocks[index].blockMode==3){
  311. diffuse_coeffs[0] = .2;
  312. diffuse_coeffs[1] = .2;
  313. diffuse_coeffs[2] = 1;
  314. diffuse_coeffs[3] = 1;
  315. } else {
  316. diffuse_coeffs[0] = .2;
  317. diffuse_coeffs[1] = 1;
  318. diffuse_coeffs[2] = .2;
  319. diffuse_coeffs[3] = 1;
  320.  
  321. }
  322. glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse_coeffs);
  323.  
  324. glPopMatrix();
  325. glPushMatrix();
  326. glTranslatef(blocks[index].currX, blocks[index].currY+blocks[index].yOffset, 0);
  327. glColor3ub(255, 0, 0);
  328. glScalef(blocks[index].length,0.2,1);
  329. glutSolidCube(1);
  330. glPopMatrix();
  331.  
  332. glutPostRedisplay();
  333. }
  334.  
  335.  
  336. void drawText(const char *text,int length , int x , int y) {
  337. if(isGameOver){
  338. text = "GAME OVER\0";
  339. length = 19;
  340. x = WIDTH/2;
  341. y = HEIGHT/2;
  342. }
  343.  
  344.  
  345. glMatrixMode(GL_PROJECTION);
  346. double matrix[16];
  347. glGetDoublev(GL_PROJECTION_MATRIX,matrix);
  348. glLoadIdentity();
  349. glOrtho(0,800,0,600,-5,5);
  350. glMatrixMode(GL_MODELVIEW);
  351. glLoadIdentity();
  352. glPushMatrix();
  353. glLoadIdentity();
  354. glRasterPos2i(x,y);
  355.  
  356.  
  357. for ( int i = 0 ; i < length ; i++)
  358. glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18,(int)text[i]);
  359.  
  360. //score
  361. int nDigits = floor(log10(abs(globalScore))) + 1;
  362. char str[20];
  363. sprintf(str, "%d", globalScore);
  364.  
  365. if(!isGameOver){
  366. for ( int i = 0 ; i < nDigits ; i++)
  367. glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18,(int)str[i]);
  368. } else {
  369. char *finalStr = "final score: ";
  370. glRasterPos2i(x,y-15);
  371. for ( int i = 0 ; i < strlen(finalStr) ; i++)
  372. glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18,(int)finalStr[i]);
  373. for ( int i = 0 ; i < nDigits ; i++)
  374. glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18,(int)str[i]);
  375. }
  376.  
  377. glPopMatrix();
  378. glMatrixMode(GL_PROJECTION);
  379. glLoadMatrixd(matrix);
  380. glMatrixMode(GL_MODELVIEW);
  381. }
  382.  
  383. void gameOver(){
  384. animation_ongoing = 0;
  385. glClearColor(1.0f, 0.4f, 0.3f, 1.0f );
  386. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  387. //drawText("GAME OVER", 9, 0, 0);
  388. isGameOver = true;
  389. printf("GAME OVER\n");
  390. }
  391.  
  392. void drawAllTheBlocks(){
  393. for(int i=0; i<NUMOFBLOCKS; i++){
  394. drawBlock(i);
  395. }
  396. }
  397.  
  398. void removeIfBreakable(index){
  399. if(blocks[index].blockMode==1 && blocks[index].yOffset < 0){
  400. blocks[index].yOffset += -0.5;
  401. }
  402. }
  403.  
  404. void rotationIncrement(){
  405. if(rotation>0 && rotation<360){
  406. rotation += 30;
  407. }
  408. if(rotation>=360){
  409. rotation = 0;
  410. }
  411. }
  412.  
  413. void checkColision(int index){
  414. if(vectorSpeedY>=0){
  415. return;
  416. }
  417. float localCurrY = currentY - 0.8;
  418. float blockY = blocks[index].currY+0.1;
  419. float blockX = blocks[index].currX;
  420. float blockLen = blocks[index].length/2;
  421. //currentFloorCoord = -10;
  422. //printf("blockX=%lf currentX=%lf\n", blockX, currentX);
  423. //printf("blockY=%lf currentY=%lf offset=%lf\n", blockY, currentY, blocks[index].yOffset);
  424. if(localCurrY <= blockY && localCurrY >= blockY-1){ //1.5 instead of 1
  425. if(currentX >= blockX-blockLen && currentX <= blockX+blockLen){
  426. if(blocks[index].blockMode==RED){
  427. //red falling block
  428. currentFloorCoord = blockY+0.8;
  429. blocks[index].yOffset = -.01;
  430. } else if(blocks[index].blockMode == YELLOW){
  431. //yellow bonus block
  432. currentFloorCoord = blockY+0.8;
  433. printf("BONUS!\n");
  434. bonusActivated = true;
  435. bonusJumps = 5;
  436. } else if(blocks[index].blockMode == BLUE){
  437. //blue boost block
  438. currentFloorCoord = blockY+0.8;
  439. rotation = 1;
  440.  
  441. } else {
  442. currentFloorCoord = blockY+0.8;
  443. }
  444. if(bonusActivated){
  445. if(bonusJumps>0){
  446. bonusJumps--;
  447. globalScore += blocks[index].bonus*2;
  448. } else {
  449. printf("BONUS ENDED :(\n");
  450. bonusActivated = false;
  451. globalScore += blocks[index].bonus;
  452. }
  453.  
  454. } else {
  455. globalScore += blocks[index].bonus;
  456. }
  457. blocks[index].bonus = 0;
  458. printf("SCORE: %d\n", globalScore);
  459.  
  460. }
  461. }
  462. }
  463.  
  464. static void onKeyboard2(unsigned char key, int x, int y){
  465. if(key=='d' || key=='D'){
  466. right=false;
  467. } else if(key=='a' || key=='A'){
  468. left=false;
  469. }
  470. }
  471.  
  472. void drawPlayer(){
  473. //jumping cube
  474. GLfloat diffuse_coeffs[4];
  475. if(bonusActivated){
  476. diffuse_coeffs[0] = 1;
  477. diffuse_coeffs[1] = .8;
  478. diffuse_coeffs[2] = 0;
  479. diffuse_coeffs[3] = 1;
  480. } else {
  481. diffuse_coeffs[0] = 1;
  482. diffuse_coeffs[1] = 1;
  483. diffuse_coeffs[2] = 1;
  484. diffuse_coeffs[3] = 1;
  485. }
  486. glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse_coeffs);
  487.  
  488.  
  489. glPushMatrix();
  490.  
  491. glTranslatef(currentX,currentY,0);
  492. glRotatef(rotation,0.0,0.0,1.0);
  493.  
  494. glColor3ub(255, 193, 7);
  495. glScalef(1,1,1);
  496.  
  497. glutSolidCube(1);
  498.  
  499.  
  500. glPopMatrix();
  501. //end for jumping cube
  502. }
  503.  
  504. void pickColor(bool x){
  505. //139 194 74
  506. RGB wallColors[] = {{0,0.6,0.544},{0.42,0.27,0.75},{1,0.61,0},{0.583,0.799,.31},{0,0.76,0.851}};
  507. RGB currentWall;
  508. if(x){
  509. currentWall = wallColors[0];
  510. } else {
  511. currentWall = wallColors[(int)randBetween(0,4)];
  512. }
  513. glClearColor(currentWall.r, currentWall.g, currentWall.b, 0);
  514. }
  515.  
  516. void restartGame(){
  517. blockInit();
  518. currentX = 0;
  519. currentY = 0;
  520. globalScore = 0;
  521. moveWorld = false;
  522. bonusActivated = false;
  523. pickColor(false);
  524. density = 0.6;
  525. isGameOver = false;
  526. printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nNEW GAME: ");
  527. }
  528.  
  529. static void onKeyboard(unsigned char key, int x, int y)
  530. {
  531. switch (key) {
  532. case 27:
  533. //exit game
  534. exit(0);
  535. break;
  536.  
  537. case 'o':
  538. case 'O':
  539. //start game
  540. printf("\to - start game\n");
  541. if (!animation_ongoing) {
  542. glutTimerFunc(TIMER_INTERVAL, on_timer, TIMER_ID);
  543. animation_ongoing = 1;
  544. }
  545. break;
  546.  
  547. case 'p':
  548. case 'P':
  549. printf("\tp - pause game\n");
  550. //pause game
  551. animation_ongoing = 0;
  552. break;
  553.  
  554. case 'a':
  555. case 'A':
  556. //move left
  557. left = true;
  558. break;
  559.  
  560. case 'd':
  561. case 'D':
  562. //move right
  563. right = true;
  564. break;
  565.  
  566. case 'r':
  567. case 'R':
  568. //restart game
  569. restartGame();
  570. break;
  571.  
  572.  
  573. case 'l':
  574. case 'L':
  575. //change background color
  576. glClearColor((double)myRandom(255)/255, (double)myRandom(255)/255, (double)myRandom(255)/255, (double)myRandom(255)/255);
  577. glutPostRedisplay();
  578. haveFloor = false;
  579. break;
  580. }
  581. }
  582.  
  583. void decreaseDensity(){
  584. if(globalScore>50){
  585. density = 0.7;
  586. } else if(globalScore>100){
  587. density = 0.8;
  588. } else if(globalScore>150){
  589. density = 0.9;
  590. } else if(globalScore>200){
  591. density = 1;
  592. }
  593. }
  594.  
  595. static void on_timer(int value)
  596. {
  597.  
  598. if (value != TIMER_ID)
  599. return;
  600.  
  601. jump();
  602. throughWall();
  603. decreaseDensity();
  604.  
  605. if(currentY<-15){
  606. gameOver();
  607. }
  608.  
  609.  
  610.  
  611. //printf("x=%f y=%f\n",currentX, currentY);
  612. if(left){
  613. moveLeft();
  614. }
  615.  
  616. if(right){
  617. moveRight();
  618. }
  619.  
  620.  
  621.  
  622.  
  623. //currentFloorCoord = (haveFloor ? -6 : -100);
  624. currentFloorCoord = -50;
  625. for(int i=0; i<NUMOFBLOCKS; i++){
  626.  
  627. //remove colision if block is breakable and removed from map
  628. if(blocks[i].yOffset==0){
  629. checkColision(i);
  630.  
  631. }
  632. removeIfBreakable(i);
  633.  
  634. }
  635.  
  636. rotationIncrement();
  637.  
  638. if(moveWorld){
  639. for(int i=0; i<NUMOFBLOCKS; i++){
  640. if(currentY>=5 && vectorSpeedY>0){
  641. blocks[i].currY -= vectorSpeedY * 1.001; //*1.2;
  642.  
  643. } else {
  644. blocks[i].currY -= worldMovementSpeed;
  645. }
  646. }
  647. setNewBlockCoords();
  648.  
  649. }
  650.  
  651.  
  652. glutPostRedisplay();
  653.  
  654.  
  655. if (animation_ongoing) {
  656. glutTimerFunc(TIMER_INTERVAL, on_timer, TIMER_ID);
  657. }
  658. }
  659.  
  660.  
  661.  
  662. static void on_display(void)
  663. {
  664. //start for jumping cube
  665. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  666. glEnable(GL_DEPTH_TEST);
  667. glMatrixMode(GL_MODELVIEW);
  668. glLoadIdentity();
  669.  
  670.  
  671. GLfloat light_position[] = { 0, 2.3, -2.3, 1 };
  672.  
  673. //lights
  674. GLfloat light_ambient[] = { .1, .1, .1, 1 };
  675. GLfloat light_diffuse[] = { .5, .5, .5, 1 };
  676. GLfloat light_specular[] = { .6, .6, .6, 1 };
  677.  
  678. glEnable(GL_LIGHTING);
  679. glEnable(GL_LIGHT0);
  680. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  681. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  682. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  683. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  684.  
  685. GLfloat ambient_coeffs[] = { .7, .7, .7, 1 };
  686. GLfloat specular_coeffs[] = { .6, .5, .6, 1 };
  687. GLfloat shininess = 80;
  688.  
  689. glMaterialfv(GL_FRONT, GL_AMBIENT, ambient_coeffs);
  690. glMaterialfv(GL_FRONT, GL_SPECULAR, specular_coeffs);
  691. glMaterialf(GL_FRONT, GL_SHININESS, shininess);
  692. //lights
  693.  
  694. gluLookAt(15,0,0,0,0,0,0,1,0);
  695. glMatrixMode(GL_PROJECTION);
  696. glLoadIdentity();
  697. gluPerspective(60, 1, 1, 100);
  698.  
  699.  
  700. drawPlayer();
  701.  
  702. //start for floor
  703. //glPushMatrix();
  704. //glTranslatef(0, -8, 0);
  705. //glColor3ub(11, 11, 11);
  706. //glScalef(20,1,1);
  707.  
  708. //glutWireCube(1);
  709. //glPopMatrix();
  710. //end for floor
  711.  
  712.  
  713. //blockInit();
  714. //drawImage();
  715.  
  716. drawAllTheBlocks();
  717. drawText("SCORE: ", 10, 1, 1);
  718.  
  719. glFlush();
  720. glutSwapBuffers();
  721. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement