当前位置:网站首页>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
     The color of the snake and its food changes constantly

Two 、 Production ideas

  1. Interface
    size 、 Color
  2. Snake initialization
    Snake shape 、 length , Appear on the map , Which direction to move in the first place
  3. The random appearance of food
    Sow :srand((unsigned int)time(NULL));
    Random function :rand()
  4. 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
  5. Snake death conditions
    Collision boundary 、 Hit yourself
  6. Calculation of score
    Eating food scores , Print on the interface
  7. 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;
}

原网站

版权声明
本文为[Xiaomurong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221502449424.html