亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于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++經典小游戲匯總

python經典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經典游戲 玩不停

javascript經典小游戲匯總

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Qt+OpenCV實現目標檢測詳解

    Qt+OpenCV實現目標檢測詳解

    這篇文章主要介紹了如何利用Qt和OpenCV中自帶xml文件實現目標檢測,文中的實現過程講解詳細,感興趣的小伙伴可以動手試一試
    2022-03-03
  • 關于C++使用std::chrono獲取當前秒級/毫秒級/微秒級/納秒級時間戳問題

    關于C++使用std::chrono獲取當前秒級/毫秒級/微秒級/納秒級時間戳問題

    這篇文章主要介紹了C++使用std::chrono獲取當前秒級/毫秒級/微秒級/納秒級時間戳,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • C++泛型編程綜合講解

    C++泛型編程綜合講解

    泛型編程與面向對象編程的目標相同,即使重用代碼和抽象通用概念的技術更加簡單。但是面向對象編程強調編程的數據方面,泛型編程強調的是獨立于特定數據類型
    2022-12-12
  • C語言實現BMP圖像細化處理

    C語言實現BMP圖像細化處理

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

    基于c++中的默認拷貝函數的使用詳解

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

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

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

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

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

    關于define與C 的內存

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

    C++11中std::function基礎用法詳解

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

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

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

最新評論