一个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 fighter
		color(yellow);
		cout << ball;
		color(white);
	}
	if (k == 14){ //The ball of marksman
		color(purple);
		cout << ball;
		color(white);
	}
	if (k == 15){ //The big ball of marksman
		color(green);
		cout << ball;
		color(white);
	}
}

void draw(void){
	initialize();
	for (int i = 0; i <= rowsize + 1; i++){
		for (int j = 0; j <= pdsize + 1; j++){
			print(p1[i][j]);
		}
		for (int j = 0; j <= linesize + 1; j++){
			print(a[i][j]);
		}
		for (int j = 0; j <= pdsize + 1; j++){
			print(p2[i][j]);
		}
		cout << endl;
	}
	
	for (int i = 1; i <= 28; i++)
		cout << space;
	cout << "time:";
	int deltatime = limittime - (nowt / 1000 - gamestart / 1000);
	if (deltatime / 60 == 3)
		cout << "3:00" << endl;
	else if ((deltatime % 60) / 10 != 0)
		cout << deltatime / 60 << ':' << deltatime % 60 << endl;
	else
		cout << deltatime / 60 << ":0" << deltatime % 60 << endl;
}

/* test */
void prtnumber(void){
	initialize();
	for (int i = 0; i <= rowsize + 1; i++){
		for (int j = 0; j <= pdsize + 1; j++)
			cout << p1[i][j] << ' ';
		for (int j = 0; j <= linesize + 1; j++)
			cout << a[i][j] << ' ';
		for (int j = 0; j <= pdsize + 1; j++)
			cout << p2[i][j] << ' ';
		cout << endl;
	}
}
/* test over */

void ChooseCharactor(player &p){
	cout << "请选择英雄: " << endl;
	int nowcur = 1;
	color(green);
	cout << "1. swordsman (剑士) <" << endl;
	color(white);		
	cout << "2. fighter (斗士) " << endl;
	cout << "3. marksman (射手) " << endl;
	cout << endl << "提示:使用'w', 's', 来控制上下,'+'来确定" << endl;
	while (true){
		char gh = '~';
		if (kbhit()){
			clr();
			cout << "请选择英雄: " << endl;
			gh = getch();
			if (gh == up && nowcur != 1)
				nowcur -= 1;
			if (gh == down && nowcur != 3)
				nowcur += 1;
			if (gh == enter){
				p.roleid = nowcur;
				clr();
				return ;
			}
				
			/* choosing */
			if (nowcur == 1){
				color(green);
				cout << "1. swordsman (剑士) <" << endl;
				color(white);
			}
			else cout << "1. swordsman (剑士) " << endl;
			
			if (nowcur == 2){
				color(green);
				cout << "2. fighter (斗士) <" << endl;
				color(white);
			}
			else cout << "2. fighter (斗士) " << endl;
			
			if (nowcur == 3){
				color(green);
				cout << "3. marksman (射手) <" << endl;
				color(white);
			}
			else cout << "3. marksman (射手) " << endl;
			
			cout << endl << "提示:使用'w', 's', 来控制上下,'+'来确定" << endl;
		}
	}
}

void helper(void){
	clr(); 
	cout << "对于"; color(red);
	cout << "[player1]"; color(white);
	cout << ",使用'w''a''s''d'分别控制上,左,下,右,'q'普攻,'e'大招。" << endl;
	
	cout << "对于"; color(blue);
	cout << "[player2]"; color(white);
	cout << ",使用'8''4''5''6'分别控制上,左,下,右,'7'普攻,'9'大招。" << endl;
	cout << "对于所有玩家,初始都拥有1000血量,10cd。每次大招消耗1cd,普攻不消耗cd。" << endl;
	
	cout << endl << "[swordsman (剑士)]" << endl;
	cout << "普攻:穿墙而过(仅能穿过厚度为1的墙)" << endl;
	cout << "大招:向前方释放长度为3,攻击力为250的攻击" << endl;
	
	cout << endl << "[fighter (斗士)]" << endl;
	cout << "普攻:向四周释放长度为1,攻击力为100的攻击" << endl;
	cout << "大招:向四周释放长度为2,攻击力为400的攻击" << endl;
	
	cout << endl << "[marksman (射手)]" << endl;
	cout << "普攻:向前方发射一个攻击力为50的子弹,每秒20格。" << endl;
	cout << "大招:向前方发射一个攻击力为500的子弹,每秒20格。" << endl;
	
	cout << endl << "每场比赛限时3分钟,获胜条件为对方血量清空。" << endl;
	cout << "若时间到了且未分出胜负,血量高者获胜。若血量一样,cd多者获胜。若cd一样,则平局。" << endl; 
	
	cout << endl << "按下任意键返回。" << endl;
	while (true)
		if (kbhit())
			return ; 
}

