当前位置:网站首页>[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
边栏推荐
- 1114 family property (25 points)
- Distinguish between releases and snapshots in nexus private library
- Notes | numpy-08 Advanced index
- Three representations of signed numbers: original code, inverse code and complement code
- The 19th Zhejiang I. barbecue
- ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
- @RequestMapping
- Career planning of counter attacking College Students
- M1 Pro install redis
- Basic knowledge of reflection (detailed explanation)
猜你喜欢

Gbase8s unique index and non unique index

Review the configuration of vscode to develop golang

SSM framework integration

How to connect the network: Chapter 1 CSDN creation punch in

Silent authorization login and registration of wechat applet

appium1.22.x 版本后的 appium inspector 需单独安装

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

Shallow and first code

Celebrate the new year together
![[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)](/img/af/8dfa783c87363a9d75c52e7680d508.jpg)
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
随机推荐
Basic use of Metasploit penetration testing framework
1094 the largest generation (25 points)
Market status and development prospect prediction of the global fire extinguisher industry in 2022
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
Gbase8s composite index (I)
Notes | numpy-11 Array operation
Market status and development prospect forecast of global button dropper industry in 2022
[research materials] 2021 China's game industry brand report - Download attached
The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
Shallow and first code
Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
The process of browser accessing the website
cookie session jwt
1107 social clusters (30 points)
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
Shell script -- condition judgment
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
On typescript and grammar
Market status and development prospects of the global automatic tea picker industry in 2022