基于easyx的C++實現貪吃蛇
更新時間:2020年07月27日 14:58:57 作者:haohulala
這篇文章主要為大家詳細介紹了基于easyx的C++實現貪吃蛇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了基于easyx的C++實現貪吃蛇的具體代碼,供大家參考,具體內容如下
本代碼來自于easyx討論群的分享
先上效果圖,其實也只是畫了簡單的圈圈代表蛇和食物,背景就是黑色的。
#include "stdafx.h" #include <iostream> #include <stdlib.h> #include <time.h> #include <conio.h> #include <graphics.h> #define N 100 using namespace std; enum moved { UP, DOWN, LEFT, RIGHT }; class Snake { private: struct { //整條蛇的信息 int x; int y; }snake[100]; struct { int life; //為1代表還活著,為0代表已經死了 int length; //代表蛇的長度,初始值為3 enum moved direction; //前進方向 }snake_head; struct { //食物的信息 int x; int y; }food; public: void display(); //顯示界面 void initSnake(); //隨機生成蛇 void move();//蛇移動 void boundary_check();//邊界判斷 void _food();//生成食物 int food_eatcheck();//檢查是否吃到食物,吃到則返回1,否則返回0 int snake_eat();//判斷貪吃蛇是否咬到自己,咬到則返回1,否則返回0 void run(); //主要運行函數 }; void Snake::display() { initgraph(800, 600); setbkcolor(WHITE); //設置背景顏色為白色 cleardevice(); //將背景顏色刷新到窗口上 setfillcolor(BLACK); //設置填充的顏色為黑色,之后話填充的圖形都是這個顏色 solidrectangle(20, 560, 560, 20);//這個區(qū)域每20*20為一個單位,一共有27*27個單位 } //構造函數 void Snake::initSnake() { srand((unsigned)time(NULL)); //因為一開始蛇是向右走的,所以不能讓蛇初始化在太靠右邊的地方 int x = rand() % 22 + 3; //范圍是3-24 int y = rand() % 22 + 3; //加三是因為初始的長度是3,必須讓整條蛇都在范圍內 this->snake[0].x = x * 20 + 10;//加十是因為要確定圓心的位置 this->snake[0].y = y * 20 + 10; //默認蛇一開始是橫著的所以三段的y坐標相同 this->snake[1].y = this->snake[2].y = this->snake[0].y; this->snake[1].x = this->snake[0].x - 20; this->snake[2].x = this->snake[0].x - 40; setfillcolor(GREEN); //設置填充色為綠色 solidcircle(this->snake[0].x, this->snake[0].y, 10); //畫圓 solidcircle(this->snake[1].x, this->snake[1].y, 10); solidcircle(this->snake[2].x, this->snake[2].y, 10); this->snake_head.length = 3; this->snake_head.life = 1; this->snake_head.direction = RIGHT; } void Snake::move() { char ch; if (_kbhit()) { //如果有輸入的話就返回1,沒有輸入的話就返回0 ch = _getch();//獲取輸入的字符 switch (ch) { case 'w' :if (this->snake_head.direction != DOWN) this->snake_head.direction = UP; break; case 'W':if (this->snake_head.direction != DOWN) this->snake_head.direction = UP; break; case 's' :if (this->snake_head.direction != UP) this->snake_head.direction = DOWN; break; case 'S':if (this->snake_head.direction != UP) this->snake_head.direction = DOWN; break; case 'a':if (this->snake_head.direction != RIGHT) this->snake_head.direction = LEFT; break; case 'A':if (this->snake_head.direction != RIGHT) this->snake_head.direction = LEFT; break; case 'd':if (this->snake_head.direction != LEFT) this->snake_head.direction = RIGHT; break; case 'D':if (this->snake_head.direction != LEFT) this->snake_head.direction = RIGHT; break; default:break; } } //將蛇尾變成黑色 int i = this->snake_head.length - 1; setfillcolor(BLACK); solidcircle(snake[i].x, snake[i].y, 10); //接下來遍歷每個身體,每個身體都更新為前一個身體,蛇頭除外 for (; i > 0; i--) { this->snake[i].x = this->snake[i - 1].x; this->snake[i].y = this->snake[i - 1].y; } switch (this->snake_head.direction) { case RIGHT:this->snake[0].x += 20; break; case LEFT:this->snake[0].x -= 20; break; case UP:this->snake[0].y -= 20; break; case DOWN:this->snake[0].y += 20; break; default:break; } setfillcolor(GREEN); solidcircle(this->snake[0].x, this->snake[0].y, 10);//繪制蛇頭 Sleep(1000); } void Snake::boundary_check() { if (this->snake[0].x <= 30 || this->snake[0].x >= 550 || this->snake[0].y <= 30 || this->snake[0].y >= 550) { this->snake_head.life = 0; } } void Snake::_food() { srand((unsigned)time(NULL)); int x = rand() % 21 + 3; //范圍是3-23 int y = rand() % 21 + 3; this->food.x = x * 20 + 10; this->food.y = y * 20 + 10; setfillcolor(YELLOW); solidcircle(this->food.x, this->food.y, 10); } int Snake::food_eatcheck() { if (this->snake[0].x == this->food.x && this->snake[0].y == this->food.y) { //如果滿足條件就是吃到食物了 this->snake_head.length++;//長度加一 setfillcolor(GREEN); solidcircle(food.x, food.y, 10); int k = this->snake_head.length; //吃到食物之后最后要在尾巴處加一個長度 switch (this->snake_head.direction) { case RIGHT:this->snake[k - 1].x = this->snake[k - 2].x - 20; this->snake[k - 1].y = this->snake[k - 2].y; break; case LEFT:this->snake[k - 1].x = this->snake[k - 2].x += 20; this->snake[k - 1].y = this->snake[k - 2].y; break; case UP:this->snake[k - 1].x = this->snake[k - 2].x; this->snake[k - 1].y = this->snake[k - 2].y + 20; break; case DOWN:this->snake[k - 1].x = this->snake[k - 2].x; this->snake[k - 1].y = this->snake[k - 2].y - 20; break; default:break; } setfillcolor(GREEN); solidcircle(this->snake[k - 1].x, this->snake[k - 1].y, 10); return 1; } return 0; } int Snake::snake_eat() { int i; for (i = 1; i < this->snake_head.length; i++) { if (this->snake[i].x == this->snake[0].x && this->snake[i].y == this->snake[0].y) { return 1; } } return 0; } void Snake::run() { display(); //顯示游戲界面 initSnake(); _food(); //生成第一個食物 while (true) { move(); //蛇移動 if (snake_eat() == 1) { //自己吃到自己了,游戲失敗 cout << "自己吃到自己了,游戲失敗" << endl; break; } boundary_check();//判斷是否撞墻 if (this->snake_head.life == 0) { //撞墻了 cout << "撞墻了,游戲結束" << endl; break; } if (food_eatcheck() == 1) { _food(); //吃到食物就重新生成一個食物 } } } int main() { Snake s; s.run(); return 0; }
更多有趣的經典小游戲實現專題,分享給大家:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
關于C++使用std::chrono獲取當前秒級/毫秒級/微秒級/納秒級時間戳問題
這篇文章主要介紹了C++使用std::chrono獲取當前秒級/毫秒級/微秒級/納秒級時間戳,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07C++ Boost Coroutine使用協(xié)程詳解
通過Boost.Coroutine,可以在C++中使用協(xié)程。協(xié)程是其他編程語言的一個特性,通常使用關鍵字yield來表示協(xié)程。在這些編程語言中,yield可以像return一樣使用2022-11-11