IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> c语言做推箱子 -> 正文阅读

[C++知识库]c语言做推箱子

下载解压可玩:推箱子

图片:

说明:

代码中的各个功能就不细说了,要是想要源代码下面又可以自取,要是自己想做的话,建议找个教程视频,我就是在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;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-07-17 16:02:42  更:2022-07-17 16:06:05 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 16:42:48-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码