去年写的代码,突然发现在硬盘里躺了很久,贴出来分享一下
- 可自定义关卡
RGB性能加持
控制台RBG驱动 and获取按键状态
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo; //设置控制台用的
void windows_init()
{
GetConsoleCursorInfo(hOut, &CursorInfo); //获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(hOut, &CursorInfo); //设置控制台光标状态
}
void set_colo(char colo)//用于设置字体颜色
{
SetConsoleTextAttribute(hOut, colo | hig);
}
void colo_print(char colo, const char* p) // 可以设置颜色的显示
{
SetConsoleTextAttribute(hOut, colo | hig);
printf(p);
SetConsoleTextAttribute(hOut, white | hig);
}
void set_y_x(int y, int x)//设置光标位置
{
COORD pos = { x,y };
SetConsoleCursorPosition(hOut, pos);
}
void y_x_colo_print(int y, int x, char colo, const char* p)//可以设置坐标以及颜色的文件显示
{
set_y_x(y, x);
colo_print(colo, p);
}
char get_key() //获得按键键值
{
char a;
if (_kbhit() == 1) //由于单使用 _getch()会导致程序堵塞,所以用先用_kbhit()获得是否有按键按下
{
a = _getch();
return a;
}
return 0;
}
定义地图结构
#define max_level 10
#define max_box 10
struct home
{
char map[16][16];
char map_color;
char player_x;
char player_y;
char player_color;
char box_x[max_box];
char box_y[max_box];
char box_color;
char box_end_x[max_box];
char box_end_y[max_box];
char box_end_color;
char box_int;
int move_int;
int use_time;
char flag;
};
自定义关卡
home levels[max_level] =
{
//leve 1***********************************
{
// map
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
},
//map_color
red,
//play_x
7,
//play_y
7,
//play_color
yellow,
//box_x
{4,10},
//box_y
{10,10},
//box_color
blue,
//box_end_x
{10,4},
//box_end_y
{2,2},
//box_end_color
green,
//box_int
2,
//
0,
0,
1
},
//leve 1***********************************
//leve 2***********************************
{
// map
{
{0,0,0,0,1,1,1,1,1,1,0,0,1,1,0,0},
{0,0,0,0,1,1,1,1,1,1,0,0,1,1,0,0},
{1,1,1,1,1,1,0,0,0,0,0,0,1,1,0,0},
{1,1,1,1,1,1,0,0,0,0,0,0,1,1,0,0},
{1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1},
{1,1,0,0,1,1,1,1,1,1,0,0,0,0,1,1},
{1,1,0,0,1,1,1,1,1,1,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,1,1,1,0,0,0,0,0,1,1},
{1,1,1,1,0,0,1,1,1,0,0,0,0,0,1,1},
{0,0,1,1,0,0,0,0,0,0,0,1,1,1,1,1},
{0,0,1,1,0,0,0,0,0,0,0,1,1,1,1,1},
{0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0}
},
//map_color
red,
//play_x
8,
//play_y
2,
//play_color
yellow,
//box_x
{4},
//box_y
{10},
//box_color
blue,
//box_end_x
{10},
//box_end_y
{0},
//box_end_color
green,
//box_int
1,
//
0,
0,
1
},
//leve 2***********************************
//leve 3***********************************
{
// map
{
{1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0},
{1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,0},
{1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,0},
{1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0},
{1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0},
{1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0},
{1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0},
{1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0},
{1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0},
{1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0},
{1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
},
//map_color
red,
//play_x
2,
//play_y
4,
//play_color
yellow,
//box_x
{6,6,4},
//box_y
{6,8,8},
//box_color
blue,
//box_end_x
{4,4,2},
//box_end_y
{6,4,6},
//box_end_color
green,
//box_int
3,
//
0,
0,
1
},
//leve 3***********************************
};
主功能代码
home game;
int level = 0;
char input_level(int a)
{
char y, x, z;
if (levels[a].flag == 1)
{
for (y = 0; y < 16; y++)
{
for (x = 0; x < 16; x++)
{
game.map[y][x] = levels[a].map[y][x];
}
}
game.map_color = levels[a].map_color;
game.player_x = levels[a].player_x;
game.player_y = levels[a].player_y;
game.player_color = levels[a].player_color;
game.box_color = levels[a].box_color;
game.box_end_color = levels[a].box_end_color;
game.box_int = levels[a].box_int;
game.move_int = levels[a].move_int;
game.use_time = levels[a].use_time;
for (z = 0; z < max_box; z++)
{
game.box_x[z] = levels[a].box_x[z];
game.box_y[z] = levels[a].box_y[z];
game.box_end_x[z] = levels[a].box_end_x[z];
game.box_end_y[z] = levels[a].box_end_y[z];
}
return 1;
}
return 0;
}
void display()
{
char y, x, z;
char buff[16][16] = { 0 };
for (y = 0; y < 16; y++)
{
for (x = 0; x < 16; x++)
{
if (game.map[y][x] == 1)
{
buff[y][x] = game.map_color;
}
}
}
for (z = 0; z < game.box_int; z++)
{
buff[game.box_end_y[z]][game.box_end_x[z]] = game.box_end_color;
buff[game.box_end_y[z] + 1][game.box_end_x[z] + 1] = game.box_end_color;
}
buff[game.player_y][game.player_x] = game.player_color;
buff[game.player_y + 1][game.player_x] = game.player_color;
buff[game.player_y][game.player_x + 1] = game.player_color;
buff[game.player_y + 1][game.player_x + 1] = game.player_color;
for (z = 0; z < game.box_int; z++)
{
buff[game.box_y[z]][game.box_x[z]] = game.box_color;
buff[game.box_y[z] + 1][game.box_x[z]] = game.box_color;
buff[game.box_y[z]][game.box_x[z] + 1] = game.box_color;
buff[game.box_y[z] + 1][game.box_x[z] + 1] = game.box_color;
}
for (z = 0; z < game.box_int; z++)
{
buff[game.box_end_y[z] + 1][game.box_end_x[z]] = game.box_end_color;
buff[game.box_end_y[z]][game.box_end_x[z] + 1] = game.box_end_color;
}
for (y = 0; y < 16; y++)
{
for (x = 0; x < 16; x++)
{
if (buff[y][x] != 0)
{
y_x_colo_print(y, x * 2, buff[y][x], "█");
}
else
{
y_x_colo_print(y, x * 2, buff[y][x], " ");
}
}
}
}
char cheack_win()
{
char a, b, c = 0;
for (a = 0; a < game.box_int; a++)
{
c = 0;
for (b = 0; b < game.box_int; b++)
{
if (game.box_y[a] == game.box_end_y[b] && game.box_x[a] == game.box_end_x[b])
{
c = 1;
}
}
if (c == 0)
{
return 0;
}
}
return 1;
}
void game_info()
{
y_x_colo_print(1, 0, green, "推箱子游戏,方向键移动,空格重置\n关卡\n按任意键开始\n吴文峰,2019-11-22");
_getch();
}
void game_over()
{
y_x_colo_print(1, 40, green, "your win! ");
_getch();
}
void run_data()
{
char str[32];
sprintf(str, "第 %d 关", level + 1);
y_x_colo_print(3, 40, white, str);
//sprintf(str, "步数:%d", game.move_int);
//y_x_colo_print(4, 40, white, str);
//sprintf(str, "用时:%d", game.use_time);
//y_x_colo_print(5, 40, white, str);
}
char box_move(char id, char dir, char mod)
{
char box_y, box_x, flag = 1, a;
box_y = game.box_y[id];
box_x = game.box_x[id];
switch (dir)
{
case uu:
game.box_y[id] -= 1;
break;
case dd:
game.box_y[id] += 1;
break;
case ll:
game.box_x[id] -= 1;
break;
case rr:
game.box_x[id] += 1;
break;
default:
break;
}
if ((game.map[game.box_y[id]][game.box_x[id]] == 1) || (game.map[game.box_y[id] + 1][game.box_x[id]] == 1) || (game.map[game.box_y[id]][game.box_x[id] + 1] == 1) || (game.map[game.box_y[id] + 1][game.box_x[id] + 1] == 1))
{
flag = 0;
}
for (a = 0; a < game.box_int; a++)
{
if (a != id)
{
if ((game.box_y[id] == game.box_y[a] && game.box_x[id] == game.box_x[a]) || (game.box_y[id] == game.box_y[a] + 1 && game.box_x[id] == game.box_x[a]) || (game.box_y[id] == game.box_y[a] && game.box_x[id] == game.box_x[a] + 1) || (game.box_y[id] == game.box_y[a] + 1 && game.box_x[id] == game.box_x[a] + 1) || (game.box_y[id] + 1 == game.box_y[a] && game.box_x[id] == game.box_x[a]) || (game.box_y[id] + 1 == game.box_y[a] + 1 && game.box_x[id] == game.box_x[a]) || (game.box_y[id] + 1 == game.box_y[a] && game.box_x[id] == game.box_x[a] + 1) || (game.box_y[id] + 1 == game.box_y[a] + 1 && game.box_x[id] == game.box_x[a] + 1) || (game.box_y[id] == game.box_y[a] && game.box_x[id] + 1 == game.box_x[a]) || (game.box_y[id] == game.box_y[a] + 1 && game.box_x[id] + 1 == game.box_x[a]) || (game.box_y[id] == game.box_y[a] && game.box_x[id] + 1 == game.box_x[a] + 1) || (game.box_y[id] == game.box_y[a] + 1 && game.box_x[id] + 1 == game.box_x[a] + 1) || (game.box_y[id] + 1 == game.box_y[a] && game.box_x[id] + 1 == game.box_x[a]) || (game.box_y[id] + 1 == game.box_y[a] + 1 && game.box_x[id] + 1 == game.box_x[a]) || (game.box_y[id] + 1 == game.box_y[a] && game.box_x[id] + 1 == game.box_x[a] + 1) || (game.box_y[id] + 1 == game.box_y[a] + 1 && game.box_x[id] + 1 == game.box_x[a] + 1))
{
flag = 0;
}
}
}
if (mod == 0)
{
game.box_y[id] = box_y;
game.box_x[id] = box_x;
}
return flag;
}
char box_tick(char dir, char mod)
{
char a;
for (a = 0; a < game.box_int; a++)
{
if ((game.player_y == game.box_y[a] && game.player_x == game.box_x[a]) || (game.player_y == game.box_y[a] + 1 && game.player_x == game.box_x[a]) || (game.player_y == game.box_y[a] && game.player_x == game.box_x[a] + 1) || (game.player_y == game.box_y[a] + 1 && game.player_x == game.box_x[a] + 1) || (game.player_y + 1 == game.box_y[a] && game.player_x == game.box_x[a]) || (game.player_y + 1 == game.box_y[a] + 1 && game.player_x == game.box_x[a]) || (game.player_y + 1 == game.box_y[a] && game.player_x == game.box_x[a] + 1) || (game.player_y + 1 == game.box_y[a] + 1 && game.player_x == game.box_x[a] + 1) || (game.player_y == game.box_y[a] && game.player_x + 1 == game.box_x[a]) || (game.player_y == game.box_y[a] + 1 && game.player_x + 1 == game.box_x[a]) || (game.player_y == game.box_y[a] && game.player_x + 1 == game.box_x[a] + 1) || (game.player_y == game.box_y[a] + 1 && game.player_x + 1 == game.box_x[a] + 1) || (game.player_y + 1 == game.box_y[a] && game.player_x + 1 == game.box_x[a]) || (game.player_y + 1 == game.box_y[a] + 1 && game.player_x + 1 == game.box_x[a]) || (game.player_y + 1 == game.box_y[a] && game.player_x + 1 == game.box_x[a] + 1) || (game.player_y + 1 == game.box_y[a] + 1 && game.player_x + 1 == game.box_x[a] + 1))
{
if (box_move(a, dir, mod) == 0)
{
return 0;
}
}
}
return 1;
}
int move_try(char dir, char mod)
{
char y, x;
int flag = -1;
y = game.player_y;
x = game.player_x;
switch (dir)
{
case uu:
game.player_y -= 1;
break;
case dd:
game.player_y += 1;
break;
case ll:
game.player_x -= 1;
break;
case rr:
game.player_x += 1;
break;
default:
break;
}
if ((game.map[game.player_y][game.player_x] == 1) || (game.map[game.player_y + 1][game.player_x] == 1) || (game.map[game.player_y][game.player_x + 1] == 1) || (game.map[game.player_y + 1][game.player_x + 1] == 1))
{
flag = -1;
}
else
{
if (box_tick(dir, mod) == 1)
{
flag = 0;
}
}
if (mod == 0)
{
game.player_y = y;
game.player_x = x;
}
return flag;
}
void move(char dir)
{
int a;
if (dir != 0)
{
a = move_try(dir, 0);
if (a != -1)
{
if (a == 0)
{
move_try(dir, 1);
}
}
}
}
enum { star, exel, over };
int main()
{
char run = star, key = 0;
y_x_colo_print(0, 0, blue, "hello\n");
windows_init();
game_info();
while (1)
{
key = get_key();
switch (run)
{
case star:
if (input_level(level))
{
run = exel;
}
else
{
run = over;
}
break;
case exel:
if (key == space)
{
run = star;
}
move(key);
run_data();
if (cheack_win() == 1)
{
level += 1;
run = star;
}
break;
case over:
game_over();
level = 0;
run = star;
break;
default:
break;
}
display();
}
return 0;
}