当前位置:网站首页>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);
边栏推荐
- Flask Framework Beginner-06-Add, Delete, Modify and Check the Database
- Hey, I had another fight with HR in the small group!
- Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
- typescript56 - generic interface
- js中常用的几种遍历处理数据的方法梳理
- 实例041:类的方法与变量
- 工程制图复习题
- nodejs install multi-version version switching
- 天地图坐标系转高德坐标系 WGS84转GCJ02
- redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
猜你喜欢
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化
redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
通用的测试用例编写大全(登录测试/web测试等)
Flask框架初学-05-命令管理Manager及数据库的使用
Use nodejs switch version (no need to uninstall and reinstall)
appium软件自动化测试框架
esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)
Installation and configuration of nodejs+npm
2022 China Computing Power Conference released the excellent results of "Innovation Pioneer"
Parquet encoding
随机推荐
Google Earth Engine - Calculates the effective width of rivers using publicly available river data
vxe-table 从页面批量删除数据 (不动数据库里的数据)
数组_滑动窗口 | leecode刷题笔记
实例040:逆序列表
FeatureNotFound( bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested:
nodejs切换版本使用(不需要卸载重装)
.NET Static Code Weaving - Rougamo Release 1.1.0
pygame 中的transform模块
什么是SVN(Subversion)?
Flask Framework Beginner-05-Command Management Manager and Database Use
Use nodejs switch version (no need to uninstall and reinstall)
html select标签赋值数据库查询结果
GNSS【0】- 专题
initramfs详解----添加硬盘驱动并访问磁盘
5.scrapy中间件&分布式爬虫
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
Promise 解决阻塞式同步,将异步变为同步
Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
ASP.NET 获取数据库的数据并写入到excel表格中
敏捷交付的工程效能治理