Advertisement
Nuppiz

Maze.c (DOS)

Sep 26th, 2021 (edited)
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <dos.h>
  5. #include <conio.h>
  6. #include <time.h>
  7.  
  8. #define VIDEO_INT           0x10      /* the BIOS video interrupt. */
  9. #define WRITE_DOT           0x0C      /* BIOS func to plot a pixel. */
  10. #define SET_MODE            0x00      /* BIOS func to set the video mode. */
  11. #define EGA_16_COLOR_MODE   0xD
  12. #define VGA_256_COLOR_MODE  0x13      /* use to set 256-color mode. */
  13. #define TEXT_MODE           0x03      /* use to set 80x25 text mode. */
  14.  
  15. #define SCREEN_WIDTH        320       /* width in pixels */
  16. #define SCREEN_HEIGHT       200       /* height in pixels */
  17. #define NUM_COLORS          16        /* number of colors in EGA */
  18.  
  19. #define TILE_WIDTH          8
  20. #define TILE_HEIGHT         8
  21. #define TILE_AREA           TILE_WIDTH*TILE_HEIGHT
  22.  
  23. uint8_t far *EGA=(uint8_t *)0xA0000000L;        /* this points to video memory. */
  24. uint8_t far *VGA=(uint8_t *)0xA0000000L;        /* this points to video memory. */
  25.  
  26. uint8_t width = 10;
  27. uint8_t height = 10;
  28.  
  29. uint8_t game_running = 1;
  30.  
  31. uint8_t level01[] =
  32.     "WWWWWWWWWW"
  33.     "W*W-^-W--W"
  34.     "W-W---W--W"
  35.     "W-WWW----W"
  36.     "W------WWW"
  37.     "W^W-W^^-^W"
  38.     "W------WWW"
  39.     "W-WWW--|-W"
  40.     "W--^---WeW"
  41.     "WWWWWWWWWW";
  42.  
  43. int player_y = 6;
  44. int player_x = 5;
  45.  
  46. /*int enemy_y = 4;
  47. int enemy_x = 4;
  48. int direction = 2;*/
  49.  
  50. uint8_t brick_wall[TILE_AREA];
  51. uint8_t player_sprite[TILE_AREA];
  52. uint8_t guard_sprite[TILE_AREA];
  53. uint8_t key_sprite[TILE_AREA];
  54. uint8_t floor_sprite[TILE_AREA];
  55. uint8_t door_c_sprite[TILE_AREA];
  56. uint8_t door_o_sprite[TILE_AREA];
  57. uint8_t exit_sprite[TILE_AREA];
  58. uint8_t mine_sprite[TILE_AREA];
  59. uint8_t expl_sprite[TILE_AREA];
  60. uint8_t grave_sprite[TILE_AREA];
  61.  
  62. struct Enemy
  63. {
  64.   uint8_t x;
  65.   uint8_t y;
  66.   uint8_t direction;
  67. };
  68.  
  69. void ticker(float seconds)
  70. {
  71.     int milliseconds = 1000 * seconds;
  72.     time_t startTime = clock();
  73.     while(clock() < startTime + milliseconds);
  74. }
  75.  
  76. void set_mode(uint8_t mode)
  77. {
  78.     union REGS regs;
  79.  
  80.     regs.h.ah = SET_MODE;
  81.     regs.h.al = mode;
  82.     int86(VIDEO_INT, &regs, &regs);
  83. }
  84.  
  85. /*int end_game(int ending)
  86. {
  87.     int response;
  88.    
  89.     if (ending == 1)
  90.     {
  91.         printf("You stepped on a landmine and blew up! Enter Y for a new game, any other key to quit:\n");
  92.     }
  93.     else if (ending == 2)
  94.     {
  95.         printf("You were caught by a guard! Enter Y for a new game, any other key to quit:\n");
  96.     }
  97.     else if (ending == 3)
  98.     {
  99.         printf("You managed to escape! Enter Y for a new game, any other key to quit:\n");
  100.     }
  101.        
  102.     scanf("%d", &response);
  103.     if (response == 89 || response == 121)
  104.     {
  105.         return 1;
  106.     }
  107.     else
  108.         return 0;
  109. }*/
  110.  
  111. void draw_sprite(int x, int y, uint8_t* sprite)
  112. {
  113.     uint8_t index_x = 0;
  114.     uint8_t index_y = 0;
  115.     uint8_t i = 0;
  116.  
  117.     for (index_y=0;index_y<TILE_HEIGHT;index_y++)
  118.     {
  119.         for (index_x=0;index_x<TILE_WIDTH;index_x++)
  120.         {
  121.             VGA[y*SCREEN_WIDTH+x]=sprite[i];
  122.             i++;
  123.             x++;
  124.         }
  125.         index_x = 0;
  126.         x = x - TILE_WIDTH;
  127.         y++;
  128.     }
  129.     index_y = 0;
  130. }
  131.  
  132. void render_maze()
  133. {  
  134.     uint16_t counter = 0;
  135.     uint8_t y = 0;
  136.     uint8_t x = 0;
  137.    
  138.     while (y < height)
  139.     {
  140.         while (x < width)
  141.         {
  142.             if (level01[counter] == 87)
  143.                 draw_sprite(x * TILE_WIDTH,y * TILE_HEIGHT,brick_wall);
  144.             if (level01[counter] == 45)
  145.                 draw_sprite(x * TILE_WIDTH,y * TILE_HEIGHT,floor_sprite);
  146.             if (level01[counter] == 94)
  147.                 draw_sprite(x * TILE_WIDTH,y * TILE_HEIGHT,mine_sprite);
  148.             if (level01[counter] == 42)
  149.                 draw_sprite(x * TILE_WIDTH,y * TILE_HEIGHT,key_sprite);
  150.             if (level01[counter] == 124)
  151.                 draw_sprite(x * TILE_WIDTH,y * TILE_HEIGHT,door_c_sprite);
  152.             if (level01[counter] == 250)
  153.                 draw_sprite(x * TILE_WIDTH,y * TILE_HEIGHT,door_o_sprite);
  154.             if (level01[counter] == 101)
  155.                 draw_sprite(x * TILE_WIDTH,y * TILE_HEIGHT,exit_sprite);
  156.                
  157.             counter = counter + 1;
  158.             x = x + 1;
  159.         }
  160.         y = y + 1;
  161.         x = 0;
  162.     }
  163. }
  164.  
  165. void save_sprite(char* filename, uint8_t* source_data, uint16_t data_size)
  166. {
  167.     FILE* file_ptr;
  168.     file_ptr = fopen(filename, "wb+");
  169.     fwrite(source_data, 1, data_size, file_ptr);
  170.     fclose(file_ptr);
  171. }
  172.  
  173. void load_sprite(char* filename, uint8_t* source_data, uint16_t data_size)
  174. {
  175.     FILE* file_ptr;
  176.     file_ptr = fopen(filename, "rb");
  177.     fread(source_data, 1, data_size, file_ptr);
  178.     fclose(file_ptr);
  179. }
  180.  
  181. void load_graphics()
  182. {
  183.     load_sprite("FLOOR.TEX", floor_sprite, 64);
  184.     load_sprite("BRICKS.TEX", brick_wall, 64);
  185.     load_sprite("PLAYER.TEX", player_sprite, 64);
  186.     load_sprite("GUARD.TEX", guard_sprite, 64);
  187.     load_sprite("KEY.TEX", key_sprite, 64);
  188.     load_sprite("DOORC.TEX", door_c_sprite, 64);
  189.     load_sprite("DOORO.TEX", door_o_sprite, 64);
  190.     load_sprite("EXIT.TEX", exit_sprite, 64);
  191.     load_sprite("MINE.TEX", mine_sprite, 64);
  192.     load_sprite("EXPLO.TEX", expl_sprite, 64);
  193.     load_sprite("GRAVE.TEX", grave_sprite, 64);
  194. }
  195.  
  196. void enemy_movement(struct Enemy* p_enemy)
  197. {
  198.    
  199.     uint8_t en_new_y = p_enemy->y;
  200.     uint8_t en_new_x = p_enemy->x;
  201.  
  202.     if (p_enemy->direction == 1) /*up*/
  203.         p_enemy->y -= 1;
  204.     else if (p_enemy->direction == 2) /*left*/
  205.         p_enemy->x -= 1;
  206.     else if (p_enemy->direction == 3) /*down*/
  207.         p_enemy->y ++;
  208.     else if (p_enemy->direction == 4) /*right*/
  209.         p_enemy->x ++;
  210.  
  211.         if (level01[en_new_y * width + en_new_x] == 'W')
  212.             {
  213.                 if (p_enemy->direction == 1) /*up*/
  214.                 {
  215.                     p_enemy->direction = 3;
  216.                     en_new_y = p_enemy->y + 1;
  217.                 }
  218.                 else if (p_enemy->direction == 2) /*left*/
  219.                 {
  220.                     p_enemy->direction = 4;
  221.                     en_new_x = p_enemy->x + 1;
  222.                 }
  223.                 else if (p_enemy->direction == 3) /*down*/
  224.                 {
  225.                     p_enemy->direction = 1;
  226.                     en_new_y = p_enemy->y - 1;
  227.                 }
  228.                 else if (p_enemy->direction == 4) /*right*/
  229.                 {
  230.                     p_enemy->direction = 2;
  231.                     en_new_x = p_enemy->x - 1;
  232.                 }
  233.             }
  234.        
  235.     p_enemy->y = en_new_y;
  236.     p_enemy->x = en_new_x;
  237.     draw_sprite(p_enemy->x * TILE_WIDTH,p_enemy->y * TILE_HEIGHT,guard_sprite);
  238.    
  239.     if (player_y == p_enemy->y && player_x == p_enemy->x)
  240.     {
  241.         draw_sprite(player_x * TILE_WIDTH,player_y * TILE_HEIGHT,grave_sprite);
  242.         getch();
  243.         game_running = 0;
  244.         //new_game = end_game(2);
  245.     }
  246. }
  247.  
  248. void draw_enemy(struct Enemy* p_enemy)
  249. {
  250.     draw_sprite(p_enemy->x * TILE_WIDTH,p_enemy->y * TILE_HEIGHT,guard_sprite);
  251. }
  252.  
  253. void main()
  254. {
  255.     uint8_t movement;
  256.     uint8_t key_acquired = 0;
  257.     struct Enemy Guard1;
  258.     struct Enemy Guard2;
  259.    
  260.     uint8_t new_x = player_x;
  261.     uint8_t new_y = player_y;
  262.     uint8_t new_game;
  263.    
  264.     set_mode(VGA_256_COLOR_MODE);       /* set the video mode. */
  265.    
  266.     load_graphics();
  267.    
  268.     render_maze();
  269.    
  270.     while(game_running == 1)
  271.     {      
  272.         Guard1.x = 4;
  273.         Guard1.y = 4;
  274.         Guard1.direction = 2;
  275.        
  276.         Guard2.x = 7;
  277.         Guard2.y = 2;
  278.         Guard2.direction = 1;
  279.        
  280.         draw_enemy(&Guard1);
  281.         draw_enemy(&Guard2);
  282.        
  283.         if (kbhit())
  284.         {
  285.             movement = getchar();
  286.  
  287.             if (movement == 119) /*w*/
  288.                 new_y = player_y - 1;
  289.             else if (movement == 97) /*a*/
  290.                 new_x = player_x - 1;
  291.             else if (movement == 115) /*s*/
  292.                 new_y = player_y + 1;
  293.             else if (movement == 100) /*d*/
  294.                 new_x = player_x + 1;
  295.             else if (movement == 27) /*ESC*/
  296.                 game_running = 0;
  297.         }
  298.         if (level01[new_y * width + new_x] != 'W')
  299.         {
  300.             if (level01[new_y * width + new_x] == 124 && key_acquired == 1)
  301.             {
  302.                 level01[new_y * width + new_x] = 250;
  303.                 player_y = new_y;
  304.                 player_x = new_x;
  305.                 render_maze();
  306.                 draw_sprite(player_x * TILE_WIDTH,player_y * TILE_HEIGHT,player_sprite);
  307.             }
  308.             else
  309.             {
  310.                 player_y = new_y;
  311.                 player_x = new_x;
  312.                 render_maze();
  313.                 draw_sprite(player_x * TILE_WIDTH,player_y * TILE_HEIGHT,player_sprite);
  314.            
  315.                 if (level01[player_y * width + player_x] == 42)
  316.                 {
  317.                     key_acquired = 1;
  318.                     level01[player_y * width + player_x] = 45;
  319.                 }
  320.                 else if (level01[player_y * width + player_x] == 94)
  321.                 {
  322.                     draw_sprite(player_x * TILE_WIDTH,player_y * TILE_HEIGHT,expl_sprite);
  323.                     draw_enemy(&Guard1);
  324.                     draw_enemy(&Guard2);
  325.                     getch();
  326.                     game_running = 0;
  327.                     //new_game = end_game(1);
  328.                 }
  329.                 else if (level01[player_y * width + player_x] == 101)
  330.                 {
  331.                     draw_enemy(&Guard1);
  332.                     draw_enemy(&Guard2);
  333.                     getch();   
  334.                     game_running = 0;
  335.                     //new_game = end_game(3);
  336.                 }
  337.                 if (new_game == 0)
  338.                     game_running = 0;
  339.             }
  340.         }
  341.         if (game_running == 1)
  342.         {
  343.             enemy_movement(&Guard1);
  344.             enemy_movement(&Guard2);   
  345.         }
  346.         ticker(0.01);
  347.     }
  348.     set_mode(TEXT_MODE);
  349.     return;
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement