打飞机游戏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;
}

2020.7.23 完善版(考虑到敌机撞毁飞机而任务失败情况)

#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");
	}
	if(enemy_x >= x) 
	{
    
    
		printf("任务失败!!!\n");
		system("pause");
		exit(0);
	}
	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;
}

如果这个还不够可以看高阶版我是电脑玩家之打飞机游戏C语言数组法实现(高阶版)

如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持,明天我们不见不散!!!

猜你喜欢

转载自blog.csdn.net/qq_44631615/article/details/105902821