c++小游戏(贪吃蛇)

#include<iostream>
using namespace std;
#include<easyx.h>        //包含图形库头文件
#include<stdlib.h>
#include<time.h>
 
//定义场景大小
#define WIDTH 1040
#define HEIGHT 640
//定义食物以及蛇的大小
#define SIZE  20
//定义蛇的朝向
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
//蛇的最大长度
#define MAXLEN 1600
 
 
typedef struct {
    int x;
    int y;
}SnakeNode;
 
SnakeNode tmp[MAXLEN];    //用另外一个数组来存储蛇原来的位置
int speed = 150;        //蛇的速度(用在睡眠函数里面)
 
//创建蛇的类
class Snake
{
    friend class Food;
public:
    Snake();                    //初始化
    void Move();                //移动
    void Draw();                //绘制蛇
    bool Eat(Food food);        //吃食物
    bool Defeat();                //失败判定
private:
    int dirt;                    //朝向
    int length;                    //长度
    SnakeNode node[MAXLEN];        //蛇的结点
};
 
//创建食物的类
class Food
{
    friend class Snake;
public:
    Food(Snake snake);            //食物初始化
    void Draw();                //绘制食物
private:
    int x, y;                    //坐标
    int score;                    //分数
};
 
int main() {
    initgraph(WIDTH, HEIGHT);
    Snake snake;
    table:
    Food food(snake);
    while (1) {
        BeginBatchDraw();
        FlushBatchDraw();
        snake.Draw();
        food.Draw();
        FlushBatchDraw();
        EndBatchDraw();//双缓冲,防止屏幕一闪一闪的
        if (snake.Eat(food)) {
            goto table;
        }
        if (snake.Defeat()) {
            break;
        }
        snake.Move();        
    }
    //提示失败信息
    HWND window = GetHWnd();
    SetWindowText(window, "提示");
    MessageBox(window, "游戏失败","提示",MB_OKCANCEL);
    return 0;
}
 
//蛇的初始化
Snake::Snake()
{
    this->dirt = RIGHT;
    this->length = 3;
    //下标是0的位置为蛇的头部
    for (int i = 0; i < length; i++) {
        this->node[i].x = 60 - ((i + 1) * SIZE);
        this->node[i].y = 0;
    }
}
 
//移动
void Snake::Move() {
    //将原来的蛇结点拷贝一份
    for (int i = 0; i < this->length; i++) {
        tmp[i].x = this->node[i].x;
        tmp[i].y = this->node[i].y;
    }
    int status = 0;//用来判断是否点击了转向按键
    if (this->dirt == RIGHT) {
        //判断是否转向
        if (GetAsyncKeyState('W') && status == 0) {
            //this->node[0].y -= SIZE;
            this->dirt = UP;
            status = 1;
        }
        else if (GetAsyncKeyState('S') && status == 0) {
            this->dirt = DOWN;
            status = 1;
        }
        else {
            this->node[0].x += SIZE;
        }
    }
    if (this->dirt == DOWN) {
        //判断是否转向
        if (GetAsyncKeyState('A') && status == 0) {
            //this->node[0].x -= SIZE;
            this->dirt = LEFT;
            status = 1;
        }
        else if (GetAsyncKeyState('D') && status == 0) {
            this->node[0].x += SIZE;
            this->dirt = RIGHT;
            status = 1;
        }
        else {
            this->node[0].y += SIZE;
        }
    }
    if (this->dirt == LEFT) {
        //判断是否转向
        if (GetAsyncKeyState('W') && status == 0) {
            //this->node[0].y -= SIZE;
            this->dirt = UP;
            status = 1;
        }
        else if (GetAsyncKeyState('S') && status == 0) {
            this->node[0].y += SIZE;
            this->dirt = DOWN;
            status = 1;
        }
        else {
            this->node[0].x -= SIZE;
        }
    }
    if (this->dirt == UP) {
        //判断是否转向
        if (GetAsyncKeyState('A') && status == 0) {
            this->node[0].x -= SIZE;
            this->dirt = LEFT;
            status = 1;
        }
        else if (GetAsyncKeyState('D') && status == 0) {
            this->node[0].x += SIZE;
            this->dirt = RIGHT;
            status = 1;
        }
        else {
            this->node[0].y -= SIZE;
        }
    }
    //移动
    for (int i = 1; i < this->length; i++) {
        this->node[i].x = tmp[i - 1].x;
        this->node[i].y = tmp[i - 1].y;
    }
    Sleep(speed);
}
 
//绘制蛇
void Snake::Draw() {
    cleardevice();//清空原先的绘图
    srand((unsigned)time(NULL));//设置随机数种子
    for (int i = 0; i < this->length; i++) {
        setfillcolor(RGB(rand()%256,rand()%256,rand()%256));
        fillrectangle(this->node[i].x, this->node[i].y, this->node[i].x + SIZE, this->node[i].y + SIZE);
    }
}
 
//吃食物
bool Snake::Eat(Food food) {
    if (food.x == this->node[0].x && food.y == this->node[0].y) {
        if (this->node[length - 1].x - this->node[length - 2].x == 0 && this->node[length - 1].y - this->node[length - 2].y == -20) {
            this->length++;
            this->node[length - 1].x = this->node[length - 2].x;
            this->node[length - 1].y = this->node[length - 2].y-SIZE;
        }
        if (this->node[length - 1].x - this->node[length - 2].x == 0 && this->node[length - 1].y - this->node[length - 2].y == 20) {
            this->length++;
            this->node[length - 1].x = this->node[length - 2].x;
            this->node[length - 1].y = this->node[length - 2].y+SIZE;
        }
        if (this->node[length - 1].x - this->node[length - 2].x == 20 && this->node[length - 1].y - this->node[length - 2].y == 0) {
            this->length++;
            this->node[length - 1].x = this->node[length - 2].x+SIZE;
            this->node[length - 1].y = this->node[length - 2].y;
        }
        if (this->node[length - 1].x - this->node[length - 2].x == -20 && this->node[length - 1].y - this->node[length - 2].y == 0) {
            this->length++;
            this->node[length - 1].x = this->node[length - 2].x-SIZE;
            this->node[length - 1].y = this->node[length - 2].y;
        }
 
        return true;
    }
    return false;
}
 
//失败判定
bool Snake::Defeat() {
    //1.碰到边界
    if (this->node[0].x < 0 || this->node[0].x >= WIDTH || this->node[0].y < 0 || this->node[0].y >= HEIGHT) {
        return true;
    }
    //2.碰到自己的身体
    for (int i = 1; i < this->length; i++) {
        if (this->node[0].x == this->node[i].x && this->node[0].y == this->node[i].y) {
            return true;
        }
    }
    return false;
}
 
//食物的初始化
Food::Food(Snake snake)
{
    table:
    srand((unsigned)time(NULL));
    this->x = (rand() % (WIDTH / SIZE)) * SIZE;
    this->y = (rand() % (HEIGHT / SIZE)) * SIZE;
    this->score = rand() % 10+1;
    for (int i = 0; i < snake.length; i++) {
        if (snake.node[i].x == this->x && snake.node[i].y == this->y) {
            goto table;
        }
    }
}
 
//绘制食物
void Food::Draw() {
    setfillcolor(GREEN);
    fillrectangle(this->x, this->y, this->x + SIZE, this->y + SIZE);
}

C++小游戏贪吃蛇源代码》是一个经典的计算机科学项目,用于初学者熟悉面向对象编程和游戏编程的基本概念。这里简单介绍一下基本框架: 1. 定义主要类:首先创建`Snake`, `Food`和`Board`等类。`Snake`类通常包含位置、长度、移动方向等属性,以及更新位置的方法;`Food`代表食物随机生成的位置;`Board`则表示游戏区域,记录边界和蛇和食物的位置。 ```cpp class Snake { public: // 构造函数,初始化蛇的初始状态 Snake(); void move(); // 其他方法... private: int x, y; // 蛇的位置 int length; // 蛇的长度 // 点向量表示蛇的身体 }; ``` 2. 更新机制:`move()`方法根据用户输入或游戏规则(如默认向右移动)改变蛇的位置,并处理边界检查和碰撞检测(例如,蛇头碰到墙或自己的身体)。 3. 游戏循环:游戏的主要循环中会不断地绘制当前游戏状态(蛇和食物),获取用户的输入,然后调用蛇的移动方法。 ```cpp void gameLoop() { while (true) { drawBoard(); // 绘制游戏画面 handleInput(); // 处理用户输入 snake.move(); // 移动蛇 if (checkCollision()) { // 检查碰撞并结束游戏 break; } } } ``` 4. 食物生成和消耗:每次蛇吃掉食物后,增加长度并在地图上生成新的食物。当蛇吃掉食物时,它的位置应更新为新食物的位置。 完整源代码通常包括各种辅助函数,如键盘事件处理、画图函数、碰撞检测等。由于篇幅原因,这里无法展示整个源代码,但以上就是大致的设计结构。如果你对具体某个部分感兴趣,或者想学习更多细节,你可以查阅在线教程、书籍或GitHub上的开源项目作为参考。如果你想要深入研究,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值