诗岑 2020-09-26 15:14 采纳率: 93%
浏览 331
已采纳

【C语言】飞机大战怎样控制速度

如题,光标下落过快,修改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();
    }
}
  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-09-26 15:55
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 9月27日

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集