当前位置:网站首页>C language --- basic function realization of push box 01
C language --- basic function realization of push box 01
2022-06-26 16:41:00 【D_ eretay】
Catalog
1. Preface
The first thing to push the box is c A classic project of language , This article will show you how to implement... From scratch c How does the language implement push box
2. Game effect display

3. Project analysis
Game elements :
- clearing 0
- The wall 1
- role 2
- The box 3
- Victory point 4
Game purpose :
The player controls the character to move in the open space of the map , Avoid obstacles by pushing the box , Push all the boxes to the victory point to succeed .

4. Map implementation
4.1 Store maps
First, we use an array to store the map , Why do we use char instead of int? Because int Generally 4 byte ,char by 1 byte , We can greatly reduce the memory required to store maps . If you want to write more than one map , Just expand the array to three dimensions , The author will explain it in a later article .
char map[10][10] =
{
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 4, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 1, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 0, 2, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 0, 0, 3, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};4.2 Print a map
Corresponding to the printing of the map, we need to traverse the map , utilize switch Statement to print the map on the console .
for (size_t i = 0; i < 10; i++)
{
for (size_t j = 0; j < 10; j++)
{
switch (map[i][j])
{
case 0:
printf(" "); // Note that there are two spaces , If it is a space, there will be some problems in map printing
break;
case 1:
printf("█");
break;
case 2:
printf("*");
break;
case 3:
printf("●");
break;
case 4:
printf("*");
break;
default:
break;
}
}
printf("\n");
}
5. Control character movement
5.1 Find controlled roles
int posX = 0, posY = 0;
for (size_t i = 0; i < 10; i++)
{
for (size_t j = 0; j < 10; j++)
{
if (2 == map[i][j] || 2 + 4 == map[i][j])
{
posX = i;
posY = j;
break;
// Find role , Record where they are x Axis and y Axis coordinates
}
}
}
5.2 Enabling mobility
// control ( keyboard :WSAD( The up and down or so ))
// You need to get the keys from the keyboard ( character )
switch (getch()) // getch() This method needs to import... At the beginning #include <conio.h>
{
case 'w':
case 'W':
printf(" Up \n");
/*
Upward logic :
Change the value of array elements
Take people as reference
It could be :
clearing Move
The wall Immobility
Be successful Move
The box Judge what is on the box
clearing Move
Be successful Move
The wall Immobility
Another box Immobility
*/
// Above is the open space or success point map[posX][posY]: Where the protagonist is
if (0 == map[posX - 1][posY] || 4 == map[posX - 1][posY])
{
// Current position: people leave
map[posX][posY] -= 2;
// People from above come here
map[posX - 1][posY] += 2;
}
// There are boxes on it ( Push the box away from the point )
else if (3 == map[posX - 1][posY] || 3 + 4 == map[posX - 1][posY])
{
// The top of the box is open space or success point map[posX - 1][posY]: Box position
if (0 == map[posX - 2][posY] || 4 == map[posX - 2][posY])
{
// Current position: people leave
map[posX][posY] -= 2;
// People from above come here
map[posX - 1][posY] += 2;
// The upper position box leaves
map[posX - 1][posY] -= 3;
// Top box on top come here
map[posX - 2][posY] += 3;
}
}
break;
case 's':
case 'S':
printf(" Down \n");
break;
case 'a':
case 'A':
printf(" towards the left \n");
break;
case 'd':
case 'D':
printf(" towards the right \n");
break;
default:
break;
}
6. Judge victory
Search through the map , If you find no boxes , It can be judged as a victory .(size_t yes C++ What's inside ,size_t amount to unsigned int, Here is only for understanding , It can be used int Instead of size_t)
bool isWin()
{
for (size_t i = 0; i < 10; i++)
{
for (size_t j = 0; j < 10; j++)
{
if (map[i][j] == 3)
{
return false;
}
}
}
return true;
}
边栏推荐
- [Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram
- 精致妆容成露营“软实力”,唯品会户外美妆护肤产品销量激增
- Greenplum数据库故障分析——semop(id=2000421076,num=11) failed: invalid argument
- Scala 基础 (二):变量和数据类型
- Knowing these commands allows you to master shell's own tools
- 构造函数和析构函数
- 板卡的分级调试经验
- Redis顺序排序命令
- 并发编程整体脉络
- JS教程之 使用 Electron.JS 构建原生桌面应用程序乒乓游戏
猜你喜欢
![[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity](/img/e8/9c5f1658a252c3c80503b5021917f6.jpg)
[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity
![[Li Kou brush questions] 11 Container holding the most water //42 Rain water connection](/img/45/1e712300ea655856762394fba09066.png)
[Li Kou brush questions] 11 Container holding the most water //42 Rain water connection

What is the process of switching C # read / write files from user mode to kernel mode?

What does the inner structure of the neural network "alchemy furnace" look like? An interpretation of the thesis by the doctor of Oxford University

【MATLAB项目实战】基于卷积神经网络与双向长短时(CNN-LSTM)融合的锂离子电池剩余使用寿命预测

MS|谢黎炜组发现混合益生菌制剂及其代谢产物可缓解结肠炎

【力扣刷题】11.盛最多水的容器//42.接雨水

Arduino UNO + DS1302简单获取时间并串口打印

Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
![[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap](/img/b6/21e51fa7f79d4a4b950f061703f0fb.png)
[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap
随机推荐
【力扣刷题】二分查找:4. 寻找两个正序数组的中位数
Redis 迁移(操作流程建议)
100+ data science interview questions and answers Summary - basic knowledge and data analysis
Unlock the value of data fusion! Tencent angel powerfl won the "leading scientific and Technological Achievement Award" at the 2021 digital Expo
Scala Basics (II): variables and data types
内存分区模型
了解下常见的函数式接口
股票开户优惠链接,我如何才能得到?在线开户安全么?
Niuke Xiaobai monthly race 50
Oilfield exploration problems
Data analysis - numpy quick start
Redis的ACID
JUnit unit test
板卡的分级调试经验
In a bad mood, I just write code like this
Structure the graduation project of actual combat camp
TCP congestion control details | 1 summary
Big talk Domain Driven Design -- presentation layer and others
[learn FPGA programming from scratch -46]: Vision - development and technological progress of integrated circuits
Stm32h7b0 replaces the h750 program, causing the MCU to hang up and unable to burn the program