当前位置:网站首页>[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
边栏推荐
- 动态规划——相关概念,(数塔问题)
- Analysis of proxy usage of ES6 new feature
- Based on RFC 3986 (unified resource descriptor (URI): general syntax)
- Shuttle + alluxio accelerated memory shuffle take-off
- The process of browser accessing the website
- Source insight garbled code solution
- Distinguish between releases and snapshots in nexus private library
- 1110 complete binary tree (25 points)
- [set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)
- 1099 build a binary search tree (30 points)
猜你喜欢
Keepalived热备与HAProxy
Concurrent operation memory interaction
cookie session jwt
Source insight garbled code solution
Thesis reading_ Tsinghua Ernie
Basic use of Metasploit penetration testing framework
On typescript and grammar
Shuttle + Alluxio 加速内存Shuffle起飞
[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)
Unity tool Luban learning notes 1
随机推荐
Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
sql语句模糊查询遇到的问题
论文阅读_清华ERNIE
1099 build a binary search tree (30 points)
MPM model and ab pressure test
Shell script -- condition judgment
Learning record of arouter principle
Sprintf formatter abnormal exit problem
Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
Market status and development prospect prediction of the global fire hose industry in 2022
JS function algorithm interview case
Thesis reading_ ICD code_ MSMN
What is UUID
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
1110 complete binary tree (25 points)
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
Three representations of signed numbers: original code, inverse code and complement code
Basic knowledge of reflection (detailed explanation)
Valentine's day limited withdrawal guide: for one in 200 million of you
论文阅读_ICD编码_MSMN