当前位置:网站首页>C language greedy snake
C language greedy snake
2022-06-22 23:17:00 【Xiaomurong】
One 、 explain
- Compile environment :vs 2019
- Need to install eazyx( Just a few seconds )
- Code comments are detailed
- Imaging diagram

Two 、 Production ideas
- Interface
size 、 Color - Snake initialization
Snake shape 、 length , Appear on the map , Which direction to move in the first place - The random appearance of food
Sow :srand((unsigned int)time(NULL));
Random function :rand() - The action of the snake ( Move and eat food )
WASD
You can't go in the opposite direction to yourself , Such as :
When you go to the right, you can't go to the left - Snake death conditions
Collision boundary 、 Hit yourself - Calculation of score
Eating food scores , Print on the interface - How to draw a snake 、 Painting food
3、 ... and 、 Code
// Cancel... In code Unicode Coded macro definitions , Let subsequent compilations begin with MBCS Code for
#undef UNICODE
#undef _UNICODE
#include<stdio.h>
#include<conio.h> // _getch _kbhit
#include<time.h>
#include<graphics.h> // To be installed easyX
// Interface size , It can be modified directly
#define M 600
#define N 400
typedef struct {
int x, y;
}point;// coordinate xy, Slightly different from the coordinates of Mathematics
struct snake {
point xy[100];
int position;
int lenth;
}snake;
struct food {
int flag = 0;// Determine whether food exists
point fdxy;
int grade = 0;
}food;
enum position {
up, down, left, right };// enumeration
// The snake , Initialize the location of the snake
void startsnake()
{
// Snakehead
snake.xy[0].x = 20;
snake.xy[0].y = 0;
snake.xy[1].x = 10;
snake.xy[1].y = 0;
snake.xy[2].x = 0;
snake.xy[2].y = 0;
// Snake initialization direction
snake.position = right;
snake.lenth = 3;
}
// Draw a snake ( To install easy_X), The colors change all the time
void drawsnake()
{
for (int i = 0; i < snake.lenth; i++)
{
setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
fillrectangle(snake.xy[i].x, snake.xy[i].y, snake.xy[i].x + 10, snake.xy[i].y + 10);
}
}
void movesnake()
{
// The snake moves forward one space
for (int i = snake.lenth - 1; i > 0; i--)
{
snake.xy[i].x = snake.xy[i - 1].x;
snake.xy[i].y = snake.xy[i - 1].y;
}
// Snake head moves
switch (snake.position)
{
case up:
snake.xy[0].y = snake.xy[0].y - 10; break;
case down:
snake.xy[0].y = snake.xy[0].y + 10; break;
case left:
snake.xy[0].x = snake.xy[0].x - 10; break;
case right:
snake.xy[0].x = snake.xy[0].x + 10; break;
}
}
// Randomly generate food
void showfood()
{
food.fdxy.x = rand() % (M/10) * 10;//0~590
food.fdxy.y = rand() % (N/10) * 10;//0~390
// The loop is used to determine whether it coincides with the snake
for (int i = 0; i < snake.lenth; i++)
{
if (snake.xy[i].x == food.fdxy.x && snake.xy[i].y == food.fdxy.y)
{
food.fdxy.x = rand() % 60 * 10;
food.fdxy.y = rand() % 40 * 10;
}
}
}
//
void drawfood()
{
setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
fillrectangle(food.fdxy.x, food.fdxy.y, food.fdxy.x + 10, food.fdxy.y + 10);
}
void eatfood()
{
if (snake.xy[0].x == food.fdxy.x && snake.xy[0].y == food.fdxy.y)
{
snake.lenth++;
food.flag = 0;// Look here main function
food.grade += 10;
}
}
// Keyboard operation
void keydown()
{
char dqown = _getch();
switch (dqown)
{
case 'W':
case 'w':
if (snake.position != down)
snake.position = up;
break;
case 'A':
case 'a':
if (snake.position != right)
snake.position = left;
break;
case 'S':
case 's':
if (snake.position != up)
snake.position = down;
break;
case 'D':
case 'd':
if (snake.position != left)
snake.position = right;
break;
case '9':Sleep(5000);// Press down 9 Pause 5 second ( You can change it yourself )
}
}
void showgrade()
{
char Grade[20] = "";
sprintf_s(Grade, "grade:%d", food.grade);
outtextxy(250, 20, Grade);
}
// Hit the wall and die , Touch yourself and never die
void dead()
{
if (snake.xy[0].x == M || snake.xy[0].x < 0 || snake.xy[0].y < 0 || snake.xy[0].y == N)
{
char over[20] = "Game Over!";
outtextxy(250, 50, over);
system("pause");
exit(0);
}
}
int main()
{
srand((unsigned int)time(NULL));// Sow
initgraph(M, N); // Screen size , It can be changed at will
setbkcolor(RGB(110, 120, 119));// The background color
// Initialize snake
startsnake();
drawsnake();
while (1)
{
cleardevice();// Clear the screen
movesnake();
drawsnake();
if (food.flag == 0)// Determine whether food is produced
{
showfood();
food.flag = 1;
}
drawfood();
if (_kbhit())// Determine whether the keyboard operation
{
keydown();
}
eatfood();
showgrade();
dead();
Sleep(100);// This can be regarded as the moving speed of the snake
}
return 0;
}
边栏推荐
猜你喜欢

three.js模拟驾驶游览艺术展厅---打造超级相机控制器

企业数字化不是各自发展,而是全面SaaS化推进

2021-04-14

C sqlsugar, hisql, FreeSQL ORM framework all-round performance test vs. sqlserver performance test

In the middle of the year, we will promote the integration of worry free, and the value-added package will be reduced by 6

2021-08-21

2021-04-05

2021-04-16

SOA Service Oriented Architecture

Three cache methods and principles
随机推荐
2021-04-05
Spark SQL Start(2.4.3)
C language -- 17 function introduction
【STM32技巧】使用STM32 HAL库的硬件I2C驱动RX8025T实时时钟芯片
Greedy interval problem (2)
Do domestic mobile phones turn apples? It turned out that it was realized by 100 yuan machine and sharp price reduction
2021-08-22
Remote access and control - SSH Remote Management and TCP wrappers access control
Zynq ultrascale + rfsoc zcu111 RF clock tree learning 1
js判断浏览器是否打开了控制台
获取当前所在周的起始和结束的日期
Lua -- iterator, module, meta table
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
Codeup longest palindrome substring
Phantomjs实用代码段(持续更新中……)
Greedy interval problem (4)
2021-08-21
Introduction to database access tools
多种方案实现图片的懒加载
Greedy interval problem (1)