Advertisement
Guest User

rogue.c

a guest
Oct 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. #include <conio.h>
  6. #define CLS SetConsoleCursorPosition(handle, (COORD){0, 0})
  7.  
  8. #define W (40)
  9. #define H (20)
  10. #define INB(v, a, b) (v >= a && v <= b)
  11.  
  12. static char buff[W * H];
  13.  
  14. static void setchar(int x, int y, char c)
  15. {
  16.     if (INB(x, 0, W - 1) && INB(y, 0, H - 1))
  17.         buff[x + y * W] = c;
  18. }
  19.  
  20. static void render(void)
  21. {
  22.     for (int i = 0; i < W * H; ++i)
  23.     {
  24.         if (i % W == 0)
  25.             putchar('\n'); 
  26.         putchar(buff[i]);
  27.     }
  28.     putchar('\n');
  29. }
  30.  
  31. int main(void)
  32. {
  33.     system("cls");
  34.     SetConsoleTitle("Roguelike");
  35.     ShowCursor(FALSE);
  36.    
  37.     HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
  38.     char running = 1;
  39.     int x = W / 2, y = H / 2;
  40.     int cmd;
  41.  
  42.     while (running)
  43.     {
  44.         // render
  45.         CLS;
  46.         memset(buff, '_', sizeof buff);
  47.         setchar(x, y, '@');
  48.         render();  
  49.        
  50.         // update
  51.         switch ((cmd = getch()))
  52.         {
  53.             case 'a': x--; break;
  54.             case 'd': x++; break;
  55.             case 'w': y--; break;
  56.             case 's': y++; break;
  57.             case 27: running = 0; break;
  58.             default: break;
  59.         }
  60.  
  61.     }
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement