当前位置:网站首页>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;
}
边栏推荐
- Leetcode one week race 298, first three questions
- 【MATLAB项目实战】基于卷积神经网络与双向长短时(CNN-LSTM)融合的锂离子电池剩余使用寿命预测
- mha 切换(操作流程建议)
- Big talk Domain Driven Design -- presentation layer and others
- What is flush software? Is it safe to open an account online?
- Scala Foundation (2): variables et types de données
- Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
- Science | 红树林中发现的巨型细菌挑战传统无核膜观念
- 【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃
- Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's Mae
猜你喜欢
C语言所有知识点小结
Memory partition model
【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)
MS | Xie Liwei group found that mixed probiotics and their metabolites could alleviate colitis
Knowing these commands allows you to master shell's own tools
How to implement interface current limiting?
[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
100+ data science interview questions and answers Summary - basic knowledge and data analysis
Développer un opérateur basé sur kubebuilder (démarrer)
Structure the graduation project of actual combat camp
随机推荐
Natural language inference with attention and fine tuning Bert pytorch
Data analysis - numpy quick start
请指教同花顺软件究竟是什么?网上开户是否安全么?
proxy
基于STM32+华为云IOT设计的云平台监控系统
长安链交易防重之布谷鸟过滤器
板卡的分级调试经验
Cloud platform monitoring system based on stm32+ Huawei cloud IOT design
Some instance methods of mono
R language plot visualization: plot visualizes the normalized histogram, adds the density curve KDE to the histogram, and uses geom at the bottom edge of the histogram_ Adding edge whisker graph with
TCP congestion control details | 1 summary
GUI+SQLServer考试系统
【从删库到跑路】MySQL基础 完结篇(入个门先跑路了。。)
股票开户优惠链接,我如何才能得到?在线开户安全么?
建立自己的网站(16)
SAP OData development tutorial - from getting started to improving (including segw, rap and CDP)
JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
mha 切换(操作流程建议)
若依微服务特殊字符串被过滤的解决办法
当一个程序员一天被打扰 10 次,后果很惊人!