[II. Airplane games]

[II. Airplane game (2)]

In this section, we will Previous section On the basis of, the aircraft game is transformed and improved.

Basic framework

From this section, in order to avoid putting all the code into the main function and making the code look bloated, we will implement the game content through the following basic framework.

//Global variable definition
int main(){
	startup(); //Game initialization
	while(1) //Game cycle
	{
		show(); //Display interface
		updateWithoutInput(); //Input independent updates
		updateWithInput(); //Input related updates
	}
	return 0;
}

code refactoring

Now we will refactor the code in the previous section through the basic framework.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
//global variable
int position_x,position_y; //Aircraft coordinates
int high,width; //Picture size

void startup() //Data initialization
{
	high = 20;
	width = 30;
	position_x = high/2;
	position_y = width/2;
}

void show() //display frame
{
	system("cls"); //Clear screen function, linux uses clear command
	int i,j;
	for(i=0; i<high; i++){
		for(j=0; j<width; j++){
			if((i==position_x)&&(j==position_y))
				printf("*"); //Output aircraft* 
			else
				printf(" "); //Output space
		}
		printf("\n");
	}
}

void updateWithoutInput() //Input independent updates
{
}

void updateWithInput() //Input related updates
{
	char input;
	if(kbhit()) //Judge whether there is input
	{
		input = getch();
		if(input=='a')
			position_y --;
		if(input=='d')
			position_y ++;
		if(input=='w')
			position_x --;
		if(input=='s')
			position_x ++;		
	}
}

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

New bullet

The laser we realized before covers the whole line, which is not friendly to the enemy (the enemy is beaten anyway). In order to ensure the game experience (no), we add a new bullet to the game.

You can set the initial bullet coordinates:

// The bullet is above the plane
bullet_x = position_x - 1;
// Bullet y coordinate unchanged
bullet_y = position_y;

// Bullets fly up 1 per cycle
bullet_x --; 

Therefore, we need to add code to show() function and update to control bullets:

// Define bullet coordinate variables
int bullet_x,bullet_y;

//Initialize bullet
bullet_x = -1;
bullet_y = position_y;

// Write in output loop
if ...
else if ((i==bullet_x)&&(j==bullet_y))
	printf("|"); //Output bullet
else ...

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

// Written in user input judgment
if(input == ' ')
{	
	// The bullet is above the plane
	bullet_x = position_x - 1;
	// Bullet y coordinate unchanged
	bullet_y = position_y;
}

The enemy plane [at last]

Use "@" to represent enemy aircraft, and use enemy_x and enemy_y represents the coordinates of the enemy aircraft. Use the same method as adding bullets to join the enemy aircraft.

Note: the enemy plane can't move like bullets, otherwise you can only see a big black mouse running past each time.

// Enemy aircraft position
int enemy_x,enemy_y;

// Data initialization
enemy_x = 0;
enemy_y = position_y;

// Add output cycle
if ...
else if ((i==enemy_x)&&(j==enemy_y))
	printf("@");
else ...

// Control the movement of enemy aircraft
static int speed = 0;
if(speed<10)
	speed ++;
if(speed == 10){
	enemy_x ++;
	speed = 0;
}

It can be seen that when realizing the movement of enemy aircraft, in order to avoid the swish of big black mice, we used the static keyword to define a static variable speed to realize the slow movement of enemy aircraft.

The local variable defined by static will not redefine its value in each cycle. The first cycle will define speed as 0 according to the statement, and the assignment statement will not be re executed in subsequent cycles.

When the value of each cycle is less than 10, add one and continue the following statements. If the value is equal to 10, add one to the x coordinate of the enemy aircraft (that is, the enemy aircraft moves), and reset the speed to zero.

Kill and score

Yes, after completing the above code, you will find:
Two blood lock hooks appear on the screen!!
No matter how hard you fight, you can't die, and one will disappear!

So we need to add the kill function and, of course, score (kill).

Moreover, after each kill, a new enemy aircraft must be generated (in fact, the enemy aircraft coordinates must be re assigned).

// Add updateWithoutInput update cycle
if((bullet_x == enemy_x)&&(bullet_y == enemy_y))
{ //The bullet hit the enemy plane
	score ++; //Score plus one
	// Enemy aircraft refresh
	enemy_x = -1;
	enemy_y = rand() % width;
	bullet_x = -2; //The bullet doesn't work. You can remove it if you want it to penetrate
}
if(enemy_x > high) // The enemy plane flew out of the border
{
	// Enemy aircraft refresh
	enemy_x = -1;
	enemy_y = rand() % width;
	// Optional, enemy aircraft out of bounds score minus one
	score --;
}

// Define score and display
int score;
// The display part is added at the end of the show() function
printf("score: %d\n", score);

Clear screen upgrade

After implementing the above code, we find that the screen flickers seriously during the game. Here is a method to optimize the screen clearing.

//Move cursor before screen clearing
#include <windows.h>

void gotoxy(int x, int y){
	HANDLE handle = GetStdHandle(STD_UOTPUT_HANDLE);
	CROOD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
//Resolve cursor flicker
void HideCursor(){
	CONSOLE_CURSOR_INFO cursor_info = {1, 0}; //A second value of 0 hides the cursor
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

Thanks for reading, pay attention to me and see more~

Tags: C Game Development

Posted by bettyatolive on Sun, 17 Apr 2022 04:34:06 +0930