打飛機遊戲C語言重構函數封裝(進階版)

代碼重構

這樣可以提供一個簡化的遊戲框架,實現代碼複用

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int x, y;
int high, width;

void startup()
{
	high = 20;
	width = 30;
	x = high / 2;
	y = width / 2;
}

void show()
{
	system("cls");
	int i, j;
	for(i = 0; i < high; i++)			//顯示飛機 
	{
		for(j = 0; j < width; j++)
		{
			if((i == x) && (j == y))
				printf("*");
			else
				printf(" ");
		}
		printf("\n");
	}
}

void updateWithoutInput()
{
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == 'a')
			y--;
		if(input == 'd')
			y++;
		if(input == 'w')
			x--;
		if(input == 's')
			x++;
	}
}

int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}

讓子彈飛

低配版飛機:只有一個機頭*

新式子彈低配飛機

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int x, y;
int bullet_x, bullet_y;
int high, width;

void startup()
{
	high = 20;
	width = 30;
	x = high / 2;
	y = width / 2;
	bullet_x = 0;
	bullet_y = y;		//子彈藥與飛機頭同列 
}

void show()
{
	system("cls");
	int i, j;
	for(i = 0; i < high; i++)
	{
		for(j = 0; j < width; j++)
		{
			if((i == x) && (j == y))
				printf("*");
			else if((i == bullet_x) && (j == bullet_y))
				printf("|");
			else
				printf(" ");
		}
		printf("\n");
	}
}

void updateWithoutInput()		//與用戶輸入無關的更新 
{
	if(bullet_x > -1)
		bullet_x--;
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == 'a')
			y--;
		if(input == 'd')
			y++;
		if(input == 'w')
			x--;
		if(input == 's')
			x++;
		if(input == ' ')
		{
			bullet_x = x - 1;		//讓子彈飛 
			bullet_y = y;
		}
	}
 } 

int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
} 

高配版飛機:

新式子彈高配飛機

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int x, y;
int bullet_x, bullet_y;
int high, width;

void startup()
{
	high = 20;
	width = 30;
	x = high / 2;
	y = width / 2;
	bullet_x = 0;
	bullet_y = y;		//子彈藥與飛機頭同列 
}

void show1()
{
	int i, j;
	for(i = 0; i < x; i++)
		printf("\n");
	for(j = 0; j < y; j++)
		printf(" ");
	printf("*\n");
	for(j = 0; j < y - 2; j++)
		printf(" ");
	printf(" * *\n");
	for(j = 0; j < y - 2; j++)
		printf(" ");
	printf("* * *\n");
	for(j = 0; j < y - 6; j++)
		printf(" ");
	printf("* * * * * * *\n");
	for(j = 0; j < y - 8; j++)
		printf(" ");
	printf("* * * * * * * * *\n");		
	for(j = 0; j < y- 1; j++)
		printf(" ");
	printf("* * \n");
	for(j = 0; j < y - 9; j++)
		printf(" ");
}

void show()
{
	int i, j;
	system("cls");
	
	for(i = 0; i < x; i++)
	{
		for(j = 0; j < width; j++)
		{
			if((i == bullet_x) && (j == bullet_y))
				printf("|");
			else
				printf(" ");
		}
		printf("\n");
	}
	for(j = 0; j < y; j++)
		printf(" ");
	printf("*\n");
	for(j = 0; j < y - 2; j++)
		printf(" ");
	printf(" * *\n");
	for(j = 0; j < y - 2; j++)
		printf(" ");
	printf("* * *\n");
	for(j = 0; j < y - 6; j++)
		printf(" ");
	printf("* * * * * * *\n");
	for(j = 0; j < y - 8; j++)
		printf(" ");
	printf("* * * * * * * * *\n");		
	for(j = 0; j < y- 1; j++)
		printf(" ");
	printf("* * \n");
	for(j = 0; j < y - 9; j++)
		printf(" ");
}

void updateWithoutInput()		//與用戶輸入無關的更新 
{
	if(bullet_x > -1)
		bullet_x--;
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == 'a')
			y--;
		if(input == 'd')
			y++;
		if(input == 'w')
			x--;
		if(input == 's')
			x++;
		if(input == ' ')
		{
			bullet_x = x - 1;		//讓子彈飛 
			bullet_y = y;
		}
	}
 } 

int main()
{
	startup();
	show1();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
} 

靜止敵機

這裏先把敵機粗略的設置爲@

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int x, y;
int bullet_x, bullet_y;
int enemy_x, enemy_y;		//敵機位置 
int high, width;

void startup()
{
	high = 20;
	width = 30;
	x = high / 2;
	y = width / 2;
	bullet_x = -1;
	bullet_y = y;
	enemy_x = 0;
	enemy_y = y;
}

void show()
{
	system("cls");
	int i, j;
	for(i = 0; i < high; i++)
	{
		for(j = 0; j < width; j++)
		{
			if((i == x) && (j == y))
				printf("*");
			else if((i == enemy_x) && (j == enemy_y))
				printf("@");
			else if((i == bullet_x) && (j == bullet_y))
				printf("|");
			else 
				printf(" ");
		}
		printf("\n");
	}
}

void updateWithoutInput()
{
	if(bullet_x > -1)
		bullet_x--;
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == 'a')
			y--;
		if(input == 'd')
			y++;
		if(input == 'w')
			x--;
		if(input == 's')
			x++;
		if(input == ' ')
		{
			bullet_x = x - 1;
			bullet_y = y;
		}
	}
}

int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}

部分運行結果如下:
在這裏插入圖片描述

敵機移動

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int x, y;
int bullet_x, bullet_y;
int enemy_x, enemy_y;		//敵機位置 
int high, width;

void startup()
{
	high = 20;
	width = 30;
	x = high / 2;
	y = width / 2;
	bullet_x = -1;
	bullet_y = y;
	enemy_x = 0;
	enemy_y = y;
}

void show()
{
	system("cls");
	int i, j;
	for(i = 0; i < high; i++)
	{
		for(j = 0; j < width; j++)
		{
			if((i == x) && (j == y))
				printf("*");
			else if((i == enemy_x) && (j == enemy_y))
				printf("@");
			else if((i == bullet_x) && (j == bullet_y))
				printf("|");
			else 
				printf(" ");
		}
		printf("\n");
	}
}

void updateWithoutInput()
{
	if(bullet_x > -1)
		bullet_x--;
	static int speed = 0;
	if(speed < 10)
		speed++;
	if(speed == 10)
	{
		enemy_x++;
		speed = 0;
	 } 
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == 'a')
			y--;
		if(input == 'd')
			y++;
		if(input == 'w')
			x--;
		if(input == 's')
			x++;
		if(input == ' ')
		{
			bullet_x = x - 1;
			bullet_y = y;
		}
	}
}

int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}

擊殺敵機

擊殺敵人C語言遊戲

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int x, y;
int bullet_x, bullet_y;
int enemy_x, enemy_y;
int high, width;      //遊戲畫面尺寸 
int score;

void startup()
{
	high = 20;
	width = 30;
	x = high / 2;
	y = width / 2;
	bullet_x = -2;
	bullet_y = y;
	enemy_x = 0;
	enemy_y = y;
	score = 0;
}

void show()
{
	system("cls");
	int i, j;
	for(i = 0; i < high; i++)
	{
		for(j = 0; j < width; j++)
		{
			if((i == x) && (j == y))
				printf("*");
			else if((i == x + 1) && (j == y - 2))
				printf(" * *"); 
			else if((i == x + 2) && (j == y - 2))
				printf("* * *");
			else if((i == x + 3) && (j == y - 6))
				printf("* * * * * * *");
			else if((i == x + 4) && (j == y - 8))
				printf("* * * * * * * * *");
			else if((i == x + 5) && (j == y - 1))
				printf("* * ");
			else if((i == enemy_x) && (j == enemy_y))
				printf("@");
			else if((i == bullet_x) && (j == bullet_y))
				printf("|");
			else
				printf(" ");
		}
		printf("\n");
	}
	printf("得分:%d\n", score);
}

