| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1236 人关注过本帖, 1 人收藏
标题:飞机游戏------数据输出和我赋的值不一样,尝试了好多次也没用(飞机撞击敌机 ...
只看楼主 加入收藏
云不喜
Rank: 2
等 级:论坛游民
威 望:1
帖 子:11
专家分:16
注 册:2019-8-25
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:2 
飞机游戏------数据输出和我赋的值不一样,尝试了好多次也没用(飞机撞击敌机时score清零,然后1分1分加,但是这个程序运行时却是变成了0X(X为清零前个位
# include <stdio.h>
# include <conio.h>
# include <windows.h>

//全局变量
int i,j;//循环变量
int height,width;//画面尺寸变量
int position_x,position_y;//飞机坐标变量
int bullet_x,bullet_y;//子弹坐标变量
int enemy_x,enemy_y;//敌机坐标变量
int score;//分数

//函数声明
void initial();//初始化函数
int value(int *);//把score清零
void present();//显示函数
void UpdateWithoutInput();//不需要用户输入的更新函数
void UpdateWithInput();//需要用户输入的更新函数
void gotoxy(int x,int y);//光标移动到(x,y)位置
void HideCursor();//隐藏光标

//主函数
int main(void)
{
    initial();
   
    while(1)
    {
        present();
        UpdateWithoutInput();
        UpdateWithInput();
    }
   
    return 0;
}


void initial()        //初始化函数
{
    printf("请输入尺寸height width: ");
    scanf("%d %d",&height,&width);//确定画面尺寸
   
    position_x = width/2;
    position_y = height/2;//确定飞机位置
   
    bullet_x = position_x;
    bullet_y = - 1;//确定子弹位置
   
    enemy_x = position_x;
    enemy_y = 0;//确定敌机位置
   
    score = 0;//初始得分为0
   
    HideCursor();  //隐藏光标
}

void present()     //显示函数
{
    gotoxy(0,0);   //光标移动到原点位置,以下重画清屏
   
    for(j=0; j<height; ++j)
    {
        for(i=0; i<width; ++i)
        {
            if(i == position_x && j == position_y)
                printf("*");//输出飞机
            else if(i == bullet_x && j == bullet_y)
                printf("|");//输出子弹
            else if(i == enemy_x && j == enemy_y)
                printf("$");//输出敌机
            else
                printf("  ");
        }
        printf("\n");
    }

    printf("\n\n");
    printf("得分:%d",score);
}

void UpdateWithoutInput()  //不需要用户输入的更新函数
{
    static int speed = 0;//控制敌机下落速度
   
    if(bullet_y > -1)
        bullet_y--;//子弹向上飞
   
    if(bullet_x == enemy_x && bullet_y == enemy_y)//子弹击中敌机
    {
        score++;//得分+1
        enemy_y = 0;
        enemy_x = rand()%width;
        bullet_y = -1;
    }
   
    if(speed < 10)
        speed++;
    if(speed == 10)
    {
        if(enemy_y > -1 && enemy_y <= height)
            enemy_y++;//敌机下落
        else
        {
            enemy_y = 0;
            enemy_x = rand()%width;//敌机从随机一列第一行落下
        }
        speed = 0;//重新加速,降低难度
    }

    if(position_x == enemy_x && position_y == enemy_y)//敌机撞到飞机
    {
        score = value(&score);
        enemy_y = 0;
        enemy_x = rand()%width;
        bullet_y = -1;
        position_x = width/2;
        position_y = height/2;
    }
}

void UpdateWithInput()  //需要用户输入的更新函数
{
    char input;//移动变量
   
    if(kbhit())//一旦按下键盘
    {
        input = getch();//输入指令
        
        if(input == 'w')
            position_y--;
        if(input == 's')
            position_y++;
        if(input == 'a')
            position_x--;
        if(input == 'd')
            position_x++;
        if(input == ' ')
        {
            bullet_y = position_y - 1;
            bullet_x = position_x;
        }
    }
   
}

int value(int * score)
{
    *score = 0;
    return *score;
}

void gotoxy(int x,int y)   //光标移动到(x,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};  //第二个值为0表示隐藏光标
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}

[此贴子已经被作者于2019-10-2 09:51编辑过]

搜索更多相关主题的帖子: 函数 score void int 飞机 
2019-10-02 09:50
云不喜
Rank: 2
等 级:论坛游民
威 望:1
帖 子:11
专家分:16
注 册:2019-8-25
收藏
得分:0 
回复 楼主 云不喜
运行起来没有error和warning,个人觉得问题在于UpdateWithoutInput函数中的
    if(position_x == enemy_x && position_y == enemy_y)//敌机撞到飞机
    {
        score = value(&score);
        enemy_y = 0;
        enemy_x = rand()%width;
        bullet_y = -1;
        position_x = width/2;
        position_y = height/2;
    }
这一段代码中,但是找不到问题,直接让score = 0;也尝试过了,不行才用了指针
int value(int * score)
{
    *score = 0;
    return *score;
}
求大佬帮助!


收获越多,动力越大!
2019-10-02 09:58
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:20 
程序代码:
void present()     //显示函数
{
    gotoxy(0,0);   //光标移动到原点位置,以下重画清屏
    
    for(j=0; j<height; ++j)
    {
        for(i=0; i<width; ++i)
        {
            if(i == position_x && j == position_y)
                printf("*");//输出飞机
            else if(i == bullet_x && j == bullet_y)
                printf("|");//输出子弹
            else if(i == enemy_x && j == enemy_y)
                printf("$");//输出敌机
            else
                printf("  ");
        }
        printf("\n");
    }
    printf("       ");
    gotoxy(0,height);
    printf("得分:%d",score);
}
//.....
 if(position_x == enemy_x && position_y == enemy_y)//敌机撞到飞机
    {
        score = 0;

应该是历史数据没刷新吧

剑栈风樯各苦辛,别时冰雪到时春
2019-10-03 21:23
快速回复:飞机游戏------数据输出和我赋的值不一样,尝试了好多次也没用(飞机撞 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017256 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved