Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <random>
  3. #include <iostream>
  4. #include <glut.h>
  5. #include <windows.h>
  6. #include <mmsystem.h>
  7.  
  8. using std::cout;
  9. using std::endl;
  10.  
  11. struct enemy {
  12. int color;
  13. int st_points[2];
  14. int end_points[2];
  15. int b_points_0[2];
  16. int b_points_1[2];
  17. float t;
  18. };
  19. struct powerup {
  20. int st_points[2];
  21. int end_points[2];
  22. int b_points_0[2];
  23. int b_points_1[2];
  24. float t;
  25. };
  26.  
  27. bool playing;
  28. int counter;
  29. int speX;
  30. int speY;
  31. bool gen = false;
  32. enemy enemies[20];
  33. int numEnemies;
  34. powerup powerups[20];
  35. int numPowerups;
  36. int p0[2];
  37. int p3[2];
  38.  
  39. int mouthAngle = 50;
  40.  
  41.  
  42. void generateEnemyPowerup() {
  43. gen = false;
  44. int x = rand() % 100;
  45. if (x < 14) {
  46. powerups[numPowerups].t = 1;
  47. powerups[numPowerups].st_points[0] = 0;
  48. powerups[numPowerups].st_points[1] = rand() % 300;
  49. powerups[numPowerups].end_points[0] = 800;
  50. powerups[numPowerups].end_points[1] = rand() % 300;
  51. powerups[numPowerups].b_points_0[0] = rand() % 400;
  52. powerups[numPowerups].b_points_0[1] = rand() % 300;
  53. powerups[numPowerups].b_points_1[0] = rand() % 400 + 400;
  54. powerups[numPowerups].b_points_1[1] = rand() % 300;
  55.  
  56. numPowerups++;
  57.  
  58. //cout << "\nPowerup! x=";
  59. }
  60. else {
  61. enemies[numEnemies].t = 1;
  62. enemies[numEnemies].st_points[0] = 0;
  63. enemies[numEnemies].st_points[1] = rand() % 300;
  64. enemies[numEnemies].end_points[0] = 800;
  65. enemies[numEnemies].end_points[1] = rand() % 300;
  66. enemies[numEnemies].b_points_0[0] = rand() % 400;
  67. enemies[numEnemies].b_points_0[1] = rand() % 300;
  68. enemies[numEnemies].b_points_1[0] = rand() % 400 + 400;
  69. enemies[numEnemies].b_points_1[1] = rand() % 300;
  70. enemies[numEnemies].color = rand() % 3;
  71.  
  72. numEnemies++;
  73. //cout << "\nEnemy >:( x=";
  74. }
  75. glutPostRedisplay();
  76. }
  77.  
  78. int* bezier(float t, int* p0, int* p1, int* p2, int* p3)
  79. {
  80. int res[2];
  81. res[0] = pow((1 - t), 3)*p0[0] + 3 * t*pow((1 - t), 2)*p1[0] + 3 * pow(t, 2)*(1 - t)*p2[0] + pow(t, 3)*p3[0];
  82. res[1] = pow((1 - t), 3)*p0[1] + 3 * t*pow((1 - t), 2)*p1[1] + 3 * pow(t, 2)*(1 - t)*p2[1] + pow(t, 3)*p3[1];
  83. return res;
  84. }
  85.  
  86. void removeP(int index) {
  87. for (int i = index; i < numPowerups - 1; i++) {
  88. powerups[i] = powerups[i + 1];
  89. }
  90. numPowerups--;
  91. }
  92.  
  93. void removeE(int index) {
  94. for (int i = index; i < numEnemies - 1; i++) {
  95. enemies[i] = enemies[i + 1];
  96. }
  97. numEnemies--;
  98. }
  99.  
  100. void moveX(int x) {
  101. if (x == 0)return;
  102. if (x > 0)
  103. x--;
  104. else
  105. x++;
  106. speX += x;
  107. glutTimerFunc(30, moveX, x);
  108. }
  109.  
  110. void playMusicIn(int x) {
  111. switch (x) {
  112. case 0: PlaySound(TEXT("C:\\Users\\im-is\\Documents\\graphics assignment\\ConsoleApplication1\\pacman_intro.wav"), NULL, SND_ASYNC | SND_FILENAME);; break;
  113. case 1: PlaySound(TEXT("C:\\Users\\im-is\\Documents\\graphics assignment\\ConsoleApplication1\\pacman_chomp.wav"), NULL, SND_ASYNC | SND_FILENAME);; break;
  114. }
  115. }
  116.  
  117. void reset() {
  118. speX = 170;
  119. speY = 200;
  120. numPowerups = 0;
  121. numEnemies = 0;
  122. counter = 0;
  123. playing = false;
  124. playMusicIn(0);
  125. }
  126.  
  127. void renderPowerups() {
  128.  
  129. for (int i = 0; i < numPowerups; i++) {
  130. glPushMatrix();
  131. glPointSize(20);
  132. glColor3f(1, 1, 0);
  133. glBegin(GL_POINTS);
  134. int* b = bezier(powerups[i].t, powerups[i].st_points, powerups[i].b_points_0, powerups[i].b_points_1, powerups[i].end_points);
  135. glVertex3d(b[0], b[1], 0);
  136. glEnd();
  137. glPopMatrix();
  138. if ((b[0] <= speX + 30 && b[0] >= speX - 30 && b[1] <= speY + 30 && b[1] >= speY - 30)) {
  139. counter++;
  140. if (counter == 3) {
  141. reset();
  142. return;
  143. }
  144. moveX(10);
  145. removeP(i);
  146. }
  147. if (powerups[i].t <= 0)removeP(i);
  148. }
  149. }
  150.  
  151. void renderEnemies() {
  152. for (int i = 0; i < numEnemies; i++) {
  153. glPushMatrix();
  154. glPointSize(40);
  155. glColor3f(1, 0, 0);
  156. glBegin(GL_POINTS);
  157. int* b = bezier(enemies[i].t, enemies[i].st_points, enemies[i].b_points_0, enemies[i].b_points_1, enemies[i].end_points);
  158. glVertex3d(b[0], b[1], 0);
  159. glEnd();
  160. glPopMatrix();
  161. if ((b[0] <= speX + 30 && b[0] >= speX - 30 && b[1] <= speY + 30 && b[1] >= speY - 30)) {
  162. counter--;
  163. if (counter == -3) {
  164. reset();
  165. return;
  166. }
  167. moveX(-10);
  168. removeE(i);
  169. }
  170. if (enemies[i].t <= 0)removeE(i);
  171. }
  172. }
  173.  
  174. void runPacmanMouth(int x) {
  175. if (x == 0) {
  176. mouthAngle -= 1;
  177. if (mouthAngle <= 0) {
  178. x = 1;
  179. mouthAngle = 0;
  180. }
  181. }
  182. else {
  183. mouthAngle += 1;
  184. if (mouthAngle >= 50) {
  185. x = 0;
  186. mouthAngle = 50;
  187. }
  188. }
  189. //int y = x;
  190. glutTimerFunc(5, runPacmanMouth, x);
  191. }
  192.  
  193.  
  194.  
  195.  
  196. void Display() {
  197. glClearColor(0, 0, 0, 0.0f);
  198. glClear(GL_COLOR_BUFFER_BIT);
  199.  
  200. //Pacman
  201. GLUquadricObj* quadratic;
  202. quadratic = gluNewQuadric();
  203. glPushMatrix();
  204. glColor3f(1, 1, 0);
  205. glTranslated(speX, speY, 0);
  206. gluDisk(quadratic, 0, 30, 302, 302);
  207. glPushMatrix();
  208. //Pacman's mouth
  209. glBegin(GL_TRIANGLES);
  210. glColor3f(0.0f, 0.0f, 0.0f);
  211. glVertex3f(-7.0f, 0.0f, 0.0f);
  212. glColor3f(0.0f, 0.0f, 0.0f);
  213. glVertex3f(60.0f, mouthAngle, 0.0f);
  214. glColor3f(0.0f, 0.0f, 0.0f);
  215. glVertex3f(60.0f, 0 - mouthAngle, 0.0f);
  216. glEnd();
  217. glPopMatrix();
  218. glPopMatrix();
  219.  
  220.  
  221.  
  222.  
  223. if (gen) {
  224. generateEnemyPowerup();
  225. }
  226.  
  227. renderPowerups();
  228. renderEnemies();
  229.  
  230. glFlush();
  231. }
  232.  
  233.  
  234.  
  235. void time(int val) {
  236. gen = true;
  237. glutPostRedisplay();
  238. glutTimerFunc(1000, time, 0);
  239. }
  240.  
  241. void moveY(int x) {
  242. if (x == 0)return;
  243. if (x > 0)
  244. x--;
  245. else
  246. x++;
  247. speY += x;
  248. if (speY <= 34) {
  249. speY = 34;
  250. return;
  251. }
  252. if (speY >= 215) {
  253. speY = 215;
  254. return;
  255. }
  256. glutTimerFunc(30, moveY, x);
  257. }
  258.  
  259. void spe(int k, int x, int y)
  260. {
  261. if (k == GLUT_KEY_UP)
  262. moveY(10);
  263. if (k == GLUT_KEY_DOWN)
  264. moveY(-10);
  265. glutPostRedisplay();
  266. }
  267.  
  268.  
  269. void blerp(int val) {
  270. for (int i = 0; i < numPowerups; i++) {
  271. powerups[i].t -= 0.002;
  272. }
  273. for (int i = 0; i < numEnemies; i++) {
  274. enemies[i].t -= 0.003;
  275. }
  276. glutPostRedisplay();
  277. glutTimerFunc(10, blerp, 0);
  278. }
  279.  
  280. void main(int argc, char** argr) {
  281. glutInit(&argc, argr);
  282.  
  283. glutInitWindowSize(850, 400);
  284. glutInitWindowPosition(150, 150);
  285.  
  286. glutCreateWindow("Control");
  287. glutDisplayFunc(Display);
  288. glutSpecialFunc(spe);
  289. glPointSize(25);
  290. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  291. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  292. glMatrixMode(GL_PROJECTION);
  293. glLoadIdentity();
  294. gluOrtho2D(0.0, 500, 0.0, 250);
  295. glutTimerFunc(0, time, 0);
  296. glutTimerFunc(0, blerp, 0);
  297. reset();
  298. runPacmanMouth(0);
  299. glutMainLoop();
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement