为什么我的代码的飞机显示不出来,帮我修改一下#pragma once #include <stdio.h> #include <stdlib.h> #include <Windows.h> int sx = 20, sy = 8;//横坐标max和纵坐标max int px = 5, py =2 ;//飞机的初始位置 void show()//显示飞机 { system("cls"); for (int i = 0; i < sy;i++)//有多少行循环多少次,外循环 { for (int j = 0; j < sx;j++)//有多少列循环多少次,内循环 { if (i == py && j == px) { printf(" A");//输出飞机 } else if (i==py+1&&j==px)//什么样的条件画飞机翅膀 { printf("====="); } else if (i == py + 2 && j == px)//什么样的条件输出尾巴 { printf(" H H "); } else//输出空格 { printf(" "); } printf("\n");//走到最后,换行 } } } void move() { //不用scanf接收键盘输入,因为要按回车,玩游戏时不理解 char input; input = getch(); if (input == 'w') { py--; } if (input == 's') { py++; } if (input == 'a') { px--; } if (input == 'd') { px++; } } int main() { while (1) { show(); move(); show(); } return 0; }
时间: 2024-01-02 19:03:02 浏览: 25
你的代码存在几个问题:
1. 在循环中,内循环输出完一行之后,应该不需要再输出换行符 `\n`。应该在外循环结束后再输出换行符。
2. 在 `move()` 函数中,你使用了 `getch()` 函数,但是没有在代码中包含 `conio.h` 头文件,导致编译错误。所以你需要添加 `#include <conio.h>` 头文件。
3. 在 `move()` 函数中,你没有对飞机是否超出边界进行判断,导致飞机可以飞出屏幕。你需要在移动飞机前加上边界判断。
修改后的代码如下所示:
```c
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h> // 添加头文件
int sx = 20, sy = 8; // 横坐标max和纵坐标max
int px = 5, py = 2; // 飞机的初始位置
void show() // 显示飞机
{
system("cls");
for (int i = 0; i < sy; i++) // 有多少行循环多少次,外循环
{
for (int j = 0; j < sx; j++) // 有多少列循环多少次,内循环
{
if (i == py && j == px)
{
printf(" A"); // 输出飞机
}
else if (i == py + 1 && j >= px - 2 && j <= px + 2) // 修改翅膀的判断条件
{
printf("="); // 输出翅膀
}
else if (i == py + 2 && j >= px - 1 && j <= px + 1) // 修改尾巴的判断条件
{
printf(" H"); // 输出尾巴
}
else
{
printf(" "); // 输出空格
}
}
printf("\n"); // 每一行输出完后换行
}
}
void move()
{
char input;
input = getch();
if (input == 'w' && py > 0) // 上移并判断边界
{
py--;
}
if (input == 's' && py < sy - 3) // 下移并判断边界
{
py++;
}
if (input == 'a' && px > 0) // 左移并判断边界
{
px--;
}
if (input == 'd' && px < sx - 1) // 右移并判断边界
{
px++;
}
}
int main()
{
while (1)
{
show();
move();
}
return 0;
}
```
现在你应该可以看到飞机在屏幕上移动了。