Guest User

Untitled

a guest
Apr 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.87 KB | None | 0 0
  1. #define _POSIX_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <termios.h>
  6. #include <time.h>
  7.  
  8. #define KEY_UP    65
  9. #define KEY_DOWN  66
  10. #define KEY_LEFT  68
  11. #define KEY_RIGHT 67
  12. #define KEY_SPACE 32
  13.  
  14. #define MAX_BULLETS 20
  15. #define STEP_B 2500
  16. #define MAX_ENEMIES 204
  17. #define STEP_E 8000
  18.  
  19. typedef struct
  20. {
  21.     int x; int y;
  22.     char  image ;
  23. } Entity;
  24.  
  25. void checkcollision();
  26. void shoot();
  27. void movebullets();
  28. void moveenemies();
  29. void moveentity(Entity* entity, int addx, int addy);
  30. void end();
  31. void draw();
  32. void drawentity(Entity* entity);
  33. void clrscr();
  34. void gotoxy(int x, int y);
  35. int  getkey();
  36.  
  37. Entity player;
  38. Entity* bullets[MAX_BULLETS];
  39. int bullet_count = 0;
  40. int step_b = 0;
  41. Entity* enemies[MAX_ENEMIES];
  42. int enemies_count = 0;
  43. int need_draw = 1;
  44. int step_e = 0;
  45. int direction = 1;
  46.  
  47. int main()
  48. {
  49.     int key;
  50.  
  51.     player.x = 40; player.y = 24; player.image = '#';
  52.  
  53.     int initial_x = 1;
  54.     int initial_y = 1;
  55.     for(int i = 0; i < MAX_ENEMIES; i++)
  56.     {
  57.         enemies[i] = malloc(sizeof(Entity));
  58.         enemies[i]->x = initial_x % 40;
  59.         enemies[i]->y = initial_y;
  60.         enemies[i]->image = '@';
  61.         enemies_count++;
  62.         initial_x += 2;
  63.         if (initial_x % 41 == 0) { initial_y++; }
  64.     }
  65.  
  66.     while(1)
  67.     {
  68.         key = getkey();
  69.         if (key == KEY_RIGHT) { moveentity(&player,  1,  0); } // derecha
  70.         if (key == KEY_LEFT)  { moveentity(&player,  -1, 0); } // izquierda
  71.         if (key == KEY_SPACE) { shoot(); }
  72.  
  73.         if (enemies_count <= 0) { end(); break; }
  74.         moveenemies();
  75.         movebullets();
  76.         checkcollision();
  77.         draw();
  78.     }
  79.  
  80.     return 0;
  81. }
  82.  
  83. void checkcollision()
  84. {
  85.     for(int i = 0; i < MAX_ENEMIES; i++)
  86.     {
  87.         for(int j = 0; j < MAX_BULLETS; j++)
  88.         {
  89.             if (enemies[i] == NULL) { continue; }
  90.             if (bullets[j] == NULL) { continue; }
  91.             if (bullets[j]->y < 1) { bullets[j] = NULL; bullet_count--; continue; }
  92.             if (bullets[j]->x != enemies[i]->x) { continue; }
  93.             if (bullets[j]->y != enemies[i]->y) { continue; }
  94.             enemies[i] = NULL; enemies_count--;
  95.             bullets[j] = NULL; bullet_count--;
  96.         }
  97.     }
  98. }
  99.  
  100. void shoot()
  101. {
  102.     if (bullet_count >= MAX_BULLETS) { return; }
  103.     bullet_count++;
  104.     int i;
  105.     for(int j = 0; j < bullet_count; j++) { if (bullets[j] == NULL) { i = j; } }
  106.     bullets[i] = malloc(sizeof(Entity));
  107.     bullets[i]->x = player.x;
  108.     bullets[i]->y = player.y - 1;
  109.     bullets[i]->image = '*';
  110. }
  111.  
  112. void movebullets()
  113. {
  114.     if (step_b < STEP_B) { step_b++; return; }
  115.     step_b = 0;
  116.     for(int j = 0; j < MAX_BULLETS; j++)
  117.     {
  118.         if (bullets[j] != NULL)
  119.         {
  120.             moveentity(bullets[j], 0, -1);
  121.         }
  122.     }
  123. }
  124. void moveenemies()
  125. {
  126.     if (step_e < STEP_E) { step_e++; return; }
  127.     step_e = 0;
  128.     int move_down = 0;
  129.     for(int i = 0; i < MAX_ENEMIES; i++)
  130.     {
  131.         if (enemies[i] == NULL) { continue; }
  132.         if (direction == 1) { moveentity(enemies[i], 1, 0);  } // derecha
  133.         if (direction == 0) { moveentity(enemies[i], -1, 0); } // izquierda
  134.     }
  135.     for(int i = 0; i < MAX_ENEMIES; i++)
  136.     {
  137.         if (enemies[i] == NULL) { continue; }
  138.         if (enemies[i]->x > 79) { direction = 0; move_down = 1; }
  139.         if (enemies[i]->x < 1)  { direction = 1; move_down = 1; }
  140.     }
  141.     if (move_down == 1)
  142.     {
  143.         for(int i = 0; i < MAX_ENEMIES; i++)
  144.         {      
  145.             if (enemies[i] == NULL) { continue; }
  146.             moveentity(enemies[i], 0, 1);
  147.         }
  148.     }
  149. }
  150.  
  151. void moveentity(Entity* entity, int addx, int addy)
  152. {
  153.     entity->x += addx;
  154.     entity->y += addy;
  155.     need_draw = 1;
  156. }
  157.  
  158. void draw()
  159. {
  160.     if (need_draw == 0) { return; }
  161.     clrscr();
  162.     drawentity(&player);
  163.     for(int i = 0; i < MAX_ENEMIES; i++)
  164.     {
  165.         if (enemies[i] == NULL) { continue; }
  166.         drawentity(enemies[i]);
  167.     }
  168.     for(int i = 0; i < MAX_BULLETS; i++)
  169.     {
  170.         if (bullets[i] == NULL) { continue; }
  171.         drawentity(bullets[i]);
  172.     }
  173.     gotoxy(0, 0);
  174.     need_draw = 0;
  175. }
  176.  
  177. void end()
  178. {
  179.     clrscr();
  180.     gotoxy(34, 11);
  181.     printf("ENHORABUENA");
  182.     gotoxy(35, 12);
  183.     printf("HAS GANADO");
  184. }
  185.  
  186. void drawentity(Entity* entity)
  187. {
  188.     gotoxy(entity->x, entity->y);
  189.     printf("%c", entity->image);
  190. }
  191.  
  192. void clrscr()
  193. {
  194.   printf("\033[2J");   // Borra pantalla
  195.   printf("\033[1;1H"); // Posicionamos el cursor en la fila 1 columna 1
  196. }
  197.  
  198. void gotoxy(int x, int y)
  199. {
  200.   printf("\033[%d;%dH", y, x);  // Posicionamos el cursor en X, Y
  201. }
  202.  
  203. int getkey()
  204. {
  205.     int character;
  206.     struct termios orig_term_attr;
  207.     struct termios new_term_attr;
  208.  
  209.     /* set the terminal to raw mode */
  210.     tcgetattr(fileno(stdin), &orig_term_attr);
  211.     memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
  212.     new_term_attr.c_lflag &= ~(ECHO|ICANON);
  213.     new_term_attr.c_cc[VTIME] = 0;
  214.     new_term_attr.c_cc[VMIN] = 0;
  215.     tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
  216.  
  217.     /* read a character from the stdin stream without blocking */
  218.     /*   returns EOF (-1) if no character is available */
  219.     character = fgetc(stdin);
  220.  
  221.     /* restore the original terminal attributes */
  222.     tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  223.  
  224.     return character;
  225. }
Add Comment
Please, Sign In to add comment