int fballnum = 0;

class fball{ //fighter's ball
	public:
		bool changed = false;
		fball(){;}
		fball(int x, int y){
			this -> x = x;
			this -> y = y;
		}
		int x, y;
		clock_t time = clock();
		void set(int x, int y){
			this -> x = x;
			this -> y = y;
			a[x][y] = 13;
			time = clock();
		}
		void update(void){
			clock_t nowtime = clock();
			if (nowtime - time >= 500 && changed == false)
				a[x][y] = 0, changed = true;
		}
} fb[100000];

void FightersBallUpdate(void){
	for (int i = 0; i <= fballnum - 1; i++)
		fb[i].update();
}

bool testermode = false;

class bullet{
	public:
		bool stop = false;
		bool isbig = false;
		char way = '~';
		int speed = 50;
		int kill = 50;
		int x, y;
		clock_t time = clock();
		void set(int x, int y, char way){
			this -> x = x;
			this -> y = y;
			this -> way = way;
			a[x][y] = 14 + isbig;
			time = clock();
		}
		void move(void){
			if (stop == false){
				if (a[x][y] == 2)
					a[x][y] = 0, player1.blood -= kill, stop = true;
				if (a[x][y] == 3)
					a[x][y] = 0, player2.blood -= kill, stop = true;
				if (a[x][y] == -1)
					a[x][y] = -1, stop = true;
			}
			if (stop == false){
				clock_t nowtime = clock();
				if (nowtime - time >= speed){
					switch (way){
						case 'w':
							if (a[x - 1][y] == 0)
								a[x - 1][y] = 14 + isbig, a[x][y] = 0, x = x - 1;
							if (a[x - 1][y] == 2)
								a[x][y] = 0, player1.blood -= kill, stop = true;
							if (a[x - 1][y] == 3)
								a[x][y] = 0, player2.blood -= kill, stop = true;
							if (a[x - 1][y] == 1 || a[x - 1][y] == -1)
								a[x][y] = 0, stop = true;
							break;
						case 's':
							if (a[x + 1][y] == 0)
								a[x + 1][y] = 14 + isbig, a[x][y] = 0, x = x + 1;
							if (a[x + 1][y] == 2)
								a[x][y] = 0, player1.blood -= kill, stop = true;
							if (a[x + 1][y] == 3)
								a[x][y] = 0, player2.blood -= kill, stop = true;
							if (a[x + 1][y] == 1 || a[x + 1][y] == -1)
								a[x][y] = 0, stop = true;
							break;
						case 'd':
							if (a[x][y + 1] == 0)
								a[x][y + 1] = 14 + isbig, a[x][y] = 0, y = y + 1;
							if (a[x][y + 1] == 2)
								a[x][y] = 0, player1.blood -= kill, stop = true;
							if (a[x][y + 1] == 3)
								a[x][y] = 0, player2.blood -= kill, stop = true;
							if (a[x][y + 1] == 1 || a[x][y + 1] == -1)
								a[x][y] = 0, stop = true;
							break;
						case 'a':
							if (a[x][y - 1] == 0)
								a[x][y - 1] = 14 + isbig, a[x][y] = 0, y = y - 1;
							if (a[x][y - 1] == 2)
								a[x][y] = 0, player1.blood -= kill, stop = true;
							if (a[x][y - 1] == 3)
								a[x][y] = 0, player2.blood -= kill, stop = true;
							if (a[x][y - 1] == 1 || a[x][y - 1] == -1)
								a[x][y] = 0, stop = true;
							break;
					}
					time = clock();
				}
			}
		}
} bl[100000];

int bulletnum = 0;

void MoveBullet(void){
	for (int i = 0; i <= bulletnum; i++)
		bl[i].move();
}

