[II. Airplane games]

[II. Airplane game (1)]

In this section, we will be in the previous section Bounce Away Complete a simple aircraft game based on the implementation.

scanf controls aircraft movement

We can use scanf function to detect the input, using a, s, D and W respectively To control the X and Y coordinates to realize the movement of the aircraft.

#include <stdio.h>
#include <stdlib.h>
int main(){
	int i,j;
	int x = 5;
	int y = 10;
	char input; //Variables declared here are used to detect input
	
	while(1){
		system("cls"); //Clear screen, Linux changed to clear
		//Blank line above output
		for(i=0; i<x; i++)
			printf("\n");
		//Output left space
		for(j=0; j<y; j++)
			printf(" ");
		printf("*"); //Aircraft are represented by *
		printf("\n");
	
		scanf("%c", &input); //Detection input
		if(input == 'a')
			y --;
		if(input == 'd')
			y ++;
		if(input == 's')
			x ++;
		if(input == 'w')
			x --;
	}
	return 0;
}

explain:
In the scanf function,% c indicates that the input value type is character (char), & input indicates that the input value is assigned to input.

getch controls the movement of the aircraft

Previously, we used scanf function to control movement. It requires pressing enter after each input to continue. The interaction effect is not good.

In order to optimize the effect, we use the getch function to get the input characters without carriage return. First include < conio h>.

be careful
General Unix/Linnux systems do not have < conio h> The header file needs to be downloaded from the Internet.

In addition, the kbhit function is also required to listen to user input. If the user inputs, this function returns 1, otherwise it returns 0, so as to avoid pausing without input.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(){
	int i,j;
	int x = 5;
	int y = 10;
	char input; //Variables declared here are used to detect input
	
	while(1){
		system("cls"); //Clear screen, Linux changed to clear
		//Blank line above output
		for(i=0; i<x; i++)
			printf("\n");
		//Output left space
		for(j=0; j<y; j++)
			printf(" ");
		printf("*"); //Aircraft are represented by *
		printf("\n");
	
		if(kbhit()){ //Judge whether there is input
			input = getch();
				if(input == 'a')
					y --;
				if(input == 'd')
					y ++;
				if(input == 's')
					x ++;
				if(input == 'w')
					x --;
		}
	}
	return 0;
}

A cooler plane

Whose plane has only one "*"?
Do you think this is a shining red star?

It's too hasty to replace the plane with a simple asterisk!

So we have to find a way to make a cooler alternative to the output aircraft.

//A cooler plane
for(j=0; j<y; j++)
	printf(" ");
printf("  *  \n"); //nose of an airplane
for(j=0; j<y; j++)
	printf(" ");
printf("*****\n"); //Aircraft body
for(j=0; j<y; j++)
	printf(" ");
printf(" * * \n"); //Aircraft tail

Emitting laser

Usually, the airplane games we see can be clicked, so now we'll add the function of emitting laser to our airplane.

You can define a variable isFire to represent the laser state. The default is 0. Press the space and change it to 1. Then you can add a judgment sentence. If it is 1, the laser will be output (represented by "|"), otherwise there will be no output.

if(isFire == 0) 
{	//No laser, normal output blank line
	for(i=0; i<x; i++)
	printf("\n");
}
else
{	//There is a laser, which outputs "|" above the aircraft
	for(i=0; i<x; i++) //First, traverse the x coordinate to ensure that there is a laser in each line above
	{
		for(j=0; j<y; j++)
			printf(" "); //Output a blank line before the y coordinate
		printf("  |\n"); //Coordinate output "|" when reaching the corresponding aircraft head 
	}
	isFire = 0; //Set the value of isFire to zero after each laser output, otherwise it will be output all the time
}

Target practice

Before joining the enemy, we must first complete the target shooting, that is, we must first know how to judge whether the enemy is destroyed.

We use "+" to represent the target, and use the variable isKilled to record whether it has been hit. When its value is 0, it means it has not been hit, and the target is output; Otherwise, the target will not be output.

First, define the amount about the target:

int ny = 5; //ny represents the y coordinate of the target, which is in the first line by default
int isKilled = 0; //Hit status is 0

Then output the target in the first line of the dead cycle:

if(!isKilled){
	for(j=0; j<ny; j++)
		printf(" ");
	printf("+\n");
}

Here is the judgment condition! It refers to "non", because the default isKilled is 0, which means "no", so the condition here is "non-NO", and the double positive table is negative. Then when isKilled is 1, the condition becomes "non yes", that is, the output is not executed.

Finally, after outputting the laser, judge whether it is hit:

if(y + 2 == ny){
	isKilled = 1;
}

Here y + 2 is because the laser is output in the center of the aircraft, and there are two spaces between the center of the aircraft and the Y coordinate.

Note that this code must be inserted after the output laser and before isFire is zeroed (if isFire is zeroed first, you can hit a hammer).

Summary

Full code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(){
	int i,j;
	int x = 5;
	int y = 10;
	char input; //Variables declared here are used to detect input
	int ny = 5; //ny represents the y coordinate of the target, which is in the first line by default
	int isKilled = 0; //Hit status is 0
	int isFire = 0; //The laser status is 0
	
	while(1){
		system("cls"); //Clear screen, Linux changed to clear
		if(isFire == 0) 
		{	//No laser, normal output blank line
			for(i=0; i<x; i++)
				printf("\n");
		}
		else
		{	//There is a laser, which outputs "|" above the aircraft
			for(i=0; i<x; i++) //First, traverse the x coordinate to ensure that there is a laser in each line above
			{
				for(j=0; j<y; j++)
					printf(" "); //Output a blank line before the y coordinate
				printf("  |\n"); //Coordinate output "|" when reaching the corresponding aircraft head 
			}
			if(y + 2 == ny)
				isKilled = 1;
			isFire = 0; //Set the value of isFire to zero after each laser output, otherwise it will be output all the time
		}
		//A cooler plane
		for(j=0; j<y; j++)
			printf(" ");
		printf("  *  \n"); //nose of an airplane
		for(j=0; j<y; j++)
			printf(" ");
		printf("*****\n"); //Aircraft body
		for(j=0; j<y; j++)
			printf(" ");
		printf(" * * \n"); //Aircraft tail
		printf("\n");
	
		if(kbhit()){ //Judge whether there is input
			input = getch();
				if(input == 'a')
					y --;
				if(input == 'd')
					y ++;
				if(input == 's')
					x ++;
				if(input == 'w')
					x --;
		}
	}
	return 0;
}

Thinking questions:
1. Hit a moving target?

At present, the game is still very simple. Don't worry. There will be more and more complex games in the future.

Thanks for reading

Tags: C Game Development

Posted by pablogosse on Mon, 18 Apr 2022 06:59:45 +0300