Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include "cab202_graphics.h"
  7. #include "cab202_timers.h"
  8. #include "cab202_sprites.h"
  9.  
  10.  
  11.  
  12. sprite_id alien[10];
  13. sprite_id player;
  14. timer_id tim;
  15. timer_id drop;
  16. sprite_id bullet;
  17. sprite_id bomb[4];
  18. int spot;
  19. int life = 3;
  20. int SCORE = 0;
  21. int quit = 0;
  22. bool shoot = false;
  23. int count = 0;
  24. void info() {
  25. int x = screen_width()/2;
  26. draw_string(x - 5, screen_height() -1, "Level: 1" );
  27. draw_string(0, screen_height()-2, "Georgina Hine (n8872597)");
  28. draw_string(screen_width() - 11, screen_height() - 2, "Lives:");
  29. draw_string(screen_width()-22, screen_height() - 2, "Score:");
  30. draw_line(0, screen_height() - 3, screen_width(), screen_height() - 3, '-');
  31. draw_int(screen_width() - 4, screen_height() - 2, life);
  32. draw_int(screen_width() - 15, screen_height() - 2, SCORE);
  33.  
  34.  
  35. show_screen();
  36. }
  37.  
  38. void setup() {
  39. setup_screen();
  40. player = create_sprite(screen_width()/2, screen_height() - 5, 1, 1, "S");
  41. player->dy = 0;
  42. player->dx = 0;
  43.  
  44. bullet = create_sprite(-1, -1, 1, 1, "*");
  45. bullet->dy = 0;
  46. bullet->dx = 0;
  47.  
  48. for (int b = 0; b< 4; b++){
  49. bomb[b] = create_sprite(8, 9, 1, 1, "&");
  50. bomb[b]->dx = 0;
  51. bomb[b]->dy = 0;
  52. bomb[b]->is_visible = false;
  53.  
  54. }
  55.  
  56. for (int i = 0; i < 3; i++) {
  57. alien[i] = create_sprite(4+4*i, 0, 1, 1, "@");
  58.  
  59. }
  60. for (int j = 0; j < 4; j++) {
  61. alien[j+3] = create_sprite(2+4*j, 3, 1, 1, "@");
  62.  
  63. }
  64. for (int k = 0; k < 3; k++) {
  65. alien[k+7] = create_sprite(4+4*k, 6, 1, 1, "@");
  66.  
  67. }
  68. for (int r = 0; r < 10; r++) {
  69. alien[r]->dx = 0.1;
  70. alien[r]->dy = 0;
  71.  
  72. }
  73.  
  74.  
  75. tim = create_timer(25);
  76. drop = create_timer(3000);
  77. srand(time(NULL));
  78. }
  79.  
  80. void draws() {
  81.  
  82. clear_screen();
  83. draw_sprite(bullet);
  84. draw_sprite(player);
  85. for (int i = 0; i < 10; i++) {
  86. draw_sprite(alien[i]);
  87.  
  88. }
  89. for(int j = 0; j < 4; j++) {
  90. draw_sprite(bomb[j]);
  91. }
  92. show_screen();
  93. }
  94.  
  95. void check_input() {
  96. int key = get_char();
  97.  
  98.  
  99.  
  100. if (key == 'a'){
  101. player->dx = -1;
  102.  
  103. }
  104. else if (key == 'd'){
  105. player->dx = 1;
  106.  
  107. }
  108. else if (key == 'q'){
  109. quit = 1;
  110. }
  111. else if (key == 's'){
  112. if(shoot == false) {
  113. bullet->y = screen_height() - 5;
  114. bullet->dy = -1;
  115. spot = player->x;
  116. shoot = true;
  117. }
  118. }
  119. else {
  120. player->dx = 0;
  121. player->dy = 0;
  122.  
  123. }
  124.  
  125.  
  126.  
  127. while (get_char() != -1) {
  128.  
  129. }
  130.  
  131.  
  132. }
  133.  
  134. void update_state() {
  135.  
  136. //Check if we've hit the boundaries
  137. if (player->x + player->dx < 0 || player->x + player->dx >= screen_width()){
  138. player->dx = 0;
  139. player->dy = 0;
  140. }
  141.  
  142. //Move player
  143. player->x += player->dx;
  144. player->y +=player->dy;
  145.  
  146. //Reset move vector
  147. player->dx = 0;
  148. player->dy = 0;
  149.  
  150.  
  151. if (shoot == true) {
  152. bullet->y += bullet->dy;
  153. bullet->x = spot;
  154.  
  155. }
  156. else {
  157. bullet->dy = 0;
  158. bullet->x = -1;
  159. bullet->y = -1;
  160. }
  161.  
  162. if (bullet->y < 0) {
  163. shoot = false;
  164. }
  165.  
  166. for (int i = 0; i < 10; i++) {
  167. if (alien[i]->x + alien[i]->dx > screen_width() - 1) {
  168. alien[i]->x = -1;
  169. }
  170. }
  171.  
  172. for (int r = 0; r < 10; r++) {
  173. alien[r]->x += alien[r]->dx ;
  174. alien[r]->y += alien[r]->dy;
  175. }
  176.  
  177. for (int l = 0; l<10; l++) {
  178. if (round(bullet->x) == round(alien[l]->x) && round(bullet->y) + round(bullet->dy) == round(alien[l]->y)) {
  179. alien[l]->x = -1;
  180. alien[l]->y = -3;
  181. alien[l]->dy= 0;
  182. alien[l]->dx = 0;
  183. SCORE = SCORE + 30;
  184. shoot = false;
  185. count = count + 1;
  186. }
  187. }
  188.  
  189. for(int z = 0; z < 4; z++) {
  190. if(round(bomb[z]->x) == round(player->x) && round(bomb[z]->y) + round(bomb[z]->dy) == round(player->y)) {
  191. life = life - 1;
  192. bomb[z]->is_visible = false;
  193. }
  194. }
  195.  
  196.  
  197.  
  198. for(int f = 0; f < 4; f++) {
  199. if(bomb[f]->x == player->x + player->dx && bomb[f]->y == player->dy + player->y) {
  200. bomb[f]->is_visible = false;
  201. }
  202. }
  203.  
  204. for(int z = 0; z < 4; z++) {
  205. if(bomb[z]->y > screen_height() - 4) {
  206. bomb[z]->is_visible = false;
  207. }
  208. }
  209.  
  210. if (timer_expired(drop)) { //make the random number smaller as the number of aliens decreases
  211. int c = rand() %10;
  212. int h = rand() %4;
  213. if (bomb[h]->is_visible == false) {
  214. bomb[h]->dy = 0.3;
  215. bomb[h]->x = alien[c]->x + bomb[h]->dx;
  216. bomb[h]->y = alien[c]->y + bomb[h]->dy;
  217. bomb[h]->is_visible = true;
  218.  
  219. }
  220.  
  221. reset_timer(drop);
  222. }
  223. for(int z = 0; z < 4; z++) {
  224. if(bomb[z]->is_visible == true) {
  225. bomb[z]->y += bomb[z]->dy;
  226. }
  227.  
  228. else {
  229. bomb[z]->dy = 0;
  230. bomb[z]->x = -3;
  231. bomb[z]->y = -4;
  232. }
  233.  
  234.  
  235. }
  236. int no = 10;
  237.  
  238. if(count == no) {
  239.  
  240. for (int r = 0; r < 10; r++) {
  241. alien[r]->dx = 0.1;
  242. alien[r]->dy = 0;
  243. }
  244.  
  245. for (int i = 0; i < 3; i++) {
  246. alien[i]->x = 4+4*1;
  247. alien[i]->y = 0; //= create_sprite(4+4*i, 0, 1, 1, "@");
  248. }
  249. for (int j = 0; j < 4; j++) {
  250. alien[j+3]->x = 2+4*j;
  251. alien[j+3]->y = 3; //= create_sprite(2+4*j, 3, 1, 1, "@");
  252. }
  253. for (int k = 0; k < 3; k++) {
  254. alien[k+7]->x = 4+4*k;
  255. alien[k+7]->y = 6; //= create_sprite(4+4*k, 6, 1, 1, "@");
  256. }
  257.  
  258. no = no + 10;
  259. }
  260.  
  261.  
  262.  
  263.  
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement