Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. #include <curses.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define ESC 27// ASCII for escape
  6.  
  7. typedef struct {
  8.     int y,x,hp;
  9. }ent;
  10.  
  11. #define ENTS_ 12
  12. ent ent_l[ENTS_];
  13.  
  14. #define Y_ 10
  15. #define X_ 15
  16. ent *ent_m[Y_][X_];
  17.  
  18. char *map[]={
  19.     "###############",
  20.     "#      #      #",
  21.     "#             #",
  22.     "#      ### ####",
  23.     "#### ###   #  #",
  24.     "#          #  #",
  25.     "#          #  #",
  26.     "#             #",
  27.     "#          #  #",
  28.     "###############"
  29. };
  30.  
  31. void init_ent() {
  32.     for (int e=0;e<ENTS_;e++) {
  33.         ent *ce=&ent_l[e];
  34.         ce->hp=3;
  35.         do {
  36.             ce->y=rand()%Y_;
  37.             ce->x=rand()%X_;
  38.         } while ( '#'==map[ce->y][ce->x] || NULL!=ent_m[ce->y][ce->x] );
  39.         ent_m[ce->y][ce->x]=ce;
  40.     }
  41. }
  42.  
  43. //move player if there is no living entity on the way
  44. void move_to(int *y,int *x,int dy,int dx) {
  45.     //remove reference to the player's old position
  46.     ent_m[*y][*x]=NULL;
  47.    
  48.     //if the destination tile has an entity in it
  49.     if (NULL!=ent_m[*y+dy][*x+dx]) {
  50.         //decrement hitpoints of the entity at the destination tile
  51.         ent *de=ent_m[*y+dy][*x+dx];
  52.         de->hp--;
  53.         //if it's still alive don't move into its place
  54.         if (0 < de->hp) {
  55.             dy=0;
  56.             dx=0;
  57.         //if it's dead remove its reference
  58.         } else {
  59.             ent_m[*y+dy][*x+dx]=NULL;
  60.         }
  61.     }
  62.     //update player's position
  63.     *y+=dy;
  64.     *x+=dx;
  65.  
  66.     //add reference to the player's new position
  67.     ent_m[*y][*x]=&ent_l[0];
  68. }
  69.  
  70. int main() {
  71.     //initialize curses
  72.     keypad(initscr(),1);
  73.     curs_set(0);
  74.  
  75.     //initialize entities
  76.     srand(time(0));
  77.     init_ent();
  78.  
  79.     //player's starting coordinates
  80.     int *y=&ent_l[0].y;
  81.     int *x=&ent_l[0].x;
  82.  
  83.     //last key pressed
  84.     int c=0;
  85.     do {
  86.         //draw map
  87.         for (int yy=0;yy<Y_;yy++)
  88.             for (int xx=0;xx<X_;xx++)
  89.                 mvaddch(yy,xx,map[yy][xx]);
  90.  
  91.         //move player if there is no wall on the way
  92.         if (KEY_UP==c && ' '==map[*y-1][*x])
  93.             move_to(y,x,-1,0);
  94.         if (KEY_DOWN==c && ' '==map[*y+1][*x])
  95.             move_to(y,x,1,0);
  96.         if (KEY_LEFT==c && ' '==map[*y][*x-1])
  97.             move_to(y,x,0,-1);
  98.         if (KEY_RIGHT==c && ' '==map[*y][*x+1])
  99.             move_to(y,x,0,1);
  100.  
  101.         //draw entities
  102.         for (int e=0;e<ENTS_;e++) {
  103.             if (ent_l[e].hp>0)
  104.                 mvaddch(ent_l[e].y,ent_l[e].x, 0==e?'@':'m');
  105.         }
  106.     //quit when ESC is pressed
  107.     } while ((ESC!=(c=getch())));
  108.  
  109.     //clean up after we've finished using curses
  110.     endwin();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement