- 编译环境:VS2017(VS其他版本皆可),EasyX图形库
- 编程语言:c/c++
- 当前版本:snakeGame1.0
- 修改时间:2019/6/13
- 项目组成:
5.1 头文件:snake\snake.h,
5.2 源文件:main.cpp, snake\snake.cpp,
5.3 资源文件:1.mp3, jinbi.jpg, jinbi.mp3, siwang.mp3,
一、头文件:snake\snake.h
#pragma once
#include <time.h>
#include <conio.h>
#include <graphics.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
constexpr auto U = 1;
constexpr auto D = 2;
constexpr auto L = 3;
constexpr auto R = 4;
struct snake
{
int x, y;
int color;
struct snake* next;
};
class snakeGame
{
public:
HWND hWnd;
int score;
int add;
int grade;
int sleeptime;
int endGamestatus;
int status;
snake *head, *food;
snake *q;
IMAGE jinbi;
snakeGame(int score = 0, int add = 10, int grade = 1,
int sleeptime = 150, int endGamestatus = 0);
void initGame();
void drawGame();
void ctolGame();
void initSnake();
void creatMap();
void createFood();
void snakeMove();
int endGame();
int snakeMain();
};
二、源文件:main.cpp
#include "snake\snake.h"
int main()
{
snakeGame Snake;
Snake.snakeMain();
return 0;
}
三、源文件:snake\snake.cpp
#include "snake.h"
snakeGame::snakeGame(int score, int add, int grade, int sleeptime, int endGamestatus)
{
this->score = score;
this->add = add;
this->grade = grade;
this->sleeptime = sleeptime;
this->endGamestatus = endGamestatus;
}
void snakeGame::initGame()
{
creatMap();
initSnake();
createFood();
mciSendString("open snake\\1.mp3", NULL, NULL, NULL);
mciSendString("play snake\\1.mp3 repeat", NULL, NULL, NULL);
loadimage(&jinbi, "snake\\jinbi.jpg", 20, 20, true);
}
void snakeGame::drawGame()
{
char mon[32] = {
NULL };
char vel[32] = {
NULL };
char dif[32] = {
NULL };
char one[32] = {
NULL };
BeginBatchDraw();
setfillcolor(RGB(195, 195, 195));
solidrectangle(0, 0, 680, 680);
setfillcolor(RGB(128, 255, 0));
solidrectangle(20, 20, 660, 660);
setfillcolor(RGB(0, 0, 255));
for (int i = 40; i <= 640; i += 20)
for (int j = 40; j <= 640; j += 20)
solidcircle(i, j, 1);
q = head;
setfillcolor(YELLOW);
solidcircle(q->x + 10, q->y + 10, 10);
q = q->next;
while (q != NULL)
{
setfillcolor(q->color);
solidcircle(q->x + 10, q->y + 10, 10);
q = q->next;
}
putimage(food->x, food->y, &jinbi);
setfillcolor(RGB(0, 0, 0));
solidrectangle(680, 0, 1020, 680);
setbkcolor(RGB(0, 0, 0));
_itoa_s(score, mon, 10);
_itoa_s((10000 / sleeptime), vel, 10);
_itoa_s((300 - sleeptime) / 30, dif, 10);
_itoa_s(add, one, 10);
setcolor(RGB(255, 255, 255));
outtextxy(730, 10, "金币:"); outtextxy(775, 10, mon);
outtextxy(730, 30, "速度:"); outtextxy(775, 30, vel);
outtextxy(730, 60, "难度:"); outtextxy(775, 60, dif);
outtextxy(730, 80, "单价:"); outtextxy(775, 80, one);
outtextxy(700, 150, "不能穿墙,不能咬到自己!");
outtextxy(700, 200, "控制方向:");
outtextxy(800, 200, "↑↓←→");
outtextxy(700, 250, "加速:F1");
outtextxy(780, 250, "减速:F2");
outtextxy(700, 270, "退出:Esc");
outtextxy(780, 270, "暂停:Space");
EndBatchDraw();
}
void snakeGame::ctolGame()
{
if (GetAsyncKeyState(VK_UP) && status != D)
{
status = U;
}
else if (GetAsyncKeyState(VK_DOWN) && status != U)
{
status = D;
}
else if (GetAsyncKeyState(VK_LEFT) && status != R)
{
status = L;
}
else if (GetAsyncKeyState(VK_RIGHT) && status != L)
{
status = R;
}
else if (GetAsyncKeyState(VK_SPACE))
{
while (1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE))
{
break;
}
}
}
else if (GetAsyncKeyState(VK_ESCAPE))
{
while (1)
{
if (!GetAsyncKeyState(VK_ESCAPE))
{
endGamestatus = 3;
endGame();
break;
}
}
}
else if (GetAsyncKeyState(VK_F1))
{
while (1)
{
if (!GetAsyncKeyState(VK_F1))
{
if (sleeptime > 60)
{
sleeptime = sleeptime - 30;
add = add + 2;
}
break;
}
}
}
else if (GetAsyncKeyState(VK_F2))
{
while (1)
{
if (!GetAsyncKeyState(VK_F2))
{
if (sleeptime < 270)
{
sleeptime = sleeptime + 30;
add = add - 2;
}
break;
}
}
}
Sleep(sleeptime);
snakeMove();
}
void snakeGame::initSnake()
{
snake *tail;
int i;
tail = (snake*)malloc(sizeof(snake));
tail->x = 200;
tail->y = 40;
tail->color = RGB(255, 0, 0);
tail->next = NULL;
for (i = 1; i <= 4; i++)
{
head = (snake*)malloc(sizeof(snake));
head->next = tail;
head->x = 200 + 20 * i;
head->y = 40;
head->color = RGB(255, 0, 0);
tail = head;
}
status = R;
}
void snakeGame::creatMap()
{
hWnd = initgraph(1020, 680);
MessageBox(hWnd, "欢迎来到贪吃蛇游戏!", "帅气的龙少提示你:", MB_OK);
outtextxy(320, 260, "用↑.↓.←.→分别控制蛇的移动, F1 为加速,F2为减速");
outtextxy(360, 280, "加速将能得到更高的分数");
getch();
cleardevice();
}
void snakeGame::createFood()
{
snake *food_1;
srand((unsigned)time(NULL));
food_1 = (snake*)malloc(sizeof(snake));
if (food_1 == NULL) return createFood();
do
{
food_1->x = rand() % 620 + 20;
food_1->y = rand() % 620 + 20;
} while ((food_1->x % 20 != 0) || (food_1->y % 20 != 0));
food_1->color = RGB(rand() % 256, rand() % 256, rand() % 256);
q = head;
while (q)
{
if (q->x == food_1->x && q->y == food_1->y)
{
free(food_1);
createFood();
return;
}
q = q->next;
}
food = food_1;
}
void snakeGame::snakeMove()
{
snake * nexthead = (snake*)malloc(sizeof(snake));
if (nexthead == NULL) return snakeMove();
if (status == U)
{
nexthead->x = head->x;
nexthead->y = head->y - 20;
nexthead->color = RGB(255, 0, 0);
if (nexthead->x == food->x && nexthead->y == food->y)
{
mciSendString("open snake\\jinbi.mp3 alias media", NULL, NULL, NULL);
mciSendString("set media time format milliseconds", NULL, NULL, NULL);
mciSendString("play media from 0", NULL, NULL, NULL);
nexthead->next = head;
head = nexthead;
q = head;
while (q != NULL)
{
q = q->next;
}
score = score + add;
createFood();
}
else
{
nexthead->next = head;
head = nexthead;
q = head;
while (q->next->next != NULL)
{
q = q->next;
}
free(q->next);
q->next = NULL;
}
}
if (status == D)
{
nexthead->x = head->x;
nexthead->y = head->y + 20;
nexthead->color = RGB(255, 0, 0);
if (nexthead->x == food->x && nexthead->y == food->y)
{
mciSendString("open snake\\jinbi.mp3 alias media", NULL, NULL, NULL);
mciSendString("set media time format milliseconds", NULL, NULL, NULL);
mciSendString("play media from 0", NULL, NULL, NULL);
nexthead->next = head;
head = nexthead;
q = head;
while (q != NULL)
{
q = q->next;
}
score = score + add;
createFood();
}
else
{
nexthead->next = head;
head = nexthead;
q = head;
while (q->next->next != NULL)
{
q = q->next;
}
free(q->next);
q->next = NULL;
}
}
if (status == L)
{
nexthead->x = head->x - 20;
nexthead->y = head->y;
nexthead->color = RGB(255, 0, 0);
if (nexthead->x == food->x && nexthead->y == food->y)
{
mciSendString("open snake\\jinbi.mp3 alias media", NULL, NULL, NULL);
mciSendString("set media time format milliseconds", NULL, NULL, NULL);
mciSendString("play media from 0", NULL, NULL, NULL);
nexthead->next = head;
head = nexthead;
q = head;
while (q != NULL)
{
q = q->next;
}
score = score + add;
createFood();
}
else
{
nexthead->next = head;
head = nexthead;
q = head;
while (q->next->next != NULL)
{
q = q->next;
}
free(q->next);
q->next = NULL;
}
}
if (status == R)
{
nexthead->x = head->x + 20;
nexthead->y = head->y;
nexthead->color = RGB(255, 0, 0);
if (nexthead->x == food->x && nexthead->y == food->y)
{
mciSendString("open snake\\jinbi.mp3 alias media", NULL, NULL, NULL);
mciSendString("set media time format milliseconds", NULL, NULL, NULL);
mciSendString("play media from 0", NULL, NULL, NULL);
nexthead->next = head;
head = nexthead;
q = head;
while (q != NULL)
{
q = q->next;
}
score = score + add;
createFood();
}
else
{
nexthead->next = head;
head = nexthead;
q = head;
while (q->next->next != NULL)
{
q = q->next;
}
free(q->next);
q->next = NULL;
}
}
snake *self;
self = head->next;
while (self)
{
if (self->x == head->x && self->y == head->y)
{
endGamestatus = 2;
endGame();
}
self = self->next;
}
if (head->x < 20 || head->x > 640 || head->y < 20 || head->y > 640)
{
endGamestatus = 1;
endGame();
}
}
int snakeGame::endGame()
{
mciSendString("stop snake\\1.mp3", NULL, NULL, NULL);
mciSendString("open snake\\siwang.mp3", NULL, NULL, NULL);
mciSendString("play snake\\siwang.mp3", NULL, NULL, NULL);
switch (endGamestatus)
{
case 1: MessageBox(hWnd, "对不起,您撞到墙了,结束游戏!", "帅气的龙少提示你:", NULL); break;
case 2: MessageBox(hWnd, "对不起,您咬到自己了,结束游戏!", "帅气的龙少提示你:", NULL); break;
case 3: MessageBox(hWnd, "您的游戏已经结束了!", "帅气的龙少提示你:", NULL); break;
}
closegraph();
exit(0);
}
int snakeGame::snakeMain()
{
initGame();
while (1)
{
drawGame();
ctolGame();
}
return 0;
}
四、资源文件
1、1.mp3
百度网盘提取码:xpdm
2、jinbi.jpg
百度网盘提取码:pq7b
3、jinbi.mp3
百度网盘提取码:sow8
4、siwang.mp3
百度网盘提取码:xc1n