c语言坦克走迷宫小游戏
时间: 2023-12-05 22:40:53 浏览: 88
c语言走迷宫
3星 · 编辑精心推荐
以下是C语言键盘控制走迷宫小游戏的代码,供您参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define ROW 10
#define COL 10
int map[ROW][COL] = {
{0,0,0,0,0,0,0,0,0,0},
{0,1,1,0,1,1,1,0,1,0},
{0,1,1,0,1,1,1,0,1,0},
{0,1,1,1,1,0,0,1,1,0},
{0,1,0,0,0,1,1,1,1,0},
{0,1,1,1,0,1,1,1,1,0},
{0,1,0,1,1,1,0,1,1,0},
{0,1,0,0,0,1,0,0,1,0},
{0,0,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0}
};
void draw_map();
void gotoxy(int x, int y);
void move(int *x, int *y);
int main()
{
int x = 1, y = 1;
draw_map();
while (map[8][8] != 2) {
move(&x, &y);
draw_map();
}
printf("\nCongratulations! You win!\n");
return 0;
}
void draw_map()
{
system("cls");
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (map[i][j] == 0) {
printf("#");
} else if (map[i][j] == 1) {
printf(" ");
} else if (map[i][j] == 2) {
printf("@");
}
}
printf("\n");
}
}
void gotoxy(int x, int y)
{
COORD pos = {x, y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void move(int *x, int *y)
{
char ch = getch();
switch (ch) {
case 'w':
if (map[*y-1][*x] != 0) {
map[*y][*x] = 1;
*y -= 1;
map[*y][*x] = 2;
}
break;
case 's':
if (map[*y+1][*x] != 0) {
map[*y][*x] = 1;
*y += 1;
map[*y][*x] = 2;
}
break;
case 'a':
if (map[*y][*x-1] != 0) {
map[*y][*x] = 1;
*x -= 1;
map[*y][*x] = 2;
}
break;
case 'd':
if (map[*y][*x+1] != 0) {
map[*y][*x] = 1;
*x += 1;
map[*y][*x] = 2;
}
break;
default:
break;
}
}
```
阅读全文