当前位置:网站首页>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);边栏推荐
- - heavy OpenCV 】 【 mapping
- 一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
- typescript48 - type compatibility between functions
- 【日志框架】
- 静态/动态代理模式
- Intranet penetration - application
- VR panorama shooting online exhibition hall, 3D panorama brings you an immersive experience
- .NET Static Code Weaving - Rougamo Release 1.1.0
- Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
- GraphQL背后处理及执行过程是什么
猜你喜欢

5.scrapy中间件&分布式爬虫

Quickly build a website with static files

this巩固训练,从两道执行题加深理解闭包与箭头函数中的this

js中常用的几种遍历处理数据的方法梳理
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?

Flask Framework Beginner-06-Add, Delete, Modify and Check the Database

MySQL回表指的是什么

esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)

工程制图复习题

持续投入商品研发,叮咚买菜赢在了供应链投入上
随机推荐
Thinkphp commonly used techniques
持续投入商品研发,叮咚买菜赢在了供应链投入上
网络带宽监控,带宽监控工具哪个好
TensoFlow学习记录(二):基础操作
Observability:你所需要知道的关于 Syslog 的一些知识
ThreadLocal
【Untitled】
C 学生管理系统_分析
字符串的排列
How to copy baby from Taobao (or Tmall store) through API interface to Pinduoduo interface code docking tutorial
ASP.NET 获取数据库的数据并写入到excel表格中
什么是SVN(Subversion)?
特征值与特征向量
nodejs+express realizes the access to the database mysql and displays the data on the page
字符串变形
js中常用的几种遍历处理数据的方法梳理
Is there any jdbc link to Youxuan database documentation and examples?
持续投入商品研发,叮咚买菜赢在了供应链投入上
网页三维虚拟展厅为接入元宇宙平台做基础
esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)