当前位置:网站首页>Snake game bug analysis and function expansion
Snake game bug analysis and function expansion
2022-08-04 01:40:00 【Wave Rain 123】
用CThe language implements the snake game,Although the overall implementation logic is relatively simple,But first-time writers may encounter some trickier problems,不知道是什么原因造成的,Also don't know how to deal with it,This post will list what I came across while writing the Snake gameshengways to solve these problems
目录
问题1 :The moment the snake has eaten its food,The top left corner of the window produces a body block
问题2:after food is generated,Snakes cannot eat food
问题3:The snake flickers slightly as it walks,不是很流畅
扩展1:Achieve the snake through the wall
问题解决
问题1 :The moment the snake has eaten its food,The top left corner of the window produces a body block
为什么会产生这样的结果呢?原因并不复杂,When we define the body of a snake,The size is directly given by the array,Except that we assign initial values to the initialized body coordinates during initialization,The coordinates of the rest of the body of the body array are all at the initial value0的状态.
The reason for this is after the snake has eaten a piece of food,At this time, the program determines that the number of bodies is increased by one,但此时,The coordinates of the newly added body are not assigned,It is still initial at this point0值,Then the draw function draws the body again,ran to the upper left corner,The solution is to put the coordinate assignment function before the body drawing function,Let the coordinates of the newly added section of the body be assigned first,Then draw it again,This returns to normal.
问题2:after food is generated,Snakes cannot eat food
The main reason for this problem is the existence of coordinate deviations,The window size we create is generally 640,480.If we control the speed of the snake's movement to go once per cycle10个坐标点,And the coordinate value of randomly generated food is not10的倍数的话,Then the coordinates of the snake's body will deviate from the coordinates of the food,Even if it looks like it's eaten,But the actual coordinates are not completely covered,The judgment logic of eating food is that the coordinates of the snake head and the coordinates of the food are completely covered,So it can't be eaten.Suppose the initial coordinates of the snake head are (320,240),The coordinates where the food is generated are(329,240)So no matter how the snake goes,Can't eat this food,Unless the snake moves nine points per move,In order to achieve complete coverage of the coordinates
The solution is that the coordinate value of food generation must be an integer multiple of the speed of the snake's body,And the initial position of the snake head should also be an integer multiple of the moving speed,The recommended speed is here10,使用起来很方便
//create food
void Creatfood()
{
while (1)
{
food.x = rand() % 62 * 10;
food.y = rand() % 46 * 10;
if (food.x >= 20 && food.y >= 20)
{
food.state = false;
break;
}
}
}
//The reason I add loops here is that I don't want food to be generated at the border
问题3:The snake flickers slightly as it walks,不是很流畅
The slight flicker is because the graphs are drawn one by one,然后清空,Re-draw the pattern one by one,In the process of continuously drawing and emptying,Some empty traces left.
The solution to this problem is to change the way of drawing,Use batch drawing to draw the game screen.Batch drawing is to put the pattern to be drawn into the buffer first,Then draw the content of a picture step by step,After drawing everything that needs to be drawn, it is presented on a screen,At the same time, the second buffer starts to draw the picture generated after the next cycle,The drawn picture is adapted to the frequency of the human eye(即帧率)Play to window,然后不断刷新,放置,In this way, the picture we see is very smooth
Batch plotting requires two functions
BeginBatchDraw() //开始批量绘图
.......
//The pattern that needs to be drawn is placed in the middle
.......
EedBatchDraw() //结束批量绘图
It should be noted here that if the game over screen is set,Don't put the game over screen in the middle of a batch drawing function,Because the screen will be refreshed after the batch drawing is over,The game over interface cannot be displayed.
功能扩展
扩展1:Achieve the snake through the wall
The penetration of the snake through the wall is that the snake walks behind a certain boundary of the window,Then pass the snake from the opposite boundary of that boundary,This will prevent the snake from running away,It should be noted that the determination of boundary transmission is smaller than the boundary value,不能等于,Otherwise, half of the body will spread to the opposite side,The other half is still herebug
//穿墙
void Throughwell()
{
if (snake.szb[0].x < 0 || snake.szb[0].x > LENGTH)
{
if (snake.szb[0].x < 0)
{
snake.szb[0].x = LENGTH;
}
else if (snake.szb[0].x > LENGTH)
{
snake.szb[0].x = 0;
}
}
else if (snake.szb[0].y > WIDTH || snake.szb[0].y < 0)
{
if (snake.szb[0].y > WIDTH)
{
snake.szb[0].y = 0;
}
else if (snake.szb[0].y < 0)
{
snake.szb[0].y = WIDTH;
}
}
}
扩展2:音乐播放
Play games without music,总会感觉少了点什么,That has to be added with music,You can find music by yourself
The way to add music starts with including the library
#pragma comment(lib,"winmm.lib")
Just enter the command to play the music
mciSendString("open xxx.mp3 alias BGM", 0, 0, 0);
mciSendString("play BGM repeat", 0, 0, 0);
边栏推荐
- LDO investigation
- Flink jdbc connector 源码改造sink之 clickhouse多节点轮询写与性能分析
- IDEA02:配置SQL Server2019数据库
- FeatureNotFound( bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested:
- nodejs 安装多版本 版本切换
- nodejs+express realizes the access to the database mysql and displays the data on the page
- Use of lombok annotation @RequiredArgsConstructor
- 持续投入商品研发,叮咚买菜赢在了供应链投入上
- Flask Framework Beginner-05-Command Management Manager and Database Use
- 螺旋矩阵_数组 | leecode刷题笔记
猜你喜欢
随机推荐
敏捷交付的工程效能治理
nodejs安装及环境配置
- heavy OpenCV 】 【 mapping
【QT小记】QT中信号和槽的基本使用
GNSS[0]- Topic
实例040:逆序列表
HBuilderX的下载安装和创建/运行项目
工程制图复习题(带答案)
实例038:矩阵对角线之和
持续投入商品研发,叮咚买菜赢在了供应链投入上
Is there any jdbc link to Youxuan database documentation and examples?
Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)
KunlunBase 1.0 发布了!
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
nodejs+express realizes the access to the database mysql and displays the data on the page
Flink jdbc connector 源码改造sink之 clickhouse多节点轮询写与性能分析
Use of lombok annotation @RequiredArgsConstructor
通用的测试用例编写大全(登录测试/web测试等)
splice随机添加和删除的写法
vxe-table 从页面批量删除数据 (不动数据库里的数据)