一个C++双人对战小游戏(DEV-c++可运)

        一个很简单的小游戏,两个人选择角色,对战,谁先把对面血量归零谁赢。

代码:(windows环境下,DEV-c++可运)

#include <iostream>
#include <cmath>
#include <ctime>
#include <random>
#include <windows.h>
#include <conio.h>

#define rowsize 20
#define linesize 30
#define pdsize 12 //player display size

/* colorid define */
#define red 1
#define blue 2
#define green 3
#define yellow 4
#define purple 5
#define white 6

/* roleid define */
#define swordsman 1 //■->| 
#define fighter 2 // ●●■●●(up and down either)
#define marksman 3 //■->●(moving)
 
using namespace std;

clock_t gamestart = clock();
clock_t nowt = clock();
const string block = "■";
const string ball = "●";
const string space = "  ";
const char up = 'w';
const char down = 's';
const char esc = '-';
const char enter = '+';
const int limittime = 180;
int a[rowsize + 2][linesize + 2] = {
  {0}}; //map
int p1[rowsize + 2][pdsize + 2] = {
  {0}};
int p2[rowsize + 2][pdsize + 2] = {
  {0}};

class player{ //The player
	public:
		player(int x, int y, char upop){
			this -> x = x;
			this -> y = y;
			this -> upop = upop;
		}
		player(){;}
		int x, y; //the pos
		int roleid = 1; // The roleid
		int blood = 1000;
		int cd = 10;
		char upop;
} player1(1, 1, 's'), player2(rowsize, linesize, '8'); //the born place

int countdigit(int n){
	int ret = 0, m = n;
	while (m != 0)
		m /= 10, ret += 1;
	return ret;
}

void HideCursor(void){ //hide the cursur (for 'clear')
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void clear(void){ //clear the screen
	HideCursor();
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos; pos.X = 0, pos.Y = 0;
    SetConsoleCursorPosition(handle, pos);
}

void clr(void){ //clear the all screen
	HideCursor();
	system("cls");
}

void color(int colorid){
	switch (colorid){
		case 1:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); break;
		case 2:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE); break;
		case 3:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN); break;
		case 4:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); break;
		case 5:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); break;
		case 6:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break;
		default:
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break;
	}
}

void SetFont(int size = 30) {
	CONSOLE_FONT_INFOEX cfi;
	cfi.cbSize = sizeof cfi;
	cfi.nFont = 0;
	cfi.dwFontSize.X = 0;
	cfi.dwFontSize.Y = size;  //设置字体大小
	cfi.FontFamily = FF_DONTCARE;
	cfi.FontWeight = FW_NORMAL; //字体粗细 FW_BOLD
	SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
}

void initialize(void){ //the initialization
	/* The map initialization */
	for (int i = 0; i <= rowsize + 1; i++)
		for (int j = 0; j <= linesize + 1; j++)
			if (a[i][j] != 13 && a[i][j] != 14 && a[i][j] != 15 && a[i][j] != -1)
				a[i][j] = 0; 
	
	for (int i = 0; i <= rowsize + 1; i++)
		a[i][0] = 1, a[i][linesize + 1] = 1;
	for (int i = 0; i <= linesize + 1; i++)
		a[0][i] = 1, a[rowsize + 1][i] = 1;
		
	/* The player1 initialization */
	for (int i = 0; i <= rowsize + 1; i++)
		p1[i][0] = 1;
	for (int i = 0; i <= pdsize + 1; i++)
		p1[0][i] = 1, p1[rowsize + 1][i] = 1;
	for (int i = 1; i <= 4; i++)
		for (int j = 2; j <= pdsize + 1; j++)
			p1[i][j] = 4;
	for (int i = 1; i <= 4; i++)
		p1[i][1] = 4 + i;
		
	/* The player2 initialization */
	for (int i = 0; i <= rowsize + 1; i++)
		p2[i][pdsize + 1] = 1;
	for (int i = 0; i <= pdsize + 1; i++)
		p2[0][i] = 1, p2[rowsize + 1][i] = 1;
	for (int i = 1; i <= 4; i++)
		for (int j = 0; j <= pdsize; j++)
			p2[i][j] = 4;
	for (int i = 1; i <= 4; i++)
		p2[i][1] = 8 + i;
		
	a[player1.x][player1.y] = 2, a[player2.x][player2.y] = 3;
}

