基于easyx的C++實(shí)現(xiàn)貪吃蛇
本文實(shí)例為大家分享了基于easyx的C++實(shí)現(xiàn)貪吃蛇的具體代碼,供大家參考,具體內(nèi)容如下
本代碼來自于easyx討論群的分享
先上效果圖,其實(shí)也只是畫了簡單的圈圈代表蛇和食物,背景就是黑色的。
#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代表已經(jīng)死了 int length; //代表蛇的長度,初始值為3 enum moved direction; //前進(jìn)方向 }snake_head; struct { //食物的信息 int x; int y; }food; public: void display(); //顯示界面 void initSnake(); //隨機(jī)生成蛇 void move();//蛇移動(dòng) void boundary_check();//邊界判斷 void _food();//生成食物 int food_eatcheck();//檢查是否吃到食物,吃到則返回1,否則返回0 int snake_eat();//判斷貪吃蛇是否咬到自己,咬到則返回1,否則返回0 void run(); //主要運(yùn)行函數(shù) }; void Snake::display() { initgraph(800, 600); setbkcolor(WHITE); //設(shè)置背景顏色為白色 cleardevice(); //將背景顏色刷新到窗口上 setfillcolor(BLACK); //設(shè)置填充的顏色為黑色,之后話填充的圖形都是這個(gè)顏色 solidrectangle(20, 560, 560, 20);//這個(gè)區(qū)域每20*20為一個(gè)單位,一共有27*27個(gè)單位 } //構(gòu)造函數(shù) void Snake::initSnake() { srand((unsigned)time(NULL)); //因?yàn)橐婚_始蛇是向右走的,所以不能讓蛇初始化在太靠右邊的地方 int x = rand() % 22 + 3; //范圍是3-24 int y = rand() % 22 + 3; //加三是因?yàn)槌跏嫉拈L度是3,必須讓整條蛇都在范圍內(nèi) this->snake[0].x = x * 20 + 10;//加十是因?yàn)橐_定圓心的位置 this->snake[0].y = y * 20 + 10; //默認(rèn)蛇一開始是橫著的所以三段的y坐標(biāo)相同 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); //設(shè)置填充色為綠色 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); //接下來遍歷每個(gè)身體,每個(gè)身體都更新為前一個(gè)身體,蛇頭除外 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; //吃到食物之后最后要在尾巴處加一個(gè)長度 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(); //生成第一個(gè)食物 while (true) { move(); //蛇移動(dòng) if (snake_eat() == 1) { //自己吃到自己了,游戲失敗 cout << "自己吃到自己了,游戲失敗" << endl; break; } boundary_check();//判斷是否撞墻 if (this->snake_head.life == 0) { //撞墻了 cout << "撞墻了,游戲結(jié)束" << endl; break; } if (food_eatcheck() == 1) { _food(); //吃到食物就重新生成一個(gè)食物 } } } int main() { Snake s; s.run(); return 0; }
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C++ 情懷游戲貪吃蛇的實(shí)現(xiàn)流程詳解
- 利用C/C++實(shí)現(xiàn)貪吃蛇游戲
- C++入門指南之貪吃蛇游戲的實(shí)現(xiàn)
- C++實(shí)現(xiàn)簡易貪吃蛇游戲
- C++實(shí)現(xiàn)簡單貪吃蛇小游戲
- C++實(shí)現(xiàn)貪吃蛇游戲
- C++代碼實(shí)現(xiàn)貪吃蛇小游戲
- C++控制臺(tái)循環(huán)鏈表實(shí)現(xiàn)貪吃蛇
- C++控制臺(tái)實(shí)現(xiàn)貪吃蛇游戲
- C++通過類實(shí)現(xiàn)控制臺(tái)貪吃蛇
- C++結(jié)構(gòu)體數(shù)組實(shí)現(xiàn)貪吃蛇
- c++實(shí)現(xiàn)超簡單的貪吃蛇游戲?qū)嵗榻B
相關(guān)文章
Qt+OpenCV實(shí)現(xiàn)目標(biāo)檢測詳解
這篇文章主要介紹了如何利用Qt和OpenCV中自帶xml文件實(shí)現(xiàn)目標(biāo)檢測,文中的實(shí)現(xiàn)過程講解詳細(xì),感興趣的小伙伴可以動(dòng)手試一試2022-03-03關(guān)于C++使用std::chrono獲取當(dāng)前秒級(jí)/毫秒級(jí)/微秒級(jí)/納秒級(jí)時(shí)間戳問題
這篇文章主要介紹了C++使用std::chrono獲取當(dāng)前秒級(jí)/毫秒級(jí)/微秒級(jí)/納秒級(jí)時(shí)間戳,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07詳解C++中OpenSSL動(dòng)態(tài)鏈接庫的使用
這篇文章主要介紹了OpenSSL動(dòng)態(tài)鏈接庫的使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11C++ Boost Coroutine使用協(xié)程詳解
通過Boost.Coroutine,可以在C++中使用協(xié)程。協(xié)程是其他編程語言的一個(gè)特性,通常使用關(guān)鍵字yield來表示協(xié)程。在這些編程語言中,yield可以像return一樣使用2022-11-11C++11中std::function基礎(chǔ)用法詳解
std::function是C++11標(biāo)準(zhǔn)庫中提供的一種可調(diào)用對(duì)象的通用類型,它可以存儲(chǔ)任意可調(diào)用對(duì)象,本文就來和大家講講它的基礎(chǔ)用法,希望對(duì)大家有所幫助2023-04-04