欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++加EasyX简单贪吃蛇游戏

程序员文章站 2024-03-18 10:08:46
...

C++加EasyX简单贪吃蛇游戏

游戏规则就不多说了

新建一个C++控制台应用,项目结构如下
C++加EasyX简单贪吃蛇游戏

snake.h包含了用到的所有方法的定义
snake.cpp包含了所有方法的实现

snake.h代码如下

#pragma once
#include <iostream>
#include<graphics.h>
#include<time.h>
#include<conio.h>
#include<vector>
using namespace std;


void initSnaker();//初始化????
void drawSnake();//画????
void  moveSnaker();//移动????
void keyDown();//键盘事件
void  initFood(int num);//初始化食物
void drawFood();//画食物
void eatFood();//吃食物
void showGrade();//显示分数
bool gameOver();//判断是否挂掉

snake.cpp代码如下

#include "snake.h"


//坐标
struct  point
{
    int x;
    int y;
};

//????
struct snaker
{
    vector<point> xy;	// 每节坐标
    point next; //下一节预留位置
    vector<COLORREF> color;//每一节的颜色
    int num;//长度
    int position;//方向
}snaker;

//食物
struct Food
{
    point fxy[10];//食物座标
    int grade;//分数
    int num = 1;//食物总数
    COLORREF color[10];//实物颜色
}food;

//蛇的方向
enum  position
{
    u,
    d,
    l,
    r
};
//初始化????
void initSnaker()
{
    point xy;
    xy.x = 20;
    xy.y = 0;
    snaker.xy.push_back(xy);
    snaker.color.push_back(RGB(rand() % 256, rand() % 256, rand() % 256));
    xy.x = 10;
    xy.y = 0;
    snaker.xy.push_back(xy);
    snaker.color.push_back(RGB(rand() % 256, rand() % 256, rand() % 256));
    xy.x = 0;
    xy.y = 0;
    snaker.xy.push_back(xy);
    snaker.color.push_back(RGB(rand() % 256, rand() % 256, rand() % 256));
    snaker.num = 3;
    snaker.position = r;
}


//画????
void drawSnake()
{
    for (int i = 0; i < snaker.num; i++)
    {
        setfillcolor(snaker.color[i]);
        fillrectangle(snaker.xy[i].x, snaker.xy[i].y, snaker.xy[i].x + 10, snaker.xy[i].y + 10);
    }
}

//移动蛇
void  moveSnaker()
{
    snaker.next = snaker.xy[snaker.num - 1];
    //除蛇头以外其他都移动到前一个位置
    for (int i = snaker.num - 1; i >= 1; i--)
    {
        snaker.xy[i] = snaker.xy[i - 1];
    }
    //移动蛇头
    switch (snaker.position)
    {
    case u:
        snaker.xy[0].y -= 10;
        break;
    case d:
        snaker.xy[0].y += 10;
        break;
    case l:
        snaker.xy[0].x -= 10;
        break;
    case r:
        snaker.xy[0].x += 10;
        break;
    default:
        break;
    }
}

void keyDown()
{
    char key = _getch();
    if (key == -32)//方向键判断
    {
        key = -_getch();//具体的方向键
    }
    switch (key)
    {
    case 'w':
    case 'W':
    case -72:
        if (snaker.position != d)
            snaker.position = u;
        break;
    case 's':
    case 'S':
    case -80:
        if (snaker.position != u)
            snaker.position = d;
        break;
    case 'a':
    case 'A':
    case -75:
        if (snaker.position != r)
            snaker.position = l;
        break;
    case 'd':
    case 'D':
    case -77:
        if (snaker.position != l)
            snaker.position = r;
        break;
    }
}
//初始化实物
void  initFood(int num)//食物编号
{
    food.fxy[num].x = rand() % 80 * 10;
    food.fxy[num].y = rand() % 60 * 10;
    for (int i = 0; i < snaker.num; i++)
    {
        if (food.fxy[num].x == snaker.xy[i].x && food.fxy[num].y == snaker.xy[i].y) //判断食物是否再????身上
        {
            food.fxy[num].x = rand() % 80 * 10;
            food.fxy[num].y = rand() % 600 * 10;
        }
    }
}
//画食物
void drawFood()
{
    for (int i = 0; i <= food.num - 1; i++)
    {
        setfillcolor(food.color[i] = RGB(rand() % 256, rand() % 253, rand() % 256));
        fillrectangle(food.fxy[i].x, food.fxy[i].y, food.fxy[i].x + 10, food.fxy[i].y + 10);
    }
}

//吃食物
void eatFood()
{
    for (int i = 0; i <= food.num - 1; i++)
    {
        if (snaker.xy[0].x == food.fxy[i].x && snaker.xy[0].y == food.fxy[i].y)
        {
            snaker.num += 1;
            snaker.xy.push_back(snaker.next);//新增的放大蛇尾
            snaker.color.push_back(food.color[i]);//新增的颜色等于吃掉的食物的颜色
            food.grade += 100;//吃掉一个食物得100分
            initFood(i);
            if (food.num < 10 && food.grade % 500 == 0 && food.grade > 0)
            {
                food.num++;									// 每得 500 分,增加一个食物,但食物总数不超过 10 个
                initFood(food.num - 1);						// 初始化新增加的食物
            }
        }
    }
}

//显示分数
void showGrade()
{
    wchar_t g[20] = L"";
    swprintf_s(g, L"分数:%d", food.grade);
    outtextxy(650, 50, g);
}


//游戏结束
bool gameOver()
{
    //撞墙 分为四个方向
    if (snaker.position == l && snaker.xy[0].x < -10) return true;
    if (snaker.position == r && snaker.xy[0].x > 810) return  true;
    if (snaker.position == u && snaker.xy[0].y < -10)return true;
    if (snaker.position == d && snaker.xy[0].y > 610)return true;

    //撞自己 不包括蛇头 分为四个方向
    for (int i = 1; i < snaker.num; i++)
    {
        if (snaker.xy[0].x <= snaker.xy[i].x + 10 && snaker.xy[0].x >= snaker.xy[i].x && snaker.xy[0].y == snaker.xy[i].y && snaker.position == l)
            return true;
        if (snaker.xy[0].x + 10 >= snaker.xy[i].x && snaker.xy[0].x + 10 <= snaker.xy[i].x + 10 && snaker.xy[0].y == snaker.xy[i].y && snaker.position == r)
            return true;
        if (snaker.xy[0].y <= snaker.xy[i].y + 10 && snaker.xy[0].y >= snaker.xy[i].y && snaker.xy[0].x == snaker.xy[i].x && snaker.position == u)
            return true;
        if (snaker.xy[0].y + 10 >= snaker.xy[i].y && snaker.xy[0].y + 10 <= snaker.xy[i].y + 10 && snaker.xy[0].x == snaker.xy[i].x && snaker.position == d)
            return true;
    }
    return false;


}

主程序代码如下

// 简单贪吃蛇.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include<graphics.h>
#include<time.h>
#include<conio.h>
#include<vector>
#include "snake.h"
using namespace std;


int main()
{
    initgraph(800, 600);   
    setbkcolor(RGB(95, 183, 72));
    cleardevice();
    srand((unsigned)time(NULL));
    initSnaker();
    drawSnake();
    initFood(0);
    while (!gameOver())
    {
        Sleep(150);
        BeginBatchDraw();			// 开始批量绘图,作用是避免闪烁
        cleardevice();
        if (_kbhit()) keyDown();
        moveSnaker();
        eatFood();
        drawFood();
        drawSnake();
        showGrade();
        EndBatchDraw();				// 结束批量绘图
    }
     outtextxy(330, 300, L"游戏结束,按任意键继续");
    _getch();						// 按任意键退出
    return 0;
}


运行效果如图
C++加EasyX简单贪吃蛇游戏