Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.44 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <GL/glut.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include <math.h>
  6. //#include<iostream>
  7. //using namespace std;
  8.  
  9.  
  10. int score = 0;
  11. int brick_color = 1, ball_color = 3, level = 0, paddle_color = 2, text_color = 5, size = 1;;
  12. GLfloat twoModel[] = { GL_TRUE };
  13. int game_level[] = { 7,5,2 };
  14. float rate = game_level[level];
  15.  
  16. GLfloat brick_color_array[][3] = { { 1,0,0 },{ 0,0,1 },{ 0,1,0 },{ 1,0,1 },{ 1,1,0 },{ 0,1,1 } };
  17. GLfloat paddle_color_array[][3] = { { 1,0,0 },{ 0,0,1 },{ 0,1,0 },{ 1,0,1 },{ 1,1,0 },{ 0,1,1 } };
  18. GLfloat text_color_array[][4] = { { 1,0,0,1 },{ 0,0,1,1 },{ 0,1,0,1 },{ 1,0,1,1 },{ 1,1,0,1 },{ 0,1,1,1 } };
  19. GLfloat paddle_size[] = { 2,4,6 };
  20. //The grid parameters for the bricks
  21. int rows = 4;
  22. int columns = 10;
  23.  
  24. // Structure to store the coordinates of each brick
  25. struct brick_coords {
  26.  
  27. GLfloat x;
  28. GLfloat y;
  29. };
  30.  
  31. //Array to store the bricks
  32. brick_coords brick_array[50][50];
  33. GLfloat px, bx = 0, by = -12.8, speed = 0, dirx = 0, diry = 0, start = 0;
  34.  
  35.  
  36.  
  37. // Function to draw the paddle
  38. void draw_paddle()
  39. {
  40. glDisable(GL_LIGHTING);
  41. glColor3fv(paddle_color_array[paddle_color]);
  42. glBegin(GL_POLYGON);
  43. glVertex3f(-paddle_size[size] + px, 0 - 15, 0);
  44. glVertex3f(paddle_size[size] + px, 0 - 15, 0);
  45. glVertex3f(paddle_size[size] + px, 1 - 15, 0);
  46. glVertex3f(-paddle_size[size] + px, 1 - 15, 0);
  47. glEnd();
  48. glEnable(GL_LIGHTING);
  49. }
  50.  
  51.  
  52. //Function to draw a single brick
  53. void brick(GLfloat x, GLfloat y, GLfloat z)
  54. {
  55.  
  56. glDisable(GL_LIGHTING);
  57. glColor3fv(brick_color_array[brick_color]);
  58. glBegin(GL_QUADS);
  59. glVertex3f(x, y, z);
  60. glVertex3f(x + 3, y, z);
  61. glVertex3f(x + 3, y + 1, z);
  62. glVertex3f(x, y + 1, z);
  63. glEnd();
  64. glEnable(GL_LIGHTING);
  65. }
  66.  
  67.  
  68. // Function to draw the grid of bricks
  69. void draw_bricks()
  70. {
  71.  
  72. int i, j;
  73. if (start == 0)
  74. {
  75. for (i = 1;i <= rows;i++)
  76. {
  77. for (j = 1;j <= columns;j++)
  78. {
  79.  
  80. brick_array[i][j].x = (GLfloat)(j * 4 * 0.84);
  81. brick_array[i][j].y = (GLfloat)(i * 2 * 0.6);
  82. }
  83. }
  84. }
  85.  
  86.  
  87. glPushMatrix();
  88. glTranslatef(-19.5, 5, 0);
  89.  
  90. for (i = 1;i <= rows;i += 1)
  91. {
  92. for (j = 1;j <= columns;j += 1)
  93. {
  94.  
  95. if (brick_array[i][j].x == 0 || brick_array[i][j].y == 0)
  96. {
  97. continue;
  98. }
  99. brick(brick_array[i][j].x, brick_array[i][j].y, 0);
  100. }
  101. }
  102. glPopMatrix();
  103.  
  104. }
  105.  
  106. //Function to draw the spherical ball
  107. void draw_ball()
  108. {
  109. GLfloat ambient1[] = { 1,1,1 };
  110. GLfloat diffuse1[] = { 0.4,0.4,0.4 };
  111. GLfloat specular1[] = { 1,1,1 };
  112.  
  113. GLfloat position[] = { 0,0,-50,1 };
  114. GLfloat ambient2[] = { 0,0,0 };
  115. GLfloat diffuse2[] = { 1,1,1 };
  116. GLfloat specular2[] = { 0,1,1 };
  117.  
  118. float materialColours[][3] = { { 1,0,0 },{ 0,0,1 },{ 0,1,0 },{ 1,0,1 },{ 1,1,0 },{ 0,1,1 } };
  119. GLfloat matAmbient1[] = { 1,1,1 };
  120. GLfloat matDiffuse1[] = { 1,1,1 };
  121. GLfloat matSpecular1[] = { 1,1,1 };
  122. GLfloat shininess[] = { 1000 };
  123.  
  124. glLightfv(GL_LIGHT0, GL_SPECULAR, specular1);
  125. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient1);
  126. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse1);
  127.  
  128. glLightfv(GL_LIGHT1, GL_POSITION, position);
  129. glLightfv(GL_LIGHT1, GL_SPECULAR, specular2);
  130. glLightfv(GL_LIGHT1, GL_AMBIENT, ambient2);
  131. glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse2);
  132.  
  133. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
  134. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, materialColours[ball_color]);
  135.  
  136.  
  137.  
  138. glPushMatrix();
  139. glTranslatef(bx, by, 0);
  140. glScalef(1.0, 1.0, 0.5);
  141. //glScalef(size[i], size[], size[]);
  142. glutSolidSphere(1.0, 52, 52);
  143.  
  144. glPopMatrix();
  145.  
  146. }
  147.  
  148.  
  149. //mouse function
  150. void mousemotion(int x, int y)
  151. {
  152.  
  153. if (start == 1)
  154. {
  155. px = (x - glutGet(GLUT_WINDOW_WIDTH) / 2) / 20;
  156. if (px>15)
  157. {
  158. px = 15;
  159. }
  160. if (px<-15)
  161. {
  162. px = -15;
  163. }
  164. }
  165.  
  166. else glutSetCursor(GLUT_CURSOR_INHERIT);
  167. }
  168.  
  169. //handle brick color
  170. void change_brick_color(int action)
  171. {
  172.  
  173. brick_color = action - 1;
  174. }
  175.  
  176. //handle ball color
  177. void change_ball_color(int action)
  178. {
  179.  
  180. ball_color = action - 1;
  181. }
  182.  
  183. //handle level
  184. void change_difficulty(int action)
  185. {
  186.  
  187. level = action - 1;
  188. }
  189.  
  190. //handle menu
  191. void handle_menu(int action)
  192. {
  193.  
  194. }
  195.  
  196. //handle paddle color
  197. void change_paddle_color(int action)
  198. {
  199. paddle_color = action - 1;
  200. }
  201.  
  202. //handle paddle color
  203. void change_text_color(int action)
  204. {
  205. text_color = action - 1;
  206. }
  207.  
  208. //handle paddle size
  209. void change_paddle_size(int action)
  210. {
  211. size = action - 1;
  212. }
  213.  
  214. //add menu
  215. void addMenu()
  216. {
  217.  
  218.  
  219. int submenu1 = glutCreateMenu(change_brick_color);
  220. glutAddMenuEntry("Red", 1);
  221. glutAddMenuEntry("Blue", 2);
  222. glutAddMenuEntry("Green", 3);
  223. glutAddMenuEntry("Purple", 4);
  224. glutAddMenuEntry("Yellow", 5);
  225. glutAddMenuEntry("Cyan", 6);
  226.  
  227. int submenu2 = glutCreateMenu(change_ball_color);
  228. glutAddMenuEntry("Red", 1);
  229. glutAddMenuEntry("Blue", 2);
  230. glutAddMenuEntry("Green", 3);
  231. glutAddMenuEntry("Purple", 4);
  232. glutAddMenuEntry("Yellow", 5);
  233. glutAddMenuEntry("Cyan", 6);
  234.  
  235. int submenu4 = glutCreateMenu(change_paddle_color);
  236. glutAddMenuEntry("Red", 1);
  237. glutAddMenuEntry("Blue", 2);
  238. glutAddMenuEntry("Green", 3);
  239. glutAddMenuEntry("Purple", 4);
  240. glutAddMenuEntry("Yellow", 5);
  241. glutAddMenuEntry("Cyan", 6);
  242.  
  243. int submenu3 = glutCreateMenu(change_difficulty);
  244. glutAddMenuEntry("Easy", 1);
  245. glutAddMenuEntry("Medium", 2);
  246. glutAddMenuEntry("Hard", 3);
  247.  
  248. int submenu5 = glutCreateMenu(change_text_color);
  249. glutAddMenuEntry("Red", 1);
  250. glutAddMenuEntry("Blue", 2);
  251. glutAddMenuEntry("Green", 3);
  252. glutAddMenuEntry("Purple", 4);
  253. glutAddMenuEntry("Yellow", 5);
  254. glutAddMenuEntry("Cyan", 6);
  255.  
  256. int submenu6 = glutCreateMenu(change_paddle_size);
  257. glutAddMenuEntry("Small", 1);
  258. glutAddMenuEntry("Medium", 2);
  259. glutAddMenuEntry("Large", 3);
  260.  
  261. glutCreateMenu(handle_menu);
  262. glutAddSubMenu("Bricks Color", submenu1);
  263. glutAddSubMenu("Ball Color", submenu2);
  264. glutAddSubMenu("Paddle Color", submenu4);
  265. glutAddSubMenu("Text Color", submenu5);
  266. glutAddSubMenu("Difficulty", submenu3);
  267. glutAddSubMenu("Paddle Size", submenu6);
  268. glutAttachMenu(GLUT_RIGHT_BUTTON);
  269.  
  270. }
  271.  
  272.  
  273.  
  274. //Function to print the score on the screen
  275. void text(int sc)
  276. {
  277. glDisable(GL_LIGHTING);
  278. char text[40];
  279. char difficulty[10];
  280. if (level == 0)
  281. {
  282. printf(difficulty, "Easy");
  283. }
  284.  
  285. if (level == 1)
  286. {
  287. printf(difficulty, "Medium");
  288. }
  289.  
  290. if (level == 2)
  291. {
  292. printf(difficulty, "Hard");
  293. }
  294. if (sc <40)
  295. printf(text, "Difficulty: %s Your Score: %d", difficulty, sc);
  296. else
  297. {
  298. printf(text, "You have won !!");
  299. start = 0;
  300. by = -12.8;
  301. bx = 0;
  302. dirx = 0;
  303. diry = 0;
  304. px = 0;
  305.  
  306. }
  307. // The color
  308. glColor4fv(text_color_array[text_color]);
  309. // Position of the text to be printer
  310. glPushMatrix();
  311. glTranslatef(-1, 0, 0);
  312. glRasterPos3f(0, 0, 20);
  313. for (int i = 0; text[i] != '\0'; i++)
  314. glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text[i]);
  315. glEnable(GL_LIGHTING);
  316. glPopMatrix();
  317. }
  318.  
  319. //The main display function
  320. void display(void) {
  321.  
  322. glClearColor(0.0, 0.0, 0.0, 1.0);
  323. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  324. glLoadIdentity();
  325. gluLookAt(0, 0, 0, 0, 0, -25, 0, 1, 0);
  326. glTranslatef(0, 0, -25);
  327. draw_paddle();
  328. draw_bricks();
  329. draw_ball();
  330. text(score);
  331. glutSwapBuffers();
  332. }
  333.  
  334.  
  335.  
  336. //function to turn on lights
  337. void lightsOn()
  338. {
  339. glEnable(GL_LIGHTING);
  340. glEnable(GL_LIGHT0);
  341. glEnable(GL_LIGHT1);
  342. }
  343.  
  344. void reshape(int w, int h) {
  345. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  346. glMatrixMode(GL_PROJECTION);
  347. glLoadIdentity();
  348. gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 1000.0);
  349. glMatrixMode(GL_MODELVIEW);
  350. }
  351.  
  352.  
  353. //function to take in keyboard entries
  354. void keyboard(unsigned char key, int x, int y)
  355. {
  356. switch (key)
  357. {
  358. case 'd': px += 3; break;
  359. case 'a': px -= 3; break;
  360. case 'q': exit(0); break;
  361. case 's':
  362. if (!start)
  363. {
  364. dirx = diry = 1;
  365. rate = game_level[level];
  366. start = 1;
  367. score = 0;
  368. glutSetCursor(GLUT_CURSOR_NONE);
  369.  
  370. }
  371. break;
  372. }
  373. if (px>15)
  374. {
  375. px = 15;
  376. }
  377. if (px<-15)
  378. {
  379. px = -15;
  380. }
  381. if (start == 0)
  382. {
  383. px = 0;
  384. }
  385. glutPostRedisplay();
  386. }
  387.  
  388.  
  389. //Function to handle the case when the ball strikes the bricks
  390. void hit()
  391. {
  392. int i, j;
  393. for (i = 1;i <= rows;i++)
  394. for (j = 1;j <= columns;j++)
  395. {
  396. if ((bx >= brick_array[i][j].x - 19.5 - 0.1) && (bx <= brick_array[i][j].x + 3 - 19.5 + 0.1))
  397. {
  398. if (by >= brick_array[i][j].y + 5 - 0.1 && by <= brick_array[i][j].y + 5 + 1.2 + 0.1)
  399. {
  400. brick_array[i][j].x = 0;
  401. brick_array[i][j].y = 0;
  402. diry = diry*-1;
  403.  
  404. score++;
  405. }
  406. //cout<<bx<<" "<<by<<"\t"<<brick_array[i][j].x<<" "<<brick_array[i][j].y;
  407. }
  408. else if (by >= brick_array[i][j].y + 5 - 0.1 && by <= brick_array[i][j].y + 5 + 1.2 + 0.1)
  409. {
  410. if ((bx >= brick_array[i][j].x - 19.5 - 0.1) && (bx <= brick_array[i][j].x + 3 - 19.5 + 0.1))
  411. {
  412. brick_array[i][j].x = 0;
  413. brick_array[i][j].y = 0;
  414. dirx = dirx*-1;
  415. score++;
  416. }
  417. //cout<<bx<<" "<<by<<"\t"<<brick_array[i][j].x<<" "<<brick_array[i][j].y;
  418. }
  419. }
  420. }
  421.  
  422.  
  423. //The idle function. Handles the motion of the ball along with rebounding from various surfaces
  424. void idle()
  425. {
  426. hit();
  427. if (bx<-16 || bx>16 && start == 1)
  428. {
  429. dirx = dirx*-1;
  430. }
  431. if (by<-15 || by>14 && start == 1)
  432. {
  433. diry = diry*-1;
  434. }
  435. bx += dirx / (rate);
  436. by += diry / (rate);
  437. rate -= 0.001; // Rate at which the speed of ball increases
  438.  
  439. float x = paddle_size[size];
  440. //Make changes here for the different position of ball after rebounded by paddle
  441. if (by <= -12.8 && bx<(px + x * 2 / 3) && bx>(px + x / 3) && start == 1)
  442. {
  443. dirx = 1;
  444. diry = 1;
  445. }
  446. else if (by <= -12.8 && bx<(px - x / 3) && bx>(px - x * 2 / 3) && start == 1)
  447. {
  448. dirx = -1;
  449. diry = 1;
  450. }
  451. else if (by <= -12.8 && bx<(px + x / 3) && bx>(px - x / 3) && start == 1)
  452. {
  453. dirx = dirx;
  454. diry = 1;
  455. }
  456. else if (by <= -12.8 && bx<(px - (x * 2 / 3)) && bx>(px - (x + 0.3)) && start == 1)
  457. {
  458. dirx = -1.5;
  459. diry = 0.8;
  460. }
  461. else if (by <= -12.8 && bx<(px + (x + 0.3)) && bx>(px + x / 3) && start == 1)
  462. {
  463. dirx = 1.5;
  464. diry = 0.8;
  465. }
  466. else if (by<-13)
  467. {
  468. start = 0;
  469. by = -12.8;
  470. bx = 0;
  471. dirx = 0;
  472. diry = 0;
  473. px = 0;
  474. }
  475. glutPostRedisplay();
  476. }
  477.  
  478.  
  479. int main(int argc, char **argv) {
  480. glutInit(&argc, argv);
  481. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
  482. glutInitWindowSize(900, 900);
  483. glutInitWindowPosition(100, 100);
  484. glutCreateWindow("Brick Breaker");
  485. glutDisplayFunc(display);
  486. glutReshapeFunc(reshape);
  487. glEnable(GL_DEPTH_TEST);
  488. glutIdleFunc(idle);
  489. glutPassiveMotionFunc(mousemotion);
  490. glutKeyboardFunc(keyboard);
  491. lightsOn();
  492. addMenu();
  493.  
  494. glutMainLoop();
  495. return 0;
  496. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement