亲宝软件园·资讯

展开

基于easyx的C++实现贪吃蛇

haohulala 人气:0

本文着重为大家仔细讲解了基于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);//这个区域每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经典小游戏汇总

加载全部内容

相关教程
猜你喜欢
用户评论