void play(void){
	draw();
	int dt = limittime - (nowt / 1000 - gamestart / 1000);
	while (true){
		nowt = clock();
		dt = limittime - (nowt / 1000 - gamestart / 1000);
		if (player1.blood <= 0 || player2.blood <= 0 || dt <= 0)
			goto winner;
		clear();
		FightersBallUpdate();
		MoveBullet();
		if (kbhit()){
			char gh = '~';
			gh = getch();
			/* tester's operate*/
			if (gh == '\\'){
				testermode = !testermode;
			}
			
			/* player1's operate */
			if (gh == 'w'){
				if (a[player1.x - 1][player1.y] == 0)
					player1.x -= 1;
				player1.upop = 'w';
			}
			if (gh == 's'){
				if (a[player1.x + 1][player1.y] == 0)
					player1.x += 1;
				player1.upop = 's';
			}
			if (gh == 'a'){
				if (a[player1.x][player1.y - 1] == 0)
					player1.y -= 1;
				player1.upop = 'a';
			}
			if (gh == 'd'){
				if (a[player1.x][player1.y + 1] == 0)
					player1.y += 1;
				player1.upop = 'd';
			}
			if (gh == 'q'){
				int role = player1.roleid;
				if (role == 1){
					switch (player1.upop){
						case 's':
							if ((a[player1.x + 1][player1.y] == 0 || -1) && 
								a[player1.x + 2][player1.y] == 0 &&
								player1.x + 2 <= rowsize)
								player1.x += 2;
							break;
						case 'w':
							if ((a[player1.x - 1][player1.y] == 0 || -1) && 
								a[player1.x - 2][player1.y] == 0 &&
								player1.x - 2 >= 0)
								player1.x -= 2;
							break;
						case 'a':
							if ((a[player1.x][player1.y - 1] == 0 || -1) && 
								a[player1.x][player1.y - 2] == 0 &&
								player1.y - 2 >= 0)
								player1.y -= 2;
							break;
						case 'd':
							if ((a[player1.x][player1.y + 1] == 0 || -1) && 
								a[player1.x][player1.y + 2] == 0 &&
								player1.y + 2 <= linesize)
								player1.y += 2;
							break;
					}
				}
				if (role == 2){
					switch (a[player1.x + 1][player1.y]){
						case 3:
							player2.blood -= 100;
							break;
						case 0:
							fb[fballnum].set(player1.x + 1, player1.y);
							fballnum += 1;
							break;
					}
					switch (a[player1.x - 1][player1.y]){
						case 3:
							player2.blood -= 100;
							break;
						case 0:
							fb[fballnum].set(player1.x - 1, player1.y);
							fballnum += 1;
							break;
					}
					switch (a[player1.x][player1.y + 1]){
						case 3:
							player2.blood -= 100;
							break;
						case 0:
							fb[fballnum].set(player1.x, player1.y + 1);
							fballnum += 1;
							break;
					}
					switch (a[player1.x][player1.y - 1]){
						case 3:
							player2.blood -= 100;
							break;
						case 0:
							fb[fballnum].set(player1.x, player1.y - 1);
							fballnum += 1;
							break;
					}
				}
				if (role == 3){
					switch (player1.upop){
						case 'w':
							bl[bulletnum].set(player1.x - 1, player1.y, 'w');
							bulletnum += 1;
							break;
						case 's':
							bl[bulletnum].set(player1.x + 1, player1.y, 's');
							bulletnum += 1;
							break;
						case 'a':
							bl[bulletnum].set(player1.x, player1.y - 1, 'a');
							bulletnum += 1;
							break;
						case 'd':
							bl[bulletnum].set(player1.x, player1.y + 1, 'd');
							bulletnum += 1;
							break;
					}
				}
			}
			if (gh == 'e' && player1.cd >= 1){
				int role = player1.roleid;
				player1.cd -= 1;
				if (role == 1){
					switch (player1.upop){
						case 's':
							switch (a[player1.x + 1][player1.y]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x + 1, player1.y);
									fballnum += 1;
									break;
							}
							switch (a[player1.x + 2][player1.y]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x + 2, player1.y);
									fballnum += 1;
									break;
							}
							switch (a[player1.x + 3][player1.y]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x + 3, player1.y);
									fballnum += 1;
									break;
							}
							break;
						case 'w':
							switch (a[player1.x - 1][player1.y]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x - 1, player1.y);
									fballnum += 1;
									break;
							}
							switch (a[player1.x - 2][player1.y]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x - 2, player1.y);
									fballnum += 1;
									break;
							}
							switch (a[player1.x - 3][player1.y]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x - 3, player1.y);
									fballnum += 1;
							}
							break;
						case 'a':
							switch (a[player1.x][player1.y - 1]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x, player1.y - 1);
									fballnum += 1;
									break;
							}
							switch (a[player1.x][player1.y - 2]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x, player1.y - 2);
									fballnum += 1;
									break;
							}
							switch (a[player1.x][player1.y - 3]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x, player1.y - 3);
									fballnum += 1;
							}
							break;
						case 'd':
							switch (a[player1.x][player1.y + 1]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x, player1.y + 1);
									fballnum += 1;
									break;
							}
							switch (a[player1.x][player1.y + 2]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x, player1.y + 2);
									fballnum += 1;
									break;
							}
							switch (a[player1.x][player1.y + 3]){
								case 3:
									player2.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player1.x, player1.y + 3);
									fballnum += 1;
							}
							break;
					}
				}
				if (role == 2){
					switch (a[player1.x + 1][player1.y]){
						case 3:
							player2.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player1.x + 1, player1.y);
							fballnum += 1;
							break;
					}
					switch (a[player1.x - 1][player1.y]){
						case 3:
							player2.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player1.x - 1, player1.y);
							fballnum += 1;
							break;
					}
					switch (a[player1.x][player1.y + 1]){
						case 3:
							player2.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player1.x, player1.y + 1);
							fballnum += 1;
							break;
					}
					switch (a[player1.x][player1.y - 1]){
						case 3:
							player2.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player1.x, player1.y - 1);
							fballnum += 1;
							break;
					}
					switch (a[player1.x + 2][player1.y]){
						case 3:
							player2.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player1.x + 2, player1.y);
							fballnum += 1;
							break;
					}
					switch (a[player1.x - 2][player1.y]){
						case 3:
							player2.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player1.x - 2, player1.y);
							fballnum += 1;
							break;
					}
					switch (a[player1.x][player1.y + 2]){
						case 3:
							player2.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player1.x, player1.y + 2);
							fballnum += 1;
							break;
					}
					switch (a[player1.x][player1.y - 2]){
						case 3:
							player2.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player1.x, player1.y - 2);
							fballnum += 1;
							break;
					}
				}
				if (role == 3){
					switch (player1.upop){
						case 'w':
							bl[bulletnum].kill = 500;
							bl[bulletnum].isbig = true;
							bl[bulletnum].set(player1.x - 1, player1.y, 'w');
							bulletnum += 1;
							break;
						case 's':
							bl[bulletnum].kill = 500;
							bl[bulletnum].isbig = true;
							bl[bulletnum].set(player1.x + 1, player1.y, 's');
							bulletnum += 1;
							break;
						case 'a':
							bl[bulletnum].kill = 500;
							bl[bulletnum].isbig = true;
							bl[bulletnum].set(player1.x, player1.y - 1, 'a');
							bulletnum += 1;
							break;
						case 'd':
							bl[bulletnum].kill = 500;
							bl[bulletnum].isbig = true;
							bl[bulletnum].set(player1.x, player1.y + 1, 'd');
							bulletnum += 1;
							break;
					}
				}
			}
			
			/* player2's operate */
			if (gh == '8'){
				if (a[player2.x - 1][player2.y] == 0)
					player2.x -= 1;
				player2.upop = '8';
			}
			if (gh == '5'){
				if (a[player2.x + 1][player2.y] == 0)
					player2.x += 1;
				player2.upop = '5';
			}
			if (gh == '4'){
				if (a[player2.x][player2.y - 1] == 0)
					player2.y -= 1;
				player2.upop = '4';
			}
			if (gh == '6'){
				if (a[player2.x][player2.y + 1] == 0)
					player2.y += 1;
				player2.upop = '6';
			}
			if (gh == '7'){
				int role = player2.roleid;
				if (role == 1){
					switch (player2.upop){
						case '5':
							if ((a[player2.x + 1][player2.y] == 0 || -1) && 
								a[player2.x + 2][player2.y] == 0 &&
								player2.x + 2 <= rowsize)
								player2.x += 2;
							break;
						case '8':
							if ((a[player2.x - 1][player2.y] == 0 || -1) && 
								a[player2.x - 2][player2.y] == 0 &&
								player2.x - 2 >= 0)
								player2.x -= 2;
							break;
						case '4':
							if ((a[player2.x][player2.y - 1] == 0 || -1) && 
								a[player2.x][player2.y - 2] == 0 &&
								player2.y - 2 >= 0)
								player2.y -= 2;
							break;
						case '6':
							if ((a[player2.x][player2.y + 1] == 0 || -1) && 
								a[player2.x][player2.y + 2] == 0 &&
								player2.y + 2 <= linesize)
								player2.y += 2;
							break;
					}
				}
				if (role == 2){
					switch (a[player2.x + 1][player2.y]){
						case 2:
							player1.blood -= 100;
							break;
						case 0:
							fb[fballnum].set(player2.x + 1, player2.y);
							fballnum += 1;
							break;
					}
					switch (a[player2.x - 1][player2.y]){
						case 2:
							player1.blood -= 100;
							break;
						case 0:
							fb[fballnum].set(player2.x - 1, player2.y);
							fballnum += 1;
							break;
					}
					switch (a[player2.x][player2.y + 1]){
						case 2:
							player1.blood -= 100;
							break;
						case 0:
							fb[fballnum].set(player2.x, player2.y + 1);
							fballnum += 1;
							break;
					}
					switch (a[player2.x][player2.y - 1]){
						case 2:
							player1.blood -= 100;
							break;
						case 0:
							fb[fballnum].set(player2.x, player2.y - 1);
							fballnum += 1;
							break;
					}
				}
				if (role == 3){
					switch (player2.upop){
						case '8':
							bl[bulletnum].set(player2.x - 1, player2.y, 'w');
							bulletnum += 1;
							break;
						case '5':
							bl[bulletnum].set(player2.x + 1, player2.y, 's');
							bulletnum += 1;
							break;
						case '4':
							bl[bulletnum].set(player2.x, player2.y - 1, 'a');
							bulletnum += 1;
							break;
						case '6':
							bl[bulletnum].set(player2.x, player2.y + 1, 'd');
							bulletnum += 1;
							break;
					}
				}
			}
			if (gh == '9' && player2.cd >= 1){
				int role = player2.roleid;
				player2.cd -= 1;
				if (role == 1){
					switch (player2.upop){
						case '5':
							switch (a[player2.x + 1][player2.y]){
								case 2:
									player1.blood -= 250;
									break;
								case 0:
									fb[fballnum].set(player2.x + 1, player2.y);
									fballnum += 1;
									break;
							}
							switch (a[player2.x + 2][player2.y]){
								case 2:
									player1.blood -= 250;
									break;
								case 0:
									fb[fballnum].set(player2.x + 2, player2.y);
									fballnum += 1;
									break;
							}
							switch (a[player2.x + 3][player2.y]){
								case 2:
									player1.blood -= 250;
									break;
								case 0:
									fb[fballnum].set(player2.x + 3, player2.y);
									fballnum += 1;
									break;
							}
							break;
						case '8':
							switch (a[player2.x - 1][player2.y]){
								case 2:
									player1.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player2.x - 1, player2.y);
									fballnum += 1;
									break;
							}
							switch (a[player2.x - 2][player2.y]){
								case 2:
									player1.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player2.x - 2, player2.y);
									fballnum += 1;
									break;
							}
							switch (a[player2.x - 3][player2.y]){
								case 2:
									player1.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player2.x - 3, player2.y);
									fballnum += 1;
							}
							break;
						case '4':
							switch (a[player2.x][player2.y - 1]){
								case 2:
									player1.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player2.x, player2.y - 1);
									fballnum += 1;
									break;
							}
							switch (a[player2.x][player2.y - 2]){
								case 2:
									player1.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player2.x, player2.y - 2);
									fballnum += 1;
									break;
							}
							switch (a[player2.x][player2.y - 3]){
								case 2:
									player1.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player2.x, player2.y - 3);
									fballnum += 1;
							}
							break;
						case '6':
							switch (a[player2.x][player2.y + 1]){
								case 2:
									player1.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player2.x, player2.y + 1);
									fballnum += 1;
									break;
							}
							switch (a[player2.x][player2.y + 2]){
								case 2:
									player1.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player2.x, player2.y + 2);
									fballnum += 1;
									break;
							}
							switch (a[player2.x][player2.y + 3]){
								case 2:
									player1.blood -= 500;
									break;
								case 0:
									fb[fballnum].set(player2.x, player2.y + 3);
									fballnum += 1;
							}
							break;
					}
				}
				if (role == 2){
					switch (a[player2.x + 1][player2.y]){
						case 2:
							player1.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player2.x + 1, player2.y);
							fballnum += 1;
							break;
					}
					switch (a[player2.x - 1][player2.y]){
						case 2:
							player1.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player2.x - 1, player2.y);
							fballnum += 1;
							break;
					}
					switch (a[player2.x][player2.y + 1]){
						case 2:
							player1.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player2.x, player2.y + 1);
							fballnum += 1;
							break;
					}
					switch (a[player2.x][player2.y - 1]){
						case 2:
							player1.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player2.x, player2.y - 1);
							fballnum += 1;
							break;
					}
					switch (a[player2.x + 2][player2.y]){
						case 2:
							player1.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player2.x + 2, player2.y);
							fballnum += 1;
							break;
					}
					switch (a[player2.x - 2][player2.y]){
						case 2:
							player1.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player2.x - 2, player2.y);
							fballnum += 1;
							break;
					}
					switch (a[player2.x][player2.y + 2]){
						case 2:
							player1.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player2.x, player2.y + 2);
							fballnum += 1;
							break;
					}
					switch (a[player2.x][player2.y - 2]){
						case 2:
							player1.blood -= 400;
							break;
						case 0:
							fb[fballnum].set(player2.x, player2.y - 2);
							fballnum += 1;
							break;
					}
				}
				if (role == 3){
					switch (player2.upop){
						case '8':
							bl[bulletnum].kill = 500;
							bl[bulletnum].isbig = true;
							bl[bulletnum].set(player2.x - 1, player2.y, 'w');
							bulletnum += 1;
							break;
						case '5':
							bl[bulletnum].kill = 500;
							bl[bulletnum].isbig = true;
							bl[bulletnum].set(player2.x + 1, player2.y, 's');
							bulletnum += 1;
							break;
						case '4':
							bl[bulletnum].kill = 500;
							bl[bulletnum].isbig = true;
							bl[bulletnum].set(player2.x, player2.y - 1, 'a');
							bulletnum += 1;
							break;
						case '6':
							bl[bulletnum].kill = 500;
							bl[bulletnum].isbig = true;
							bl[bulletnum].set(player2.x, player2.y + 1, 'd');
							bulletnum += 1;
							break;
					}
				}
			}
		}
		testermode ? prtnumber() : draw();
		//Sleep(100);
	}
	
	winner:
		Sleep(1000);
		if (dt == 0){
			if (player1.blood > player2.blood || (player1.blood == player2.blood && player1.cd > player2.cd)){
				clr(); color(red);
				SetFont(100);
				system("mode con cols=130 lines=30");
				cout << "[player1] win!(time's up)" << endl;
				color(white);
				SetFont(50);
				Sleep(10000);
				exit(0);
			}
			if (player2.blood > player1.blood || (player1.blood == player2.blood && player1.cd < player2.cd)){
				clr(); color(blue);
				SetFont(100);
				system("mode con cols=130 lines=30");
				cout << "[player2] win!(time's up)" << endl;
				color(white);
				SetFont(50);
				Sleep(10000);
				exit(0);
			}
			SetFont(100);
			system("mode con cols=130 lines=30");
			cout << "Nobody win!(time's up)" << endl;
			SetFont(50);
			Sleep(10000);
			exit(0);
		}
		else if (player1.blood <= 0){
			clr(); color(blue);
			SetFont(100);
			system("mode con cols=130 lines=30");
			cout << "[player2] win!" << endl;
			color(white);
			SetFont(50);
			Sleep(10000);
			exit(0);
		}
		else{
			clr(); color(red);
			SetFont(100);
			system("mode con cols=130 lines=30");
			cout << "[player1] win!" << endl;
			color(white);
			SetFont(50);
			Sleep(10000);
			exit(0);
		}
}

