wei___123 2017-10-27 08:47 采纳率: 75%
浏览 335

打飞机代码有一行无法显示,不知道错在哪

#include
#include
#include
int pos_x,pos_y; //飞机位置
int height,width; //画面长宽
int bullet_x,bullet_y;//子弹长宽
int enemy_x,enemy_y; //敌机位置
int score;
int laser;
// 函数外全局变量定义
void startup()
{
height=20;
width=30;
pos_x=height/2;
pos_y=width/2;
bullet_x=0;
bullet_y=pos_y;
enemy_x=0;
enemy_y=pos_y;
score=0;
laser=0;
}
void gotoxy(int x,int y)//类似于清屏函数,光标移动到原点位置进行重画
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
int show()
{
gotoxy(0,0);
int i,j;
int count;
for(i=0;i {
for(j=0;j {
if(laser==1&&(j==pos_y)&&(i!=0))
{
printf("|");
if(i==pos_x-1)
{
laser=0;
count=1;
}
}
else if((i==pos_x)&&(j==pos_y))
{
printf("*"); //输出飞机
}
else if((i==enemy_x)&&(j==enemy_y))
{
printf("@"); //输出敌机
}
else if((i==bullet_x)&&(j==bullet_y))
{
printf("|"); //输出子弹
}
else if(i==0||height==i+1) printf("-");
else if(j==0||width==j+1) printf("|");
else printf(" ");
}
if(i==8) printf("\t\t\t打飞机游戏:请勿连续敲打空格武器");
if(i==9) puts("\t\t\t wasd操纵飞机,空格飞弹,J激光弹");
printf("\n");
}
printf("得分:%d",score);
return count;
}
int updateWithoutInput(int count)
{
if(bullet_x>-1) bullet_x--; //控制子弹
if((enemy_x==pos_x)&&(enemy_y==pos_y)) //飞机撞到了
{
printf("\n defeat!!!!!!");
score=0;
Sleep(1000);
system("cls");
enemy_x=-1;
enemy_y=rand()%(width-1);
if(enemy_y==0) enemy_y=1;
pos_x=height/2;
pos_y=width/2;
}
if((enemy_x==bullet_x)&&(enemy_y==bullet_y)) //击落敌机生成敌机
{
score++;
bullet_x=-2;
enemy_x=-1;
enemy_y=rand()%(width-1);
if(enemy_y==0) enemy_y=1;
}
if((count==1)&&(pos_y==enemy_y)&&(enemy_x {
score++;
enemy_x=-1;
enemy_y=rand()%(width-1);
if(enemy_y==0) enemy_y=1;
}
if (enemy_x>height) // 敌机跑出显示屏幕
{
enemy_x = -1; // 产生新的飞机
enemy_y = rand()%(width-1);
if(enemy_y==0) enemy_y=1;
}
static int speed = 0; //控制敌机移动速度
if (speed speed++;
if (speed == 10)
{
enemy_x++;
speed = 0;
}
if(pos_x>(height-2)) pos_x=height-2;
else if(pos_x if(pos_y>(width-2)) pos_y=1;
else if(pos_y<1) pos_y=width-2;
}
void updateWithInput()
{
char input;
if(kbhit())
{ input=getch();
if(input=='w') pos_x--;
else if(input=='s') pos_x++;
else if(input=='a') pos_y--;
else if(input=='d') pos_y++;
else if(input==' ')
{
bullet_x=pos_x-1;
bullet_y=pos_y;
}
else if(input=='j') laser=1;
}
}
main()
{
int count;
startup(); // 数据初始化
while(1) // 游戏循环执行
{
count=show(); // 显示画面
updateWithoutInput(count); // 与用户输入无关的更新
updateWithInput(); // 与用户输入有关的更新
}
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?