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

【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;
}
  • 273
    点赞
  • 1511
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 59
    评论
很高兴回答您关于C语言双人格斗小游戏的问题!为了帮助您更好地了解该游戏的开发,我将为您提供一些基本的思路和步骤。 首先,您需要选择一个合适的开发环境,比如Code::Blocks或者Dev-C++,以便编写和运行C语言代码。 接下来,您可以考虑以下几个方面来设计和实现您的双人格斗小游戏: 1. 角色选择和初始化:让玩家选择自己的角色,并初始化双方角色的属性,如血量、攻击力等。 2. 控制和输入:使用键盘输入来控制角色的移动和攻击操作。您可以使用C语言的输入函数(如scanf)来获取玩家的输入。 3. 游戏循环:使用一个循环结构来实现游戏的进行。在每一轮循环中,检测玩家的输入并更新游戏状态,最后显示结果。 4. 攻击和防御:定义角色的攻击和防御动作,并在游戏中实现。您可以使用条件语句(如if-else)来判断角色之间的攻击和防御关系,并更新双方角色的血量。 5. 游戏结束条件:当其中一方角色的血量归零时,游戏结束。您可以使用条件语句来判断并显示获胜方。 6. 图形界面:如果您希望为游戏增加一些可视化效果,您可以考虑使用图形库(如graphics.h)来绘制角色和游戏场景。 这只是一个简单的思路,您可以根据自己的需求和实际情况进行更详细的设计和开发。希望这些信息对您有所帮助!如果您有任何进一步的问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

404name

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值