void ChooseMap(void){
	cout << "请选择地图: " << endl;
	int nowcur = 1;
	color(green);
	cout << "1. nothing (无障碍) <" << endl;
	color(white);		
	cout << "2. ruins (废墟) " << endl;
	cout << "3. random (随机) " << endl;
	cout << endl << "提示:使用'w', 's', 来控制上下,'+'来确定" << endl;
	while (true){
		char gh = '~';
		if (kbhit()){
			clr();
			cout << "请选择地图: " << endl;
			gh = getch();
			if (gh == up && nowcur != 1)
				nowcur -= 1;
			if (gh == down && nowcur != 3)
				nowcur += 1;
			if (gh == enter){
				for (int i = 1; i <= rowsize; i++)
					for (int j = 1; j <= linesize; j++)
						a[i][j] = 0;
				switch (nowcur){
					case 1:
						break;
					case 2:
						for (int i = 5; i <= rowsize - 4; i++)
							a[i][5] = -1, a[i][linesize - 4] = -1;
						for (int i = 5; i <= linesize - 4; i++)
							a[5][i] = -1, a[rowsize - 4][i] = -1;
						a[rowsize / 2][5] = 0, a[rowsize / 2 + 1][linesize - 4] = 0;
						a[5][linesize / 2] = 0, a[rowsize - 4][linesize / 2 + 1] = 0;
						
						for (int i = 8; i <= rowsize - 7; i++)
							a[i][8] = -1, a[i][linesize - 7] = -1;
						for (int i = 8; i <= linesize - 7; i++)
							a[8][i] = -1, a[rowsize - 7][i] = -1;
						a[rowsize / 2][8] = 0, a[rowsize / 2 + 1][linesize - 7] = 0;
						a[8][linesize / 2] = 0, a[rowsize - 7][linesize / 2 + 1] = 0;
						break;
					case 3:
						srand(time(NULL));
						for (int i = 1; i <= rowsize; i++)
							for (int j = 1; j <= linesize; j++)
								if (rand() % 20 == 0)
									a[i][j] = -1;
						break;
				}
				clr();
				return ;
			}
				
			/* choosing */
			if (nowcur == 1){
				color(green);
				cout << "1. nothing (无障碍) <" << endl;
				color(white);
			}
			else cout << "1. nothing (无障碍) " << endl;
			
			if (nowcur == 2){
				color(green);
				cout << "2. ruins (废墟) <" << endl;
				color(white);
			}
			else cout << "2. ruins (废墟) " << endl;
			
			if (nowcur == 3){
				color(green);
				cout << "3. random (随机) <" << endl;
				color(white);
			}
			else cout << "3. random (随机) " << endl;
			
			cout << endl << "提示:使用'w', 's', 来控制上下,'+'来确定" << endl;
		}
	}
}

