Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.47 KB | None | 0 0
  1. #include <graphics_lib.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <unistd.h>
  6.  
  7. int initial_x_position= 100;
  8. int initial_y_position = 350;
  9. int total_Score = 0;
  10. int velocity;
  11. int angle = 45;
  12. int shoot = 0;
  13. int initial_projectile_x = 150;
  14. int initial_projectile_y = 295;
  15. int Target_X;
  16. int bin_x = 20000;
  17. int tree_x = 20000;
  18.  
  19. //void main_menu();
  20.  
  21. int get_velocity(void)
  22. {
  23. int velocity;
  24.  
  25.  
  26. printf("Enter a velocity: \n");
  27. scanf("%d", &velocity );
  28. return velocity;
  29.  
  30. }
  31. int get_angle(void)
  32. { int angle;
  33.  
  34. printf("Enter an angle: \n");
  35. scanf("%d", &angle );
  36. angle = angle * 0.0174533;
  37.  
  38. return angle;
  39.  
  40. }
  41. void draw_target(Target_X, initial_projectile_x)
  42. {
  43. int distance = (-initial_projectile_x + Target_X);
  44. GFX_DrawLine(Target_X, 410, Target_X, 470, 2);
  45. GFX_InitFont();
  46.  
  47. draw_Well(Target_X - 15, 365);
  48. char Target_Text[20];
  49. GFX_SetColour(BLACK);
  50. sprintf(Target_Text, "Distance = %d", distance);
  51. GFX_DrawText(Target_X - 125, 450, Target_Text);
  52.  
  53. }
  54. void display_score(score)
  55. {
  56. GFX_DrawFilledRectangle(50,50, 350, 80, LIGHTBLUE);
  57. GFX_InitFont();
  58. char score_text[20];
  59. sprintf(score_text, "Total Score: = %d", score);
  60. GFX_DrawText(50, 50, score_text);
  61. }
  62. int draw_Well(x,y)
  63. {
  64.  
  65. GFX_InitBitmap();
  66. BITMAP well = GFX_LoadBitmap("well.png");
  67. GFX_DrawBitmap(well, x, y);
  68. GFX_UpdateDisplay();
  69.  
  70. }
  71. int draw_Man(x,y)
  72. {
  73. GFX_InitBitmap();
  74. BITMAP man = GFX_LoadBitmap("man3.png");
  75. GFX_DrawBitmap(man, x, y);
  76. GFX_UpdateDisplay();
  77.  
  78. }
  79. int draw_tree(x,y)
  80. {
  81. GFX_InitBitmap();
  82. BITMAP tree = GFX_LoadBitmap("tree.png");
  83. GFX_DrawBitmap(tree, x, y);
  84. GFX_UpdateDisplay();
  85.  
  86. }
  87. int draw_bin(x,y)
  88. {
  89. GFX_InitBitmap();
  90. BITMAP bin = GFX_LoadBitmap("bin.png"); //bin is 70x72px
  91. GFX_DrawBitmap(bin, x, y);
  92. GFX_UpdateDisplay();
  93.  
  94. }
  95.  
  96. int draw_title(x,y)
  97. {
  98. GFX_InitBitmap();
  99. BITMAP title = GFX_LoadBitmap("title.png");
  100. GFX_DrawBitmap(title, x, y);
  101. GFX_UpdateDisplay();
  102.  
  103. }
  104. int draw_Background(x,y)
  105. {
  106.  
  107. GFX_DrawFilledRectangle(0,0,800,500,LIGHTBLUE);
  108. GFX_InitBitmap();
  109. BITMAP background = GFX_LoadBitmap("Background.png");
  110. GFX_DrawBitmap(background, x, y);
  111. GFX_UpdateDisplay();
  112.  
  113. }
  114.  
  115. int calculate_Score(int Target_X, int final_X)
  116. {
  117. int score;
  118. int temp;
  119. temp = abs(abs(Target_X)- abs(final_X));
  120. score = 1000-temp;
  121. return score;
  122.  
  123. }
  124. int draw_stickman(int x, int y, int colour)
  125. {
  126. GFX_SetColour(colour);
  127. GFX_DrawLine(x, y, x + 40, y + 60, 2);
  128. GFX_DrawLine(x, y, x - 40, y + 60, 2);
  129. GFX_DrawLine(x, y, x, y + 5, 2);
  130. GFX_DrawLine(x, y, x, y - 70, 2);
  131. GFX_DrawCircle (x, y-90, 20, 2);
  132. GFX_DrawLine(x - 50 , y - 55, x + 50, y - 55, 2);
  133.  
  134. void;
  135. }
  136. int draw_ground(x)
  137. { draw_Background(350,250);
  138. GFX_DrawFilledRectangle(0, x+6, 800, x+400, BROWN);
  139. GFX_SetColour(GREEN);
  140. GFX_DrawLine(0, x, 800, x, 5);
  141. GFX_SetColour(BLACK);
  142. GFX_DrawLine(0, x + 7, 800, x + 7, 10);
  143.  
  144.  
  145. void;
  146. }
  147.  
  148. int draw_projectile(int initial_x, int initial_y, int vel_x, int vel_y)
  149. {
  150. float gravity = 9.81;
  151. float time;
  152. float x_position;
  153. float y_position;
  154. int hit;
  155.  
  156. y_position = initial_y;
  157. x_position = initial_x;
  158. GFX_SetColour(BLACK);
  159. while(y_position < 410)
  160. {
  161. time = (x_position - initial_x)/ vel_x;
  162. y_position = initial_y - (vel_y*time) + (gravity*time*time)/2;
  163. GFX_DrawLineTo((int)x_position, (int)y_position, 3);
  164.  
  165.  
  166. x_position += 1;
  167. if(((int)y_position > 350) && ((bin_x -25) < (int)x_position))
  168. {
  169. if (((int)x_position)< (bin_x + 25))
  170. {
  171.  
  172. hit = 0;
  173. break;
  174.  
  175. }
  176. }
  177. else
  178. {
  179. hit = 1;
  180. }
  181. if(((int)y_position > 250) && ((tree_x -35) < (int)x_position))
  182. {
  183. if (((int)x_position)< (tree_x + 25))
  184. {
  185.  
  186. hit = 0;
  187. break;
  188.  
  189. }
  190. }
  191. else
  192. {
  193. hit = 1;
  194. }
  195. GFX_UpdateDisplay();
  196. }
  197. if (hit != 0)
  198. {
  199. draw_playagain_menu();
  200. }
  201. else
  202. {
  203.  
  204. gameover();
  205.  
  206. }
  207.  
  208.  
  209. return x_position;
  210. }
  211.  
  212. int check_target(int final_x, int target_x)
  213. {
  214. if (final_x > (target_x-50) && final_x < (target_x+50))
  215. {
  216. printf("Well done\n");
  217. }
  218. else
  219. {
  220. printf("Better luck next time\n");
  221. }
  222.  
  223. }
  224. int gameover()
  225. {
  226. int count = 0;
  227. GFX_DrawFilledRectangle(0, 65, 800, 120, LIGHTBLUE);
  228. GFX_DrawText(370, 68, "Game Over!");
  229. GFX_UpdateDisplay();
  230. sleep(3);
  231. main_menu();
  232. }
  233. int initialise_window(x,y)
  234. {
  235. //GFX_SetColour(RED);
  236.  
  237. //draw_stickman(x, y, 5);
  238.  
  239. draw_ground(x+310);
  240. draw_Man(x+9,y-22);
  241.  
  242. void;
  243. }
  244. void draw_vel_controls(velocity)
  245. {
  246. GFX_DrawFilledTriangle(40,100, 40, 80, 30, 90, BLUE);
  247. GFX_DrawFilledTriangle(170,100, 170, 80, 180, 90, BLUE);
  248. GFX_InitFont();
  249.  
  250. char vel_text[20];
  251. sprintf(vel_text, "Velocity: %d", velocity);
  252. GFX_DrawText(50, 80, vel_text);
  253.  
  254.  
  255.  
  256. GFX_UpdateDisplay();
  257. }
  258. void draw_ang_controls(angle)
  259. {
  260. GFX_DrawFilledTriangle(220,100, 220, 80, 210, 90, BLUE);
  261. GFX_DrawFilledTriangle(320,100, 320, 80, 330, 90, BLUE);
  262. GFX_InitFont();
  263. char vel_text[20];
  264.  
  265. sprintf(vel_text, "Angle: %d", angle);
  266. GFX_DrawText(230, 80, vel_text);
  267.  
  268.  
  269.  
  270. GFX_UpdateDisplay();
  271. }
  272. void draw_playagain_menu()
  273. {
  274. GFX_DrawText(500, 70, "Next Level");
  275. GFX_DrawText(500, 100, "Main Menu");
  276. GFX_UpdateDisplay();
  277. }
  278. void get_controls()
  279. {
  280. int mouseX, mouseY;
  281. int mouseX2, mouseY2;
  282. int control_check = 1;
  283. GFX_CreateEventQueue();
  284. GFX_InitMouse();
  285. GFX_RegisterMouseEvents();
  286.  
  287.  
  288. while(control_check == 1)
  289. {
  290. GFX_WaitForEvent();
  291. if (GFX_IsEventMouseButton())
  292. {
  293. GFX_GetMouseCoordinates(&mouseX, &mouseY);
  294.  
  295. if ((30 < mouseX && mouseX < 40) && (80 < mouseY && mouseY < 100))
  296. {
  297. if (velocity >=5)
  298. {
  299. velocity = velocity - 5;
  300. GFX_DrawFilledRectangle(50,80,150,100,LIGHTBLUE);
  301. draw_vel_controls(velocity);
  302. GFX_UpdateDisplay();
  303. }
  304. }
  305.  
  306. else if ((170 < mouseX && mouseX < 180) && (80 < mouseY && mouseY < 100))
  307. {
  308. velocity = velocity + 5;
  309. GFX_DrawFilledRectangle(50,80,179,100,LIGHTBLUE);
  310. draw_vel_controls(velocity);
  311. GFX_UpdateDisplay();
  312. }
  313. else if ((350 < mouseX && mouseX < 420) && (70 < mouseY && mouseY < 110))
  314. { int i;
  315.  
  316. i = fire(velocity, angle);
  317. if (i != 0)
  318. {
  319. control_check = 0;
  320. }
  321.  
  322. int done=1;
  323. while(done)
  324. {
  325. GFX_WaitForEvent();
  326. if (GFX_IsEventMouseButton())
  327. {
  328. GFX_GetMouseCoordinates(&mouseX2, &mouseY2);
  329. if ((500 <mouseX2 && mouseX2 < 650) && (70 < mouseY2 && mouseY2< 85))
  330. {
  331. GFX_ClearWindow();
  332. initialise_window(initial_x_position, initial_y_position);
  333. game();
  334. GFX_UpdateDisplay();
  335. done=0;
  336. }
  337. else if ((500 <mouseX2 && mouseX2 < 650) && (100 < mouseY2 && mouseY2< 120 ))
  338. { GFX_ClearWindow();
  339. main_menu();
  340. total_Score = 0;
  341. GFX_UpdateDisplay();
  342.  
  343. }
  344. }
  345. }
  346. }
  347.  
  348. else if ((210 < mouseX && mouseX < 220) && (80 < mouseY && mouseY < 100))
  349. {
  350. if (angle >= 5)
  351. {
  352. angle = angle - 5;
  353. GFX_DrawFilledRectangle(221,80,319,100,LIGHTBLUE);
  354. draw_ang_controls(angle);
  355. GFX_WaitForEvent();
  356.  
  357.  
  358. }
  359. }
  360.  
  361. else if ((320 < mouseX && mouseX < 330) && (80 < mouseY && mouseY < 100))
  362. {
  363. if (angle <= 80)
  364. {
  365. angle = angle + 5;
  366. GFX_DrawFilledRectangle(221,80,319,100,LIGHTBLUE);
  367. draw_ang_controls(angle);
  368. GFX_UpdateDisplay();
  369. }
  370. }
  371. }
  372.  
  373.  
  374. }
  375. }
  376. int fire(int velocity, int angle)
  377. {
  378. int vel_x;
  379. int vel_y;
  380. int final_x;
  381. int score;
  382. int distance;
  383. GFX_UpdateDisplay();
  384. vel_x = velocity * cos(angle*0.0174533);
  385. vel_y = velocity * sin(angle*0.0174533);
  386. GFX_MoveTo(initial_projectile_x, initial_projectile_y);
  387. final_x = draw_projectile(initial_projectile_x, initial_projectile_y,vel_x, vel_y);
  388. score = calculate_Score(final_x, Target_X);
  389. total_Score= total_Score + score;
  390. display_score(total_Score);
  391. GFX_UpdateDisplay();
  392. shoot = 0;
  393. distance = -(initial_projectile_x - final_x);
  394. printf("You threw %d metres\n", distance);
  395. printf("You scored %d\n", score);
  396. check_target(final_x, Target_X);
  397. GFX_UpdateDisplay();
  398.  
  399. return final_x;
  400.  
  401. }
  402. void clear_screen()
  403. { draw_target();
  404. GFX_UpdateDisplay();
  405.  
  406.  
  407. }
  408. void display_shoot()
  409. {
  410. GFX_DrawRectangle(350, 70, 420, 110, 2);
  411. GFX_DrawText(360, 80, "Shoot!");
  412.  
  413. GFX_UpdateDisplay();
  414.  
  415. }
  416.  
  417. int get_highscores(x,y)
  418. {
  419. int i=0,totalNums;
  420. double Scores[100];
  421. char line[100]; /* declare a char array */
  422.  
  423. FILE *file; /* declare a FILE pointer */
  424. file = fopen("highscores.txt", "r"); /* open a text file for reading */
  425.  
  426. while(fgets(line, sizeof line, file)!=NULL) { /* keep looping until NULL pointer... */
  427. // printf("Lines of numbers.txt file are: %s", line);
  428. Scores[i]=atoi(line); /* convert string to double float*/
  429. i++;
  430. }
  431.  
  432. char highscore1[20];
  433. sprintf(highscore1, "1. %d", (int)Scores[0]);
  434. GFX_DrawText(x, y + 20 , highscore1);
  435.  
  436. char highscore2[20];
  437. sprintf(highscore2, "2. %d", (int)Scores[1]);
  438. GFX_DrawText(x,y + 40 , highscore2);
  439. fclose(file);
  440.  
  441. char highscore3[20];
  442. sprintf(highscore3, "3. %d", (int)Scores[2]);
  443. GFX_DrawText(x, y+ 60 , highscore3);
  444.  
  445. char highscore4[20];
  446. sprintf(highscore4, "4. %d", (int)Scores[3]);
  447. GFX_DrawText(x, y+ 80 , highscore4);
  448. }
  449. int game(void)
  450. {
  451.  
  452.  
  453. velocity = 50;
  454. srand(time(NULL));
  455. if( total_Score < 1500)
  456. {
  457. Target_X = GFX_RandNumber(250, 750);
  458. draw_target(Target_X);
  459.  
  460. }
  461.  
  462. else if ((total_Score >=1501) && (total_Score <= 5000))
  463. {
  464. Target_X = GFX_RandNumber(500, 750);
  465. if (Target_X < 500)
  466. {
  467. bin_x = GFX_RandNumber(Target_X + 50, Target_X + 200);
  468. }
  469. else if (Target_X > 500)
  470. {
  471. bin_x = GFX_RandNumber(250, Target_X - 200);
  472. }
  473. draw_target(Target_X, initial_projectile_x);
  474.  
  475. draw_bin(bin_x, 387);
  476.  
  477. }
  478.  
  479. else if (total_Score > 5000)
  480. {
  481. Target_X = GFX_RandNumber(400, 750);
  482. tree_x = GFX_RandNumber(300, Target_X - 100);
  483.  
  484. if (Target_X <500)
  485. {
  486. bin_x = GFX_RandNumber(Target_X + 50, 750);
  487. }
  488. bin_x = GFX_RandNumber(Target_X + 50, Target_X + 200);
  489. draw_tree(tree_x, 322);
  490. draw_target(Target_X, initial_projectile_x);
  491. draw_bin(bin_x, 387);
  492. }
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500. display_score(total_Score);
  501. display_shoot();
  502. draw_vel_controls(velocity);
  503. draw_ang_controls(angle);
  504. get_controls();
  505. GFX_UpdateDisplay();
  506.  
  507. }
  508. void main_menu()
  509. {
  510. int mouseX, mouseY;
  511. int check = 1;
  512. GFX_UpdateDisplay();
  513. draw_Background(350,250);
  514. draw_ground(initial_x_position+310);
  515. draw_title(395, 30);
  516. GFX_DrawRectangle(200, 120, 600, 170, 2);
  517. GFX_InitFont();
  518. GFX_DrawText(380, 138, "Play");
  519. GFX_DrawRectangle(200,190,600,240,2);
  520. GFX_DrawText(380,208, "Exit");
  521. GFX_DrawText(200, 65, "Welcome! Adjust the angle and velocity to throw ");
  522. GFX_DrawText(200, 77, "the coin as close to the well as possible. Try");
  523. GFX_DrawText(200, 89, "and avoid any obstacles. Good luck! ");
  524. GFX_DrawText(357, 250, "HIGHSCORES:");
  525. get_highscores(360, 260);
  526.  
  527. GFX_UpdateDisplay();
  528. GFX_CreateEventQueue();
  529. GFX_InitMouse();
  530. GFX_RegisterMouseEvents();
  531.  
  532.  
  533. while(check == 1)
  534. {
  535. GFX_WaitForEvent();
  536. if (GFX_IsEventMouseButton())
  537. {
  538. GFX_GetMouseCoordinates(&mouseX, &mouseY);
  539.  
  540. if ((200 < mouseX && mouseX < 600) && (120 < mouseY && mouseY < 170))
  541. {
  542. check = 0;
  543. GFX_ClearWindow();
  544. initialise_window(initial_x_position, initial_y_position);
  545. game();
  546.  
  547. }
  548. else if ((200 < mouseX && mouseX < 600) && (190 < mouseY && mouseY < 240))
  549. {
  550. check = 0;
  551. GFX_CloseWindow();
  552. }
  553. }
  554. }
  555. }
  556. int main(void)
  557. {
  558.  
  559. GFX_InitWindow(800, 480);
  560.  
  561.  
  562. main_menu();
  563.  
  564. getchar();
  565. GFX_CloseWindow();
  566.  
  567. return 0;
  568. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement