下载解压可玩:推箱子
图片:
说明:
代码中的各个功能就不细说了,要是想要源代码下面又可以自取,要是自己想做的话,建议找个教程视频,我就是在b站搜c语言推箱子找的视频,没必要看文字去做这个东西。
只是我的这个代码基本只能在vs上运行,这是因为codeblocks和dev-C++中不能运行打印的头文件:#include<graphics.h>。也就是如果想要彩印需要用vs编译
如上第一个图所示,有个exe文件可以直接玩,这个东西每次编译都会自动出现,vs需要把编译方式调成Release后,出来的exe文件才能随便给别人发,然后可以换个图标。
如何换c语言编译的exe文件图标
这样好像和发布的游戏都差不多了
细节:
推箱子的基本应该是二维数组存储每个点是什么,视频中大概是用5表示了人和空地叠加,6表示人和目的地叠加,7表示箱子和目的地的叠加,而我的代码是直接全局变量x和y表示人的位置,这样可以先判断该点是不是人所在地,在判断空和目的地,对于推箱子的函数大大简单
可以多增加功能,比如制作一个箭标,就是在合适位置打印->。
主函数:
//主菜单处理 int main() { ? ? initgraph(10 * 64, 10 * 64); ? ? ? ?//画布大小 ? ? loadimg(); ? ? ? ?//加载图片为变量 end:; ? ? page(); ? ? while (level < MAX) { ? ? ? ? gamedraw(); ? ? ? ?//打印主页面 ? ? ? ? if (end()) { ? ? ? ?//通关 ? ? ? ? ? ? while (!s.empty()) s.pop(); ? ? ? ? ? ? if (level < MAX-1) { ? ? ? ? ? ? ? ? setbkmode(TRANSPARENT); ? ? ? ? ? ? ? ? settextcolor(RGB(0,0,0)); ? ? ? ? ? ? ? ? outtextxy(280, 250, "恭喜通关"); ? ? ? ? ? ? ? ? char ch = _getch(); ? ? ? ? ? ? ? ? ch = _getch(); ? ? ? ? ? ? } ? ? ? ? ? ? else { ? ? ? ? ? ? ? ? putimage(0, 0, img_all + 8); ? ? ? ? ? ? ? ? MessageBox(0, "恭喜通过全部关卡", "晚安", 0); ? ? ? ? ? ? ? ? PlaySound(NULL, NULL, SND_FILENAME); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? ? ? init(); ? ? ? ? } ? ? ? ? if (keyEvent()) goto end; ? ? ? ?//返回主菜单 ? ? } ? ? system("pause"); ? ? closegraph(); ? ? return 0; } ?
源码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<algorithm>
#include<graphics.h>
#include<conio.h> //_getch() 接受按键
#include<easyx.h>
#include<windows.h>
#include<Mmsystem.h>
#pragma comment(lib,"Winmm.lib")
using namespace std;
#define ll long long
#define MAX 4 //最大关卡数
enum mine {
space, //空地
wall, //墙
box, //箱子
};
int dx[MAX] = { 4,3,3 ,4};
int dy[MAX] = { 4,3,3 ,4};
int x = dx[0], y = dy[0], level = 0,flag=0;
IMAGE img_all[10];
struct Node {
int a, b, data;
Node() {}
Node(int x, int y, int z) { a = x; b = y; data = z; }
};
struct node {
Node mapupdate[2];
int mx, my, flag;
node() {}
node(int x, int y, int f) { mx = x, my = y, flag = f; }
node(int x, int y, int f, Node xx, Node yy) { mx = x; my = y; flag = f; mapupdate[0] = xx; mapupdate[1] = yy; }
};
stack<node>s;
int map[MAX][10][10] = {
{
/*******第一关箱子和墙******/
{3,3,3,1,1,1,3,3,3,3},
{3,3,3,1,0,1,3,3,3,3},
{3,3,3,1,2,1,1,1,1,3},
{3,1,1,1,0,0,2,0,1,3},
{3,1,0,2,0,0,1,1,1,3},
{3,1,1,1,1,0,1,3,3,3},
{3,3,3,3,1,2,1,3,3,3},
{3,3,3,3,1,0,1,3,3,3},
{3,3,3,3,1,1,1,3,3,3},
{3,3,3,3,3,3,3,3,3,3}
},
/*******第二关箱子和墙******/
{
{3,3,3,1,1,1,1,3,3,3},
{3,3,1,0,0,0,1,3,3,3},
{3,1,1,0,0,0,1,1,3,3},
{3,1,0,0,2,0,0,1,3,3},
{3,1,0,2,1,2,0,1,3,3},
{3,1,0,0,1,0,0,1,3,3},
{3,1,0,0,0,2,0,1,3,3},
{3,1,1,1,0,0,0,1,3,3},
{3,3,3,1,1,1,1,1,3,3},
{3,3,3,3,3,3,3,3,3,3}
},
{
/*******第三关箱子和墙******/
{3,3,1,1,1,1,1,3,3,3},
{3,3,1,0,0,0,1,3,3,3},
{3,1,1,0,2,0,1,1,3,3},
{3,1,0,0,2,0,0,1,3,3},
{3,1,0,2,1,2,0,1,1,3},
{3,1,0,0,1,0,2,0,1,3},
{3,1,0,1,1,2,0,0,1,3},
{3,1,0,0,0,0,0,0,1,3},
{3,1,1,1,1,1,1,1,1,3},
{3,3,3,3,3,3,3,3,3,3}
},
{
/*******第四关箱子和墙******/
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,2,1,2,1,1,1,0,1},
{1,0,0,0,0,1,0,1,0,1},
{1,0,1,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,0,0,1},
{1,0,1,0,1,0,1,0,0,1},
{1,0,1,2,1,0,1,0,0,1},
{1,0,0,0,1,0,1,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
}
};
int vis[MAX][10][10] = {
{
/*******第一关目的地******/
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,0},
{0,0,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,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,0,1,0,1,0,0,0,0},
{0,0,0,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},
{0,0,0,0,0,0,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,0,0,0,0,0,0,0},
{0,0,0,1,0,1,0,0,0,0},
{0,0,0,1,0,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,0,0,0,0,1,0,0,0},
{0,0,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,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,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}
}
};
void background()
{
putimage(0, 0, img_all + 7);
}
//打印背景
void gamedraw()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
int a = j * 64, b = i * 64;
if (i == x && j == y)
{
if (vis[level][i][j] == 1) //printf("★");
putimage(a, b, img_all + 6);
else //printf("●");
putimage(a, b, img_all + 5);
}
else if (map[level][i][j] == space)
{
if (vis[level][i][j] == 1) //printf("☆");
putimage(a, b, img_all + 4);
else //printf(" ");
putimage(a, b, img_all + 0);
}
else if (map[level][i][j] == wall)
{
//printf("▓ ");
putimage(a, b, img_all + 1);
}
else if (map[level][i][j] == box)
{
if (vis[level][i][j] == 1) //printf("■");
putimage(a, b, img_all + 3);
else //printf("□");
putimage(a, b, img_all + 2);
}
}
printf("\n");
}
setbkmode(TRANSPARENT);
settextcolor(RGB(250, 250, 250));
outtextxy(10, 50, "按 2 可查看按键");
}
void add()
{
cout << x << " " << y << endl;
s.push(node(x, y,0));
}
void add(int x1, int y1, int x2, int y2)
{
s.push(node(x,y,1,Node(x1,y1,map[level][x1][y1]),Node(x2,y2,map[level][x2][y2])));
}
void pop()
{
x = s.top().mx;
y = s.top().my;
if (s.top().flag == 1) {
for (int i = 0; i < 2; i++) {
int mx = s.top().mapupdate[i].a;
int my = s.top().mapupdate[i].b;
int data = s.top().mapupdate[i].data;
map[level][mx][my] = data;
}
}
s.pop();
}
void update()
{
if (flag == 0) {
PlaySound(TEXT("lcon/1.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
flag = 1;
}
else {
PlaySound(NULL, NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
flag = 0;
}
}
void Key();
int keyEvent()
{
if (s.empty()) add();
//上 H 72,下 P 80,右 M 77,左 K 75
char ch = _getch();
if (ch == 50) {
Key();
}
else if (ch == 49) {
update();
}
else if (ch == 27) {
return 1;
}
else if (ch==8){
while (!s.empty()) pop();
}
else if (ch==13) { //Esc键用来重新开始,回车键(Enter)和清除键(Backspace)执行回撤
if (!s.empty()) pop();
}
else if (ch == 'w' || ch == 'W' || ch == 72) {
if (map[level][x - 1][y] == space) x-- ;
else if (map[level][x - 1][y] == box && map[level][x - 2][y] == space) {
add(x-1,y,x-2,y);
map[level][x - 1][y] = space;
map[level][x - 2][y] = box;
x--;
}
}
else if (ch == 's' || ch == 'S' || ch == 80) {
if (map[level][x + 1][y] == space) x++;
else if (map[level][x + 1][y] == box && map[level][x + 2][y] == space) {
add(x + 1, y, x + 2, y);
map[level][x + 1][y] = space;
map[level][x + 2][y] = box;
x++;
}
}
else if (ch == 'd' || ch == 'D' || ch == 77) {
if (map[level][x][y + 1] == space) y++;
else if (map[level][x][y + 1] == box && map[level][x][y + 2] == space) {
add(x , y+1, x , y+2);
map[level][x][y + 1] = space;
map[level][x][y + 2] = box;
y++;
}
}
else if (ch == 'a' || ch == 'A' || ch == 75) {
if (map[level][x][y - 1] == space) y--;
else if (map[level][x][y - 1] == box && map[level][x][y - 2] == space) {
add(x, y-1, x, y-2);
map[level][x][y - 1] = space;
map[level][x][y - 2] = box;
y--;
}
}
return 0;
}
bool end()
{
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
if (vis[level][i][j] == 1 && map[level][i][j] != box) return false;
return true;
}
void init()
{
background();
level++;
x = dx[level];
y = dy[level];
gamedraw();
}
void loadimg()
{
for (int i = 0; i < 7; i++)
{
char file[20] = "";
sprintf_s(file, "./lcon/%d.jpg", i);
loadimage(img_all + i, file, 64, 64);
}
loadimage(img_all + 7, "./lcon/7.jpg", 640, 640);
loadimage(img_all + 8, "./lcon/8.jpg", 640, 640);
PlaySound(TEXT("lcon/1.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
flag = 1;
}
void Key()
{
background();
setbkmode(TRANSPARENT);
settextcolor(RGB(250, 250, 250));
outtextxy(150, 200, "开启(关闭)背景音乐");
outtextxy(350, 200, " 1键");
outtextxy(150, 250, "回退");
outtextxy(350, 250, "Enter回车键");
outtextxy(150, 300, "重新开始本关");
outtextxy(350, 300, "Backspace清除键");
outtextxy(150, 350, "返回主菜单");
outtextxy(350, 350, "Esc返回键");
outtextxy(450, 400, "按任意键返回");
char ch = _getch();
if(ch!=50) ch = _getch();
background();
}
//按键查看
void lastprint(int last)
{
setbkmode(TRANSPARENT);
settextcolor(RGB(0, 250, 250));
if(last==0) outtextxy(210, 200, "->");
else if(last==1) outtextxy(210, 250, "->");
else if(last==2) outtextxy(210, 300, "->");
}
void pageprint(int last)
{
background();
setbkmode(TRANSPARENT);
settextcolor(RGB(250, 250, 250));
outtextxy(280, 200, "开始游戏");
outtextxy(280, 250, "按键查看");
outtextxy(280, 300, "退出");
lastprint(last);
}
//打印主页面
void page()
{
int last = 0;
while (1) {
pageprint(last);
char ch = _getch();
if (ch == 80 && last < 2) last++;
else if (ch == 72 && last > 0) last--;
else if (ch == 13) {
if (last == 0) return;
else if (last == 1) Key();
else if (last == 2) {
exit(0);
}
}
else if (ch == 49) update();
}
}
//主菜单处理
int main()
{
initgraph(10 * 64, 10 * 64);
loadimg();
end:;
page();
while (level < MAX) {
gamedraw();
if (end()) {
while (!s.empty()) s.pop();
if (level < MAX-1) {
setbkmode(TRANSPARENT);
settextcolor(RGB(0,0,0));
outtextxy(280, 250, "恭喜通关");
char ch = _getch();
ch = _getch();
}
else {
putimage(0, 0, img_all + 8);
MessageBox(0, "恭喜通过全部关卡", "晚安", 0);
PlaySound(NULL, NULL, SND_FILENAME);
break;
}
init();
}
if (keyEvent()) goto end;
}
system("pause");
closegraph();
return 0;
}
|