Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _POSIX_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <termios.h>
- #include <time.h>
- #define KEY_UP 65
- #define KEY_DOWN 66
- #define KEY_LEFT 68
- #define KEY_RIGHT 67
- #define KEY_SPACE 32
- #define MAX_BULLETS 20
- #define STEP_B 2500
- #define MAX_ENEMIES 204
- #define STEP_E 8000
- typedef struct
- {
- int x; int y;
- char image ;
- } Entity;
- void checkcollision();
- void shoot();
- void movebullets();
- void moveenemies();
- void moveentity(Entity* entity, int addx, int addy);
- void end();
- void draw();
- void drawentity(Entity* entity);
- void clrscr();
- void gotoxy(int x, int y);
- int getkey();
- Entity player;
- Entity* bullets[MAX_BULLETS];
- int bullet_count = 0;
- int step_b = 0;
- Entity* enemies[MAX_ENEMIES];
- int enemies_count = 0;
- int need_draw = 1;
- int step_e = 0;
- int direction = 1;
- int main()
- {
- int key;
- player.x = 40; player.y = 24; player.image = '#';
- int initial_x = 1;
- int initial_y = 1;
- for(int i = 0; i < MAX_ENEMIES; i++)
- {
- enemies[i] = malloc(sizeof(Entity));
- enemies[i]->x = initial_x % 40;
- enemies[i]->y = initial_y;
- enemies[i]->image = '@';
- enemies_count++;
- initial_x += 2;
- if (initial_x % 41 == 0) { initial_y++; }
- }
- while(1)
- {
- key = getkey();
- if (key == KEY_RIGHT) { moveentity(&player, 1, 0); } // derecha
- if (key == KEY_LEFT) { moveentity(&player, -1, 0); } // izquierda
- if (key == KEY_SPACE) { shoot(); }
- if (enemies_count <= 0) { end(); break; }
- moveenemies();
- movebullets();
- checkcollision();
- draw();
- }
- return 0;
- }
- void checkcollision()
- {
- for(int i = 0; i < MAX_ENEMIES; i++)
- {
- for(int j = 0; j < MAX_BULLETS; j++)
- {
- if (enemies[i] == NULL) { continue; }
- if (bullets[j] == NULL) { continue; }
- if (bullets[j]->y < 1) { bullets[j] = NULL; bullet_count--; continue; }
- if (bullets[j]->x != enemies[i]->x) { continue; }
- if (bullets[j]->y != enemies[i]->y) { continue; }
- enemies[i] = NULL; enemies_count--;
- bullets[j] = NULL; bullet_count--;
- }
- }
- }
- void shoot()
- {
- if (bullet_count >= MAX_BULLETS) { return; }
- bullet_count++;
- int i;
- for(int j = 0; j < bullet_count; j++) { if (bullets[j] == NULL) { i = j; } }
- bullets[i] = malloc(sizeof(Entity));
- bullets[i]->x = player.x;
- bullets[i]->y = player.y - 1;
- bullets[i]->image = '*';
- }
- void movebullets()
- {
- if (step_b < STEP_B) { step_b++; return; }
- step_b = 0;
- for(int j = 0; j < MAX_BULLETS; j++)
- {
- if (bullets[j] != NULL)
- {
- moveentity(bullets[j], 0, -1);
- }
- }
- }
- void moveenemies()
- {
- if (step_e < STEP_E) { step_e++; return; }
- step_e = 0;
- int move_down = 0;
- for(int i = 0; i < MAX_ENEMIES; i++)
- {
- if (enemies[i] == NULL) { continue; }
- if (direction == 1) { moveentity(enemies[i], 1, 0); } // derecha
- if (direction == 0) { moveentity(enemies[i], -1, 0); } // izquierda
- }
- for(int i = 0; i < MAX_ENEMIES; i++)
- {
- if (enemies[i] == NULL) { continue; }
- if (enemies[i]->x > 79) { direction = 0; move_down = 1; }
- if (enemies[i]->x < 1) { direction = 1; move_down = 1; }
- }
- if (move_down == 1)
- {
- for(int i = 0; i < MAX_ENEMIES; i++)
- {
- if (enemies[i] == NULL) { continue; }
- moveentity(enemies[i], 0, 1);
- }
- }
- }
- void moveentity(Entity* entity, int addx, int addy)
- {
- entity->x += addx;
- entity->y += addy;
- need_draw = 1;
- }
- void draw()
- {
- if (need_draw == 0) { return; }
- clrscr();
- drawentity(&player);
- for(int i = 0; i < MAX_ENEMIES; i++)
- {
- if (enemies[i] == NULL) { continue; }
- drawentity(enemies[i]);
- }
- for(int i = 0; i < MAX_BULLETS; i++)
- {
- if (bullets[i] == NULL) { continue; }
- drawentity(bullets[i]);
- }
- gotoxy(0, 0);
- need_draw = 0;
- }
- void end()
- {
- clrscr();
- gotoxy(34, 11);
- printf("ENHORABUENA");
- gotoxy(35, 12);
- printf("HAS GANADO");
- }
- void drawentity(Entity* entity)
- {
- gotoxy(entity->x, entity->y);
- printf("%c", entity->image);
- }
- void clrscr()
- {
- printf("\033[2J"); // Borra pantalla
- printf("\033[1;1H"); // Posicionamos el cursor en la fila 1 columna 1
- }
- void gotoxy(int x, int y)
- {
- printf("\033[%d;%dH", y, x); // Posicionamos el cursor en X, Y
- }
- int getkey()
- {
- int character;
- struct termios orig_term_attr;
- struct termios new_term_attr;
- /* set the terminal to raw mode */
- tcgetattr(fileno(stdin), &orig_term_attr);
- memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
- new_term_attr.c_lflag &= ~(ECHO|ICANON);
- new_term_attr.c_cc[VTIME] = 0;
- new_term_attr.c_cc[VMIN] = 0;
- tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
- /* read a character from the stdin stream without blocking */
- /* returns EOF (-1) if no character is available */
- character = fgetc(stdin);
- /* restore the original terminal attributes */
- tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
- return character;
- }
Add Comment
Please, Sign In to add comment