DEV-C++ 贪吃蛇小游戏免费复制

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <cmath>
#include <windows.h>
using namespace std;
 
/*** 光标定位 ***/
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
 
void locate(int x,int y)
{
    coord.X=y;
    coord.Y=x;
    SetConsoleCursorPosition(hout,coord);
};
 
/*** 隐藏光标 ***/
void hide()
{
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(hout, &cursor_info);
}
 
/*** 生成随机数 ***/
double random(double start, double end)
{
    return start+(end-start)*rand()/(RAND_MAX + 1.0);
}
 
/*** 定义地图的长宽,蛇的坐标,长度,方向,食物的位置 ***/
int m,n;
 
struct node
{
    int x,y;
}snake[1000];
 
int snake_length,dir;
node food;
int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
 
/*** 输出墙 ***/
void print_wall()
{
    cout << " ";
    for (int i=1;i<=n;i++)
        cout << "-";
    cout << endl;
    for (int j=0;j<=m-1;j++)
    {
        cout << "|";
        for (int i=1;i<=n;i++) cout << " ";
        cout << "|" << endl;
    }
    cout << " ";
    for (int i=1;i<=n;i++)
        cout << "-";
}
 
/*** 首次输出蛇,其中snake[0]代表头 ***/
void print_snake()
{
    locate(snake[0].x,snake[0].y);
    cout << "@";
    for (int i=1;i<=snake_length-1;i++)
    {
        locate(snake[i].x,snake[i].y);
        cout << "*";
    }
}
 
/*** 判断是否撞墙或者自撞 ***/
bool is_correct()
{
    if (snake[0].x==0 || snake[0].y==0 || snake[0].x==m+1 || snake[0].y==n+1) return false;
    for (int i=1;i<=snake_length-1;i++)
    {
        if (snake[0].x==snake[i].x && snake[0].y==snake[i].y) return false;
    }
    return true;
}
 
/*** 随机生成并输出食物位置 ***/
bool print_food()
{
    srand((unsigned)time(0));
    bool e;
    while (1)
    {
        e=true;
        int i=(int) random(0,m)+1,j=(int) random(0,n)+1;
        food.x=i;food.y=j;
        for (int k=0;k<=snake_length-1;k++)
        {
            if (snake[k].x==food.x && snake[k].y==food.y)
            {
                e=false;break;
            }
        }
        if (e) break;
    }
    locate(food.x,food.y);
    cout << "$";
    return true;
}
 
/*** 蛇的前进 ***/
bool go_ahead()
{
    node temp;
    bool e=false;
    temp=snake[snake_length-1];
    for (int i=snake_length-1;i>=1;i--)
        snake[i]=snake[i-1];
    snake[0].x+=direct[dir][0];
    snake[0].y+=direct[dir][1];
    locate(snake[1].x,snake[1].y);
    cout << "*";
    /*** 吃到了食物 ***/
    if (snake[0].x==food.x && snake[0].y==food.y)
    {
        snake_length++;
        e=true;
        snake[snake_length-1]=temp;
    }
    /*** 输出此时蛇状态 ***/
    if (!e)
    {
        locate(temp.x,temp.y);
        cout << " ";
    }
    else
        print_food();
    locate(snake[0].x,snake[0].y);
    cout << "@";
    /*** 如果自撞 ***/
    if (!is_correct())
    {
        system("cls");
        cout << "You lose!" << endl << "Length: " << snake_length << endl;
        return false;
    }
    return true;
}
 
/*** 主函数 ***/
int main()
{
	system("mode con cols=80 lines=30"); //设置cmd窗口大小
	system("color 2"); //设置字符颜色:0 黑;1 深蓝;2 深绿;3 深青;4 深红;5 深紫;6 深褐 
    cout << "--------------------贪吃蛇---------------------" << endl;
    cout << "先选择难度.请在1-10中输入1个数,1最简单,10则最难" << endl;
    cout << "然后进入游戏画面,以方向键控制方向.祝你游戏愉快!" << endl;
    cout << "作者:XXX 未经允许不准抄袭!!!" << endl; //可以自定义,可以个性化 //注意!!!不要写脏话 
    cout << "-----------------------------------------------" << endl;
    cout << "请输入难度级别:" ;
    m=25;
    n=40; 
    if (m<10 || n<10 || m>25 || n>40)
    {
        cout << "ERROR" << endl;
        system("pause");
        return 0;
    }
    int hard;
    cin >> hard;
    if (hard<=0 || hard>100)
    {
        cout << "ERROR" << endl;
        system("pause");
        return 0;
    }
    /*** 数据全部初始化,包括蛇长,位置,方向 ***/
    snake_length=5;
    clock_t a,b;
    char ch;
    double hard_len;
    for (int i=0;i<=4;i++)
    {
        snake[i].x=1;
        snake[i].y=5-i;
    }
    dir=3;
    /*** 输出初始地图,蛇与食物 ***/
    system("cls");
    hide();
    print_wall();
    print_food();
    print_snake();
    locate(m+2,0);
    cout << "Now length: ";
    /*** 开始游戏 ***/
    while (1)
    {
        /*** 难度随长度增加而提高 ***/
        hard_len=(double)snake_length/(double) (m*n);
        /*** 调节时间,单位是ms ***/
        a=clock();
        while (1)
        {
            b=clock();
            if (b-a>=(int)(400-30*hard)*(1-sqrt(hard_len))) break;
        }
        /*** 接受键盘输入的上下左右,并以此改变方向 ***/
        if (kbhit())
        {
            ch=getch();
            if (ch==-32)
            {
                ch=getch();
                switch(ch)
                {
                case 72:
                    if (dir==2 || dir==3)
                        dir=0;
                    break;
                case 80:
                    if (dir==2 || dir==3)
                        dir=1;
                    break;
                case 75:
                    if (dir==0 || dir==1)
                        dir=2;
                    break;
                case 77:
                    if (dir==0 || dir==1)
                        dir=3;
                    break;
                }
            }
        }
        /*** 前进 ***/
        if (!go_ahead()) break;
        /*** 在最后输出此时长度 ***/
        locate(m+2,12);
        cout << snake_length;
    }
    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Dev-C++编写贪吃蛇的基本步骤: 1. 创建一个新的C++项目,并命名为“贪吃蛇”。 2. 在项目中创建一个新的C++源文件,并将其命名为“main.cpp”。 3. 在“main.cpp”文件中,包含必要的头文件和命名空间: ``` #include <iostream> #include <conio.h> #include <windows.h> using namespace std; ``` 4. 定义贪吃蛇和食物的结构体: ``` struct Snake{ int x, y; }; struct Food{ int x, y; }; ``` 5. 定义全局变量和常量: ``` const int mapWidth = 20; const int mapHeight = 20; const int snakeMaxLen = 100; const int interval = 100; int direction = 0; int score = 0; int snakeLen = 3; Snake snake[snakeMaxLen]; Food food; ``` 6. 初始化游戏窗口和贪吃蛇的初始位置: ``` void init(){ system("mode con cols=40 lines=25");//设置窗口大小 srand((unsigned)time(NULL));//初始化随机数种子 snake[0].x = 10; snake[0].y = 10; snake[1].x = 9; snake[1].y = 10; snake[2].x = 8; snake[2].y = 10; food.x = rand() % mapWidth; food.y = rand() % mapHeight; } ``` 7. 定义绘制函数,用于绘制游戏界面: ``` void draw(){ system("cls");//清屏 for(int i = 0; i < mapHeight; i++){ for(int j = 0; j < mapWidth; j++){ if(i == 0 || i == mapHeight - 1 || j == 0 || j == mapWidth - 1){ cout << "#";//绘制边框 }else if(i == snake[0].y && j == snake[0].x){ cout << "H";//绘制蛇头 }else if(i == food.y && j == food.x){ cout << "*";//绘制食物 }else{ bool isBody = false; for(int k = 1; k < snakeLen; k++){ if(i == snake[k].y && j == snake[k].x){ cout << "o";//绘制蛇身 isBody = true; break; } } if(!isBody){ cout << " ";//绘制空格 } } } cout << endl; } cout << "Score: " << score << endl;//显示得分 } ``` 8. 定义移动函数,用于控制贪吃蛇的移动: ``` void move(){ for(int i = snakeLen - 1; i > 0; i--){ snake[i].x = snake[i - 1].x; snake[i].y = snake[i - 1].y; } switch(direction){ case 0://向上移动 snake[0].y--; break; case 1://向下移动 snake[0].y++; break; case 2://向左移动 snake[0].x--; break; case 3://向右移动 snake[0].x++; break; } } ``` 9. 定义碰撞检测函数: ``` bool check(){ if(snake[0].x == 0 || snake[0].x == mapWidth - 1 || snake[0].y == 0 || snake[0].y == mapHeight - 1){ return true;//撞到边界 } for(int i = 1; i < snakeLen; i++){ if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){ return true;//撞到自己 } } if(snake[0].x == food.x && snake[0].y == food.y){ score += 10;//吃到食物 snakeLen++; food.x = rand() % mapWidth; food.y = rand() % mapHeight; } return false; } ``` 10. 在主函数中,循环执行绘制、移动和碰撞检测等操作: ``` int main(){ init(); while(true){ if(_kbhit()){ char ch = _getch(); switch(ch){ case 'w'://向上移动 if(direction != 1){ direction = 0; } break; case 's'://向下移动 if(direction != 0){ direction = 1; } break; case 'a'://向左移动 if(direction != 3){ direction = 2; } break; case 'd'://向右移动 if(direction != 2){ direction = 3; } break; } } move(); if(check()){ cout << "Game Over!" << endl; break; } draw(); Sleep(interval);//延迟 } return 0; } ``` 11. 编译并运行程序,即可开始游戏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值