跳到正文

个人记录

羽毛球模拟器

weatherboy约 10 分钟
原文
文章信息与版本

文章信息

发布
2024年8月31日
收录
2025年12月12日
更新
2026年11月7日
篇幅
约 10 分钟
获赞
1

版本

  1. v1当前

闲着没事干,编了个小小的羽毛球模拟器(双人版)。

C++
#include<iostream>
#include<string>
#include<ctime>
#include<ostream>
#include<cstdlib>
#include<windows.h>
#include<conio.h>
using namespace std;
int lma=0,lmb=0,ax=1,ay=5,bx=11,by=5,cx=2,cy=5;
int winsA=0,winsB=0,noa,nob,times=0;
string tob="YX";
char a[50][50]= {
	"############",
	"#    -     #",
	"#    o     #",
	"#          #",
	"#          #",
	"#          #",
	"#          #",
	"#          #",
	"#          #",
	"#          #",
	"#          #",
	"#    -     #",
	"############"
};

char b[50][50];
void GoToxy(int x,int y) { //????????????X?????????Y?????????
	COORD  coord;         //?????????????????
	coord.X=x;            //????int?????????short,???????????漰???????????????short??Χ
	coord.Y=y;
	HANDLE a=GetStdHandle(STD_OUTPUT_HANDLE);  //???????????
	SetConsoleCursorPosition(a,coord);         //???????????????????????????????
}

void HideCursor() { //??????
	//CONSOLE_CURSOR_INFO???????????????????,DWORD dwSize?????????1~100????BOOL bVisible????????
	CONSOLE_CURSOR_INFO cursor_info= {1,0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); //SetConsoleCursorInfo??????????????????????С???????
}
void Begin() {
	lma=0,lmb=0,ax=1,ay=5,bx=11,by=5,cx=2,cy=5;
	for(int i=0; i<=12; i++) {
		for(int j=0; j<=11; j++) {
			b[i][j]=a[i][j];
		}
	}
	tob="X";
	lma=lmb=0;
	HideCursor();
	system("cls");
	Sleep(3000);
	int ball=0;
	while(true) {
		ball++;
		b[bx][by]='-';
		b[ax][ay]='-';
		GoToxy(0,0);
		for(int i=0; i<=12; i++) {
			for(int j=0; j<=11; j++) {
				cout<<b[i][j];
			}
			cout<<endl;
		}
		if(!kbhit()) {
			Sleep(times);
			if(tob=="ZS") {
				if(cy-1==0) {
					tob="YS";
				} else if(b[cx-1][cy-1]=='-') {
					if(lma==1)tob="ZX";
					else if(lma==2)tob="YX";
					else tob="X";
					continue;
				} else if(cx-1==0) {
					cout<<"Player B wins."<<endl;
					nob++;
					Sleep(3000);
					return ;
				} else {
					b[cx-1][cy-1]='o';
					b[cx][cy]=' ';
					cx--,cy--;
				}
			}
			if(tob=="YS") {
				if(cy+1==11) {
					tob="ZS";
				} else if(b[cx-1][cy+1]=='-') {
					if(lma==1)tob="ZX";
					else if(lma==2)tob="YX";
					else tob="X";
					continue;
				} else if(cx-1==0) {
					cout<<"Player B wins."<<endl;nob++;
					Sleep(3000);
					return ;
				} else {
					b[cx-1][cy+1]='o';
					b[cx][cy]=' ';
					cx--,cy++;
				}
			}
			if(tob=="S") {
				if(b[cx-1][cy]=='-') {
					if(lma==1)tob="ZX";
					else if(lma==2)tob="YX";
					else tob="X";
					continue;
				}
				if(cx-1==0) {
					cout<<"Player B wins."<<endl;nob++;
					Sleep(3000);
					return ;
				} else {
					b[cx-1][cy]='o';
					b[cx][cy]=' ';
					cx--;
				}
			}
			if(tob=="X") {
				if(b[cx+1][cy]=='-') {
					if(lmb==1)tob="ZS";
					else if(lmb==2)tob="YS";
					else tob="S";
					continue;
				} else if(cx+1==12) {
					cout<<"Player A wins."<<endl;noa++;
					Sleep(3000);
					return ;
				} else {
					b[cx+1][cy]='o';
					b[cx][cy]=' ';
					cx++;
				}
			}
			if(tob=="ZX") {
				if(cy-1==0) {
					tob="YX";
				} else if(b[cx+1][cy-1]=='-') {
					if(lmb==1)tob="ZS";
					else if(lmb==2)tob="YS";
					else tob="S";
					continue;
				} else if(cx+1==12) {
					cout<<"Player A wins."<<endl;noa++;
					Sleep(3000);
					return ;
				}  else {
					b[cx+1][cy-1]='o';
					b[cx][cy]=' ';
					cx++,cy--;
				}
			}
			if(tob=="YX") {
				if(cy+1==11) {
					tob="ZX";
					continue;
				} else if(b[cx+1][cy+1]=='-') {
					if(lmb==1)tob="ZS";
					else if(lmb==2)tob="YS";
					else tob="S";
					continue;
				} else if(cx+1==12) {
					cout<<"Player A wins."<<endl;noa++;
					Sleep(3000);
					return ;
				} else {
					b[cx+1][cy+1]='o';
					b[cx][cy]=' ';
					cx++;
					cy++;
				}
			}
		}
		if(kbhit()) {
			char ch=getch();
			if(ch==-32) {
				ch=getch();
				if(ch==72)if(bx-1>6)bx--,b[bx][by]='-',b[bx+1][by]=' ',lmb=0;
				if(ch==80)if(bx+1<12)bx++,b[bx][by]='-',b[bx-1][by]=' ',lmb=0;
				if(ch==75)if(by-1>0)by--,b[bx][by]='-',b[bx][by+1]=' ',lmb=1;
				if(ch==77)if(by+1<11)by++,b[bx][by]='-',b[bx][by-1]=' ',lmb=2;
			} else {
				if(ch=='a') {
					if(ay-1>0)ay--,b[ax][ay]='-',b[ax][ay+1]=' ',lma=0;
				} else if(ch=='s') {
					if(ax+1<6)ax++,b[ax][ay]='-',b[ax-1][ay]=' ',lma=0;
				} else if(ch=='d') {
					if(ay+1<11)ay++,b[ax][ay]='-',b[ax][ay-1]=' ',lma=1;
				} else if(ch=='w') {
					if(ax-1>0)ax--,b[ax][ay]='-',b[ax+1][ay]=' ',lma=2;
				}
			}
		}
	}
}
void Speed() {system("cls");
	cout<<"Speed"<<endl;
	cout<<"1.Fast"<<endl;
	cout<<"2.Medium"<<endl;
	cout<<"3.Slow"<<endl;
	int ch=getch();
	if(ch=='1')times=50;
	if(ch=='2')times=100;
	if(ch=='3')times=150;
	if(ch>'3'||ch<'1')return ;
	system("cls");
	Begin();
}
void Suc() {
	system("cls");
	cout<<"Mini Badminton"<<endl;
	cout<<"AI is building.Please wait."<<endl;
	Sleep(3000);
}
int main() {
	system("mode con cols=35 lines=27");
	for(;;) {
		tob="ZS";
		system("cls");
		cout<<"Badminton:"<<endl;
		cout<<"Player A:WASD"<<endl;
		cout<<"Player B:↑↓←→"<<endl;
		cout<<"Press F to play.Press L to leave"<<endl;
		char ch=getch();
		if(ch=='f')Speed();
		else if(ch=='s')Suc();
		else if(ch=='l') {
			break;
		}
	}
	freopen("Fight.txt","r",stdin);
	cin>>winsA>>winsB;
	winsA+=noa;
	winsB+=nob;
	freopen("Fight.txt","w",stdout);
	cout<<winsA<<' '<<winsB<<endl;
}
原文