void banpick(player &p, int k){ //k means the player id
	cout << "请"; 
	(k == 1) ? color(red) : color(blue);
	(k == 1) ? cout << "[player1]" : cout << "[player2]";
	color(white);
	cout << "选择加成: " << endl;
	int nowcur = 1;
	color(green);
	cout << "1. cd + 2 <" << endl;
	color(white);		
	cout << "2. blood + 200 " << endl;
	cout << endl << "提示:使用'w', 's', 来控制上下,'+'来确定" << endl;
	while (true){
		char gh = '~';
		if (kbhit()){
			clr();
			cout << "请"; 
			(k == 1) ? color(red) : color(blue);
			(k == 1) ? cout << "[player1]" : cout << "[player2]";
			color(white);
			cout << "选择加成: " << endl;
			gh = getch();
			if (gh == up && nowcur != 1)
				nowcur -= 1;
			if (gh == down && nowcur != 2)
				nowcur += 1;
			if (gh == enter){
				switch (nowcur){
					case 1:
						p.cd += 2;
						break;
					case 2:
						p.blood += 200;
						break;
				} 
				clr();
				return ;
			}
				
			/* choosing */
			if (nowcur == 1){
				color(green);
				cout << "1. cd + 2 <" << endl;
				color(white);
			}
			else cout << "1. cd + 2 " << endl;
			
			if (nowcur == 2){
				color(green);
				cout << "2. blood + 200 <" << endl;
				color(white);
			}
			else cout << "2. blood + 200 " << endl;
			
			cout << endl << "提示:使用'w', 's', 来控制上下,'+'来确定" << endl;
		}
	}
}

