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

摘要:
num_ a:num_ b;对于{如果{bullet_a[i][0]=-bullet_a[i][0];}elseif{bullet _a[i][1]=-bullet _a[i][1];}如果{bullet_b[i][0]=-bullet-b[i][0];}elsif{bullet_b[i][1]=-bullet_b[i][1];}画布[bullet_a[i][2]][bullet_a][3]]=0;画布[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];画布[bullet_a[i][2]][bullet_a[i][3]]=3;画布[bullet_b[i][2]][bullet_b[i][3]]=3;}对于{if//敌机与我的飞机相撞{life_a--;if{printf(“a玩家失败!”);Sleep;system;exit;}}如果//敌机撞上我的飞机{life_b--;if{printf(”b玩家失败!
【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("
");
	}
	printf("A:");
	for( i = 1; i <= 10; i++ )
		if( i <= life_a)
			printf("■");
		else printf(" ");
	printf("
B: ");
	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 玩家失败!
");
				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 玩家失败!
");
				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;
}

免责声明:文章转载自《【C语言】双人格斗小游戏(源码)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇004-核心技术-netty概述、传统IO、Reactor线程模型【转】通用权限管理设计 之 功能权限设计下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

动态规划——线性dp

我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp。在这篇文章中,我们将讨论有关线性dp的一些问题。 在有关线性dp问题中,有着几个比较经典而基础的模型,例如最长上升子序列(LIS)、最长公共子序列(LCS)、最大子序列和等,那么首先我们从这几个经典的问题出发开始对线性dp的探索。 首先我们来看最长上升子序...

美团热修复Robust-源码篇

上一篇主要分析了Robust的使用方法,这一篇就来总结一下Robust的源码分析。 我个人倾向于将Robust框架分为两个部分,自动插入代码和动态加载Patch。 一、Robust源码分析 目前我的分析将Robust动态加载分为两个部分,一部分是插桩后的代码逻辑,一部分是拉取Patch的逻辑。 我们首先来看插桩后的代码(这里面套用的是官方的代码,可能有些过...

杭电1873--看病要排队

看病要排队 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5877    Accepted Submission(s): 2417 Problem Description 看病要排队这个是地球人都...

Canvas:绘制路径

Canvas:绘制路径 绘制路径   图形的基本元素是路径。路径是[通过不同颜色和宽度的线段或曲线相连形成的不同形状的]点的集合。一个路径,甚至一个子路径,都是闭合的。   使用路径绘制图形需要一些额外的步骤。 首先,你需要创建路径起始点。 然后你使用画图命令去绘制路径。 之后把路径进行封闭。 一旦路径生成,你就能通过描边或填充路径区域来渲染图形。 函...

html5 canvas 前端生成缩略图

html5 canvas 前端生成缩略图 更新: 2013/08/01: 解决了后面遇到的bug: 图片被压扁(IOS6); 图片被旋转; 整个源码放在: https://github.com/kairyou/html5-make-thumb 新方案需要后面实现的, 就是下面的旧版本里的功能(水印/是否强制拉伸以适应目标尺寸等功能). w3ctech长沙站...

TCP内核源码分析笔记

Table of Contents 1 术语 1.1 ABC 1.2 SACK 1.3 D-SACK 1.4 F-RTO 1.5 nagle算法 1.6 cork算法 1.7 template 2 tcp_v4_connect() 3 sys_accept()3.1 tcp_accept() 4 三次握手 4.1 客户端发送SYN段 4.2...