当前位置:网站首页>[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
边栏推荐
- 音频焦点系列:手写一个demo理解音频焦点与AudioMananger
- Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
- Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
- Market status and development prospect prediction of the global fire extinguisher industry in 2022
- Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly
- Shuttle + alluxio accelerated memory shuffle take-off
- Messy change of mouse style in win system
- Go language interface learning notes
- [research materials] annual report of China's pension market in 2021 - Download attached
- Notes | numpy-08 Advanced index
猜你喜欢

Celebrate the new year together

Career planning of counter attacking College Students

leetcode435. Non overlapping interval

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

Thesis reading_ Chinese NLP_ ELECTRA

"Hands on deep learning" pytorch edition Chapter II exercise

leetcode452. Detonate the balloon with the minimum number of arrows

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

【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)

Online VR model display - 3D visual display solution
随机推荐
JS string and array methods
Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
Celebrate the new year together
Gbase8s composite index (I)
[luatos sensor] 2 air pressure bmp180
Learn to use the idea breakpoint debugging tool
Handler understands the record
112 stucked keyboard (20 points)
[backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
Interface frequency limit access
1087 all roads lead to Rome (30 points)
Shell script -- condition judgment
cookie session jwt
Distinguish between releases and snapshots in nexus private library
Messy change of mouse style in win system
[backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
Shell script Basics - basic grammar knowledge