C++追赶游戏

废话不多说,上代码:

#include <conio.h>
#include <iostream>
#include <windows.h>
using namespace std;

int jp(int a = 0) {
    int ch;

    if (_kbhit()) {
        ch = _getch();

        if (ch == 119) {//119上 97左 100右 115下 n--上 m--左
            return 0;
        }

        if (ch == 97) {
            return 1;
        }

        if (ch == 100) {
            return 2;
        }

        if (ch == 115) {
            return 3;
        }

    }

    return 5;
}

int main() {
    int ch, n, m, h = 0, q, e, fs = 0;
    char x[11][11];

    for (int i = 0; i < 11; i++) {
        for (int j = 0; j < 11; j++) {
            x[i][j] = '#';
        }
    }

    x[5][5] = '@';
    x[0][0] = '!';
    n = 5;
    m = 5;
    q = -1;
    e = -1;
    Sleep(500);

    while (1) {
        system("cls");
        h = jp(ch);

        if (h == 0) {
            x[n][m] = '#';
            n--;
            x[n][m] = '@';
        }

        if (h == 1) {
            x[n][m] = '#';
            m--;
            x[n][m] = '@';
        }

        if (h == 2) {
            x[n][m] = '#';
            m++;
            x[n][m] = '@';
        }

        if (h == 3) {
            x[n][m] = '#';
            n++;
            x[n][m] = '@';
        }//q--上 e--左

        if (q > n) {
            x[q][e] = '#';
            q--;
            x[q][e] = '!';
        } else if (q < n) {
            x[q][e] = '#';
            q++;
            x[q][e] = '!';
        } else if (e > m) {
            x[q][e] = '#';
            e--;
            x[q][e] = '!';
        } else if (e < m) {
            x[q][e] = '#';
            e++;
            x[q][e] = '!';
        }

        if (h == 5) {
            ch = q;
            q = n;
            n = ch;
            ch = e;
            e = m;
            m = ch;
            x[q][e] = '!';
            x[n][m] = '@';
            ch = 0;
        }

        for (int i = 0; i < 11; i++) {
            for (int a = 0, j = i; a < 11; a++) {
                cout << x[j][a] << " ";
                x[q][e] = '!';
                x[n][m] = '@';

                if (q != 0 && e != 0) { //补丁
                    x[0][0] = '#';
                }
            }

            cout << "\n";
        }

        cout << n << " " << m << " || " << q << " " << e << "\n分数:" << fs;

        if (q == n && e == m) {
            system("cls");
            cout << "你输了!";
            Sleep(1000);
            return 0;
        }

        x[q][e] = '!';
        x[n][m] = '@';
        Sleep(700);
        fs++;

        if (fs == 50) {
            system("cls");
            cout << "你赢了!";
            Sleep(1000);
            return 0;
        }
    }

}

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值