C Language Project [1] | ——— |Snake Snake Snake Source + Explanation

Posted by daniel_grant on Sat, 18 May 2019 06:12:04 +0200

Retro Snaker

PS: This snake is achieved by using an array of structs!

Snake Eager Project Development Platform:

VS2017+ Graphics Library

1. Add Graphics Library in VS2017 as follows

How do I add a graphics.h header file in VS2017?

2. Framework of snakes

  • 1. Snake Properties

Snake properties include length of snake + coordinates of snake + direction of snake

  • 2. Properties of food

Coordinations of food

  • 3. Initialization of snakes

* Orientation and length of the snake at the beginning of initialization

  • 4. Draw snakes and food in Windows

Draw snakes and food using functions included in the graphics library

  • 5. Snake movement and random appearance of food

Make use of changes in snake coordinates in four directions to move the snake, and use random numbers to make food appear randomly

  • 6. Snake direction

Using the _getch() input to determine and change the direction of the snake

  • 7. Conditions of Snake Death

Use conditional statements to determine whether a snake hits a wall or bites its tail

  • 8. Pause, continue and exit

Make use of _getch() input to pause, resume, or exit the game!

  • 9. There are many more features to DIY!

Code moment:

Before you paste the code, there is a note:

#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")

//Initialize music
//PlaySound();//WAV
mciSendString("open unreachable you.mp3 alias BGM", 0, 0); //Send string messages to the underlying sound card driver
mciSendString("play BGM", 0, 0, 0);

These codes are API interfaces for playing music

Unreachable You.mp3 This file is the file that plays music. You can download my demo video and watch it to see what's going on.

Video link: https://pan.baidu.com/s/1BEge-PZxsMvZ17blgvg8qw
Pick up code: yie0

snack.h file

#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")

//Maximum length of snake
#define SNACK_LENTH 500

//direction
enum Ch {
	up = 72,
	down = 80,
	left = 75,
	right = 77,
	backspase = 32,
	esc = 27
};
//coordinate
struct Coor {
	int x;
	int y;
};
struct Snack
{
	//The attributes of a snake
	int n;
	Coor szb[SNACK_LENTH];
	Ch ch;
}snack; 

struct Food {
	Coor fzb;
	int flag;
}food;


void InitSnack();
void DrawSnack();
void MoveSnack();
void ChangeSnackCh();
void CoorFood();
void DrawFood();
void EatFood();
void ExitSystem();

snack.cpp file

#include "snack.h" //reference graphics library header file
int move;
int main()
{
	InitSnack();//1. Initialize snakes
	/*
		What is the program doing before the keys are pressed?
			Move + Draw (Loop)
			//Key
		Change the direction of the snake. After the key
			Move + Draw (Loop)
	*/
	while (1) {
		while (!_kbhit()) 
		{
			if (food.flag==0) {
				CoorFood();
			}
			cleardevice();
			MoveSnack();
			DrawFood();	
			DrawSnack();
			EatFood();
			ExitSystem();
			Sleep(100);
		}
		ChangeSnackCh();
	}
	system("pause");
	return 0;
}

void InitSnack() {
	//Initialization window
	initgraph(640,480);
	srand((unsigned int)time(NULL));

	//Initialize music
	//PlaySound();//WAV
	mciSendString("open You're too far away.mp3 alias BGM", 0, 0, 0);//Send string message to underlying sound card driver
	mciSendString("play BGM", 0, 0, 0);
	//Initialize Snake
	snack.n = 3;
	snack.ch = right;
	snack.szb[0].x = 100;
	snack.szb[0].y = 100;
	snack.szb[1].x = 90;
	snack.szb[1].y = 100;
	snack.szb[2].x = 80;
	snack.szb[2].y = 100;
	/*
	snack.szb[0].x = 20;
	snack.szb[0].y = 0;
	snack.szb[1].x = 10;
	snack.szb[1].y = 0;
	snack.szb[2].x = 0;
	snack.szb[2].y = 0;
	*/
}

void DrawSnack() {
	setlinecolor(GREEN);//Line color
	setfillcolor(BLUE);//Filled Color

	//Traverse through the array to access the values of each element in the array and draw a rectangle for each element
	for (int i = 0;i < snack.n;i++) {
		fillrectangle(snack.szb[i].x, snack.szb[i].y,
			snack.szb[i].x + 10, snack.szb[i].y + 10);
	}

}

void MoveSnack() {
	for (int i = snack.n - 1;i > 0;i--) {
		snack.szb[i].x = snack.szb[i - 1].x;
		snack.szb[i].y = snack.szb[i - 1].y;
	}

	switch (snack.ch) {
	case up:
		snack.szb[0].y -= 10;
		break;
	case down:
		snack.szb[0].y += 10;
		break;
	case left:
		snack.szb[0].x -= 10;
		break;
	case right:
		snack.szb[0].x += 10;
		break;
	}
}

void ChangeSnackCh() {
	move = _getch();
	switch (move) 
	{
	case up:
		if (snack.ch != down)
			snack.ch = up;
		break;
	case down:
		if (snack.ch != up)
			snack.ch = down;
		break;
	case left:
		if (snack.ch != right)
			snack.ch = left;
		break;
	case right:
		if (snack.ch != left)
			snack.ch = right;
		break;
	}
}

void CoorFood() {
	food.fzb.x = rand() % 64 * 10;
	food.fzb.y = rand() % 48 * 10;

	food.flag = 1;
}

void DrawFood() {
	fillroundrect(food.fzb.x, food.fzb.y, food.fzb.x + 10, food.fzb.y + 10,10,10);
}

void EatFood() {
	if (snack.szb[0].x == food.fzb.x&&snack.szb[0].y == food.fzb.y) {
		snack.n++;
		food.flag = 0;
	}
	else if (snack.szb[0].x == 0 || snack.szb[0].x + 10 == 640 || snack.szb[0].y == 0 || snack.szb[0].y + 10 == 480) {
		outtextxy(150, 240, "Against the wall,Game Over!");
		system("pause");
		exit(0);
	}
	for (int i = 3;i < snack.n;i++) {
		if ((snack.szb[0].y == snack.szb[i].y + 10 && snack.szb[0].x == snack.szb[i].x&&snack.ch == down) || (snack.szb[0].y == snack.szb[i].y + 10 && snack.szb[0].x == snack.szb[i].x&& snack.ch == up) || (snack.szb[0].x == snack.szb[i].x + 10 && snack.szb[0].y == snack.szb[i].y&& snack.ch == right) || (snack.szb[0].x == snack.szb[i].x + 10 && snack.szb[0].y == snack.szb[i].y&& snack.ch == left)) {
			outtextxy(150, 240, "Bite into your own body,Game Over!");
			system("pause");
			exit(0);
		}
	}
	 
}

void ExitSystem() {
	switch (move) {
	case backspase:
		//Press the space bar to pause, press the non-space bar to continue!
		outtextxy(150, 240, "Pause, press any key to continue!");
		_getch();
		break;
	case esc:
		outtextxy(150, 240, "Exit System,thanks you play!");
		system("pause");
		exit(0);
	}
}

PS: This is the basic version of the snake. What can be changed is the effect of realization, what can not be changed is the thought of realization.

The best way to understand code is to hit it several times and understand what it does

In addition, today I know that POSITIVE ATOM is coming out of the development board of the Linux series. I'm looking forward to it!!!!

Topics: Programming Windows Linux