【C语言】双人格斗小游戏(源码)

【C语言】双人格斗小游戏(源码)【C语言】双人格斗小游戏芜湖程序介绍:【C语言】实现双人控制的战斗小游戏/*————————————–project:双人小游戏anthor:LLz操作移动逆、顺时针旋转发射子弹玩家1456879 0玩家2adws qe 空格——————————–*/#include&lt

大家好,又见面了,我是你们的朋友全栈君。

【C语言】双人格斗小游戏

芜湖

程序介绍:【C语言】实现双人控制的战斗小游戏

/*-------------------------------------- project: 双人小游戏 anthor: LLz 操作 移动 逆、顺时针旋转 发射子弹 玩家1 4568 7 9 0 玩家2 adws q e 空格 --------------------------------*/ 
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define High 20 // 游戏画面尺寸
#define Width 100 
// 全局变量
int position_x,position_y,p_x,p_y,turn_a,turn_b,num_a,num_b,num_max,life_a = 10,life_b = 10; // 飞机位置
int canvas[High][Width] = { 
   0}; // 二维数组存储游戏画布中对应的元素
                        // 0为空格,1为飞机*,2为子弹|,3为敌机@
int next[8][2] = { 
   { 
   0,1},{ 
   1,1},{ 
   1,0},{ 
   1,-1},{ 
   0,-1},{ 
   -1,-1},{ 
   -1,0},{ 
   -1,1}}; //从右 右下 下 左下 
int bullet_a[21][4];
int bullet_b[21][4];   //a b玩家子弹20发; 
void gotoxy(int x,int y)  //光标移动到(x,y)位置
{ 
   
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
} 
void startup() // 数据初始化
{ 
   
	num_a = 0;
	num_b = 0;
	turn_a = 0;
	turn_b = 0;
	p_x = High/2;
	p_y = Width* 4 / 5;
	canvas[p_x][p_y] = 5;
	position_x = High/2;
	position_y = Width/5;
	canvas[position_x][position_y] = 1;	
}
void show()  // 显示画面
{ 
   
	gotoxy(0,0);  // 光标移动到原点位置,以下重画清屏
	int i,j;
	for (i=0;i<High;i++)
	{ 
   
		for (j=0;j<Width;j++)
		{ 
   
			if( i == 0 || i == High -1 || j == 0 || j == Width -1){ 
   
				canvas[i][j] = 4;
				printf("0");
				continue;
			}
			if (canvas[i][j]==0)
				printf(" ");   // 输出空格
			else if (canvas[i][j]==1)
				printf("N");   // 输出飞机a
			else if (canvas[i][j]==2)
				printf("@");   // 输出飞机B
			else if (canvas[i][j]==3)
				printf("o");   // 输出子弹o 
			else if (canvas[i][j]==4)
				printf("o");   // 输出飞机a指向 
			else if (canvas[i][j]==5)
				printf("o");   // 输出飞机b指向 
		}
		printf("\n");
	}
	printf("A:");
	for( i = 1; i <= 10; i++ )
		if( i <= life_a)
			printf("■");
		else printf(" ");
	printf("\nB: ");
	for( i = 1; i <= 10; i++ )
		if( i <= life_b)
			printf("■");
		else printf(" ");
}	
void updateWithoutInput()  // 与用户输入无关的更新
{ 
   	
	int i,j,k;
	num_max = num_a > num_b? num_a : num_b;
	for( i = 1; i <= num_max; i++){ 
   
			if( bullet_a[i][2] == 0 || bullet_a[i][2] == High - 1){ 
   
				bullet_a[i][0] = -bullet_a[i][0];
			}
			else if( bullet_a[i][3] == 0 || bullet_a[i][3] == Width - 1){ 
   
				bullet_a[i][1] = -bullet_a[i][1];
			}
			if( bullet_b[i][2] == 0 || bullet_b[i][2] == High - 1){ 
   
				bullet_b[i][0] = -bullet_b[i][0];
			}
			else if( bullet_b[i][3] == 0 || bullet_b[i][3] == Width - 1){ 
   
				bullet_b[i][1] = -bullet_b[i][1];
			}
			canvas[ bullet_a[i][2] ][ bullet_a[i][3] ] = 0;
			canvas[ bullet_b[i][2] ][ bullet_b[i][3] ] = 0;
			bullet_a[i][2] += bullet_a[i][0];
			bullet_a[i][3] += bullet_a[i][1];
			bullet_b[i][2] += bullet_b[i][0];
			bullet_b[i][3] += bullet_b[i][1];
			canvas[ bullet_a[i][2] ][ bullet_a[i][3] ] = 3;
			canvas[ bullet_b[i][2] ][ bullet_b[i][3] ] = 3;
	}
	for (k=1;k<=num_max;k++)
	{ 
   
		if (((position_x==bullet_a[k][2]) && (position_y==bullet_a[k][3]))||((position_x==bullet_b[k][2]) && (position_y==bullet_b[k][3])))  // 敌机撞到我机
		{ 
   
			life_a--;
			if( life_a <= 0){ 
   
				printf("A 玩家失败!\n");
				Sleep(3000);
				system("pause");
				exit(0);
			}
		}
		if (((p_x==bullet_a[k][2]) && (p_y==bullet_a[k][3]))||((p_x==bullet_b[k][2]) && (p_y==bullet_b[k][3])))  // 敌机撞到我机
		{ 
   
			life_b--;
			if( life_b <= 0){ 
   
				printf("B 玩家失败!\n");
				Sleep(3000);
				system("pause");
				exit(0);
			}
		}
	}
}
void updateWithInput()  // 与用户输入有关的更新
{ 
   
	char input;
	if(kbhit())  // 判断是否有输入
	{ 
   
		input = getch();  // 根据用户的不同输入来移动,不必输入回车
		if (input == 'a' && position_y>1) 
		{ 
   
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
			canvas[position_x][position_y] = 0;
			position_y--;  // 位置左移
			canvas[position_x][position_y] = 1;
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
		}
		else if (input == 'd' && position_y<Width-2)
		{ 
   
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
			canvas[position_x][position_y] = 0;
			position_y++;  // 位置右移
			canvas[position_x][position_y] = 1;
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
		}
		else if (input == 'w' && position_x > 1)
		{ 
   
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
			canvas[position_x][position_y] = 0;
			position_x--;  // 位置上移
			canvas[position_x][position_y] = 1;
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
		}
		else if (input == 's'&& position_x < High - 2)
		{ 
   
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
			canvas[position_x][position_y] = 0;
			position_x++;  // 位置下移
			canvas[position_x][position_y] = 1;
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
		}		
		else if (input == ' ' && num_a <= 20)  // 发射子弹
		{ 
   
			num_a++;
			bullet_a[num_a][0] = next[turn_a][0];
			bullet_a[num_a][1] = next[turn_a][1];
			bullet_a[num_a][2] = position_x + bullet_a[num_a][0];
			bullet_a[num_a][3] = position_y + bullet_a[num_a][1];
			canvas[ bullet_a[num_a][2] ][ bullet_a[num_a][3] ] = 3;
		}
		else if (input == 'q')  // 炮弹换方向 
		{ 
   
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
			turn_a--;
			if(turn_a < 0)
				turn_a = 7;
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
		}
		else if (input == 'e')  // 炮弹换方向 
		{ 
   
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
			turn_a++;
			if(turn_a > 7)
				turn_a = 0;
			canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
		}
		else if (input == '4' && position_y>1) 
		{ 
   
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
			canvas[p_x][p_y] = 0;
			p_y--;  // 位置左移
			canvas[p_x][p_y] = 2;
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
		}
		else if (input == '6' && p_y<Width-2)
		{ 
   
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
			canvas[p_x][p_y] = 0;
			p_y++;  // 位置右移
			canvas[p_x][p_y] = 2;
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
		}
		else if (input == '8' && p_x > 1)
		{ 
   
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
			canvas[p_x][p_y] = 0;
			p_x--;  // 位置上移
			canvas[p_x][p_y] = 2;
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
		}
		else if (input == '5' && p_x < High - 2)
		{ 
   
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
			canvas[p_x][p_y] = 0;
			p_x++;  // 位置下移
			canvas[p_x][p_y] = 2;
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
		}		
		else if (input == '0' && num_b <= 20)  // 发射子弹
		{ 
   
			num_b++;
			bullet_b[num_b][0] = next[turn_b][0];
			bullet_b[num_b][1] = next[turn_b][1];
			bullet_b[num_b][2] = p_x + bullet_b[num_b][0];
			bullet_b[num_b][3] = p_y + bullet_b[num_b][1];
			canvas[ bullet_b[num_b][2] ][ bullet_b[num_b][3] ] = 3;
		}	
		else if (input == '7')  // 炮弹换方向 
		{ 
   
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
			turn_b--;
			if(turn_b < 0)
				turn_b = 7;
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
		}
		else if (input == '9')  // 炮弹换方向 
		{ 
   
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
			turn_b++;
			if(turn_b > 7)
				turn_b = 0;
			canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
		}
	}
}
int main()
{ 
   
	startup();  // 数据初始化
	system("color 30");
	while (1)  // 游戏循环执行
	{ 
   
		show();  // 显示画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();  // 与用户输入有关的更新
	}
	return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/144385.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 微信强制解绑手机号码方法[通俗易懂]

    微信强制解绑手机号码方法[通俗易懂]现在微信不用手机号已经无法进行注册,但是一个手机号却可以多次注册。这问题就来了,怎么将手机号进行解绑然后继续注册呢?7月27日实测有效。步骤如下:1、记住需要解绑手机的微信号,然后退出微信2、

    2022年8月1日
  • mysql导入excel表异常_mysql导入excel表格数据时出错的解决

    mysql导入excel表异常_mysql导入excel表格数据时出错的解决NavicatforMySQL导入数据时报错1:导入的是Excel2007表格格式的数据。2:报错以后数据加进去了。(选择了错误继续执行)3:这个错误对我的数据有影响吗?4:造成这个错误的原因是什么5:这个是日志文件[2012-07-1113:57:48][Msg]Importstart[2012-07-1113:57:48][Msg]Importtype-Excel20…

    2022年9月21日
  • 不动点与递归数列的极限有关吗_递推数列不动点法

    不动点与递归数列的极限有关吗_递推数列不动点法

    不动点与递归数列的极限
    有些收敛的数列才有不动点,比如x=1+x^(-1);x=1+x^(-2);x=1+x^(-3),下面写程序验证: 
    对x=1+x^(-3):
     
    (defunpow(numcount)
    (if(or(>count1)(eq count 1))
        (*num 
         (pownum 
            (-count1)

    2022年9月11日
  • 基于Packet Tracer的校园网络设计与规划「建议收藏」

    基于Packet Tracer的校园网络设计与规划「建议收藏」本文仅仅为本人课程要求而设计的方案,本文仅仅使用RIP动态路由协议,HSRP协议,浮动路由,静态路由,等价路由,DHCP地址分配,NAT技术,VLAN划分。如有问题请大佬们在本博客下留言。一、设计要求模拟设计并实现校园网规划,完成拓扑结构设计、IP地址规划、路由协议、网络管理规划、安全管理规划,用软件模拟器实现此规划。选取设备和协议根据你对校园网的了解和调研情况确定,也可以进行适当程度的假设…

    2022年10月5日
  • HttpCanary下载_HTML自我介绍

    HttpCanary下载_HTML自我介绍前言首先,我们无论学习哪个框架,都要带着问题,带着思考去学习思考1:HttpRunner是什么?思考2:HttpRunner的设计模式是什么?思考3:为什么我们要学习HttpRunner?他的

    2022年7月31日
  • Python源码保护[通俗易懂]

    Python源码保护[通俗易懂]1混淆改方法主要将函数、类名以及变量名等替换为其他符号,提高了阅读的难度,Python代码混淆网站。但该方法未改变程序的主体结构,实际效果并不是很好。具体如下图1所示:2pycpython是先把源码py文件编译成pyc或者pyo,然后由python的虚拟机执行。最简单的加密方法是将编译后的pyc二进制文件发布,详情可以参考blog。但与其他语言一样编译后的产生的pyc依然可以通过反编译得…

    2022年8月23日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号