void menu(void){
	HideCursor();
	int nowcur = 1; //The cur
	color(green);
	cout << "1. 选择player1的角色 <" << endl;
	color(white);
	cout << "2. 选择player2的角色 " << endl;
	cout << "3. 开始游戏 " << endl;
	cout << "4. 帮助 " << endl;
	cout << "5. 选择地图 " << endl;
	cout << endl << "提示:使用'w', 's', 来控制上下,'+'来确定" << endl;
	while (true){
		char gh = '~'; //getch
		if (kbhit()){
			clr();
			gh = getch();
			if (gh == up && nowcur != 1)
				nowcur -= 1;
			if (gh == down && nowcur != 5)
				nowcur += 1;
			if (gh == enter){
				switch (nowcur){
					case 1:
						ChooseCharactor(player1);
						break;
					case 2:
						ChooseCharactor(player2);
						break;
					case 3:
						banpick(player1, 1);
						banpick(player2, 2);
						gamestart = clock();
						nowt = clock();
						play();
						break;
					case 4:
						helper();
						break;
					case 5:
						ChooseMap();
						break;
				}
			}
				
			/* print menu */
			if (nowcur == 1){
				color(green);
				cout << "1. 选择player1的角色 <" << endl;
				color(white);
			}
			else{
				cout << "1. 选择player1的角色 " << endl;
			}
			
			if (nowcur == 2){
				color(green);
				cout << "2. 选择player2的角色 <" << endl;
				color(white);
			}
			else{
				cout << "2. 选择player2的角色 " << endl;
			}
			
			if (nowcur == 3){
				color(green);
				cout << "3. 开始游戏 <" << endl;
				color(white);
			}
			else{
				cout << "3. 开始游戏 " << endl;
			}
			
			if (nowcur == 4){
				color(green);
				cout << "4. 帮助 <" << endl;
				color(white);
			}
			else{
				cout << "4. 帮助 " << endl;
			}
			
			if (nowcur == 5){
				color(green);
				cout << "5. 选择地图 <" << endl;
				color(white);
			}
			else{
				cout << "5. 选择地图 " << endl;
			}
			
			cout << endl << "提示:使用'w', 's', 来控制上下,'+'来确定" << endl;
		}
	}
}

