Однопользовательская версия: не может проходить сквозь стены
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<graphics.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define NUM 200
#define SIZE 10
#define SPEED 100
#define WIDTH 640
#define HEIGHT 480
enum Ch {
up = 72, down = 80, left = 75, right = 77 };
typedef struct Coor
{
int x;
int y;
}Coor;
typedef struct Snake
{
int n;
Ch ch;
Coor xy[NUM];
}Snake;
Snake snake;
struct Food
{
int x;
int y;
bool iseat;
};
Food food;
void BGM();
void GameInit();
void DrawGame();
void SnakeMove();
void ChangeCh();
void CreateFood();
void EatFood();
int GameOver();
int main()
{
int n;
initgraph(WIDTH, HEIGHT);
BGM();
GameInit();
while (1)
{
while (!_kbhit())
{
SnakeMove();
DrawGame();
CreateFood();
EatFood();
n = GameOver();
if (n)
{
break;
}
Sleep(SPEED);
}
if (n)
{
break;
}
ChangeCh();
}
closegraph();
return 0;
}
void BGM()
{
mciSendString("open ./甩葱歌.mp3 alias BGM", 0, 0, 0);
mciSendString("play BGM repeat", 0, 0, 0);
if (0)
{
mciSendString("close BGM ", 0, 0, 0);
}
}
void GameInit()
{
srand((unsigned int)time(NULL));
snake.n = 3;
snake.ch = right;
snake.xy[0].x = 100;
snake.xy[0].y = 100;
snake.xy[1].x = 90;
snake.xy[1].y = 100;
snake.xy[2].x = 80;
snake.xy[2].y = 100;
food.iseat = true;
}
void DrawGame()
{
cleardevice();
setfillcolor(RED);
fillrectangle(snake.xy[0].x, snake.xy[0].y,
snake.xy[0].x + SIZE, snake.xy[0].y + SIZE);
setfillcolor(GREEN);
for (int i = 1; i < snake.n; i++)
{
fillrectangle(snake.xy[i].x, snake.xy[i].y,
snake.xy[i].x + SIZE, snake.xy[i].y + SIZE);
}
setfillcolor(RGB(253, 168, 198));
fillroundrect(food.x, food.y, food.x + SIZE, food.y + SIZE, SIZE / 2, SIZE / 2);
}
void SnakeMove()
{
for (int i = snake.n - 1; i > 0; i--)
{
snake.xy[i].x = snake.xy[i - 1].x;
snake.xy[i].y = snake.xy[i - 1].y;
}
switch (snake.ch)
{
case up:
snake.xy[0].y -= SIZE;
break;
case down:
snake.xy[0].y += SIZE;
break;
case left:
snake.xy[0].x -= SIZE;
break;
case right:
snake.xy[0].x += SIZE;
break;
}
}
void ChangeCh()
{
char key;
key = _getch();
switch (key)
{
case up:
if (snake.ch != down)
{
snake.ch = up;
}
break;
case down:
if (snake.ch != up)
{
snake.ch = down;
}
break;
case left:
if (snake.ch != right)
{
snake.ch = left;
}
break;
case right:
if (snake.ch != left)
{
snake.ch = right;
}
break;
}
}
void CreateFood()
{
bool flag;
if (food.iseat == true)
{
while (1)
{
flag = false;
food.x = rand() % 64 * 10;
food.y = rand() % 48 * 10;
for (int i = 0; i < snake.n; i++)
{
if (food.x == snake.xy[i].x && food.y == snake.xy[i].y)
{
flag = true;
break;
}
}
if (!flag)
{
food.iseat = false;
break;
}
}
}
}
void EatFood()
{
if (snake.xy[0].x == food.x && snake.xy[0].y == food.y)
{
food.iseat = true;
snake.n++;
}
}
int GameOver()
{
if (snake.xy[0].x == 0-SIZE || snake.xy[0].x > WIDTH || snake.xy[0].y==0-SIZE || snake.xy[0].y==HEIGHT)
{
if (snake.xy[0].x == 0-SIZE)
{
snake.xy[0].x = WIDTH;
}
if (snake.xy[0].x == WIDTH)
{
snake.xy[0].x = 0;
}
if (snake.xy[0].y == 0-SIZE)
{
snake.xy[0].y = HEIGHT;
}
if (snake.xy[0].x == HEIGHT)
{
snake.xy[0].y = 0;
}
}
for (int i = 1; i < snake.n; i++)
{
if (snake.xy[0].x == snake.xy[i].x && snake.xy[0].y == snake.xy[i].y)
return 1;
}
return 0;
}