当前位置:网站首页>[basic grammar] Snake game written in C language
[basic grammar] Snake game written in C language
2022-07-03 05:05:00 【The beginning of Hongmeng】
This time, we provide you with a c Snake code written in
Catalog
Two 、 Part of the code explanation
3、 ... and 、 Full code download link
One 、 Realization effect
Key position : Use wasd Four keys to control the direction , Press q Key to exit ( Pay attention to using English input method to realize keying at the end )
The rules : Every time a snake eats a bean, it gets 10 branch , At the same time, the side of the body is long 、 Speed up
When the snake touches the wall or bites itself, the game ends , At the same time, the game score will be output


Two 、 Part of the code explanation
(1) Define snakes and beans with structures
typedef struct Snakes
{
int x;
int y;
struct Snakes *next;
}snake;
snake *head,*tail;
struct Food
{
int x;
int y;
}food;(2) Print walls
void creatgraph()
{
int i;
for (i = 0; i<58; i += 2)// Print the top and bottom borders
{
gotoprint(i, 0);
gotoprint(i, 26);
}
for (i = 1; i < 26; i++)
{
gotoprint(0, i);
gotoprint(56, i);
}
head = (snake*)malloc(sizeof(snake));
head->x = 16;
head->y = 15;
//gotoprint(head->x, head->y);
tail = (snake*)malloc(sizeof(snake));
snake *p = (snake*)malloc(sizeof(snake));
snake *q = (snake*)malloc(sizeof(snake));
p->x = 16;
p->y = 16;
q->x = 16;
q->y = 17;
head->next = p;
p->next = q;
q->next = tail;
//gotoprint(p->x, p->y);
//gotoprint(q->x, q->y);
tail->next = NULL;
tail->x = 4;
tail->y = 2;
}
void gotoxy(int x, int y)
{
COORD pos;
HANDLE hOutput;
pos.X = x;
pos.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
}
void gotoprint(int x, int y)
{
gotoxy(x, y);
printf("■");
}
void gotodelete(int x, int y)
{
gotoxy(x, y);
printf(" ");
}(3) Generative bean
void creatfood()
{
srand((int)time(NULL));
lable:
food.y = rand() % (25 - 1 + 1) + 1;
food.x = rand() % (54 - 2 + 1) + 2;
if (food.x % 2 != 0)
{
food.x = food.x+1;
}
snake *judge = head;
while (1)
{
if (judge->next == NULL) break;
if (food.x == judge->x&&food.y == judge->y)
{
goto lable;
}
judge = judge->next;
}
gotoxy(food.x, food.y);
printf("⊙");
}(4) Click the control function
int ClickControl()
{
char c;
while (1)
{
if (Judge()==0) return 0;
if (_kbhit())
{
click = _getch();
}
MovingBody();
Eating();
}
return 1;
}(5) Mobile control
void MovingBody()
{
int count = 0;
int a = head->x, b = head->y;
snake *p = head;
while (1)
{
if (p->next == NULL) break;
gotodelete(p->x, p->y);
count++;
p = p->next;
}
switch (click)
{
case up:
head->y -= 1;
ChangeBody(a,b);
break;
case down:
head->y += 1;
ChangeBody(a,b);
break;
case left:
head->x -= 2;
ChangeBody(a,b);
break;
case right:
head->x += 2;
ChangeBody(a,b);
break;
case stop:
_getch();
break;
}
p = head;
while (1)
{
if (p->next == NULL) break;
gotoprint(p->x, p->y);
p = p->next;
}
p = head;
gotoxy(0, 28);
if (count <= 10) speed = 150;
else if (count > 10 && count <= 20) speed = 100;
else if (count > 20 && count <= 40) speed = 50;
else speed = 10;
Sleep(speed);
}(6) Change the snake
void ChangeBody(int a,int b)
{
snake *p = head->next;
int mid1, mid2,_mid1,_mid2;
mid1 = p->x;
mid2 = p->y;
while (1)
{
if (p->next->next == NULL) break;
_mid1 = p->next->x;
_mid2 = p->next->y;
p->next->x = mid1;
p->next->y = mid2;
mid1 = _mid1;
mid2 = _mid2;
p = p->next;
}
p = head->next;
{
p->x = a;
p->y = b;
}
}3、 ... and 、 Full code download link
link :https://pan.baidu.com/s/1XxCY2XBjKeoKL9q3XKgUpg
Extraction code :6666
边栏推荐
- Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
- [research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
- 1110 complete binary tree (25 points)
- 【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
- [backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
- [Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
- [backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
- Notes | numpy-08 Advanced index
- Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
- appium1.22.x 版本後的 appium inspector 需單獨安裝
猜你喜欢

Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)

Valentine's day limited withdrawal guide: for one in 200 million of you

cookie session jwt

Review the old and know the new: Notes on Data Science

leetcode452. Detonate the balloon with the minimum number of arrows
![[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology](/img/f1/d0dc4dc3fe49a2d2cd9e452a0ce31e.jpg)
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology

Use Sqlalchemy module to obtain the table name and field name of the existing table in the database

Promise

论文阅读_清华ERNIE

JS dynamic table creation
随机推荐
[XSS bypass - protection strategy] understand the protection strategy and better bypass
Market status and development prospect prediction of global colorimetric cup cover industry in 2022
Sprintf formatter abnormal exit problem
论文阅读_中文NLP_ELECTRA
Career planning of counter attacking College Students
JQ style, element operation, effect, filtering method and transformation, event object
Problems encountered in fuzzy query of SQL statements
leetcode452. Detonate the balloon with the minimum number of arrows
1099 build a binary search tree (30 points)
1115 counting nodes in a BST (30 points)
[backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
@RequestMapping
[SQL injection point] location and judgment of the injection point
Market status and development prospects of the global autonomous marine glider industry in 2022
Notes | numpy-09 Broadcast
Go language interface learning notes
Kept hot standby and haproxy
[develop wechat applet local storage with uni app]
Burp suite plug-in based on actual combat uses tips
Market status and development prospect prediction of the global fire extinguisher industry in 2022