当前位置:网站首页>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);边栏推荐
猜你喜欢

持续投入商品研发,叮咚买菜赢在了供应链投入上
![[store mall project 01] environment preparation and testing](/img/78/415b18a26fdc9e6f59b59ba0a00c4f.png)
[store mall project 01] environment preparation and testing

lombok注解@RequiredArgsConstructor的使用

Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment

【store商城项目01】环境准备以及测试

一个注解替换synchronized关键字:分布式场景下实现方法加锁

特征值与特征向量

nodejs installation and environment configuration

splice随机添加和删除的写法

企业虚拟偶像产生了实质性的价值效益
随机推荐
Summary of GNSS Articles
Apache DolphinScheduler actual combat task scheduling platform - a new generation of distributed workflow
C 学生管理系统_分析
JS 保姆级贴心,从零教你手写实现一个防抖debounce方法
redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
nodejs 安装多版本 版本切换
Thinkphp commonly used techniques
数组_滑动窗口 | leecode刷题笔记
有没有jdbc 链接优炫数据库文档及示例?
The 600MHz band is here, will it be the new golden band?
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
实例036:算素数
LDO investigation
实例039:有序列表插入元素
多渠道打包
IDEA02:配置SQL Server2019数据库
KunlunBase 1.0 发布了!
pygame 中的transform模块
【OpenCV】-重映射
工程制图复习题