void print(int k){ //turn digits into char
	if (k == -1) //-1 refer to block which can be through
		cout << block;
	if (k == 0) //0 refer to nothing
		cout << space;
	if (k == 1) //1 refer to block
		cout << block;
	if (k == 2){ //2 refer to player1
		color(red);
		cout << block;
		color(white);
	}
	if (k == 3){ //3 refer to player2
		color(blue);
		cout << block;
		color(white);
	}
	if (k == 4) ; //4 refer to skip
	if (k == 5){ //5-8 refer to where the words start(player1)
		color(red);
		cout << "player1 ";
		for (int i = 1; i <= pdsize - 4 + 1; i++)
			cout << space;
		color(white);
	}
	if (k == 6){ 
		color(red);
		int digit = 0;
		if (player1.roleid == 1){
			cout << "player1's role:swordsman";
			digit = 12;
		}
		if (player1.roleid == 2){
			cout << "player1's role:fighter";
			digit = 11;
		}
		if (player1.roleid == 3){
			cout << "player1's role:marksman ";
			digit = 12;
		}	
		if (player1.roleid == 4){	
			cout << "player1's role:bomber ";
			digit = 11;
		}
		for (int i = 1; i <= pdsize - digit + 1; i++)
			cout << space;
		color(white);
	}
	if (k == 7){ 
		color(red);
		if (player1.blood < 0)
			player1.blood = 0;
		cout << "player1's blood:" << player1.blood;
		for (int i = 1; i <= pdsize - 8 - (countdigit(player1.blood) / 2); i++)
			cout << space;
		if (player1.blood != 0)
			(countdigit(player1.blood) % 2 == 1) ? cout << ' ' : cout << space;
		else	
			cout << ' ';
		color(white);
	}
	if (k == 8){ 
		color(red);
		cout << "player1's cd:" << player1.cd << ' ';
		for (int i = 1; i <= pdsize - 7 - countdigit(player1.cd) / 2; i++)
			cout << space;
		if (player1.cd != 0)
			(countdigit(player1.cd) % 2 == 1) ? cout << ' ' : cout << space;
		else	
			cout << ' ';
		color(white);
	}
	if (k == 9){ //9-12 refer to where the words start(player2)
		color(blue);
		cout << "player2 ";
		for (int i = 1; i <= pdsize - 4 + 1; i++)
			cout << space;
		color(white);
	}
	if (k == 10){ 
		color(blue);
		int digit = 0;
		if (player2.roleid == 1){
			cout << "player2's role:swordsman";
			digit = 12;
		}
		if (player2.roleid == 2){
			cout << "player2's role:fighter";
			digit = 11;
		}
		if (player2.roleid == 3){
			cout << "player2's role:marksman ";
			digit = 12;
		}	
		if (player2.roleid == 4){	
			cout << "player2's role:bomber ";
			digit = 11;
		}
		for (int i = 1; i <= pdsize - digit + 1; i++)
			cout << space;
		color(white);
	}
	if (k == 11){ 
		color(blue);
		if (player2.blood < 0)
			player2.blood = 0;
		cout << "player2's blood:" << player2.blood;
		for (int i = 1; i <= pdsize - 8 - (countdigit(player2.blood) / 2); i++)
			cout << space;
		if (player2.blood != 0)
			(countdigit(player2.blood) % 2 == 1) ? cout << ' ' : cout << space;
		else	
			cout << ' ';
		color(white);
	}
	if (k == 12){ 
		color(blue);
		cout << "player2's cd:" << player2.cd << ' ';
		for (int i = 1; i <= pdsize - 7 - countdigit(player2.cd) / 2; i++)
			cout << space;
		if (player2.cd != 0)
			(countdigit(player2.cd) % 2 == 1) ? cout << ' ' : cout << space;
		else	
			cout << ' ';
		color(white);
	}
	if (k == 13){ //The ball of fight
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值