The game effect

Using the array knowledge to improve the air combat game, to achieve the effect of multiple enemy planes, shooting shotguns.

Implementation steps

1. Display and control the aircraft

2. Fire bullets

3. Hit the enemy aircraft

4. Multiple enemy planes

5. Fire shotguns

Build a simplified game framework

Int main() {startup(); // initialize while (1) {show(); // Display screen updateWithoutInput(); // Update updateWithInput() regardless of user input; } return 0; }Copy the code

Step 1: Display and control the aircraft

The first step is to realize the display and control of the aircraft.

– Store game data in 2d array Canvas [High][Width], output aircraft ‘*’ when element in 2d array Canvas [High][Width] is 1

Fire the bullet

– in the two-dimensional array canvas element in the [High] [Width] 2 output when the bullets’ | ‘

Hit enemy aircraft

Add falling enemy planes to canvas[High][Width] with element 3

Added the function of bullets hitting enemy planes, enemy planes colliding with us, enemy planes falling down.

Multiple enemy planes

Enemy_x [EnemyNum], enemy_y [EnemyNum] stores the positions of multiple enemy planes at the same time.

Fire the shotgun

Realize the firing of BulletWidth. (Can be analogous to the rebound ball to increase the length of the baffle)

When the score increases, the radius of the projectile increases and the movement speed of the enemy aircraft changes.

All the code

H > #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #define High 25 // Game screen size #define Width 50 Int canvas[High][Width] = {0}; #define EnemyNum 5 EnemyNum 5 int canvas[High][Width] = {0}; // A two-dimensional array stores the corresponding elements in the game screen // 0 represents space; 1 indicates aircraft; 2 is bullet; Int POSItion_x, position_y; Int enemy_x[EnemyNum], enemy_y[EnemyNum]; // Position of enemy aircraft int score; Int BulletWidth; // Bullet width int EnemyMoveSpeed; 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 startup() {position_x = High / 2; position_y = Width / 2; canvas[position_x][position_y] = 1; score = 0; BulletWidth = 0; EnemyMoveSpeed = 10; int k; for (k = 0; k < EnemyNum; k++) { enemy_x[k] = rand() % 5; enemy_y[k] = rand() % Width; canvas[enemy_x[k]][enemy_y[k]] = 3; }} void show() {gotoxy(0, 0); // Move cursor to the origin, clear screen int I, j; for (i = 0; i < High; i++) { for (j = 0; j < Width; j++) { if (canvas[i][j] == 0) printf(" "); Else if (canvas[I][j] == 1) printf("*"); / / output plane * else if (canvas [I] [j] = = 2) printf (" | "); / / output bullet | else if (canvas [I] [j] = = 3) printf (" @ "); @} printf("\n"); } printf(" score: % dn ", score); HideCursor(); // hide cursor Sleep(20); } void updateWithoutInput() {int I, j, k; for (i = 0; i < High; i++) { for (j = 0; j < Width; j++) { if (canvas[i][j] == 2) { for (k = 0; k < EnemyNum; K++) {if (I = = enemy_x [k] && j = = enemy_y [k]) / / the bullet hit the enemy {canvas [enemy_x [k]] [enemy_y [k]] = 0; enemy_x[k] = 0; // Create new enemy enemy_y[k] = rand() % Width; canvas[enemy_x[k]][enemy_y[k]] = 3; canvas[i][j] = 0; // bullets disappear score++; // Score + 1 EnemyMoveSpeed if (score % 5 == 0 && EnemyMoveSpeed < 20) If (score % 10 == 0) // Release a bullet with a certain score; }} // Canvas [I][j] = 0; if (i >= 0) canvas[i - 1][j] = 2; } } } for (k = 0; k < EnemyNum; K ++) {if (position_x == enemy_x[k] && position_y == enemy_y[k]) {printf(" Game failed! \n"); Sleep(3000); system("pause"); exit(0); } } for (k = 0; k < EnemyNum; K++) {if (enemy_x [k] > High) / / by enemy planes flying in and out lower {canvas [enemy_x [k]] [enemy_y [k]] = 0; enemy_x[k] = 0; // Create new enemy enemy_y[k] = rand() % Width; canvas[enemy_x[k]][enemy_y[k]] = 3; score--; }} static int speed = 0; if (speed < EnemyMoveSpeed) speed++; Else {// enemy aircraft drop for (k = 0; k < EnemyNum; k++) { canvas[enemy_x[k]][enemy_y[k]] = 0; enemy_x[k]++; canvas[enemy_x[k]][enemy_y[k]] = 3; } speed = 0; }} void updateWithInput() {char input; if (_kbhit()) { input = _getch(); if (input == 'a') { canvas[position_x][position_y] = 0; position_y--; canvas[position_x][position_y] = 1; } if (input == 'd') { canvas[position_x][position_y] = 0; position_y++; canvas[position_x][position_y] = 1; } if (input == 'w') { canvas[position_x][position_y] = 0; position_x--; canvas[position_x][position_y] = 1; } if (input == 's') { canvas[position_x][position_y] = 0; position_x++; canvas[position_x][position_y] = 1; } if (input == ") {int left = position_y-bulletwidth; int right = position_y + BulletWidth; if (left < 0) left = 0; if (right > Width - 1) right = Width - 1; int k; for (k = left; k <= right; k++) canvas[position_x - 1][k] = 2; } } } int main() { startup(); // initialize while (1) {show(); // Display screen updateWithoutInput(); // Update updateWithInput() regardless of user input; } return 0; }Copy the code

Compiler: Visual Studio 2019