C language reconstruction function encapsulation of airplane game (advanced version)

Keywords: Windows C

code refactoring

This can provide a simplified game framework and realize code reuse

#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++)			//Show aircraft 
	{
		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;
}

Let the bullets fly

Low configuration aircraft: only one nose*

New low configuration aircraft with bullets

#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;		//Submunitions are in the same line as the aircraft head 
}

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()		//Updates unrelated to user input 
{
	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;		//Let the bullets fly 
			bullet_y = y;
		}
	}
 } 

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

High configuration aircraft:

New bullet high configuration aircraft

#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;		//Submunitions are in the same line as the aircraft head 
}

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()		//Updates unrelated to user input 
{
	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;		//Let the bullets fly 
			bullet_y = y;
		}
	}
 } 

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

Stationary enemy aircraft

Let's roughly set the enemy plane to@

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

int x, y;
int bullet_x, bullet_y;
int enemy_x, enemy_y;		//Enemy aircraft position 
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;
}

Some operation results are as follows:

Enemy aircraft movement

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

int x, y;
int bullet_x, bullet_y;
int enemy_x, enemy_y;		//Enemy aircraft position 
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;
}

Kill the enemy aircraft

Kill enemy C language game

#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;      //Game screen size 
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("score:%d\n", score);
}

void updateWithoutInput()		//Updates unrelated to user input 
{
	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;	//Control the falling speed of enemy aircraft 
	if(speed < 10)
		speed++;
	if(speed == 10)
	{
		enemy_x++;
		speed = 0;
	} 
}

void updateWithInput()		//Updates related to user input 
{
	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;
}

Optimized version (the plane game screen no longer flashes seriously):

Optimized game of killing enemy aircraft

#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;      //Game screen size 
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("score:%d", score);
	for(i = 0; i < width - 7; i++)
		printf(" ");
	printf("#\n");
	for(i = 0; i < width; i++)
		printf("#");
}

void updateWithoutInput()		//Updates unrelated to user input 
{
	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;	//Control the falling speed of enemy aircraft 
	if(speed < 10)
		speed++;
	if(speed == 10)
	{
		enemy_x++;
		speed = 0;
	} 
}

void updateWithInput()		//Updates related to user input 
{
	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;
}

If you like my article, please remember one key and three links Oh, click like to pay attention to collection, each of your likes, each attention to each collection will be the infinite power on my way!!! A kind of (▔▽▔) A kind of Thank you for your support, we will see you tomorrow!!!

Posted by ali_p on Wed, 06 May 2020 07:57:43 -0700