专栏文章
单人枪战小游戏
休闲·娱乐参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @minu4wgo
- 此快照首次捕获于
- 2025/12/02 08:23 3 个月前
- 此快照最后确认于
- 2025/12/02 08:23 3 个月前
单人枪战小游戏
普通:单挑人机
CPP#include<bits/stdc++.h>
#include<windows.h>
#define Key_Down(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)&0x8000)?1:0)
using namespace std;
int w = 15, h = 52;
//int h1 = 5, h2 = 5;
//int h1 = 5, h2 = 20;
//int h1 = 1000, h2 = 1000;
int h1 = 10, h2 = 10;
//int h1 = 1, h2 = 1;
int x1 = w-2, yy1 = 1, x2 = w-2, y2 = h-2;
bool to_right1 = true, to_right2 = false;
void SetPos(int x, int y){
COORD pos;
pos.X=y,pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void end_game(string winner){
SetPos(w+4, 0);
printf("%s胜利 \n请按q退出 ", winner.c_str());
while(1){
if(Key_Down('Q'))break;
}
exit(0);
}
struct zidan{
int x, y;
bool way;//way: false左 true右
};
queue<zidan>q1, q2;
char mp[55][55]={
{""},
{" ####################################################"},
{" # # #"},
{" # ## #"},
{" # ##### #"},
{" # #"},
{" # ######## ##### #"},
{" # # #"},
{" # ## #"},
{" # #"},
{" # #"},
{" # #"},
{" # ### #"},
{" # #"},
{" # #"},
{" ####################################################"},
};
int main(){
srand((unsigned)(time(nullptr)));
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
for(int i = 1; i <= w; i++){
for(int j = 1; j <= h; j++){
putchar(mp[i][j]);
}
putchar('\n');
}
int lx1, lyy1, lx2, ly2, s1=0, s2=0, sum1=0, sum2=0, cl1=0, cl2=0;
bool huan1=false, huan2=false;
while(1){
if(h2 <= 0)end_game("玩家");
if(h1 <= 0)end_game("人机");
s1++;
s1%=2;
s2++;
s2%=2;
lx1=x1,lyy1=yy1,lx2=x2,ly2=y2;
if(Key_Down('Q'))break;
if(Key_Down('A')&&yy1-1>=1&&yy1-1<=h-2&&mp[x1+1][yy1]!='#')yy1--,to_right1 = false;
if(Key_Down('D')&&yy1+1>=1&&yy1+1<=h-2&&mp[x1+1][yy1+2]!='#')yy1++,to_right1 = true;
if(Key_Down('W')&&x1-1>=1&&x1-1<=w-2&&mp[x1][yy1+1]!='#')x1--;
else if(s1&&x1+1>=1&&x1+1<=w-2&&mp[x1+2][yy1+1]!='#')x1++;
if(Key_Down('S')&&yy1+1>=1&&x1+1<=w-2&&mp[x1+2][yy1+1]!='#')x1++;
if(Key_Down(32)){
if(sum1<50){
q1.push({x1, yy1, to_right1});
sum1++;
}else{
if(!huan1){
cl1=clock();
huan1=1;
}
else if(clock()-cl1>=5000)sum1=0,huan1=0;
}
}
int r=rand()%25+1;
if(r==1&&y2-1>=1&&y2-1<=h-2&&mp[x2+1][y2]!='#')y2--,to_right2 = false;
if(r==2&&y2+1>=1&&y2+1<=h-2&&mp[x2+1][y2+2]!='#')y2++,to_right2 = true;
if((r==3||r==6)&&x2-1>=1&&x2-1<=w-2&&mp[x2][y2+1]!='#')x2--;
else if(s1&&x2+1>=1&&x2+1<=w-2&&mp[x2+2][y2+1]!='#')x2++;
if(r==4&&y2+1>=1&&x2+1<=w-2&&mp[x2+2][y2+1]!='#')x2++;
if((r==5||r>=7)){
if(sum2<50){
q2.push({x2, y2, to_right2});
sum2++;
}else{
if(!huan2){
cl2=clock();
huan2=1;
}
else if(clock()-cl2>=5000)sum2=0,huan2=0;
}
}
bool m1=0, m2=0;
for(int j = 1; j <= 2; j++){
for(int i = 0; i < q1.size(); i++){
if(q1.front().x == x2 && q1.front().y == y2){
m2=1;
}
SetPos(q1.front().x, q1.front().y);
putchar(' ');
if(q1.front().way == 0){
// if(to_right1 == 0){
if(q1.front().y-1>=1&&q1.front().y-1<=h-2){
if(mp[q1.front().x+1][q1.front().y]!='#'){
q1.front().y--;
q1.push(q1.front());
}
}
if(mp[q1.front().x+1][q1.front().y]=='#'&&q1.front().y-1>=0&&q1.front().y-1<=h-1){
q1.pop();
continue;
}
}else{
if(q1.front().y+1>=1&&q1.front().y+1<=h-2){
if(mp[q1.front().x+1][q1.front().y+2]!='#'){
q1.front().y++;
q1.push(q1.front());
}
}
if(mp[q1.front().x+1][q1.front().y+2]=='#'&&q1.front().y+1>=0&&q1.front().y+1<=h-1){
q1.pop();
continue;
}
}
SetPos(q1.front().x, q1.front().y);
putchar('-');
q1.pop();
}
for(int i = 0; i < q2.size(); i++){
if(q2.front().x == x1 && q2.front().y == yy1){
m1=1;
}
SetPos(q2.front().x, q2.front().y);
putchar(' ');
if(q2.front().way == 0){
// if(to_right2 == 0){
if(q2.front().y-1>=1&&q2.front().y-1<=h-2){
if(mp[q2.front().x+1][q2.front().y]!='#'){
q2.front().y--;
q2.push(q2.front());
}
}
if(mp[q2.front().x+1][q2.front().y]=='#'&&q2.front().y-1>=0&&q2.front().y-1<=h-1){
q2.pop();
continue;
}
}else{
if(q2.front().y+1>=1&&q2.front().y+1<=h-2){
if(mp[q2.front().x+1][q2.front().y+2]!='#'){
q2.front().y++;
q2.push(q2.front());
}
}
if(mp[q2.front().x+1][q2.front().y+2]=='#'&&q2.front().y+1>=0&&q2.front().y+1<=h-1){
q2.pop();
continue;
}
}
SetPos(q2.front().x, q2.front().y);
putchar('=');
q2.pop();
}
}
h1-=(m1);
h2-=(m2);
SetPos(lx1, lyy1);
putchar(' ');
SetPos(lx2, ly2);
putchar(' ');
SetPos(x1, yy1);
putchar('a');
SetPos(x2, y2);
putchar('b');
SetPos(w, 0);
printf("玩家剩余血量:%4d\n", h1);
printf("人机剩余血量:%4d\n", h2);
printf("玩家发射子弹数:%2d\n", sum1);
printf("人机发射子弹数:%2d", sum2);
Sleep(50);
}
//32 191
}
进阶:1挑2人机人机
CPP#include<bits/stdc++.h>
#include<windows.h>
#define Key_Down(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)&0x8000)?1:0)
using namespace std;
int w = 15, h = 62;
//int h1 = 5, h2 = 5;
//int h1 = 5, h2 = 20;
//int h1 = 1000, h2 = 1000;
int h1 = 10, h2 = 10, h3 = 10;
//int h1 = 1, h2 = 1;
int x1 = w-2, yy1 = 1, x2 = w-2, y2 = h-2, x3 = 1, y3 = (h-2)/2;
#define y1 yy1
bool to_right1 = true, to_right2 = false, to_right3 = true;
void SetPos(int x, int y){
COORD pos;
pos.X=y,pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void end_game(string winner){
SetPos(w+6, 0);
printf("%s胜利 \n请按q退出 ", winner.c_str());
while(1){
if(Key_Down('Q'))break;
}
exit(0);
}
struct zidan{
int x, y;
bool way;//way: false左 true右
};
queue<zidan>q1, q2, q3;
char mp[105][105]={
{""},
{" ##############################################################"},
{" # #"},
{" # #### ###### #"},
{" # ##### #"},
{" # #"},
{" # ######## ##### #"},
{" # #"},
{" # ### #"},
{" # ###### #"},
{" # ###### ### #"},
{" # #"},
{" # ### #"},
{" # #"},
{" # #"},
{" ##############################################################"},
};
int main(){
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
for(int i = 1; i <= w; i++){
for(int j = 1; j <= h; j++){
putchar(mp[i][j]);
}
putchar('\n');
}
int lx1, ly1, lx2, ly2, lx3, ly3, s1=0, s2=0, s3=0, sum1=0, sum2=0, sum3=0, cl1=0, cl2=0, cl3=0;
bool huan1=false, huan2=false, huan3=false;
while(1){
if(h2 <= 0 && h3 <= 0)end_game("玩家");
if(h1 <= 0 && h3 <= 0)end_game("人机1");
if(h1 <= 0 && h2 <= 0)end_game("人机2");
s1++;
s1%=2;
s2++;
s2%=2;
s3++;
s3%=2;
lx1=x1,ly1=y1,lx2=x2,ly2=y2,lx3=x3,ly3=y3;
if(h1>=1&&Key_Down('Q'))break;
if(h1>=1&&Key_Down('A')&&y1-1>=1&&y1-1<=h-2&&mp[x1+1][y1]!='#')y1--,to_right1 = false;
if(h1>=1&&Key_Down('D')&&y1+1>=1&&y1+1<=h-2&&mp[x1+1][y1+2]!='#')y1++,to_right1 = true;
if(h1>=1&&Key_Down('W')&&x1-1>=1&&x1-1<=w-2&&mp[x1][y1+1]!='#')x1--;
else if(h1>=1&&s1&&x1+1>=1&&x1+1<=w-2&&mp[x1+2][y1+1]!='#')x1++;
if(h1>=1&&Key_Down('S')&&y1+1>=1&&x1+1<=w-2&&mp[x1+2][y1+1]!='#')x1++;
if(h1>=1&&Key_Down(32)){
if(sum1<50){
q1.push({x1, y1, to_right1});
sum1++;
}else{
if(!huan1){
cl1=clock();
huan1=1;
}
else if(clock()-cl1>=5000)sum1=0,huan1=0;
}
}
int r=rand()%10+1;
if(h2>=1&&r==1&&y2-1>=1&&y2-1<=h-2&&mp[x2+1][y2]!='#')y2--,to_right3 = false;
if(h2>=1&&r==2&&y2+1>=1&&y2+1<=h-2&&mp[x2+1][y2+2]!='#')y2++,to_right3 = true;
if(h2>=1&&(r==3||r>=7)&&x2-1>=1&&x2-1<=w-2&&mp[x2][y2+1]!='#')x2--;
else if(h2>=1&&s1&&x2+1>=1&&x2+1<=w-2&&mp[x2+2][y2+1]!='#')x2++;
if(h2>=1&&r==4&&y2+1>=1&&x2+1<=w-2&&mp[x2+2][y2+1]!='#')x2++;
if(h2>=1&&(r==5||r>=6)){
if(sum2<50){
q2.push({x2, y2, to_right3});
sum2++;
}else{
if(!huan2){
cl2=clock();
huan2=1;
}
else if(clock()-cl2>=5000)sum2=0,huan2=0;
}
}
r=rand()%25+1;
if(h3>=1&&r==1&&y3-1>=1&&y3-1<=h-2&&mp[x3+1][y3]!='#')y3--,to_right3 = false;
if(h3>=1&&r==2&&y3+1>=1&&y3+1<=h-2&&mp[x3+1][y3+2]!='#')y3++,to_right3 = true;
if(h3>=1&&(r==3||r==6)&&x3-1>=1&&x3-1<=w-2&&mp[x3][y3+1]!='#')x3--;
else if(h3>=1&&s3&&x3+1>=1&&x3+1<=w-2&&mp[x3+2][y3+1]!='#')x3++;
if(h3>=1&&r==4&&y3+1>=1&&x3+1<=w-2&&mp[x3+2][y3+1]!='#')x3++;
if(h3>=1&&(r==5||r>=7)){
if(sum3<50){
q3.push({x3, y3, to_right3});
sum3++;
}else{
if(!huan3){
cl3=clock();
huan3=1;
}
else if(clock()-cl3>=5000)sum3=0,huan3=0;
}
}
bool m1=0, m2=0, m3=0;
for(int j = 1; j <= 2; j++){
for(int i = 0; i < q1.size(); i++){
if(q1.front().x == x2 && q1.front().y == y2){
m2=1;
}
if(q1.front().x == x3 && q1.front().y == y3){
m3=1;
}
SetPos(q1.front().x, q1.front().y);
putchar(' ');
if(q1.front().way == 0){
// if(to_right1 == 0){
if(q1.front().y-1>=1&&q1.front().y-1<=h-2){
if(mp[q1.front().x+1][q1.front().y]!='#'){
q1.front().y--;
q1.push(q1.front());
}
}
if(mp[q1.front().x+1][q1.front().y]=='#'&&q1.front().y-1>=0&&q1.front().y-1<=h-1){
q1.pop();
continue;
}
}else{
if(q1.front().y+1>=1&&q1.front().y+1<=h-2){
if(mp[q1.front().x+1][q1.front().y+2]!='#'){
q1.front().y++;
q1.push(q1.front());
}
}
if(mp[q1.front().x+1][q1.front().y+2]=='#'&&q1.front().y+1>=0&&q1.front().y+1<=h-1){
q1.pop();
continue;
}
}
SetPos(q1.front().x, q1.front().y);
putchar('-');
q1.pop();
}
for(int i = 0; i < q2.size(); i++){
if(q2.front().x == x1 && q2.front().y == y1){
m1=1;
}
if(q2.front().x == x3 && q2.front().y == y3){
m3=1;
}
SetPos(q2.front().x, q2.front().y);
putchar(' ');
if(q2.front().way == 0){
// if(to_right2 == 0){
if(q2.front().y-1>=1&&q2.front().y-1<=h-2){
if(mp[q2.front().x+1][q2.front().y]!='#'){
q2.front().y--;
q2.push(q2.front());
}
}
if(mp[q2.front().x+1][q2.front().y]=='#'&&q2.front().y-1>=0&&q2.front().y-1<=h-1){
q2.pop();
continue;
}
}else{
if(q2.front().y+1>=1&&q2.front().y+1<=h-2){
if(mp[q2.front().x+1][q2.front().y+2]!='#'){
q2.front().y++;
q2.push(q2.front());
}
}
if(mp[q2.front().x+1][q2.front().y+2]=='#'&&q2.front().y+1>=0&&q2.front().y+1<=h-1){
q2.pop();
continue;
}
}
SetPos(q2.front().x, q2.front().y);
putchar('=');
q2.pop();
}
for(int i = 0; i < q3.size(); i++){
if(q3.front().x == x1 && q3.front().y == y1){
m1=1;
}
if(q3.front().x == x2 && q3.front().y == y2){
m2=1;
}
SetPos(q3.front().x, q3.front().y);
putchar(' ');
if(q3.front().way == 0){
// if(to_right2 == 0){
if(q3.front().y-1>=1&&q3.front().y-1<=h-2){
if(mp[q3.front().x+1][q3.front().y]!='#'){
q3.front().y--;
q3.push(q3.front());
}
}
if(mp[q3.front().x+1][q3.front().y]=='#'&&q3.front().y-1>=0&&q3.front().y-1<=h-1){
q3.pop();
continue;
}
}else{
if(q3.front().y+1>=1&&q3.front().y+1<=h-2){
if(mp[q3.front().x+1][q3.front().y+2]!='#'){
q3.front().y++;
q3.push(q3.front());
}
}
if(mp[q3.front().x+1][q3.front().y+2]=='#'&&q3.front().y+1>=0&&q3.front().y+1<=h-1){
q3.pop();
continue;
}
}
SetPos(q3.front().x, q3.front().y);
if(q3.front().way==0)putchar('<');
else putchar('>');
q3.pop();
}
}
if(h1>=1)h1-=(m1);
if(h2>=1)h2-=(m2);
if(h3>=1)h3-=(m3);
SetPos(lx1, ly1);
putchar(' ');
SetPos(lx2, ly2);
putchar(' ');
SetPos(lx3, ly3);
putchar(' ');
SetPos(x1, y1);
if(h1>=1)putchar('a');
SetPos(x2, y2);
if(h2>=1)putchar('b');
SetPos(x3, y3);
if(h3>=1)putchar('c');
SetPos(w, 0);
printf("玩家剩余血量:%4d\n", h1);
printf("人机1剩余血量:%4d\n", h2);
printf("人机2剩余血量:%4d\n", h3);
printf("玩家发射子弹数:%2d\n", sum1);
printf("人机1发射子弹数:%2d\n", sum2);
printf("人机2发射子弹数:%2d", sum3);
Sleep(50);
}
//32 191
}
食用方法:
wasd移动空格发射子弹
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...