废话不多说,直接上代码
#include “stdafx.h”
#include “graphics.h”
#include “stdlib.h”
#include “time.h”
#include “conio.h”
#include “mmsystem.h”
#include “windows.h”
#include “resource.h”
#pragma comment(lib,“winmm.lib”)
#define MAX_NUM 100
IMAGE BackImg;
IMAGE FoodImg;
IMAGE headSk;
IMAGE playBImg;
IMAGE ButtonImg1;
IMAGE ButtonImg2;
MOUSEMSG msg;
char str[10];
HANDLE hThread1, hThread2;
HANDLE hThread3;
int kBoard;
char ch = ‘0’; //0: 游戏正常运行 1:蛇死亡 2:游戏暂停
int grade = 1; //1:自由模式 2:经典模式 3:挑战模式
typedef struct CoorSnake
{
int x;
int y;
}COOR;
typedef struct Snake
{
COOR skC[MAX_NUM]; //每一节蛇的坐标
int n; //蛇当前的节数
int dir; //蛇的方向, 72上 80下 75左 77右
}SNAKE;
struct Food
{
COOR fdC;
int flag = 0; //初始化为没有食物
}food; //思考:多个食物
SNAKE snake;
void welcome();
void show();
DWORD WINAPI ThreadProc1(LPVOID lpParameter);
DWORD WINAPI ThreadProc2(LPVOID lpParameter);
void drawSnake();
void oprate();
void moveSnake();
void foodShow();
void foodEat();
void deathSnake();
void gradeGame();
DWORD WINAPI ThreadProc3(LPVOID lpParameter);
//游戏模式:1.自由模式; 2.经典模式 3.挑战模式
//更换游戏地图 更换游戏BGM 更改小蛇速度(更改游戏难度) 蛇的长度
void gradeGame()
{
InputBox(str, 2, “选择游戏等级[1][2][3]”, 0, 0, 200, 100, false);
switch (str[0])
{
case ‘1’:
grade = 1;
setbkcolor(RGB(125, 234, 50));
cleardevice();
outtextxy(200, 200, “难度为①”);
break;
case ‘2’:
grade = 2;
setbkcolor(RGB(125, 234, 50));
cleardevice();
outtextxy(200, 200, “难度为②”);
break;
case ‘3’:
grade = 3;
setbkcolor(RGB(125, 234, 50));
cleardevice();
outtextxy(200, 200, “难度为③”);
break;
}
outtextxy(200, 250, “空格键继续”);
while(kBoard != 32);
}
void foodShow()
{
if (food.flag == 0)
{
do
{
srand((unsigned int)time(NULL));
food.fdC.x = rand() % (640 / 10) * 10; //0 1 2 3 4 5 6 … 0 10 20 30 40
food.fdC.y = rand() % (480 / 10) * 10;
for (int i = 1; i < snake.n; i++)
{
if (food.fdC.x != snake.skC[i].x&& food.fdC.y != snake.skC[i].y)
{
food.flag = 1;
}
else
{
food.flag = 0;
break;
}
}
} while (food.flag != 1); //避免食物生成在蛇身上
}
putimage(food.fdC.x, food.fdC.y, &FoodImg);
//setfillstyle(image);
//fillroundrect(food.fdC.x, food.fdC.y, food.fdC.x + 8, food.fdC.y + 8, 8, 8);
}
void foodEat()
{
if (snake.skC[0].x >= food.fdC.x&& snake.skC[0].x <= food.fdC.x + 8&& snake.skC[0].y >= food.fdC.y &&snake.skC[0].y <= food.fdC.y + 8)
{
food.flag = 0;
snake.n++;
}
}
void deathSnake()
{
for (int i = 1; i < snake.n; i++)
{
if (snake.skC[0].x == snake.skC[i].x&& snake.skC[0].y == snake.skC[i].y)
{
ch = ‘1’; //表示游戏停止或结束
settextstyle(40, 0, “华文行楷”);
outtextxy(140, 200, “你咬到自己了⊙﹏⊙b”);
outtextxy(120, 240, “::><::游戏结束::><::”);
settextstyle(40, 0, “华文彩云”);
outtextxy(100, 70, “按空格键重玩,ESC退出”);
while (ch == ‘1’)
{
i = _getch();
if (i == 27)
{
PlaySound(NULL, NULL, NULL);
closegraph();
system(“close”);
}
else if (i == 32)
{
ch = ‘0’; //游戏继续
break;
}
}
show();
}
} //判断蛇是否咬到自己
if (grade == 1)
{
if (snake.skC[0].x >= 640) snake.skC[0].x = 0;
else if (snake.skC[0].x < 0) snake.skC[0].x = 640;
else if (snake.skC[0].y < 0) snake.skC[0].y = 480;
else if (snake.skC[0].y >= 480) snake.skC[0].y = 0;
//putimage(snake.skC[0].x, snake.skC[0].y, &headSk);
} //自由模式
else if (grade == 2)
{
if (snake.skC[0].x >= 640 || snake.skC[0].x < 0 || snake.skC[0].y < 0 || snake.skC[0].y >= 480)
{
ch = ‘1’; //表示游戏停止或结束
settextstyle(40, 0, “华文行楷”);
outtextxy(140, 200, “你撞到墙了⊙﹏⊙b”);
outtextxy(120, 240, “::><::游戏结束::><::”);
settextstyle(40, 0, “华文彩云”);
outtextxy(100, 70, “按空格键重玩,ESC退出”);
int i;
while (ch == ‘1’)
{
i = _getch();
if (i == 27)
{
PlaySound(NULL, NULL, NULL);
closegraph();
system(“close”);
}
else if (i == 32)
{
ch = ‘0’; //游戏继续
break;
}
}
show();
}
}
}
void drawSnake()
{
int i;
setlinecolor(RGB(200, 100, 140));
setfillcolor(RGB(200, 100, 140));
fillroundrect(snake.skC[0].x, snake.skC[0].y, snake.skC[0].x + 10, snake.skC[0].y + 10, 10, 10);
setlinecolor(RGB(100, 50, 80));
setfillcolor(RGB(100, 50, 80));
fillroundrect(snake.skC[0].x + 2, snake.skC[0].y + 2, snake.skC[0].x + 4, snake.skC[0].y + 4, 2, 2);
fillroundrect(snake.skC[0].x + 6, snake.skC[0].y + 6, snake.skC[0].x + 8, snake.skC[0].y + 8, 2, 2);
for (i = 1; i < snake.n; i++)
{
setlinecolor(RGB(215, 115, 155));
setfillcolor(RGB(215, 115, 155));
fillroundrect(snake.skC[i].x, snake.skC[i].y, snake.skC[i].x + 10, snake.skC[i].y + 10, 5, 5);
}
setlinecolor(RGB(180, 80, 120));
setfillcolor(RGB(180, 80, 120));
fillroundrect(snake.skC[i].x, snake.skC[i].y+2, snake.skC[i].x + 12, snake.skC[i].y + 8, 6, 6);
}
void oprate()
{
kBoard = _getch();
switch (kBoard)
{
case 72:
if (snake.dir != 80)
{
snake.dir = 72;
}
break;
case 80:
if (snake.dir != 72)
{
snake.dir = 80;
}
break;
case 77:
if (snake.dir != 75)
{
snake.dir = 77;
}
break;
case 75:
if (snake.dir != 77)
{
snake.dir = 75;
}
break;
case 32:
//暂停
ch = ‘2’; //表示暂停
system(“pause”);
ch = ‘0’; //表示继续
break;
}
Sleep(18); //避免快速按键导致坐标重叠
}
void moveSnake()
{
//hThread3 = CreateThread(0, NULL, ThreadProc3, 0, 0, 0);
for (int i = snake.n; i > 0; i–)
{
snake.skC[i] = snake.skC[i - 1];
}
switch (snake.dir)
{
//上
case 72:
snake.skC[0].y -= 10;
break;
//下
case 80:
snake.skC[0].y += 10;
break;
//左
case 75:
snake.skC[0].x -= 10;
break;
//右
case 77:
snake.skC[0].x += 10;
break;
}
while (ch == ‘2’)
{
settextstyle(60, 0, “华文彩云”);
outtextxy(70, 200, “游戏暂停,任意键继续”);
}
}
void welcome()
{
//mciSendString(“open BGM.mp3 alias bgm”, 0, 0, 0);
//mciSendString(“play bgm repeat”, 0, 0, 0);
PlaySound((LPCSTR)IDR_WAVE1, NULL, SND_RESOURCE| SND_ASYNC | SND_LOOP);
initgraph(640, 480);
loadimage(&BackImg, “IMAGE”, MAKEINTRESOURCE(IDR_IMAGE1), 640, 480);
putimage(0, 0, &BackImg);
}
DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{
setbkmode(TRANSPARENT);
for (int r = 0, g = 0, b = 0, c = 0, ch = 1, fx = 620;; r += 15, b += 15, ch++)
{
settextcolor(RGB(r, b, g));
settextstyle(30, 0, “华文彩云”);
outtextxy(fx, 0, “欢迎来到贪吃蛇小游戏O(∩_∩)O”);
settextcolor(RGB(g, r, b));
settextstyle(40, 0, “华文彩云”);
outtextxy(fx, 400, “O(∩_∩)O制作:PorYoung”);
//以上为字幕特效设置,以下为位置变化效果设置
//settextcolor(RGB(r, g, b));
//settextstyle(50, 0, “华文彩云”);
//outtextxy(160, 210, “开始游戏”);
//Sleep(1000);
//cleardevice();
//putimage(0, 0, &BackImg);
settextcolor(RGB(r, g, b));
settextstyle(50, 0, “华文行楷”);
outtextxy(200, 230, “开始游戏”);
Sleep(500);
cleardevice();
putimage(0, 0, &BackImg);
if (r >= 250)
{
r = 0;
b = 0;
g = 20;
}
if (ch % 2 == 0)
{
g += 15;
}
if (fx <= -420) c = 1;
else if (fx >= 610) c = 0;
if (c == 0) fx -= 20;
else fx += 20;
}
}
DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
while (1)
{
msg = GetMouseMsg();
if (msg.x >= 200 && msg.x <= 400 && msg.y >= 230 && msg.y <= 280)
{
if (msg.mkLButton)
{
//终结线程1
TerminateThread(hThread1, 1);
CloseHandle(hThread1);
//进入主界面
gradeGame();
show();
}
}
}
}
DWORD WINAPI ThreadProc3(LPVOID lpParamrter)
{
loadimage(&ButtonImg1, “JPG”, MAKEINTRESOURCE(IDR_JPG1), 20, 20);
loadimage(&ButtonImg2, “JPG”, MAKEINTRESOURCE(IDR_JPG2), 20, 20);
while (1)
{
putimage(300, 450, &ButtonImg1);
putimage(340, 450, &ButtonImg2);
}
}
void show()
{
loadimage(&FoodImg, “IMAGE”, MAKEINTRESOURCE(IDR_IMAGE2), 10, 10);
//loadimage(&headSk, “IMAGE”, MAKEINTRESOURCE(IDR_IMAGE3), 10, 10);
hThread3 = CreateThread(NULL, 0, ThreadProc3, NULL, 0, 0);
//初始化数据
snake.dir = 77;
snake.n = 2;
snake.skC[0].x = 0;
snake.skC[0].y = 0;
//开启boardGet函数获取键盘信息
ch = ‘0’;
while (1)
{
//foodEat();
//moveSnake();
//deathSnake();
//cleardevice();
if (grade == 2)
{
loadimage(&playBImg, “IMAGE”, MAKEINTRESOURCE(IDR_IMAGE5), 640, 480);
BeginBatchDraw();
putimage(0, 0, &playBImg);
foodShow();
drawSnake();
EndBatchDraw();
foodEat();
moveSnake();
deathSnake();
}
else if (grade == 1)
{
cleardevice();
BeginBatchDraw();
foodShow();
drawSnake();
EndBatchDraw();
foodEat();
moveSnake();
deathSnake();
}
Sleep(100); //调整速度
}
}
int main()
{
welcome();
//创建线程1
hThread1 = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc1, // thread function
NULL, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier
//创建线程2
hThread2 = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc2, // thread function
NULL, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier
while (1)
{
if (ch == ‘0’ || ch == ‘2’)
{
oprate();
}
}
return 0;
}
转载:https://blog.csdn.net/it_xiangqiang/article/details/105147610