最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)簡(jiǎn)單貪吃蛇游戲

 更新時(shí)間:2021年05月25日 10:08:15   作者:Johnny_Law  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

我大概在一個(gè)多月前把自己上學(xué)期寫(xiě)的c代碼的貪吃蛇游戲push到csdn上,并且說(shuō)c風(fēng)格的貪吃蛇寫(xiě)起來(lái)有些麻煩(貪吃蛇游戲的c語(yǔ)言實(shí)現(xiàn)),準(zhǔn)備用面向?qū)ο蟮腸++再寫(xiě)一遍?,F(xiàn)在我們專業(yè)恰好剛教完了c++,學(xué)校也布置了一道簡(jiǎn)單的貪吃蛇的編程題目,實(shí)現(xiàn)下來(lái),的確覺(jué)得c++的思路清晰很多,所以再次把c++的代碼push上來(lái),供大家對(duì)比參考:)

直接上代碼,c++把整個(gè)游戲拆分成幾個(gè)文件,分開(kāi)上,有一定的c++基礎(chǔ)的同學(xué)應(yīng)該可以很容易看懂。

1、全局頭文件(global.hpp)

#ifndef _GLOBAL_H_
#define _GLOBAL_H_

#ifndef SYMBOLS
#define HEAD '@'
#define BODY 'X'
#define EMPTY '+'
#define FOOD '$'
#endif // !SYMBOLS

enum direction { up = 0, down = 1, left = 2, right = 4, freeze = 5 };

struct point {
 int x;
 int y;
 point(int x = 0, int y = 0) : x(x), y(y) {}
 point(const point& another) : x(another.x), y(another.y) {}
 point& operator=(const point& other) {
 x = other.x;
 y = other.y;
 return *this;
 }
 friend bool operator==(const point& point1, const point& point2) {
 return point1.x == point2.x && point1.y == point2.y;
 }
 point& move(direction d) {
 switch (d) {
 case up:
 x--;
 break;
 case down:
 x++;
 break;
 case left:
 y--;
 break;
 case right:
 y++;
 break;
 case freeze:
 default:
 break;
 }
 return *this;
 }
};

#endif // !_GLOBAL_H_

2、snake類的聲明和實(shí)現(xiàn)(snake.hpp)

(為了簡(jiǎn)化結(jié)構(gòu),把聲明和實(shí)現(xiàn)共同放在了hpp文件里,減少了一點(diǎn)封裝性,實(shí)際上應(yīng)該分開(kāi)頭文件和實(shí)現(xiàn)文件好一點(diǎn))

此處使用了容器list作為蛇身(body)的表達(dá)形式,這樣可以非常方便地進(jìn)行表達(dá),讀者有興趣可以用數(shù)組實(shí)現(xiàn)一下,一不小心就會(huì)出現(xiàn)有趣的內(nèi)存錯(cuò)誤。。。

#ifndef _SNAKE_H_
#define _SNAKE_H_
#include <iostream>
#include <list>
#include "global.hpp"

class snake {
 point head;
 std::list<point> body;
 public:
 snake(point initial_head);
 snake();
 ~snake() {}
 point& getHead();
 std::list<point>& getbody();
 void grow(point);
 void setHead(point);
};

snake::snake() {
 head.x = 0;
 head.y = 0;
}

snake::snake(point initial_head) {
 setHead(initial_head);
}

void snake::setHead(point _head) {
 head = _head;
}

void snake::grow(point second_node) {
 this -> body.push_front(second_node);
}

point& snake::getHead() {
 return head;
}

std::list<point>& snake::getbody() {
 return body;
}
#endif

3、map類的聲明和實(shí)現(xiàn)(map.hpp)

在這里,map中應(yīng)該包含一個(gè)snake類作為組合關(guān)系。
在組合關(guān)系里面,想要直接修改snake的各種參數(shù)是不可行的,所以在前面snake類的聲明里加上了諸如setHead(), getHead(), getbody() 這一類的函數(shù)。

#ifndef _MAP_H_
#define _MAP_H_
#include <iostream>
#include "global.hpp"
#include "snake.hpp"

class map {
 private:
 char** _map;
 snake _snake;
 int height, width;
 std::list<point> foods;
 public:
 map();
 map(point initial_size, point initial_head,
 std::list<point> initial_foods);
 ~map();
 void move(direction d);
 void print();
 bool isGameOver();
 bool isEat();;
 void makemap(void);
};

map::map() {
 _map = NULL;
 height = width = 0;
}

void map::makemap() { // 這個(gè)是用來(lái)更新地圖的
 for (int i = 0; i < height; i++) {
 for (int j = 0; j < width; j++)
 _map[i][j] = 0;
 }
 for (std::list<point>::iterator i = foods.begin(); i != foods.end(); ++i) {
 _map[i->x][i->y] = FOOD;
 }
 _map[_snake.getHead().x][_snake.getHead().y] = HEAD;
 for (std::list<point>::iterator i = _snake.getbody().begin();
 i != _snake.getbody().end(); ++i) {
 _map[i->x][i->y] = BODY;
 }
 for (int i = 0; i < height; i++) {
 for (int j = 0; j < width; j++) {
 if (_map[i][j] == 0)
 _map[i][j] = EMPTY;
 }
 }
}

map::map(point initial_size, point initial_head, std::list<point> initial_foods)
{
 height = initial_size.x;
 width = initial_size.y;
 _map = new char*[height];
 for (int i = 0; i < height; i++)
 _map[i] = new char[width]();
 _snake.setHead(initial_head);
 foods = initial_foods;
 makemap();
}

map::~map() {
 for (int i = 0; i < height; i++) {
 delete []_map[i];
 }
 delete []_map;
}

void map::print() {
 for (int i = 0; i < height; i++) {
 for (int j = 0; j < width; j++) {
 std::cout << _map[i][j];
 }
 std::cout << std::endl; 
 }
 std::cout << std::endl;
}

bool map::isGameOver() {
 point temp = _snake.getHead();
 if (temp.x == height || temp.y == width || temp.x < 0 || temp.y < 0)
 return true;
 if (_map[temp.x][temp.y] == BODY) return true;
 return false;
}

bool map::isEat() {
 point temp = _snake.getHead();
 if (temp.x == height || temp.y == width || temp.x < 0 || temp.y < 0)
 return false;
 if (_map[temp.x][temp.y] == FOOD) return true;
 else return false;
}

void map::move(direction d) {
 point temp_f = _snake.getHead();
 if (!(_snake.getbody().empty())) { // 為了避免追尾問(wèn)題
 _map[_snake.getbody().back().x][_snake.getbody().back().y] = EMPTY;
 }
 _snake.getHead().move(d);
 if (_snake.getHead() == _snake.getbody().front()) { // 判斷蛇是否往回走
 _snake.setHead(temp_f);
 _map[_snake.getbody().back().x][_snake.getbody().back().y] = BODY;
 return;
 }
 if (!isGameOver()) {
 if (isEat()) {
 point eaten = _snake.getHead();
 foods.remove(eaten);
 _snake.grow(temp_f);
 } else {
 _snake.getbody().push_front(temp_f);
 _snake.getbody().pop_back();
 }
 makemap();
 } else {
 if (!(_snake.getbody().empty())) {
 _map[_snake.getbody().back().x][_snake.getbody().back().y] = BODY;
 }
 }
}
#endif

蛇移動(dòng)的算法是,頭先動(dòng),如果判斷可以走,則把頭原來(lái)的位置push_front到body的頭部,然后用pop_back把body的尾部抹去
(讀者不熟悉list容器的操作的話可以先去了解一下,很容易的:))

4、游戲運(yùn)行主文件(game.cpp)

#include "map.hpp"
#include "global.hpp"
#include <iostream>
#include <list>
#include <algorithm>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

class InvalidInputException {
 public:
 InvalidInputException() { cerr << "Invalid input!" << endl; }
};
class DuplicateInputException : public InvalidInputException {
 public:
 DuplicateInputException() { cerr << "Duplicate input!" << endl; }
};

class GameUI {
 private:
 map* world;
 point initial_size;
 point initial_head;
 std::list<point> initial_foods;

 public:
 GameUI() {
 cout << "please input two positive integers indicates the map size!"
 << endl;
 cin >> initial_size.x >> initial_size.y;
 if (initial_size.x <= 5 || initial_size.y <= 5 || initial_size.x > 15 ||
 initial_size.y > 15) {
 cout << "invalid input" << endl;
 throw InvalidInputException();
 }
 cout << "please input two positive integers(range(0, size_x-1), "
 "range(0,size_y-1)) the initialize snake head position!"
 << endl;
 cin >> initial_head.x >> initial_head.y;
 if (initial_head.x >= initial_size.x || initial_head.x < 0 ||
 initial_head.y >= initial_size.y || initial_head.y < 0) {
 cout << "invalid input" << endl;
 throw InvalidInputException();
 }
 int food_num;
 cout << "please input how many food you will put and then input food "
 "position which is different form each other"
 << endl;
 cin >> food_num;

 if (food_num <= 0) {
 throw InvalidInputException();
 }

 while (food_num > 0) {
 food_num--;
 point temp;
 cin >> temp.x >> temp.y;
 if (temp.x >= 0 && temp.x < initial_size.x && temp.y >= 0 &&
 temp.y < initial_size.y &&
 std::find(initial_foods.begin(), initial_foods.end(), temp) ==
 initial_foods.end() &&
 !(temp.x == initial_head.x && temp.y == initial_head.y)) {
 initial_foods.push_back(temp);
 } else {
 throw DuplicateInputException();
 }
 }

 world = new map(initial_size, initial_head, initial_foods);
 }

 ~GameUI() { delete world; }

 void GameLoop() {
 world->print();
 bool exit = false;
 while (true) {
 char operation = getInput();
 switch (operation) {
 case 'w':
 case 'W':
 this->world->move(up);
 break;
 case 's':
 case 'S':
 this->world->move(down);
 break;
 case 'a':
 case 'A':
 this->world->move(left);
 break;
 case 'd':
 case 'D':
 this->world->move(right);
 break;
 case 'q':
 case 'Q':
 exit = true;
 break;
 default:
 this->world->move(freeze);
 }
 world->print();
 if (world->isGameOver()) {
 cout << "Game Over!" << endl;
 break;
 }
 if (exit) {
 cout << "Bye!" << endl;
 break;
 }
 }
 }

 char getInput() {
 char temp;
 cin >> temp;
 return temp;
 }
};

int main() { // 看,main函數(shù)只有這么短?。。?!
 GameUI greedySnake;
 greedySnake.GameLoop();
 return 0;
}

(事實(shí)上為了達(dá)到封裝性,gameUI的類也應(yīng)該分開(kāi)來(lái)實(shí)現(xiàn)。)

5、小結(jié)

這個(gè)貪吃蛇還比較的低端,只能實(shí)現(xiàn)一鍵一步的行走方式,還沒(méi)有像我的c語(yǔ)言貪吃蛇那樣可以自己走,并且有AI模式。實(shí)現(xiàn)自己走要使用到windows的下的一個(gè)庫(kù),實(shí)現(xiàn)起來(lái)還比較麻煩,可以參考我的c貪吃蛇。用c++重寫(xiě)一遍貪吃蛇,主要作用是可以更加清楚地體會(huì)到類的封裝與使用。

以后如果有時(shí)間,筆者可能會(huì)為這個(gè)貪吃蛇寫(xiě)下補(bǔ)丁什么的,體會(huì)一下c++的代碼重用和方便修改的特性,這是c語(yǔ)言所沒(méi)有的優(yōu)點(diǎn)。

Enjoy coding! :)

關(guān)于C++小游戲的更多精彩內(nèi)容請(qǐng)點(diǎn)擊專題: 《C++經(jīng)典小游戲》 學(xué)習(xí)了解

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • c++ 解析yaml文件的步驟

    c++ 解析yaml文件的步驟

    這篇文章主要介紹了c++ 解析yaml文件的步驟,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下
    2020-12-12
  • C++實(shí)現(xiàn)簡(jiǎn)單的掃雷游戲(控制臺(tái)版)

    C++實(shí)現(xiàn)簡(jiǎn)單的掃雷游戲(控制臺(tái)版)

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單的掃雷游戲,控制臺(tái)版的掃雷游戲希望大家喜歡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • C++簡(jiǎn)單又好用的基本運(yùn)算符重載

    C++簡(jiǎn)單又好用的基本運(yùn)算符重載

    繼友元知識(shí)過(guò)后,就到了今天的C++運(yùn)算符重載的內(nèi)容了,運(yùn)算符重載是C++里比較重要的內(nèi)容。這篇博文不會(huì)一下子講完各種運(yùn)算符重載,因?yàn)樘嗔肆艘膊缓梦照莆?,所以運(yùn)算符重載我準(zhǔn)備分多次記錄和分享,那么接下來(lái)進(jìn)入正文
    2022-06-06
  • 利用C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單三子棋游戲

    利用C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單三子棋游戲

    這篇文章主要為大家詳細(xì)介紹了利用C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單三子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • 淺談關(guān)于指針作為參數(shù)并改變它的值的問(wèn)題

    淺談關(guān)于指針作為參數(shù)并改變它的值的問(wèn)題

    這篇文章介紹了關(guān)于指針作為參數(shù)并改變它的值的問(wèn)題,有需要的朋友可以參考一下
    2013-10-10
  • C++ Qt屬性系統(tǒng)詳細(xì)介紹

    C++ Qt屬性系統(tǒng)詳細(xì)介紹

    這篇文章主要介紹了C++ Qt屬性系統(tǒng)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • wxWidgets實(shí)現(xiàn)圖片和文件按鈕

    wxWidgets實(shí)現(xiàn)圖片和文件按鈕

    這篇文章主要為大家詳細(xì)介紹了wxWidgets實(shí)現(xiàn)圖片和文件按鈕,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C語(yǔ)言實(shí)現(xiàn)的循環(huán)單鏈表功能示例

    C語(yǔ)言實(shí)現(xiàn)的循環(huán)單鏈表功能示例

    這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)的循環(huán)單鏈表功能,結(jié)合實(shí)例形式分析了基于C語(yǔ)言實(shí)現(xiàn)的循環(huán)單鏈表定義、創(chuàng)建、添加、刪除、打印、排序等相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • 深入理解c/c++ 內(nèi)存對(duì)齊

    深入理解c/c++ 內(nèi)存對(duì)齊

    這篇文章主要介紹了c/c++ 內(nèi)存對(duì)齊,有需要的朋友可以參考一下
    2014-01-01
  • C語(yǔ)言中無(wú)符號(hào)數(shù)和有符號(hào)數(shù)之間的運(yùn)算

    C語(yǔ)言中無(wú)符號(hào)數(shù)和有符號(hào)數(shù)之間的運(yùn)算

    C語(yǔ)言中有符號(hào)數(shù)和無(wú)符號(hào)數(shù)進(jìn)行運(yùn)算默認(rèn)會(huì)將有符號(hào)數(shù)看成無(wú)符號(hào)數(shù)進(jìn)行運(yùn)算,其中算術(shù)運(yùn)算默認(rèn)返回?zé)o符號(hào)數(shù),邏輯運(yùn)算當(dāng)然是返回0或1了。下面通過(guò)一個(gè)例子給大家分享C語(yǔ)言中無(wú)符號(hào)數(shù)和有符號(hào)數(shù)之間的運(yùn)算,一起看看吧
    2017-09-09

最新評(píng)論

丰顺县| 山西省| 张家港市| 吉林省| 安庆市| 石景山区| 东至县| 济南市| 钦州市| 镇巴县| 玛多县| 五大连池市| 永嘉县| 洪泽县| 莱州市| 丰都县| 葵青区| 饶平县| 临桂县| 丽江市| 神池县| 敦化市| 左云县| 平武县| 陆川县| 调兵山市| 搜索| 鲁甸县| 福建省| 天台县| 临海市| 白沙| 永靖县| 虎林市| 凯里市| 大悟县| 绥中县| 西昌市| 香港| 靖远县| 进贤县|