void updateWithoutInput()		//與用戶輸入無關的更新 
{
	if(bullet_x > -1)
		bullet_x--;
	if((bullet_x == enemy_x) && (bullet_y == enemy_y))
	{
		score++;
		enemy_x = -1;
		enemy_y = rand() * 100 % width;
		bullet_x = -2;
	}
	if(enemy_x > high)
	{
		enemy_x = -1;
		enemy_y = rand() * 100 % width;
	}
	
	static int speed = 0;	//控制敵機下落速度 
	if(speed < 10)
		speed++;
	if(speed == 10)
	{
		enemy_x++;
		speed = 0;
	} 
}

void updateWithInput()		//與用戶輸入有關的更新 
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == 'a')
			y--;
		if(input == 'd')
			y++;
		if(input == 'w')
			x--;
		if(input == 's')
			x++;
		if(input == ' ')
		{
			bullet_x = x - 1;
			bullet_y = y;
		}
	}
}


int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}

優化版(飛機遊戲畫面不再嚴重閃爍):

優化改良後的擊殺敵機遊戲

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>

int x, y;
int bullet_x, bullet_y;
int enemy_x, enemy_y;
int high, width;      //遊戲畫面尺寸 
int score;

void startup()
{
	high = 20;
	width = 30;
	x = high / 2;
	y = width / 2;
	bullet_x = -2;
	bullet_y = y;
	enemy_x = 0;
	enemy_y = y;
	score = 0;
}

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
 } 

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}


void show()
{
	gotoxy(0, 0);
	HideCursor();
	int i, j;
	
	for(i = 0; i < high; i++)
	{
		for(j = 0; j < width; j++)
		{
			if((i == x) && (j == y))
			{
				printf("*");
			}	
			else if((i == x + 1) && (j == y - 2))
			{
				printf(" * *");
				j += 3;
			}
			else if((i == x + 2) && (j == y - 2))
			{
				printf("* * *");
				j += 4;
			}
			else if((i == x + 3) && (j == y - 6))
			{
				printf("* * * * * * *");
				j += 12;
			}
			else if((i == x + 4) && (j == y - 8))
			{
				printf("* * * * * * * * *");
				j += 16;
			}
			else if((i == x + 5) && (j == y - 1))
			{
				printf("* * ");
				j += 3;
			}
			else if((i == enemy_x) && (j == enemy_y))
				printf("@");
			else if((i == bullet_x) && (j == bullet_y))
				printf("|");
			else
				printf(" ");
		}
		printf("#\n");
	}
	printf("得分:%d", score);
	for(i = 0; i < width - 7; i++)
		printf(" ");
	printf("#\n");
	for(i = 0; i < width; i++)
		printf("#");
}

void updateWithoutInput()		//與用戶輸入無關的更新 
{
	if(bullet_x > -1)
		bullet_x--;
	if((bullet_x == enemy_x) && (bullet_y == enemy_y))
	{
		score++;
		enemy_x = -1;
		enemy_y = rand() * 100 % width;
		bullet_x = -2;
	}
	if(enemy_x > high)
	{
		enemy_x = -1;
		enemy_y = rand() * 100 % width;
	}
	
	static int speed = 0;	//控制敵機下落速度 
	if(speed < 10)
		speed++;
	if(speed == 10)
	{
		enemy_x++;
		speed = 0;
	} 
}

void updateWithInput()		//與用戶輸入有關的更新 
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == 'a')
			y--;
		if(input == 'd')
			y++;
		if(input == 'w')
			x--;
		if(input == 's')
			x++;
		if(input == ' ')
		{
			bullet_x = x - 1;
			bullet_y = y;
		}
	}
}


int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}

如果喜歡我的文章,請記得一鍵三連哦,點贊關注收藏,你的每一個贊每一份關注每一次收藏都將是我前進路上的無限動力 !!!↖(▔▽▔)↗感謝支持,明天我們不見不散!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章