日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

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

發布時間:2023/12/13 综合教程 33 生活家
生活随笔 收集整理的這篇文章主要介紹了 【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("
");
	}
	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语言】双人格斗小游戏(源码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲天堂福利 | 奇米色在线 | 色小姐在线视频 | 91在线| 久色88| 欧美在线视频一区二区三区 | 国内9l自拍| 色综合亚洲 | 国产精品三级视频 | 中文字幕一区二区三区四区欧美 | 91色站 | 精品成人在线观看 | 久久艹免费视频 | 黄色av一级 | 久久久一区二区三区四区 | 色xxxxx| 1024精品一区二区三区日韩 | 亚洲成人一区在线 | 国产妇女馒头高清泬20p多 | 午夜粉色视频 | 久久这里都是精品 | 国产精品粉嫩 | 久久99日韩| 国产精品探花一区二区三区 | 中文字幕日本一区 | 亚洲一区和二区 | 欧美巨乳美女 | 黄视频免费在线看 | 日本3级网站 | 日韩精品一二三区 | 五号特工组之偷天换月 | 欧美s码亚洲码精品m码 | 久色免费视频 | 色偷偷久久 | 国产中文网 | 日韩视频一区在线 | 91精品国产91久久久 | 91国自产精品中文字幕亚洲 | 成人免费网站视频 | 美女精品 | 午夜国产免费 | 成人免费网站在线 | 巨胸大乳www视频免费观看 | 亚洲操图 | 欧美成人免费一级 | 亚洲欧美日韩一区二区三区在线观看 | 免费在线看黄色片 | 色倩网站 | 亚洲天堂视频在线 | 日本一区二区视频在线 | 影音先锋丝袜制服 | 诱人的乳峰奶水hd | 国产一区激情 | 欧美激情视频二区 | 午夜视频在线观看网站 | 亚洲国产欧美一区 | 国产99久久久 | 精品人妻大屁股白浆无码 | 日本做爰全过程免费看 | 美女洗澡无遮挡 | 天天狠天天插天天透 | 色校园| 男女激情啪啪 | 久操热久操 | 天天操天天干天天爱 | 国产91丝袜| 伊人久久免费视频 | 中国黄色片子 | 精品无码一区二区三区在线 | 人人搞人人插 | 91综合视频| 午夜性色福利视频 | 五月婷婷深爱 | www.色午夜| 中日韩免费毛片 | 久久精品视频网 | 在线91av | 国产女女做受ⅹxx高潮 | 星空大象在线观看免费播放 | 久久伊人成人 | www.av视频在线观看 | 国产又粗又长又大 | 国产又爽又黄免费软件 | 一区二区伦理 | 亚洲黄色一级大片 | 国产99在线 | 国产精品久久久久久久免费大片 | 综合色影院 | 野外做受又硬又粗又大视频√ | 久久超碰在线 | 欧美成人三级在线播放 | 黄色一级网站 | 天堂8在线天堂资源bt | 免费午夜视频在线观看 | 欧美午夜视频在线观看 | 国产精品91一区 | 反差在线观看免费版全集完整版 | av色站 | 人与动物2免费观看完整版电影高清 |