這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)——《進(jìn)化版貪吃蛇項(xiàng)目》,也就是在我們上一個(gè)發(fā)布的文章項(xiàng)目分享的基礎(chǔ)上,實(shí)現(xiàn)貪吃蛇的自動(dòng)行動(dòng),你可以按鍵也可以不按鍵,讓蛇自己找食物!還是挺好玩的,一起來看看吧!
文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下!
項(xiàng)目實(shí)現(xiàn)要求:
1.學(xué)過C語言
2.熟悉C語言數(shù)組
3.熟悉結(jié)構(gòu)體
4.按鍵交互 程序:1.按鍵交互 2.鼠標(biāo)交互
5.創(chuàng)建圖形窗口
6.基本繪圖
但是這些在我們項(xiàng)目源碼里面都會(huì)有注釋,大家到時(shí)候?qū)W習(xí)的時(shí)候千萬不要忽略注釋,注釋可以更好地幫你理解代碼,尤其是C語言初學(xué)者。
本項(xiàng)目編譯環(huán)境:VS2019/VS2013;
插件:圖形庫插件easyX;
本案例配套學(xué)習(xí)教程:進(jìn)化版貪吃蛇——嗶哩嗶哩
源代碼示例:
/*
1.學(xué)過C語言
2.熟悉C語言數(shù)組
3.熟悉結(jié)構(gòu)體
4.按鍵交互 程序:1.按鍵交互 2.鼠標(biāo)交互
5.創(chuàng)建圖形窗口
6.基本繪圖
*/
//1.把程序所要用的數(shù)據(jù)抽象出來
typedef struct pointXY
{
int x;
int y;
}MYPOINT;
HWND hwnd = NULL;
//蛇
struct mySnake
{
int num;
MYPOINT xy[MAX];
char postion; //表示方向,標(biāo)記
}snake;
//食物
struct myFood
{
MYPOINT foodxy;
int flag;
int eatGrade;
}food;
//把方向枚舉出來
enum movPostion{right=1,left=2,down=3,up=4};
//整個(gè)項(xiàng)目劃分
//1.初始化蛇
void initSnake()
{
snake.xy[2].x = 0;
snake.xy[2].y = 0;
snake.xy[1].x = 10;
snake.xy[1].y = 0;
snake.xy[0].x = 20;
snake.xy[0].y = 0;
snake.num = 3;
snake.postion = right;
food.flag = 0;
}
//2.繪制蛇
void drawSnake()
{
for (int i = 0; i < snake.num; i++)
{
setlinecolor(RED);
setfillcolor(GREEN);
fillrectangle(snake.xy[i].x, snake.xy[i].y, snake.xy[i].x + 10, snake.xy[i].y + 10);
}
}
//3.移動(dòng)蛇
void moveSnake()
{
//除了第一節(jié)之外,后面的坐標(biāo)都是前一節(jié)坐標(biāo)
for (int i = snake.num - 1; i > 0; i--)
{
snake.xy[i].x = snake.xy[i - 1].x;
snake.xy[i].y = snake.xy[i - 1].y;
}
//蛇頭怎么走,要根據(jù)方向標(biāo)志去做移動(dòng)
switch (snake.postion)
{
case right:
snake.xy[0].x += 10;
break;
case left:
snake.xy[0].x -= 10;
break;
case down:
snake.xy[0].y += 10;
break;
case up:
snake.xy[0].y -= 10;
break;
}
}
//4.按鍵用戶:人玩蛇
void keyDown()
{
static char userkey = ''; //VC getch不需要加下劃線
//userkey=_getch(); //不可見輸入
//自己生成按鍵
switch (snake.postion)
{
case right:
case left:
if (food.foodxy.y >= snake.xy[0].y)
{
userkey = 80;
}
else if (food.foodxy.y
0 ].y){
userkey = 72;
}
break;
case up:
case down:
if (food.foodxy.x >= snake.xy[0].x)
{
userkey = 77;
}
else if (food.foodxy.x < snake.xy[0].x)
{
userkey = 75;
}
break;
}
switch (userkey)
{
case 'w':
case 'W':
case 72: //上
if (snake.postion != down)
{
//蛇往上走,你要排除掉蛇本來是朝下
snake.postion = up;
}
break;
case 's':
case 'S':
case 80: //下
if (snake.postion != up)
{
snake.postion = down;
}
break;
case 'a':
case 'A':
case 75: //左
if (snake.postion != right)
{
snake.postion = left;
}
break;
case 'd':
case 'D':
case 77: //右
if (snake.postion != left)
{
snake.postion = right;
}
}
}
//初始化食物
void initFood()
{
//0,63 630 640
//0,47 470 480
food.foodxy.x = rand() % 64 * 10; //只能是10的整數(shù)倍,蛇頭才能對(duì)齊食物
food.foodxy.y = rand() % 48 * 10;
food.flag = 1;
//食物不能出現(xiàn)在蛇身上
for (int i = 0; i < snake.num; i++)
{
if (food.foodxy.x == snake.xy[i].x&&food.foodxy.y == snake.xy[i].y)
{
food.foodxy.x = rand() % 64 * 10; //只能是10的整數(shù)倍,蛇頭才能對(duì)齊食物
food.foodxy.y = rand() % 48 * 10;
}
}
}
//繪制食物
void drawFood()
{
fillrectangle(food.foodxy.x, food.foodxy.y, food.foodxy.x + 10, food.foodxy.y + 10);
}
//蛇吃食物
void eatFood()
{
if (snake.xy[0].x == food.foodxy.x && snake.xy[0].y == food.foodxy.y)
{
snake.num++;
food.eatGrade += 10;
food.flag = 0;
}
}
//蛇死亡判斷
int snakeDie()
{
//顯示分?jǐn)?shù)
char grade[100] = { 0 };
sprintf(grade, "%d", food.eatGrade);
setbkmode(TRANSPARENT);
settextcolor(RED);
outtextxy(580, 20, "分?jǐn)?shù):"); //loadimage
outtextxy(620, 20, grade); //
//判斷蛇是否死亡
//撞墻
if (snake.xy[0].x > 640 || snake.xy[0].x<0 || snake.xy[0].y>480 || snake.xy[0].y < 0)
{
MessageBox(hwnd, "游戲結(jié)束!", "撞墻死亡!", 0);
return 1;
}
//撞自己
for (int i = 1; i < snake.num; i++)
{
if (snake.xy[0].x == snake.xy[i].x&&snake.xy[0].y == snake.xy[i].y)
{
MessageBox(hwnd, "游戲結(jié)束!", "撞自己!", 0);
return 1;
}
}
return 0;
}
int main()
{
srand((unsigned int)time(NULL));
hwnd=initgraph(640, 480);
setbkcolor(WHITE);
cleardevice();
initSnake();
while (1)
{
cleardevice(); //刷掉路徑
if (food.flag == 0)
{
initFood();
}
drawFood();
drawSnake();
if (snakeDie())
{
break;
}
eatFood();
moveSnake();
Sleep(100); //控制速度
//while (_kbhit()) //kbhit() 存在按鍵操作,返回非零
//{
// keyDown();
//}
keyDown();
}
closegraph();
printf("GameOver!!!");
system("pause");
return 0;
}
寫在最后:對(duì)于準(zhǔn)備學(xué)習(xí)C/C++編程的小伙伴,如果你想更好的提升你的編程核心能力(內(nèi)功)不妨從現(xiàn)在開始!
責(zé)任編輯:haq
-
C語言
+關(guān)注
關(guān)注
180文章
7605瀏覽量
136930 -
代碼
+關(guān)注
關(guān)注
30文章
4790瀏覽量
68653
原文標(biāo)題:C語言項(xiàng)目實(shí)戰(zhàn):《自動(dòng)版貪吃蛇》零基礎(chǔ)項(xiàng)目!源碼 + 注釋
文章出處:【微信號(hào):cyuyanxuexi,微信公眾號(hào):C語言編程學(xué)習(xí)基地】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論