Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <inttypes.h>
- #include <conio.h>
- #include <time.h>
- #include <windows.h>
- uint8_t width = 10;
- uint8_t height = 10;
- uint8_t funny_bytes[] =
- "WWWWWWWWWW"
- "W*W-^-W--W"
- "W-W---W--W"
- "W-WWW----W"
- "W------WWW"
- "W^W-W^^-^W"
- "W------WWW"
- "W-WWW--|-W"
- "W--^---WeW"
- "WWWWWWWWWW";
- int player_y = 6;
- int player_x = 5;
- int enemy_y = 4;
- int enemy_x = 4;
- int direction = 2;
- uint8_t pl_symbol = 2;
- uint8_t en_symbol = 19;
- int gotoxy(int x,int y)
- {
- COORD coord = {x,y};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
- }
- void delay(float seconds)
- {
- int milliseconds = 1000 * seconds;
- time_t startTime = clock();
- while(clock() < startTime + milliseconds);
- }
- void clear(int clear_width, int clear_height)
- {
- int y = 0;
- int x = 0;
- while (y < clear_height)
- {
- gotoxy(0, y);
- while (x < clear_width)
- {
- putchar(' ');
- x++;
- }
- x = 0;
- y++;
- }
- }
- void print_maze()
- {
- gotoxy(0,0);
- uint16_t counter = 0;
- uint8_t y = 0;
- uint8_t x = 0;
- while (y < height)
- {
- while (x < width)
- {
- printf("%c", funny_bytes[counter]);
- counter = counter + 1;
- x = x + 1;
- }
- printf("\n");
- y = y + 1;
- x = 0;
- }
- gotoxy(player_x,player_y);
- printf ("%c", pl_symbol);
- gotoxy(enemy_x,enemy_y);
- printf ("%c", en_symbol);
- printf("\n");
- }
- void enemy_movement()
- {
- int en_new_y = enemy_y;
- int en_new_x = enemy_x;
- int new_direction = direction;
- if (direction == 1) /*up*/
- en_new_y = enemy_y - 1;
- else if (direction == 2) /*left*/
- en_new_x = enemy_x - 1;
- else if (direction == 3) /*down*/
- en_new_y = enemy_y + 1;
- else if (direction == 4) /*right*/
- en_new_x = enemy_x + 1;
- if (funny_bytes[en_new_y * width + en_new_x] == 87)
- {
- if (direction == 1) /*up*/
- {
- new_direction = 3;
- en_new_y = enemy_y + 1;
- }
- else if (direction == 2) /*left*/
- {
- new_direction = 4;
- en_new_x = enemy_x + 1;
- }
- else if (direction == 3) /*down*/
- {
- new_direction = 1;
- en_new_y = enemy_y - 1;
- }
- else if (direction == 4) /*right*/
- {
- new_direction = 2;
- en_new_x = enemy_x - 1;
- }
- }
- enemy_y = en_new_y;
- enemy_x = en_new_x;
- direction = new_direction;
- }
- int end_game(int ending)
- {
- int response;
- if (ending == 1)
- {
- pl_symbol = 15;
- print_maze();
- gotoxy(0,11);
- printf("You stepped on a landmine and blew up! Enter Y for a new game, any other key to quit:\n");
- }
- else if (ending == 2)
- {
- pl_symbol = 5;
- print_maze();
- gotoxy(enemy_x,enemy_y);
- printf ("%c", pl_symbol);
- gotoxy(0,11);
- printf("You were caught by a guard! Enter Y for a new game, any other key to quit:\n");
- }
- else if (ending == 3)
- {
- pl_symbol = 1;
- print_maze();
- gotoxy(0,11);
- printf("You managed to escape! Enter Y for a new game, any other key to quit:\n");
- }
- scanf("%d", &response);
- if (response == 89 || response == 121)
- {
- return 1;
- }
- else
- return 0;
- }
- int main()
- {
- int game_running = 1;
- uint8_t movement;
- uint8_t key_acquired = 0;
- clock_t start = clock();
- clear(80,25);
- print_maze();
- while(game_running == 1)
- {
- int new_x = player_x;
- int new_y = player_y;
- int new_game;
- if (kbhit())
- {
- movement = _getch();
- if (movement == 119) /*w*/
- new_y = player_y - 1;
- else if (movement == 97) /*a*/
- new_x = player_x - 1;
- else if (movement == 115) /*s*/
- new_y = player_y + 1;
- else if (movement == 100) /*d*/
- new_x = player_x + 1;
- else if (movement == 27) /*ESC*/
- game_running = 0;
- }
- if (funny_bytes[new_y * width + new_x] != 87)
- {
- if (funny_bytes[new_y * width + new_x] == 124 && key_acquired == 0)
- {
- gotoxy(0,11);
- printf("You're missing the key for this door.\n");
- }
- else if (funny_bytes[new_y * width + new_x] == 124 && key_acquired == 1)
- {
- gotoxy(0,11);
- printf("Opened door with the key.\n");
- funny_bytes[new_y * width + new_x] = 95;
- player_y = new_y;
- player_x = new_x;
- print_maze();
- }
- else
- {
- player_y = new_y;
- player_x = new_x;
- print_maze();
- if (funny_bytes[player_y * width + player_x] == 42)
- {
- key_acquired = 1;
- funny_bytes[player_y * width + player_x] = 45;
- gotoxy(0,11);
- printf(" ");
- gotoxy(0,11);
- printf("Key acquired.\n");
- }
- else if (funny_bytes[player_y * width + player_x] == 94)
- {
- new_game = end_game(1);
- }
- else if (player_y == enemy_y && player_x == enemy_x)
- {
- new_game = end_game(2);
- }
- else if (funny_bytes[player_y * width + player_x] == 101)
- {
- new_game = end_game(3);
- }
- if (new_game == 0)
- game_running = 0;
- }
- enemy_movement();
- }
- delay(0.1);
- clock_t game_time = (clock() - start) / 1000;
- gotoxy(0,10);
- printf("Time wasted: %d", game_time);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement