欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于easyx的C++實(shí)現(xiàn)貪吃蛇

 更新時(shí)間:2020年07月27日 14:58:57   作者:haohulala  
這篇文章主要為大家詳細(xì)介紹了基于easyx的C++實(shí)現(xiàn)貪吃蛇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

javascript經(jīng)典小游戲匯總

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

相關(guān)文章

  • Qt+OpenCV實(shí)現(xiàn)目標(biāo)檢測詳解

    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í)間戳問題

    關(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++泛型編程綜合講解

    C++泛型編程綜合講解

    泛型編程與面向?qū)ο缶幊痰哪繕?biāo)相同,即使重用代碼和抽象通用概念的技術(shù)更加簡單。但是面向?qū)ο缶幊虖?qiáng)調(diào)編程的數(shù)據(jù)方面,泛型編程強(qiáng)調(diào)的是獨(dú)立于特定數(shù)據(jù)類型
    2022-12-12
  • C語言實(shí)現(xiàn)BMP圖像細(xì)化處理

    C語言實(shí)現(xiàn)BMP圖像細(xì)化處理

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)BMP圖像細(xì)化處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 基于c++中的默認(rèn)拷貝函數(shù)的使用詳解

    基于c++中的默認(rèn)拷貝函數(shù)的使用詳解

    本篇文章對(duì)c++中默認(rèn)拷貝函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下
    2013-05-05
  • 詳解C++中OpenSSL動(dòng)態(tài)鏈接庫的使用

    詳解C++中OpenSSL動(dòng)態(tài)鏈接庫的使用

    這篇文章主要介紹了OpenSSL動(dòng)態(tài)鏈接庫的使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-11-11
  • C++ Boost Coroutine使用協(xié)程詳解

    C++ Boost Coroutine使用協(xié)程詳解

    通過Boost.Coroutine,可以在C++中使用協(xié)程。協(xié)程是其他編程語言的一個(gè)特性,通常使用關(guān)鍵字yield來表示協(xié)程。在這些編程語言中,yield可以像return一樣使用
    2022-11-11
  • 關(guān)于define與C 的內(nèi)存

    關(guān)于define與C 的內(nèi)存

    本文主要介紹了C語言中#define到底存在程序的哪個(gè)區(qū),以及工作流程和效率與普通函數(shù)的區(qū)別,希望能幫助需要的小伙伴
    2016-07-07
  • C++11中std::function基礎(chǔ)用法詳解

    C++11中std::function基礎(chǔ)用法詳解

    std::function是C++11標(biāo)準(zhǔn)庫中提供的一種可調(diào)用對(duì)象的通用類型,它可以存儲(chǔ)任意可調(diào)用對(duì)象,本文就來和大家講講它的基礎(chǔ)用法,希望對(duì)大家有所幫助
    2023-04-04
  • C語言圖書管理系統(tǒng)課程設(shè)計(jì)

    C語言圖書管理系統(tǒng)課程設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了C語言圖書管理系統(tǒng)課程設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評(píng)論