c++简易贪吃蛇

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define High 20  // 游戏画面尺寸
#define Width 30
#define x first
#define y second
using namespace std;
int moveDirection; // 小蛇移动方向,上下左右分别用1,2,3,4表示
int dx[]= {0,-1,1,0,0};
int dy[]= {0,0,0,-1,1};
pair<int,int>food;
int g[High][Width] = {0}; // 二维数组存储游戏画布中对应的元素
// 0为空格,-1为边框#,-2为食物F,1为蛇头@,大于1的正数为蛇身*
void newfood() {
    int low=2;
    food.x = rand()%(High-1-2*low) + low;
    food.y = rand()%(Width-1-2*low) + low;
    while(g[food.x][food.y]!=0) {
        food.x = rand()%(High-1-2*low) + low;
        food.y = rand()%(Width-1-2*low) + low;
    }
    g[food.x][food.y] = -2;
}
void gotoxy(int x,int y) { //光标移动到(x,y)位置
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}
 
void moveSnakeByDirection() {
    int i,j,maxx = 0;
    pair<int,int>oldTail,oldHead,newHead;
//  扫描所有元素,找到正数元素都+1,同时找到蛇头和蛇尾
    for (i=1; i<High-1; i++)
        for (j=1; j<Width-1; j++) {
            if (g[i][j]>0) {
                if (maxx<g[i][j]) {
                    maxx = g[i][j];
                    oldTail=make_pair(i,j);
                }
                if (g[i][j]==1) {
                    oldHead=make_pair(i,j);
                }
                g[i][j]++;
            }
        }
    newHead.x=oldHead.x+dx[moveDirection];
    newHead.y=oldHead.y+dy[moveDirection];
    // 新蛇头如果吃到食物
    if (g[newHead.x][newHead.y]==-2) {
        g[food.x][food.y] = 0;
        // 产生一个新的食物
        newfood();
    } else // 否则,去掉原来的蛇尾
        g[oldTail.x][oldTail.y] = 0;
 
    // 蛇头撞到边界或身体(值不空),游戏结束
    if (g[newHead.x][newHead.y]!=0 ) {
        printf("游戏结束!\n你吃到了%d个食物\n",maxx-5);
        Sleep(1000);
        system("pause");
        exit(0);
    } else
        g[newHead.x][newHead.y] = 1;
    Sleep(80);
}
 
void startup() { // 初始化
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};  // 第二个值为0表示隐藏光标
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    srand(time(0));
    int i,j;
 
    // 初始化边框
    for (i=0; i<High; i++) {
        g[i][0] = -1;
        g[i][Width-1] = -1;
    }
    for (j=0; j<Width; j++) {
        g[0][j] = -1;
        g[High-1][j] = -1;
    }
 
    // 初始化蛇头位置
    g[High/2][Width/2] = 1;
    // 初始化蛇身,画布中元素值分别为2,3,4,5....
    for (i=1; i<=4; i++)
        g[High/2][Width/2-i] = i+1;
 
    // 初始小蛇向右移动
    moveDirection = 4;
    newfood();
}
 
void show() { // 显示画面
    gotoxy(0,0);  // 光标移动到原点位置,重画界面
    int i,j;
    for (i=0; i<High; i++) {
        for (j=0; j<Width; j++) {
            if (g[i][j]==0)
                printf(" ");   //   输出空格
            else if (g[i][j]==-1)
                printf("#");   //   输出边框#
            else if (g[i][j]==1)
                printf("@");   //   输出蛇头@
            else if (g[i][j]>1)
                printf("*");   //   输出蛇身*
            else if (g[i][j]==-2)
                printf("F");   //   输出食物F
        }
        printf("\n");
    }
}
 
void updateWithInput() { // 与用户输入有关的更新
    if (GetAsyncKeyState('A') && moveDirection!=4) {
        moveDirection = 3;   // 位置左移
        moveSnakeByDirection();
    } else if (GetAsyncKeyState('D') && moveDirection!=3) {
        moveDirection = 4;  // 位置右移
        moveSnakeByDirection();
    } else if (GetAsyncKeyState('W') && moveDirection!=2) {
        moveDirection = 1;  // 位置上移
        moveSnakeByDirection();
    } else if (GetAsyncKeyState('S') && moveDirection!=1) {
        moveDirection = 2;   // 位置下移
        moveSnakeByDirection();
    }
}
 
int main() {
    startup();  // 数据初始化
    while (1) { //  游戏循环执行
        show();  // 显示画面
        moveSnakeByDirection();
        updateWithInput();  // 与用户输入有关的更新
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值