int main(){
	system("mode con cols=130 lines=30");
	color(white);
	initialize();
	menu(); 
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您写一个c语言的双人回合制对战小游戏。以下是一个基本的游戏框架: 1. 定义角色结构体,包括名称、生命值、攻击力、防御力等属性。 typedef struct { char name[20]; int hp; int attack; int defense; } Character; 2. 定义攻击函数,计算伤害值并扣除对方角色的生命值。 void attack(Character* attacker, Character* target) { int damage = attacker->attack - target->defense; if (damage < 0) { damage = 0; } target->hp -= damage; printf("%s攻击了%s,造成了%d点伤害。\n", attacker->name, target->name, damage); if (target->hp <= 0) { printf("%s被%s击败了!\n", target->name, attacker->name); } } 3. 定义主函数,首先创建两个角色,然后循环进行双方回合制的对战,直到有一方阵亡。 int main() { Character player1 = {"玩家1", 100, 20, 10}; Character player2 = {"玩家2", 100, 20, 10}; int round = 1; char input; printf("游戏开始!\n"); while (player1.hp > 0 && player2.hp > 0) { printf("第%d回合:\n", round); printf("%s的生命值:%d\n", player1.name, player1.hp); printf("%s的生命值:%d\n", player2.name, player2.hp); printf("轮到%s攻击(按回车继续):", player1.name); scanf("%c", &input); attack(&player1, &player2); if (player2.hp <= 0) { break; } printf("轮到%s攻击(按回车继续):", player2.name); scanf("%c", &input); attack(&player2, &player1); if (player1.hp <= 0) { break; } round++; } printf("游戏结束!"); return 0; } 您可以根据需要修改角色属性或者添加其他游戏要素,比如道具、技能等。希望这个简单的示例能帮到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值