C语言小游戏 - 推箱子
其他窗口操作:汉字对应的ASCLL
引入窗口控制头文件
windwos.h
定义游戏中所有实物
{" ", "■", "", "☆", "□", "▲", "", "★", "▲"};
将游戏中所有的实物赋予不同的颜色
{0, FOREGROUND_RED, -1, FOREGROUND_GREEN, FOREGROUND_GREEN, FOREGROUND_GREEN, -1, BACKGROUND_GREEN, FOREGROUND_GREEN}
void Color (int m) {
HANDLE consolehwnd;
consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consolehwnd, m);
}
将文件中的数据地图存储到数组中
while(!feof(fp)) {
fscanf(fp,"%d",&map[mapRow][mapCol++]);
if(mapCol%LIE==0) {
mapRow++;
mapCol=0;
}
}
在地图中绘制各种实物
case 0:
case 1:
case 3:
case 4:
case 5:
case 7:
case 8:
Color(mapBrickColor[now]);
printf(mapBrickChar[now]);
break;
选择关卡
case 1:
case 2:
case 3:
case 4:
case 5:
case 6: {
char s[30] = "";
sprintf(s, path, k);
if((fp=fopen(s,"rt"))==NULL) {
return 2;
}
}
以上均为废话,为了凑字数 代码中有注释
推箱子源码
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#define f(i, a, b) for(int (i)=(a);(i)<(b);(i)++)
#define LIE 10
const char mapBrickChar[][5] = {" ", "■", "", "☆", "□", "▲", "", "★", "▲"};
const int mapBrickColor[] = {0, FOREGROUND_RED, -1, FOREGROUND_GREEN, FOREGROUND_GREEN, FOREGROUND_GREEN, -1, BACKGROUND_GREEN, FOREGROUND_GREEN};
int map[20][LIE];
char path[30] = "./关卡%d.txt";
int k;
int mapRow=0;
int mapCol=0;
int ROW, COL;
void Color (int m) {
HANDLE consolehwnd;
consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consolehwnd, m);
}
int createmap() {
scanf("%d",&k);
FILE *fp;
switch (k) {
case 0: {
return 1;
}
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6: {
char s[30] = "";
sprintf(s, path, k);
if((fp=fopen(s,"rt"))==NULL) {
return 2;
}
}
break;
}
mapRow = mapCol = 0;
while(!feof(fp)) {
fscanf(fp,"%d",&map[mapRow][mapCol++]);
if(mapCol%LIE==0) {
mapRow++;
mapCol=0;
}
}
if(fclose(fp)) return 2;
return 0;
}
void DrawMap() {
system("cls");
printf("方向键控制上下左右,r重新游戏,t退出游戏\n");
f(i, 0,mapRow) {
f (j, 0, LIE) {
int now = map[i][j];
switch (now) {
case 0:
case 1:
case 3:
case 4:
case 5:
case 7:
case 8:
Color(mapBrickColor[now]);
printf(mapBrickChar[now]);
break;
}
}
printf("\n");
}
}
void getPersonPosition() {
int row, col;
f(i, 0, mapRow) {
f(j, 0, LIE) {
if (map[i][j] == 5||map[i][j] == 8) {
row = i;
col = j;
}
}
}
if(ROW!=row||COL!=col) {
Color(15);
printf("(%d,%d)\n", row, col);
}
ROW = row;
COL = col;
}
int PlayGame() {
int flag=0;
getPersonPosition();
char input;
input = getch();
switch (input) {
case 72:
if (map[ROW - 1][COL] == 0||map[ROW-1][COL]==3) {
map[ROW][COL] -= 5;
map[ROW - 1][COL] += 5;
flag=-1;
} else if (map[ROW - 1][COL] == 4 || map[ROW - 1][COL] == 7) {
if (map[ROW - 2][COL] == 0 || map[ROW - 2][COL] == 3) {
map[ROW - 2][COL] += 4;
map[ROW - 1][COL] += (5-4);
map[ROW][COL] -= 5;
flag=-1;
}
}
break;
case 80:
if (map[ROW + 1][COL] == 0 || map[ROW + 1][COL] == 3) {
map[ROW][COL] -= 5;
map[ROW + 1][COL] += 5;
flag=-2;
} else if (map[ROW + 1][COL] == 4 || map[ROW + 1][COL] == 7) {
if (map[ROW + 2][COL] == 0 || map[ROW + 2][COL] == 3) {
map[ROW + 2][COL] += 4;
map[ROW + 1][COL] += 1;
map[ROW][COL] -= 5;
flag=-2;
}
}
break;
case 75:
if (map[ROW][COL - 1] == 0 || map[ROW][COL - 1] == 3) {
map[ROW][COL] -= 5;
map[ROW][COL - 1] += 5;
flag=-3;
} else if (map[ROW][COL - 1] == 4 || map[ROW][COL - 1] == 7) {
if (map[ROW][COL - 2] == 0 || map[ROW][COL - 2] == 3) {
map[ROW][COL - 2] += 4;
map[ROW][COL - 1] += 1;
map[ROW][COL] -= 5;
flag=-3;
}
}
break;
case 77:
if (map[ROW][COL + 1] == 0 || map[ROW][COL + 1] == 3) {
map[ROW][COL] -= 5;
map[ROW][COL + 1] += 5;
flag=-4;
} else if (map[ROW][COL + 1] == 4 || map[ROW][COL + 1] == 7) {
if (map[ROW][COL + 2] == 0 || map[ROW][COL + 2] == 3) {
map[ROW][COL + 2] += 4;
map[ROW][COL + 1] += 1;
map[ROW][COL] -= 5;
flag=-4;
}
}
break;
case 'r':
mapRow=0;
mapCol=0;
ROW=0;
COL=0;
flag=-10;
createmap();
break;
case 't':
return 1;
break;
}
return flag;
}
int success() {
f(i, 0, mapRow) {
f(j, 0, LIE) {
if (map[i][j]==3) {
return 0;
}
}
}
return 1;
}
int main() {
ShellExecute(NULL,"open","https://blog.csdn.net/qq_44009311",NULL,NULL,SW_SHOWNORMAL);
while(1) {
printf("请选择关卡\n");
printf("退出'0':Logout\n");
printf("关卡'1':非常简单\n");
printf("关卡'2':简单\n");
printf("关卡'3':常规\n");
printf("关卡'4':困哪\n");
printf("关卡'5':较难\n");
printf("关卡'6':非常困难\n");
int t = createmap();
if(t != 0) return 0;
DrawMap();
while (1) {
int tp = PlayGame();
if(tp > 0) {
Color(10);
printf("退出!\n");
break;
} else if(tp==0) {
} else {
if(success()) {
Color(10);
printf("成功!\n");
system("pause");
break;
}
DrawMap();
}
}
system("cls");
}
return 0;
}
关卡1.txt
0 0 0 0 1 1 1 0 0 0
0 0 0 0 1 3 1 0 0 0
1 1 1 1 1 4 1 1 1 1
1 3 0 0 4 5 4 0 3 1
1 1 1 1 1 4 1 1 1 1
0 0 0 0 1 3 1 0 0 0
0 0 0 0 1 1 1 0 0 0
关卡2.txt
0 1 1 1 1 1 1 1 0 0
0 1 3 3 3 3 0 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