Upper Code:
#include<stdio.h> #include<stdlib.h> #include<conio.h> int main() { int i, j; int x = 0, y = 5,xy=5; char in; int is = 0,isk=0; while(1) { system("cls"); if (isk == 0){ for (j = 0; j < xy; j++) { printf(" "); } printf(" +\n"); } if (is == 0) { for (i = 0; i < x; i++) { printf("\n"); } } else { for (i = 0; i < x; i++) { for (j = 0; j < y; j++) printf(" "); printf(" |\n"); if (y == xy) isk = 1; } is = 0; } for (j = 0; j < y; j++) { printf(" "); } printf(" O\n"); for (j = 0; j < y; j++) { printf(" "); } printf("00000\n"); for (j = 0; j < y; j++) { printf(" "); } printf(" 0 0\n"); in=_getch(); if (in == 'a') y--; if (in == 'd') y++; if (in == 'w') x--; if (in == 's') x++; if (in == ' ') is = 1; } return 0; }
Explanation:
for (i = 0; i < x; i++) { for (j = 0; j < y; j++) printf(" "); printf(" |\n"); if (y == xy) isk = 1; } is = 0; } for (j = 0; j < y; j++) { printf(" "); } printf(" O\n"); for (j = 0; j < y; j++) { printf(" "); } printf("00000\n"); for (j = 0; j < y; j++) { printf(" "); } printf(" 0 0\n"); }
The general idea is to set a canvas with coordinates x and y. The creation of canvas is realized through two-layer for loop. When the canvas is created, I create a simple aircraft composed of "O" in the upper left corner. Such an initialization is completed.
in=_getch(); if (in == 'a') y--; if (in == 'd') y++; if (in == 'w') x--; if (in == 's') x++; if (in == ' ') is = 1;
So how does the plane move? This is done by pressing the key. I use four if statements to complete the movement of the aircraft. Here, x represents the vertical axis and y represents the horizontal axis. When "a" is pressed, y --, This means that when I press a, the whole canvas will shrink to the left (the canvas is realized through spaces), but the position of the aircraft remains unchanged according to the proportion of the canvas. If the spaces are reduced, the aircraft will move to the left accordingly. Then the principle of w, s and d is the same as the above method. A paragraph "in=_getch()" appears in the above code, This line of code is equivalent to replacing the scanf statement if it is not used_ If you use scanf instead of getch (), you will find that you need to press enter once every time you press the move key, and the letter corresponding to the pressed key position will appear. However, getch () does not need to press enter and no letter will appear, _getch () needs to use "#include < conio. H > This header file. Although the movement is indeed completed, it will be found that after each movement, an additional aircraft will appear on the screen. This is because there is no "screen clearing". To complete the "screen clearing" operation, you need to use "system("cls ") This string of codes is used to clear the screen once every operation. However, if you want the program to run normally, you need to use the header file "#include < conio. H >".
if (is == 0) { for (i = 0; i < x; i++) { printf("\n"); } } else { for (i = 0; i < x; i++) { for (j = 0; j < y; j++) printf(" "); printf(" |\n");
The movement of the aircraft has been completed, so how to complete the shooting of the aircraft? This is also done through loops and judgment statements. Through the loop, the "|" is output at the top of the aircraft to simulate the aircraft firing bullets. But how do the bullets of the plane disappear? It needs to be realized through judgment statements. We set a variable "is" and give it an initial value of 0. When "is" is zero, it will not be output, but when we press the space (that is, "), the value of" is "will become 1 and" | "will be output. But how will" | "disappear after the plane hits the target? Then look back.
if (isk == 0){ for (j = 0; j < xy; j++) { printf(" "); } printf(" +\n"); } if (is == 0) { for (i = 0; i < x; i++) { printf("\n"); } } else { for (i = 0; i < x; i++) { for (j = 0; j < y; j++) printf(" "); printf(" |\n"); if (y == xy) isk = 1; } is = 0; }
To make the "|" disappear, you first need to judge whether you hit the target, so how to set the target? Yes, I still use loops and judgment statements. We still set a variable "isk" with an initial value of 0. When the value of isk is zero, the target appears. When the x-axis position of "|" we shoot is the same as that of the target (that is, if (y = = xy) isk = 1;), Then the of the isk becomes 1. When we proceed to the next operation, the target will disappear. If you want the "|" to disappear, you will continue to assign is to 1, and the bullet will disappear after the next operation.
This extremely simple aircraft battle is completed, but there is a big loophole that the target can only appear once and can not appear randomly.