当前位置:网站首页>C language as a push box
C language as a push box
2022-07-18 21:09:00 【Int me】
Download, extract and play : tuixiangzi
picture :



explain :
I won't elaborate on the functions in the code , If you want the source code, you can get it yourself , If you want to do it , It is recommended to find a tutorial video , I'm just b Station search c Video of language push box search , There is no need to read words to do this .
But my code can only be used in vs Up operation , This is because codeblocks and dev-C++ Cannot run the printed header file in :#include<graphics.h>. That is, if you want to use color printing vs compile
As shown in the first figure above , There is one exe Files can be played directly , This thing will appear automatically every time it is compiled ,vs You need to adjust the compilation mode to Release after , Coming out exe Documents can be sent to others casually , Then you can change the icon .
How to change c Language compiled exe File Icon
![]()
This seems to be similar to the released Games
details :
The basic of pushing boxes should be a two-dimensional array to store what each point is , The video probably uses 5 It shows the superposition of people and open space ,6 It means that people and destinations are superimposed ,7 Indicates the superposition of boxes and destinations , And my code is a direct global variable x and y Indicates the position of a person , In this way, you can first judge whether the point is where the person is , In judging empty and destination , The function of pushing boxes is very simple
![]()
You can add more functions , For example, make an arrow , Is to print in the right place ->.

The main function :
// Main menu processing
int main()
{
initgraph(10 * 64, 10 * 64); // Canvas size
loadimg(); // Load image as variable
end:;
page();
while (level < MAX) {
gamedraw(); // Print main page
if (end()) { // clearance
while (!s.empty()) s.pop();
if (level < MAX-1) {
setbkmode(TRANSPARENT);
settextcolor(RGB(0,0,0));
outtextxy(280, 250, " Congratulations on customs clearance ");
char ch = _getch();
ch = _getch();
}
else {
putimage(0, 0, img_all + 8);
MessageBox(0, " Congratulations on passing all levels ", " Good night, ", 0);
PlaySound(NULL, NULL, SND_FILENAME);
break;
}
init();
}
if (keyEvent()) goto end; // Back to main menu
}
system("pause");
closegraph();
return 0;
}
Source code :
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<algorithm>
#include<graphics.h>
#include<conio.h> //_getch() Accept the button
#include<easyx.h>
#include<windows.h>
#include<Mmsystem.h>
#pragma comment(lib,"Winmm.lib")
using namespace std;
#define ll long long
#define MAX 4 // Maximum number of levels
enum mine {
space, // clearing
wall, // wall
box, // The 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] = {
{
/******* The first is the box and the wall ******/
{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}
},
/******* The second is the box and the wall ******/
{
{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}
},
{
/******* The third is the box and the wall ******/
{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}
},
{
/******* The fourth is the box and the wall ******/
{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] = {
{
/******* The first level is destination ******/
{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}
},
{
/******* The second level is destination ******/
{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}
},
{
/******* The third level is destination ******/
{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}
},
/******* The fourth level is destination ******/
{
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,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);
}
// Print the background
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, " Press 2 You can view the keys ");
}
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();
// On H 72, Next P 80, Right M 77, Left 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 Key to restart , Enter key (Enter) And clear key (Backspace) Execute fallback
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, " Turn on ( close ) The background music ");
outtextxy(350, 200, " 1 key ");
outtextxy(150, 250, " Back off ");
outtextxy(350, 250, "Enter Enter key ");
outtextxy(150, 300, " Restart this level ");
outtextxy(350, 300, "Backspace Clear key ");
outtextxy(150, 350, " Back to main menu ");
outtextxy(350, 350, "Esc Return key ");
outtextxy(450, 400, " Press any key to return to ");
char ch = _getch();
if(ch!=50) ch = _getch();
background();
}
// Press to view
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, " Start the game ");
outtextxy(280, 250, " Press to view ");
outtextxy(280, 300, " sign out ");
lastprint(last);
}
// Print main page
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();
}
}
// Main menu processing
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, " Congratulations on customs clearance ");
char ch = _getch();
ch = _getch();
}
else {
putimage(0, 0, img_all + 8);
MessageBox(0, " Congratulations on passing all levels ", " Good night, ", 0);
PlaySound(NULL, NULL, SND_FILENAME);
break;
}
init();
}
if (keyEvent()) goto end;
}
system("pause");
closegraph();
return 0;
}
边栏推荐
- uniapp Request请求封装的方法
- Markdown basic syntax format
- putchar()
- The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping quantile of dataframe data
- MySQL查看数据库性能常用命令
- app安全检测指南学习笔记
- JS 中的事件委托是什么?
- Interview question 5
- Redis implements distributed locks
- 剑指 Offer 53 - I. 在排序数组中查找数字 I
猜你喜欢

sentinel

Stm32f407---- power management

Uniapp request request encapsulation method

一步到位玩透Ansible-目录

激活navicat提示rsa public key not find的问题

How to do fine operation management in industrial parks

Markdown basic syntax format

数据统计分析案例(对比分析、销量定比分析、同比、双坐标图、环比、shift、贡献度分析(帕累托法则)、差异化分析、resample、季节性波动分析)

VMware 恢复快照出现 无法创建 5040 MB 的匿名分页文件: 系统资源不足,无法完成请求的服务

What skills do software test engineers need to master if they need to speak 20K? I dare to be higher with these skills~
随机推荐
Brits: detailed explanation of bidirectional recursive imputation for time series
知识图谱de构建与应用(四):知识获取
剑指 Offer 53 - II. 0~n-1中缺失的数字
Data statistical analysis cases (comparative analysis, sales comparison analysis, year-on-year, double coordinate diagram, month on month, shift, contribution analysis (Pareto Law), differentiation an
app安全检测指南学习笔记
UE4阴影:PerObjectShadow验证
鸿蒙应用开发项目新建过程与hap包生成方法
Find out the motivation and needs of enterprise location, and carry out investment attraction work efficiently
leetcode:1552. 两球之间的磁力【最值的最值 = 二分】
Kotlin correctly exits the foreach and foreachindexed loop functions
Have you met these use case design questions during the interview?
World Tour Finals 2019 D - special boxes
[try to hack] NTLM and LM Foundation
基于STM32电源模块开发
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型
20. Network principles - Basic Concepts
VirtualBox virtual machine failed to start, e_ FAIL( 0x80004005)
博客从 CloudBase 迁移至云主机
【Try to Hack】NTLM和LM基础
Scrcpy projection