当前位置:网站首页>[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
边栏推荐
- 1110 complete binary tree (25 points)
- leetcode860. Lemonade change
- [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)
- leetcode406. Rebuild the queue based on height
- Messy change of mouse style in win system
- 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
- Self introduction and objectives
- JS function algorithm interview case
- Market status and development prospect forecast of global button dropper industry in 2022
- 【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
猜你喜欢
Flutter monitors volume to realize waveform visualization of audio
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
String matching: find a substring in a string
M1 Pro install redis
Shallow and first code
Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
Review the configuration of vscode to develop golang
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
[set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)
随机推荐
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
The 19th Zhejiang I. barbecue
1095 cars on campus (30 points)
Three representations of signed numbers: original code, inverse code and complement code
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
[SQL injection point] location and judgment of the injection point
[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)
String matching: find a substring in a string
Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
@RequestMapping
Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
Celebrate the new year together
Valentine's day limited withdrawal guide: for one in 200 million of you
Learn to use the idea breakpoint debugging tool
Market status and development prospect prediction of global SoC Test Platform Industry in 2022
My first Smartphone
Do you know UVs in modeling?
Notes | numpy-11 Array operation
Online VR model display - 3D visual display solution
动态规划——相关概念,(数塔问题)