基于C++和EasyX 实现的《双人贪吃蛇》小游戏,你不找个小伙伴陪你一起玩吗?
生活随笔
收集整理的這篇文章主要介紹了
基于C++和EasyX 实现的《双人贪吃蛇》小游戏,你不找个小伙伴陪你一起玩吗?
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
兩條蛇實(shí)現(xiàn)原理:先定義好一條蛇,然后派生出另外一條蛇,重要第二條蛇的移動(dòng)方向的方法,換成鍵盤上的另外四個(gè)鍵,然后就是正常的游戲判斷,兩條蛇相碰的判斷等等 . . .
素材就是一個(gè)背景加上一個(gè)音樂(lè),其它的都是基于 EasyX 里的方法畫出來(lái)的
游戲效果如下所示:
小游戲制作貪吃蛇
代碼如下所示:
#include <iostream> #include <graphics.h> #include <ctime> #include <conio.h> #include <cstdlib> #include <cmath> #include <mmsystem.h> #pragma comment (lib,"winmm")void InitGame(); //初始化游戲const int WIDTH = 640; const int HEIGHT = 480;class Snake // 1 紅色 2 綠色 { private:const static int NUM = 100; //最大節(jié)數(shù)struct Coord //每一節(jié)的坐標(biāo){int x;int y;};int s_count; //初始為3節(jié)int fruit_x, fruit_y; //果實(shí)的位置Coord snake[NUM]; //一共多少節(jié)protected:enum Direction { Up = 72, Down = 80, Left = 75, Right = 77 }; //枚舉方向Direction direction; //默認(rèn)為左方向public://構(gòu)造Snake();//析構(gòu)virtual ~Snake();//初始蛇 和 果實(shí)的坐標(biāo)void InitSnakeFruitCoord();//移動(dòng) (改變坐標(biāo)) 畫蛇 判斷吃果實(shí)void Move();//刷新果實(shí) 位置void RefreshFruit();//改變蛇的方向virtual void ChangeDirection(const char& ch);//判斷游戲 輸贏void JudgeWinLose(const Snake& op, const int flag)const;};class Snake1 :public Snake // 繼承一個(gè)類 達(dá)到兩條蛇 { public://構(gòu)造Snake1();virtual void ChangeDirection(const char& ch); };int main() {srand((unsigned)time(0)); //種下隨機(jī)數(shù)的種子DWORD t1 = GetTickCount(), tt1; //判斷是否可以移動(dòng)Snake snake1; //兩條蛇Snake1 snake2;InitGame();snake1.InitSnakeFruitCoord(); //畫出蛇剛開(kāi)始的位置snake2.InitSnakeFruitCoord();while (1){if (_kbhit()) //判斷改變位置{char ch = _getch();snake1.ChangeDirection(ch);snake2.ChangeDirection(ch);}tt1 = GetTickCount();if (tt1 - t1 > 100) //自移{setfillcolor(RED);snake1.Move();setfillcolor(GREEN);snake2.Move();t1 = tt1;}snake1.JudgeWinLose(snake2, 1);snake2.JudgeWinLose(snake1, 2);}return 0; }Snake::Snake() :direction(Left), s_count(4), fruit_x(0), fruit_y(0) {int x = (rand() % (WIDTH - 100) + 50) / 15 * 15; //蛇的初始坐標(biāo)int y = (rand() % (HEIGHT - 150) + 50) / 15 * 15;for (int i = 0; i < s_count; i++)snake[i] = { (i * 15) + x, y }; }Snake1::Snake1() :Snake() {} // 使用默認(rèn)構(gòu)造 函數(shù) Snake() ----Snake::~Snake() {}void InitGame() {initgraph(WIDTH, HEIGHT);MOUSEMSG msg;mciSendString("open music.mp3 alias huameng", 0, 0, 0);mciSendString("play huameng repeat", 0, 0, 0);loadimage(nullptr, "start_background.jpg", WIDTH, HEIGHT);std::cin.get();setbkcolor(RGB(255, 128, 192));cleardevice();settextcolor(RED);settextstyle(40, 25, "楷體");outtextxy(200, 100, "開(kāi)始游戲");setlinecolor(RGB(155, 155, 0));rectangle(190, 90, 410, 145); //------settextstyle(20, 10, "楷體");settextcolor(RGB(50, 128, 255));outtextxy(130, 200, "游 戲 規(guī) 則 :");outtextxy(180, 230, "玩家一:小紅 控制:4個(gè)光標(biāo)鍵");outtextxy(180, 260, "玩家二:小綠 控制:W A S D");outtextxy(80, 320, "勝利條件:1.對(duì)方觸碰邊界 2.對(duì)方觸碰己方身體");outtextxy(180, 355, "3.吃滿果實(shí)(100個(gè)) >> __ <<");outtextxy(170, 400, "注: 同灰余燼 算小綠 贏 !!!");settextstyle(40, 25, "楷體");while (1){msg = GetMouseMsg();int x = msg.x; //rectangle(190,90,410,145); 長(zhǎng) 220 寬 55int y = msg.y;if (x - 190 < 220 && x - 190 > 0 && y - 90 > 0 && y - 90 <= 55 && msg.mkLButton)break;else if (x - 190 < 220 && x - 190 > 0 && y - 90 > 0 && y - 90 <= 55){settextcolor(RGB(0, 255, 255));outtextxy(200, 100, "開(kāi)始游戲");}else{settextcolor(RED);outtextxy(200, 100, "開(kāi)始游戲");}}setbkcolor(RGB(0, 255, 255));cleardevice(); }void Snake::RefreshFruit() {fruit_x = (rand() % (WIDTH - 100) + 50) / 15 * 15; // 刷新位置fruit_y = (rand() % (HEIGHT - 100) + 50) / 15 * 15; }void Snake::InitSnakeFruitCoord() {for (int i = 0; i < s_count; i++)solidrectangle(snake[i].x, snake[i].y, snake[i].x + 15, snake[i].y + 15);fruit_x = (rand() % (WIDTH - 100) + 50) / 15 * 15;fruit_y = (rand() % (HEIGHT - 100) + 50) / 15 * 15; }void Snake::Move() {clearrectangle(snake[s_count - 1].x, snake[s_count - 1].y, snake[s_count - 1].x + 15, snake[s_count - 1].y + 15);for (int i = s_count - 1; i > 0; i--){snake[i].x = snake[i - 1].x;snake[i].y = snake[i - 1].y;}switch (direction){case Snake::Up:snake[0].y -= 15;break;case Snake::Down:snake[0].y += 15;break;case Snake::Left:snake[0].x -= 15;break;case Snake::Right:snake[0].x += 15;break;}for (int i = 0; i < s_count; i++)solidrectangle(snake[i].x, snake[i].y, snake[i].x + 15, snake[i].y + 15);solidrectangle(fruit_x, fruit_y, fruit_x + 15, fruit_y + 15);if (snake[0].x == fruit_x && snake[0].y == fruit_y){RefreshFruit();Move(); //防止吃到果實(shí) 卡頓s_count++;} }void Snake::ChangeDirection(const char& ch) {switch (ch){case 72:direction = Up; break;case 75:direction = Left; break;case 77:direction = Right; break;case 80:direction = Down; break;} }void Snake1::ChangeDirection(const char& ch) {switch (ch){case 'W':direction = Up; break;case 'A':direction = Left; break;case 'D':direction = Right; break;case 'S':direction = Down; break;} }void Snake::JudgeWinLose(const Snake& op, const int flag)const {if (flag == 1){if (snake[0].x <= 0 || snake[0].x >= WIDTH || snake[0].y <= 0 || snake[0].y >= HEIGHT || s_count >= 99){MessageBox(nullptr, "小綠勝利了 >>_<<", "游戲結(jié)束!!", MB_OK);exit(0);}elsefor (int i = 0; i < op.s_count; i++){if (snake[0].x == op.snake[i].x && snake[0].y == op.snake[i].y){MessageBox(nullptr, "小綠勝利了 >>_<<", "游戲結(jié)束!!", MB_OK);exit(0);}}}else{if (snake[0].x <= 0 || snake[0].x >= WIDTH || snake[0].y <= 0 || snake[0].y >= HEIGHT || s_count >= 99){MessageBox(nullptr, "小紅勝利了 >>_<<", "游戲結(jié)束!!", MB_OK);exit(0);}elsefor (int i = 0; i < op.s_count; i++){if (snake[0].x == op.snake[i].x && snake[0].y == op.snake[i].y){MessageBox(nullptr, "小紅勝利了 >>_<<", "游戲結(jié)束!!", MB_OK);exit(0);}}} }總結(jié)
以上是生活随笔為你收集整理的基于C++和EasyX 实现的《双人贪吃蛇》小游戏,你不找个小伙伴陪你一起玩吗?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算机发展历史及其前景展望,论计算机发展
- 下一篇: uniapp+unicloud开发微信小