黄戈 • 11个月前
1.修改名字(bushi)
2.方块无缝连接
3.新增方块:白砖(用枪1/2概率打破)
4.新增地图
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
using namespace std;
int SIZ = 20;
bool yinshen=0;
int ma1=25,ma2=25;
int MapMode=0;
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
HANDLE hCon;
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
void SetColor(Color c) {
if(hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, c);
}
void gotoxy(int x,int y)
{
COORD pos;
pos.X=y;
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
SYSTEMTIME sys;
struct PLAYER {
int x,y;
int hp;
int gun;
int direct;
int kill;
} p1,p2;
int maP[1005][1005];
int abs(int x) {
if(x < 0) return -x;
return x;
}
void locate(int x,int y) {
coord.X=y - 1;
coord.Y=x - 1;
SetConsoleCursorPosition(hout,coord);
}
void print_map() {
locate(1,1);
SetColor(GRAY);
for(int i = 1; i <= SIZ; i++) cout<<"";
locate(SIZ,1);
for(int i = 1; i <= SIZ; i++) cout<<"";
for(int i = 2; i < SIZ; i++) {
locate(i,1);
cout<<"";
locate(i,SIZ*2-1);
cout<<"";
}
locate(SIZ+1,1);
SetColor(WHITE);
}
void create_tree(int x,int y) {
maP[x][y] = maP[x+1][y] = maP[x-1][y] = maP[x][y+1] = maP[x][y-1] = 2;
}
void use_map(int x) {
if(x == 1) {
SIZ = 20;
SetColor(DARKGREEN);
maP[16][6]=maP[15][6]=maP[17][6]=maP[16][7]=maP[16][5]=maP[14][13]=maP[13][12]=maP[13][13]=2;
for(int i = 2; i < SIZ; i++) {
for(int j = 2; j < SIZ; j++) {
if(maP[i][j] == 2) {
locate(i,j*2-1);
cout<<"";
}
}
}
SetColor(GRAY);
for(int i = 5; i <= 15; i++) {
maP[i][i] = 1;
locate(i,i*2-1);
cout<<"";
}
SetColor(WHITE);
}
else if(x == 2) {
SIZ = 30;
SetColor(GRAY);
for(int i = 4; i <= 26; i++) {
if(i == 13 || i == 14 ||i == 15) continue;
maP[i][4] = maP[4][i] = maP[26][i] = maP[i][26] = 1;
}
for(int i = 1; i <= SIZ; i++) {
for(int j = 1; j <= SIZ; j++) {
if(maP[i][j] == 1) {
locate(i,j*2-1);
cout<<"";
}
}
}
SetColor(DARKGREEN);
for(int i = 10; i<=20; i++) {
if(i == 13 || i == 17) continue;
maP[i][10] = maP[10][i] = maP[20][i] = maP[i][20] = 2;
}
create_tree(5,5);
create_tree(18,18);
for(int i = 1; i <= SIZ; i++) {
for(int j = 1; j <= SIZ; j++) {
if(maP[i][j] == 2) {
locate(i,j*2-1);
cout<<"";
}
}
}
SetColor(WHITE);
}
else if(x == 3){
system("cls");
Sleep(1500);
ma1=5;
ma2=5;
MapMode = 3;
SIZ = 15;
for(int i = 1; i <= SIZ; i++) {
for(int j = 1; j <= SIZ; j++) {
if(maP[i][j] == 1) {
locate(i,j*2-1);
}
}
}
for(int i = 5; i<=20; i++) {
if(i == 13 || i == 17) continue;
maP[i][10] = maP[10][i] = maP[20][i] = maP[i][20] = 2;
}
create_tree(5,5);
create_tree(18,18);
for(int i = 1; i <= SIZ; i++) {
for(int j = 1; j <= SIZ; j++) {
if(maP[i][j] == 2) {
locate(i,j*2-1);
}
}
}
maP[7][7] = 4;
locate(7,7*2-1);
SetColor(WHITE);
}
else if(x == 4) {
SIZ = 25;
SetColor(GRAY);
for(int i = 4; i <= 15; i++) {
if(i == 13 || i == 14 ||i == 15) continue;
maP[i][4] = maP[4][i] = maP[26][i] = maP[i][26] = 1;
}
for(int i = 1; i <= SIZ; i++) {
for(int j = 1; j <= SIZ; j++) {
if(maP[i][j] == 1) {
locate(i,j*2-1);
cout<<"";
}
}
}
SetColor(DARKGREEN);
for(int i = 10; i<=20; i++) {
if(i == 13 || i == 17) continue;
maP[i][10] = maP[10][i] = maP[20][i] = maP[i][20] = 2;
}
create_tree(5,5);
create_tree(18,18);
for(int i = 1; i <= SIZ; i++) {
for(int j = 1; j <= SIZ; j++) {
if(maP[i][j] == 2) {
locate(i,j*2-1);
cout<<"";
}
}
}
SetColor(WHITE);
}
else if(x == 5){
Sleep(1500);
SIZ = 9;
SetColor(GRAY);
maP[5][5] = 1;
gotoxy(4,8);
cout<<"";
locate(7,7*2-1);
ma1=5,ma2=5;
}
else if(x == 6){
yinshen=1;
Sleep(1500);
SIZ = 30;
SetColor(GRAY);
for(int i = 4; i <= 15; i++) {
if(i == 13 || i == 14 ||i == 15) continue;
maP[i][4] = maP[4][i] = maP[26][i] = maP[i][26] = 1;
}
for(int i = 1; i <= SIZ; i++) {
for(int j = 1; j <= SIZ; j++) {
if(maP[i][j] == 1) {
locate(i,j*2-1);
cout<<"";
}
}
}
SetColor(DARKGREEN);
for(int i = 10; i<=20; i++) {
if(i == 13 || i == 17) continue;
maP[i][10] = maP[10][i] = maP[20][i] = maP[i][20] = 2;
}
create_tree(5,5);
create_tree(18,18);
for(int i = 1; i <= 15; i++) {
for(int j = 1; j <= 20; j++) {
if(maP[i][j] == 2) {
locate(i,j*2-1);
cout<<"";
}
}
}
}
else if(x == 7){
cout<<"可以射没";
ma1=15;
ma2=15;
Sleep(1500);
SIZ = 15;
SetColor(RED);
for(int i = 1; i <= 15; i++) {
for(int j = 1; j <= 15; j++) {
if(rand()%5==0){
maP[i][j] = 5;
}
}
}
for(int i = 1; i <= 15; i++) {
for(int j = 1; j <= 15; j++) {
if(maP[i][j] == 5) {
locate(i,j*2-1);
cout<<"";
}
}
}
}
else if(x == 8){
SIZ = 30;
cout<<"世上本没有路";
Sleep(1500);
ma1=5;
ma2=5;
SetColor(WHITE);//2 3 3 3 3 2 2829 2828 2928
for(int i=1;i<=30;i++){
for(int j=1;j<=30;j++){
if(i==2&&j==3||i==3&&j==3||i==3&&j==2||i==28&&j==29||i==28&&j==28||i==29&&j==28){
continue;
}
maP[i][j]=4;
locate(i,j*2-1);
cout<<"";
}
}
}
}
void cleanbody(int x,int y);
void putbody(int x,int y,int z);
void player_init() {
p1.hp = p2.hp = 600;
p1.gun = p2.gun = 1;
p1.direct = 4;
p2.direct = 2;
p1.x = 2;
p1.y = 2;
maP[2][2]=3;
p2.x = SIZ - 1;
p2.y = SIZ - 1;
maP[SIZ-1][SIZ-1]=3;
putbody(p1.x,p1.y,1);
putbody(p2.x,p2.y,2);
}
void mapinit() {
for(int i = 1; i <= SIZ; i++) {
maP[i][1] = maP[1][i] = maP[SIZ][i] = maP[i][SIZ] = 1;
}
}
void init() {
printf("Use Which map?\n");
int x;
cin>>x;
system("cls");
use_map(x);
mapinit();
print_map();
player_init();
}
void putbody(int x,int y,int z) {
PLAYER player;
if(z == 1){
SetColor(BLUE);
player = p1;
}
else if(z == 2){
SetColor(RED);
player = p2;
}
locate(x,y*2-1);
int q=rand()%2;
if(!yinshen){
if(player.direct==4){
cout<<"";
}
else if(player.direct==2){
cout<<"";
}
else if(player.direct==1){
cout<<"";
}
else{
cout<<"";
}
}
else{
if(q==0){
if(player.direct==4){
cout<<"";
}
else if(player.direct==2){
cout<<"";
}
else if(player.direct==1){
cout<<"";
}
else{
cout<<"";
}
}
}
SetColor(WHITE);
}
void cleanbody(int x,int y) {
locate(x,y*2-1);
cout<<" ";
}
bool judge(int x,int y, PLAYER &player) {
if(maP[x][y] == 1) return false;
if(maP[x][y] == 2) return false;
if(maP[x][y] == 3) return false;
if(maP[x][y] == 4) return false;
if(maP[x][y] == 5){
player.hp-=150;
if(rand()%3==0){
maP[x][y]=0;
return true;
}
return false;
}
return true;
}
bool judge_gun(int x,int y) {
if(maP[x][y] == 1) return 0;
if(maP[x][y] == 2) return 0;
if(maP[x][y] == 3) {
if(p1.x == x && p1.y == y) p1.hp -= 50;
else p2.hp -= 50;
return 0;
}
if(maP[x][y] == 4){
if(rand()%2==0){
maP[x][y]=0;
return true;
}
return false;
}
if(maP[x][y] == 5){
if(rand()%3==0){
maP[x][y]=0;
return true;
}
return false;
}
return 1;
}
int cnt;
struct Clip {
int x,y;
int derect;
int force;
int start;
bool flag;
} clip[1000000];
void create_clip(int y,int x,int a,int b) {
int X,Y;
if(y == 1) {
if(!judge_gun(a-1,b)) return;
X = a-1;
Y = b;
} else if(y == 2) {
if(!judge_gun(a,b-1)) return;
X = a;
Y = b-1;
} else if(y == 3) {
if(!judge_gun(a+1,b)) return;
X = a+1;
Y = b;
} else if(y == 4) {
if(!judge_gun(a,b+1)) return;
X = a;
Y = b+1;
}
cnt++;
GetLocalTime( &sys );
clip[cnt].start = sys.wMilliseconds + sys.wSecond * 60 + sys.wHour * 3600;
clip[cnt].x = X;
clip[cnt].y = Y;
if(x == 1) {
clip[cnt].derect = p1.direct;
} else if(x == 2) {
clip[cnt].derect = p2.direct;
}
}
void shoot(int x) {
if(MapMode == 0){
if(x == 1) {
create_clip(p1.direct,1,p1.x,p1.y);
} else if(x == 2) {
create_clip(p2.direct,2,p2.x,p2.y);
}
}
else if(MapMode == 3){
if(x == 1) {
create_clip(p1.direct,1,p1.x,p1.y);
} else if(x == 2) {
create_clip(p2.direct,2,p2.x,p2.y);
}
}
}
void show(int direct){
if(direct==4){
cout<<"";
}
else if(direct==2){
cout<<"";
}
else if(direct==1){
cout<<"";
}
else{
cout<<"";
}
}
void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void clean_clip(int x,int y) {
locate(x,y*2-1);
cout<<" ";
locate(1,1);
}
void print_clip(int x,int y,int i) {
if(clip[i].flag) {
clean_clip(x,y);
return;
}
locate(x,y*2-1);
SetColor(YELLOW);
cout<<"●";
locate(1,1);
}
void clipmove() {
GetLocalTime( &sys );
int t = sys.wMilliseconds + sys.wSecond * 60 + sys.wHour * 3600;
for(int i = 1; i <= cnt; i++) {
if(clip[i].flag) continue;
if(abs(clip[i].start - t) > 50) {
clip[i].start = t;
int x = clip[i].x;
int y = clip[i].y;
if(clip[i].derect==1) {
if(!judge_gun(clip[i].x-1,clip[i].y)) {
clip[i].flag = 1;
clean_clip(x,y);
continue;
}
clean_clip(clip[i].x,clip[i].y);
clip[i].x--;
print_clip(clip[i].x,clip[i].y,i);
} else if(clip[i].derect==2) {
if(!judge_gun(clip[i].x,clip[i].y-1)) {
clip[i].flag = 1;
clean_clip(x,y);
continue;
}
clean_clip(clip[i].x,clip[i].y);
clip[i].y--;
print_clip(clip[i].x,clip[i].y,i);
} else if(clip[i].derect==3) {
if(!judge_gun(clip[i].x+1,clip[i].y)) {
clip[i].flag = 1;
clean_clip(x,y);
continue;
}
clean_clip(clip[i].x,clip[i].y);
clip[i].x++;
print_clip(clip[i].x,clip[i].y,i);
} else if(clip[i].derect==4) {
if(!judge_gun(clip[i].x,clip[i].y+1)) {
clip[i].flag = 1;
clean_clip(x,y);
continue;
}
clean_clip(clip[i].x,clip[i].y);
clip[i].y++;
print_clip(clip[i].x,clip[i].y,i);
}
}
}
}
void judge_hp() {
int x = p1.hp;
int y = p2.hp;
int k = p1.kill;
int k1 = p2.kill;
if(k>=5){
locate(1,1);
system("cls");
printf("GAME OVER!\nTHE WINNER IS P1!");
Sleep(5000);
exit(0);
}
if(k1>=5){
locate(1,1);
system("cls");
printf("GAME OVER!\nTHE WINNER IS P2!");
Sleep(5000);
exit(0);
}
if(x<=0){
p1.hp=600;
p2.kill++;
}
if(y<=0){
p2.hp=600;
p1.kill++;
}
}
void prog() {
char ch;
unsigned long long ddp1,ddp2;
while(true) {
gotoxy(0,70);
cout<<"|P1血量:"<<p1.hp;
gotoxy(0,81);
cout<<"|";
gotoxy(1,70);
cout<<"| |";
gotoxy(1,71);
cout<<"kill:"<<p1.kill;
gotoxy(2,70);printf("-----------");
gotoxy(3,70);
cout<<"|P2血量:"<<p2.hp;
gotoxy(3,81);cout<<"|";
gotoxy(4,70);cout<<"|";gotoxy(4,81);cout<<"|";
gotoxy(4,71);
cout<<"kill:"<<p2.kill;
gotoxy(0,0);
if(kbhit()) {
ch=getch();
if(ch == 'w') {
p1.direct = 1;
if(judge(p1.x-1,p1.y,p1)){
cleanbody(p1.x,p1.y);
maP[p1.x][p1.y] = 0;
putbody(--p1.x,p1.y,1);
maP[p1.x][p1.y] = 3;
}
} else if(ch == 'i') {
p2.direct = 1;
if( judge(p2.x-1,p2.y,p2)){
cleanbody(p2.x,p2.y);
maP[p2.x][p2.y] = 0;
putbody(--p2.x,p2.y,2);
maP[p2.x][p2.y] = 3;
}
} else if(ch == 'a') {
p1.direct = 2;
if(judge(p1.x,p1.y-1,p1)){
cleanbody(p1.x,p1.y);
maP[p1.x][p1.y] = 0;
putbody(p1.x,--p1.y,1);
maP[p1.x][p1.y] = 3;
}
} else if(ch == 'j') {
p2.direct = 2;
if(judge(p2.x,p2.y-1,p2)){
cleanbody(p2.x,p2.y);
maP[p2.x][p2.y] = 0;
putbody(p2.x,--p2.y,2);
maP[p2.x][p2.y] = 3;
}
} else if(ch == 's') {
p1.direct = 3;
if(judge(p1.x+1,p1.y,p1)){
cleanbody(p1.x,p1.y);
maP[p1.x][p1.y] = 0;
putbody(++p1.x,p1.y,1);
maP[p1.x][p1.y] = 3;
}
} else if(ch == 'k') {
p2.direct = 3;
if(judge(p2.x+1,p2.y,p2)){
cleanbody(p2.x,p2.y);
maP[p2.x][p2.y] = 0;
putbody(++p2.x,p2.y,2);
maP[p2.x][p2.y] = 3;
}
} else if(ch == 'd') {
p1.direct = 4;
if(judge(p1.x,p1.y+1,p1)){
cleanbody(p1.x,p1.y);
maP[p1.x][p1.y] = 0;
putbody(p1.x,++p1.y,1);
maP[p1.x][p1.y] = 3;
}
} else if(ch == 'l') {
p2.direct = 4;
if(judge(p2.x,p2.y+1,p2)){
cleanbody(p2.x,p2.y);
maP[p2.x][p2.y] = 0;
putbody(p2.x,++p2.y,2);
maP[p2.x][p2.y] = 3;
}
} else if(ch == 'p'&&ddp2>=ma1) {
shoot(2);
ddp2=0;
} else if(ch == 'r'&&ddp1>=ma2) {
shoot(1);
ddp1=0;
}
Sleep(20);
}
clipmove();
judge_hp();
Sleep(10);
ddp1++;
ddp2++;
}
}
void welcome() {
SetColor(YELLOW);
printf("操作方法:\n玩家1 wasd控制移动,r攻击\n玩家2 ijkl控制移动,p攻击\n谁先杀死对方5次谁赢\n----------------[list]\n1.木棍 | 2.方中方中方 | 3.机枪射手与看不见的障碍 | 4.赤石\n5.狭路相逢 | 6.我在哪里? | 7.注意脚下\n8.扩山工\n");
Sleep(1000);
}
int main() {
srand(time(NULL));
HideCursor();
welcome();
GetLocalTime( &sys );
init();
prog();
return 0;
}
评论: