《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(); }