如题,光标下落过快,修改speed值后仍是这样
#include<stdio.h>
#include<Windows.h>
#include<conio.h>
int height, width;
int plane_x, plane_y;//飞机位置
int bullet_x, bullet_y;//子弹位置
int score;
int flag;
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 start() {
height = 18, width = 26;
plane_x = height - 3;
plane_y = width - 2;
bullet_x = 0;
bullet_y = plane_y;
score = 0;
flag = 0;
}
void show() {
int i, j;
for (i = 0;i < height;i++) {
for (j = 0;j < width;j++) {
if (flag) {
break;
}
else if (i == plane_x && j == plane_y) {
printf("*");
}
else if (i == bullet_x && j == bullet_y) {
printf("&");
}
else {
printf(" ");
}
}
printf("\n");
}
printf("\n");
if (plane_x == bullet_x && plane_y == bullet_y) {
flag = 1;
printf("GAME OVER");
}
else {
printf("YOU WIN!");
}
}
void bulletMove() {
//控制速度
static int speed;
if (speed < 3) {
speed++;
}
if (speed == 3) {
if (bullet_x < height) {
bullet_x++;
}
else {
bullet_x = 0;
bullet_y = 2 + rand() % width - 2;
}
}
}speed = 0;
void planeMove() {
char input;
if (_kbhit()) {
input = _getch();
if (input == 'w' && plane_x >= 1) {
plane_x--;
}
if (input == 's' && plane_x< height - 2) {
plane_x++;
}
if (input == 'a' && plane_y > 1) {
plane_y--;
}
if (input == 'd' && plane_y < width - 2) {
plane_y++;
}
}
}
int main() {
system("color 2f");
start();
while (1) {
gotoxy(0, 0);
show();
bulletMove();
planeMove();
}
}