Advertisement
Nuppiz

Box.c

Sep 18th, 2021 (edited)
1,483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <inttypes.h>
  3. #include <conio.h>
  4. #include <time.h>
  5. #include <windows.h>
  6.  
  7. uint8_t width = 10;
  8. uint8_t height = 10;
  9.  
  10. uint8_t funny_bytes[] =
  11.     "WWWWWWWWWW"
  12.     "W*W-^-W--W"
  13.     "W-W---W--W"
  14.     "W-WWW----W"
  15.     "W------WWW"
  16.     "W^W-W^^-^W"
  17.     "W------WWW"
  18.     "W-WWW--|-W"
  19.     "W--^---WeW"
  20.     "WWWWWWWWWW";
  21.  
  22. int player_y = 6;
  23. int player_x = 5;
  24.  
  25. int enemy_y = 4;
  26. int enemy_x = 4;
  27. int direction = 2;
  28.  
  29. uint8_t pl_symbol = 2;
  30. uint8_t en_symbol = 19;
  31.  
  32. int gotoxy(int x,int y)
  33. {
  34.     COORD coord = {x,y};
  35.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
  36. }
  37.  
  38. void delay(float seconds)
  39. {
  40.     int milliseconds = 1000 * seconds;
  41.     time_t startTime = clock();
  42.     while(clock() < startTime + milliseconds);
  43. }
  44.  
  45. void clear(int clear_width, int clear_height)
  46. {
  47.     int y = 0;
  48.     int x = 0;
  49.  
  50.     while (y < clear_height)
  51.     {
  52.         gotoxy(0, y);
  53.         while (x < clear_width)
  54.         {
  55.             putchar(' ');
  56.             x++;
  57.         }
  58.         x = 0;
  59.         y++;
  60.     }
  61. }
  62.  
  63. void print_maze()
  64. {  
  65.     gotoxy(0,0);
  66.     uint16_t counter = 0;
  67.     uint8_t y = 0;
  68.     uint8_t x = 0;
  69.    
  70.     while (y < height)
  71.     {
  72.         while (x < width)
  73.         {
  74.             printf("%c", funny_bytes[counter]);
  75.             counter = counter + 1;
  76.             x = x + 1;
  77.         }
  78.         printf("\n");
  79.         y = y + 1;
  80.         x = 0;
  81.     }
  82.     gotoxy(player_x,player_y);
  83.     printf ("%c", pl_symbol);
  84.     gotoxy(enemy_x,enemy_y);
  85.     printf ("%c", en_symbol);
  86.     printf("\n");
  87. }
  88.  
  89. void enemy_movement()
  90. {
  91.     int en_new_y = enemy_y;
  92.     int en_new_x = enemy_x;
  93.     int new_direction = direction;
  94.  
  95.     if (direction == 1) /*up*/
  96.         en_new_y = enemy_y - 1;
  97.     else if (direction == 2) /*left*/
  98.         en_new_x = enemy_x - 1;
  99.     else if (direction == 3) /*down*/
  100.         en_new_y = enemy_y + 1;
  101.     else if (direction == 4) /*right*/
  102.         en_new_x = enemy_x + 1;
  103.  
  104.         if (funny_bytes[en_new_y * width + en_new_x] == 87)
  105.             {
  106.                 if (direction == 1) /*up*/
  107.                 {
  108.                     new_direction = 3;
  109.                     en_new_y = enemy_y + 1;
  110.                 }
  111.                 else if (direction == 2) /*left*/
  112.                 {
  113.                     new_direction = 4;
  114.                     en_new_x = enemy_x + 1;
  115.                 }
  116.                 else if (direction == 3) /*down*/
  117.                 {
  118.                     new_direction = 1;
  119.                     en_new_y = enemy_y - 1;
  120.                 }
  121.                 else if (direction == 4) /*right*/
  122.                 {
  123.                     new_direction = 2;
  124.                     en_new_x = enemy_x - 1;
  125.                 }
  126.             }
  127.        
  128.     enemy_y = en_new_y;
  129.     enemy_x = en_new_x;
  130.     direction = new_direction;
  131. }
  132.  
  133. int end_game(int ending)
  134. {
  135.     int response;
  136.    
  137.     if (ending == 1)
  138.     {
  139.         pl_symbol = 15;
  140.         print_maze();
  141.         gotoxy(0,11);
  142.         printf("You stepped on a landmine and blew up! Enter Y for a new game, any other key to quit:\n");
  143.     }
  144.     else if (ending == 2)
  145.     {
  146.         pl_symbol = 5;
  147.         print_maze();
  148.         gotoxy(enemy_x,enemy_y);
  149.         printf ("%c", pl_symbol);
  150.         gotoxy(0,11);
  151.         printf("You were caught by a guard! Enter Y for a new game, any other key to quit:\n");
  152.     }
  153.     else if (ending == 3)
  154.     {
  155.         pl_symbol = 1;
  156.         print_maze();
  157.         gotoxy(0,11);
  158.         printf("You managed to escape! Enter Y for a new game, any other key to quit:\n");
  159.     }
  160.        
  161.     scanf("%d", &response);
  162.     if (response == 89 || response == 121)
  163.     {
  164.         return 1;
  165.     }
  166.     else
  167.         return 0;
  168. }
  169.    
  170. int main()
  171. {
  172.     int game_running = 1;
  173.    
  174.     uint8_t movement;
  175.     uint8_t key_acquired = 0;
  176.    
  177.     clock_t start = clock();
  178.    
  179.     clear(80,25);
  180.    
  181.     print_maze();
  182.    
  183.     while(game_running == 1)
  184.     {
  185.         int new_x = player_x;
  186.         int new_y = player_y;
  187.         int new_game;
  188.        
  189.         if (kbhit())
  190.         {
  191.             movement = _getch();
  192.  
  193.             if (movement == 119) /*w*/
  194.                 new_y = player_y - 1;
  195.             else if (movement == 97) /*a*/
  196.                 new_x = player_x - 1;
  197.             else if (movement == 115) /*s*/
  198.                 new_y = player_y + 1;
  199.             else if (movement == 100) /*d*/
  200.                 new_x = player_x + 1;
  201.             else if (movement == 27) /*ESC*/
  202.                 game_running = 0;
  203.         }
  204.         if (funny_bytes[new_y * width + new_x] != 87)
  205.         {
  206.             if (funny_bytes[new_y * width + new_x] == 124 && key_acquired == 0)
  207.             {
  208.                 gotoxy(0,11);
  209.                 printf("You're missing the key for this door.\n");
  210.             }
  211.             else if (funny_bytes[new_y * width + new_x] == 124 && key_acquired == 1)
  212.             {
  213.                 gotoxy(0,11);
  214.                 printf("Opened door with the key.\n");
  215.                 funny_bytes[new_y * width + new_x] = 95;
  216.                 player_y = new_y;
  217.                 player_x = new_x;
  218.                 print_maze();
  219.             }
  220.             else
  221.             {
  222.                 player_y = new_y;
  223.                 player_x = new_x;
  224.                 print_maze();
  225.            
  226.                 if (funny_bytes[player_y * width + player_x] == 42)
  227.                 {
  228.                     key_acquired = 1;
  229.                     funny_bytes[player_y * width + player_x] = 45;
  230.                     gotoxy(0,11);
  231.                     printf("                                                                                ");
  232.                     gotoxy(0,11);
  233.                     printf("Key acquired.\n");
  234.                 }
  235.                 else if (funny_bytes[player_y * width + player_x] == 94)
  236.                 {
  237.                     new_game = end_game(1);
  238.                 }
  239.                 else if (player_y == enemy_y && player_x == enemy_x)
  240.                 {
  241.                     new_game = end_game(2);
  242.                 }
  243.                 else if (funny_bytes[player_y * width + player_x] == 101)
  244.                 {
  245.                     new_game = end_game(3);
  246.                 }
  247.                 if (new_game == 0)
  248.                     game_running = 0;
  249.             }
  250.         enemy_movement();      
  251.         }
  252.        
  253.         delay(0.1);
  254.         clock_t game_time = (clock() - start) / 1000;
  255.         gotoxy(0,10);
  256.         printf("Time wasted: %d", game_time);
  257.     }
  258.    
  259.     return 0;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement