《Matchman Fight》

我的第一个格斗游戏,自我感觉还不错,以后会改进。

#include <iostream>
#include <cstdio>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <stdlib.h>

using namespace std;

int sumPlayer = 0;
int sumEnemy = 75;
int hpPlayer = 100;
int hpEnemy = 100;
bool programIsRunning = true;

char player[5][5];
char enemy[5][5];

char playerDefault[5][5] =
{
    {' ', ' ', 'O', ' ', ' '},
    {' ', '/','|', '\\', '_'},
    {' ', '\\', '|', ' ', ' '},
    {' ', '/', ' ', '\\', ' '},
    {'/', ' ', ' ', ' ', '\\'}
};

char playerHandAttack[5][5] =
{
    {' ', ' ', 'O', '_', '_'},
    {' ', '/', '|', ' ', ' '},
    {' ', '\\', '|', ' ', ' '},
    {' ', '/', ' ', '\\', ' '},
    {'/', ' ', ' ', ' ', '\\'}
};

char playerFootAttack[5][5] =
{
    {' ', ' ', ' ', ' ', ' '},
    {'O', '_', '_', ' ', '/'},
    {'/', '\\', ' ', '/', ' '},
    {'\\', ' ', '|', ' ', ' '},
    {' ', ' ', '|', ' ', ' '}
};

char playerDie[5][5] = 
{
    {' ', ' ', '/', ' ', ' '},
    {'O', '/', '_', '_', ' '},
    {' ', '\\', ' ', ' ', '/'},
    {' ', ' ', '\\', '/', ' '},
    {' ', ' ', ' ', '\\', '_'}
};

char enemyDefault[5][5] =
{
    {' ', ' ', 'O', ' ', ' '},
    {'_', '/', '|', '\\', ' '},
    {' ', ' ', '|', '/', ' '},
    {' ', '/', ' ', '\\', ' '},
    {'/', ' ', ' ', ' ', '\\'}
};

char enemyHandAttack[5][5] =
{
    {'_', '_', 'O', ' ', ' '},
    {' ', ' ', '|', '\\', ' '},
    {' ', ' ', '|', '/', ' '},
    {' ', '/', ' ', '\\', ' '},
    {'/', ' ', ' ', ' ', '\\'}
};

char enemyFootAttack[5][5] =
{
    {' ', ' ', ' ', ' ', ' '},
    {'\\', ' ', '_', '_', 'O'},
    {' ', '\\', ' ', '/', '\\'},
    {' ', ' ', '|', ' ', '/'},
    {' ', ' ', '|', ' ', ' '}
};

char enemyDie[5][5] =
{
    {' ', ' ', '\\', ' ', ' '},
    {' ', '_', '_', '\\', 'O'},
    {'\\', ' ', ' ', '/', ' '},
    {' ', '\\', '/', ' ', ' '},
    {'_', '/', ' ', ' ', ' '}
};

void PlayerControl();
void EnemyControl();
void Show();
void SetColor(int);

int suiJi();

int main(int argc, char const *argv[])
{
    for (int i=0; i<5; i++)
    {
        for (int j=0; j<5; j++)
        {
            player[i][j] = playerDefault[i][j];
        }
    }
    for (int i=0; i<5; i++)
    {
        for (int j=0; j<5; j++)
        {
            enemy[i][j] = enemyDefault[i][j];
        }
    }
    while (programIsRunning)
    {
        Show();
        PlayerControl();
        if (programIsRunning) EnemyControl();
    }
    Show();
    if (hpPlayer > 0 and hpEnemy == 0)
    {
        SetColor(11);
        for (int i=1; i<=39; i++) cout << ' ';
        cout << "win" << endl;
        SetColor(7);
    }
    else if (hpPlayer == 0)
    {
        SetColor(12);
        for (int i=1; i<=39; i++) cout << ' ';
        cout << "lose" << endl;
        SetColor(7);
    }
    getch();
    cout << "Press any key to continue...";
    getch();
    return 0;
}

void PlayerControl()
{
    while (kbhit())
    {
        char ch = getch();
        if (ch == 'a')
        {
            if (sumPlayer > 0)
            {
                sumPlayer--;
            }

        }
        else if (ch == 'd')
        {
            if (sumPlayer < sumEnemy-5)
            {
                sumPlayer++;
            }

        }
        else if (ch == 'j')
        {
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    player[i][j] = playerHandAttack[i][j];
                }
            }
            Show();
            if (sumEnemy == sumPlayer+5)
            {
                hpEnemy -= 10;
                if (hpEnemy <= 0)
                {
                    hpEnemy = 0;
                    for (int i=0; i<5; i++)
                    {
                        for (int j=0; j<5; j++)
                        {
                            enemy[i][j] = enemyDie[i][j];
                        }
                    }
                    programIsRunning = false;
                }
            }
            Show();
            Show();
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    player[i][j] = playerDefault[i][j];
                }
            }
        }
        else if (ch == 'i')
        {
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    player[i][j] = playerFootAttack[i][j];
                }
            }
            Show();
            if (sumEnemy == sumPlayer+5)
            {
                hpEnemy -= 20;
                if (sumEnemy <= 75-10)
                {
                    sumEnemy += 10;
                }
                if (hpEnemy <= 0)
                {
                    hpEnemy = 0;
                    for (int i=0; i<5; i++)
                    {
                        for (int j=0; j<5; j++)
                        {
                            enemy[i][j] = enemyDie[i][j];
                        }
                    }
                    programIsRunning = false;
                }
            }
            Show();
            Show();
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    player[i][j] = playerDefault[i][j];
                }
            }
        }
        else if (ch == 'q')
        {
            programIsRunning = false;
        }
    }
}

void EnemyControl()
{
    if (sumEnemy > sumPlayer+5)
    {
        sumEnemy--;
    }
    else
    {
        int sj = suiJi()%2;
        if (sj == 0)
        {
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    enemy[i][j] = enemyHandAttack[i][j];
                }
            }
            Show();
            if (sumEnemy == sumPlayer+5)
            {
                hpPlayer -= 10;
                if (hpPlayer <= 0)
                {
                    hpPlayer = 0;
                    for (int i=0; i<5; i++)
                    {
                        for (int j=0; j<5; j++)
                        {
                            player[i][j] = playerDie[i][j];
                        }
                    }
                    programIsRunning = false;
                }
            }
            Show();
            Show();
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    enemy[i][j] = enemyDefault[i][j];
                }
            }
        }
        else
        {
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    enemy[i][j] = enemyFootAttack[i][j];
                }
            }
            Show();
            if (sumEnemy == sumPlayer+5)
            {
                hpPlayer -= 20;
                if (sumPlayer >= 10)
                {
                    sumPlayer -= 10;
                }
                if (hpPlayer <= 0)
                {
                    hpPlayer = 0;
                    for (int i=0; i<5; i++)
                    {
                        for (int j=0; j<5; j++)
                        {
                            player[i][j] = playerDie[i][j];
                        }
                    }
                    programIsRunning = false;
                }
            }
            Show();
            Show();
            for (int i=0; i<5; i++)
            {
                for (int j=0; j<5; j++)
                {
                    enemy[i][j] = enemyDefault[i][j];
                }
            }
        }
    }
}

void Show()
{
    Sleep(80);
    system("cls");
    for (int i=0; i<5; i++)
    {
        for (int j=1; j<=sumPlayer; j++)
        {
            cout << ' ';
        }
        for (int j=0; j<5; j++)
        {
            cout << player[i][j];
        }
        for (int j=1; j<=sumEnemy-sumPlayer-5; j++)
        {
            cout << ' ';
        }
        for (int j=0; j<5; j++)
        {
            cout << enemy[i][j];
        }
        if (sumEnemy < 75)
        {
            cout << endl;
        }
    }
    cout << "================================================================================";
    SetColor(11);
    cout << "player";
    SetColor(7);
    cout << "|";
    SetColor(10);
    for (int i=1; i<=hpPlayer/10; i++) cout << "###";
    for (int i=1; i<=10-hpPlayer/10; i++) cout << "   ";
    SetColor(7);
    for (int i=1; i<=7; i++) cout << "|";
    SetColor(10);
    for (int i=1; i<=10-hpEnemy/10; i++) cout << "   ";
    for (int i=1; i<=hpEnemy/10; i++) cout << "###";
    SetColor(7);
    cout << "|";
    SetColor(12);
    cout << "enemy";
    SetColor(7);
    cout << "--------------------------------------------------------------------------------";
    cout << "Press a, d to move, press j, i to attack." << endl;
}

void SetColor(int a)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}

int suiJi()
{
    srand(time(0));
    return rand();
}
全部评论

相关推荐

11-03 14:38
重庆大学 Java
AAA求offer教程:我手都抬起来了又揣裤兜了
点赞 评论 收藏
分享
10-09 00:50
已编辑
长江大学 算法工程师
不期而遇的夏天:1.同学你面试评价不错,概率很大,请耐心等待;2.你的排名比较靠前,不要担心,耐心等待;3.问题不大,正在审批,不要着急签其他公司,等等我们!4.预计9月中下旬,安心过节;5.下周会有结果,请耐心等待下;6.可能国庆节前后,一有结果我马上通知你;7.预计10月中旬,再坚持一下;8.正在走流程,就这两天了;9.同学,结果我也不知道,你如果查到了也告诉我一声;10.同学你出线不明朗,建议签其他公司保底!11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务