1.2.1
Use scanf to input different strings, press W, a, s and D to change the values of coordinates x and y, so as to control the movement of aircraft * characters up, down, left and right.
This VS may have a scanf C4996 error and a C6031 warning, which indicates that the modifier or attribute specified in the declaration is not recommended. We add code #pragma warning(disable:4996) at the top to disable it.
The switch function can also be used to determine whether the input character is a raw character.
#include <stdio.h> #include <cstdlib> #pragma warning(disable:4996) using namespace std; int main(){ int i, j; int x = 5; int y = 10; char input; while (true) { system("cls"); //Output the empty line above the aircraft for (i = 0; i < x; i++) printf("\n"); //Output the space on the left of the aircraft for (j = 0; j < y; j++) printf(" "); printf("*"); printf("\n"); scanf("%c", &input); if (input == 'a') y--; if (input == 'd') y++; if (input == 'w') x--; if (input == 's') x++; } return 0; }
1.2.2 getch controls the movement of the aircraft
The scanf function needs to press a space every time it inputs a character, which is not good. Therefore, we can consider using a new input function getch function (in the header file #include < conio. H >), so that we can get the input control characters without entering. In addition, the kbhit function returns 1 when the user has keyboard input, otherwise it returns 0; when there is no keyboard input, the statements below if(kbhit()) will not run, So as to avoid the situation that the user pauses without entering the game.
#include <stdio.h> #include <stdlib.h> #include <conio.h> #pragma warning(disable:4996) using namespace std; int main(){ int i, j; int x = 5; int y = 10; char input; while (true) { system("cls"); //Output the empty line above the aircraft for (i = 0; i < x; i++) printf("\n"); //Output the space on the left of the aircraft for (j = 0; j < y; j++) printf(" "); printf("*"); printf("\n"); if (kbhit()) { input = getch(); if (input == 'a') y--; if (input == 'd') y++; if (input == 'w') x--; if (input == 's') x++; } } return 0; }
1.2. 3 display complex aircraft patterns
If the aircraft is only represented by a "*", it seems a little crude. We can display a more complex aircraft pattern.
#include <stdio.h> #include <stdlib.h> #include <conio.h> #pragma warning(disable:4996) using namespace std; int main(){ int i, j; int x = 5; int y = 10; char input; while (true) { system("cls"); //Output the empty line above the aircraft for (i = 0; i < x; i++) printf("\n"); //Output a complex aircraft pattern for (j = 0; j < y; j++) printf(" "); printf(" *\n"); for (j = 0; j < y; j++) printf(" "); printf("*****\n"); for (j = 0; j < y; j++) printf(" "); printf(" * * \n"); if (kbhit()) { input = getch(); if (input == 'a') y--; if (input == 'd') y++; if (input == 'w') x--; if (input == 's') x++; } } return 0; }
1.2. 4 emitting laser
We have a plane. How can we be without weapons? Next, we will create a weapon for the aircraft.
Our purpose is that when we press the space, the aircraft will fire laser bullets, that is, a column of vertical lines' | 'will be displayed above the aircraft.
We can define a variable isFire to record whether the aircraft is firing bullets. When isFire is equal to 1, the laser vertical line will be output directly above the aircraft.
#include <stdio.h> #include <stdlib.h> #include <conio.h> #pragma warning(disable:4996) using namespace std; int main(){ int i, j; int x = 5; int y = 10; char input; int isFire = 0; while (true) { system("cls"); if (isFire == 0) { for (j = 0; j < x; j++) printf("\n"); } else { for (i = 0; i < x; i++) { for (j = 0; j < y; j++) printf(" "); printf(" |\n"); } isFire = 0; } //Output a complex aircraft pattern for (j = 0; j < y; j++) printf(" "); printf(" *\n"); for (j = 0; j < y; j++) printf(" "); printf("*****\n"); for (j = 0; j < y; j++) printf(" "); printf(" * * \n"); if (kbhit()) { input = getch(); if (input == 'a') y--; if (input == 'd') y++; if (input == 'w') x--; if (input == 's') x++; if (input == ' ') isFire = 1; } } return 0; }
Target practice
Our plane has a weapon, and then we need an enemy.
We're going to add a target '+' in the first line. We can control the plane to shoot a laser at it. We can define a variable to save one of our achievements.
#include <stdio.h> #include <stdlib.h> #include <conio.h> #pragma warning(disable:4996) using namespace std; int main(){ int i, j; int x = 5; int y = 10; char input; int isFire = 0; int ny = 5; int isKilled = 0; while (true) { system("cls"); if (!isKilled) { for (j = 0; j < ny; j++) printf(" "); printf("+\n"); } if (isFire == 0) { for (j = 0; j < x; j++) printf("\n"); } else { for (i = 0; i < x; i++) { for (j = 0; j < y; j++) printf(" "); printf(" |\n"); } if (y + 2 == ny) isKilled = 1; isFire = 0; } //Output a complex aircraft pattern for (j = 0; j < y; j++) printf(" "); printf(" *\n"); for (j = 0; j < y; j++) printf(" "); printf("*****\n"); for (j = 0; j < y; j++) printf(" "); printf(" * * \n"); if (kbhit()) { input = getch(); if (input == 'a') y--; if (input == 'd') y++; if (input == 'w') x--; if (input == 's') x++; if (input == ' ') isFire = 1; } } return 0; }
1.2. 6 free play
For a better game experience, we can implement some other functions.
(1) Let the target move
(2) Statistics and display of scores and other data.
(3) Partition the program.