C语言用函数实现反弹球消砖块

 更新时间:2022年05月12日 11:41:28   作者:辉小歌  
这篇文章主要为大家详细介绍了C语言用函数实现反弹球消砖块,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言用函数实现反弹球消砖块的具体代码,供大家参考,具体内容如下

一、项目描述和最终的成果展示

这是在上一次弹跳小项目上进行了一系列的优化和封装。项目: 弹跳的小球
上次没有用函数进行的封装。这次在上次的基础上进行封装和一些功能的优化。

最终效果图如下:

二、封装后的弹跳小球

代码如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>

//全局变量
int high,width; //游戏画面大小
int ball_x,ball_y;//小球的坐标
int ball_vx,ball_vy;//小球的速度

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 startup()//数据的初始化
{
    high =  15;
    width = 20;
    ball_x = 0;
    ball_y = width/2;
    ball_vx = 1;
    ball_vy = 1;
}

void show()//显示画面
{
    gotoxy(0,0);//光标移动到原点位置,以下重画清屏
    int i,j;
    for(i=0;i<=high;i++)
    {
        for(j=0;j<=width;j++)
        {
            if( ( i == ball_x) && ( j == ball_y ) )
                printf("O");//输出小球
            else if( j == width)
                printf("+");//输出右边框
            else if( i == high)
                printf("-");//输出下边框
            else
                printf(" ");//输出空格
        }
        printf("\n");
    }
}

void updateWithoutInput()//与用户输入无关的更新
{
    ball_x = ball_x + ball_vx;
    ball_y = ball_y + ball_vy;

    if( (ball_x == 0 ) || (ball_x == high-1 ))
        ball_vx = -ball_vx;
    if( (ball_y == 0 ) || (ball_y == width-1 ))
        ball_vy = -ball_vy;

    Sleep(50);
}

void updateWithInput()//与用户输入有关的更新
{

}
int main(void)
{
    startup();//数据的初始化
    while(1)
    {
        show();//显示画面
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
    }
    return 0;
}

效果图如下:

三、显示移动挡板

代码如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>

//全局变量
int high,width; //游戏画面大小
int ball_x,ball_y;//小球的坐标
int ball_vx,ball_vy;//小球的速度
int position_x,position_y;//挡板的中心坐标
int ridus;//挡板的半径大小
int left,right;//挡板的左右位置

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 startup()//数据的初始化
{
    high =  15;
    width = 20;
    ball_x = 0;
    ball_y = width/2;
    ball_vx = 1;
    ball_vy = 1;
    ridus = 5;
    position_x = high;
    position_y = width/2;
    left = position_y -ridus;
    right = position_y + ridus;
}

void show()//显示画面
{
    gotoxy(0,0);//光标移动到原点位置,以下重画清屏
    int i,j;
    for(i=0;i<=high+1;i++)
    {
        for(j=0;j<=width;j++)
        {
            if( ( i == ball_x) && ( j == ball_y ) )
                printf("O");//输出小球
            else if( j == width)
                printf("+");//输出右边框
            else if( i == high+1)
                printf("-");//输出下边框
            else if ( (i==high)&&(j>=left)&&(j<=right))
                printf("*");
            else
                printf(" ");//输出空格
        }
        printf("\n");
    }
}

void updateWithoutInput()//与用户输入无关的更新
{
    ball_x = ball_x + ball_vx;
    ball_y = ball_y + ball_vy;

    if( (ball_x == 0 ) || (ball_x == high-1 ))
        ball_vx = -ball_vx;
    if( (ball_y == 0 ) || (ball_y == width-1 ))
        ball_vy = -ball_vy;

    Sleep(50);
}

void updateWithInput()//与用户输入有关的更新
{
    char input;
    if(kbhit())
    {
        input = getch();
        if( input == 'a' || input == 'A' )
        {
            position_y--;//位置左移
            left = position_y-ridus;
            right = position_y+ridus;
        }
        if( input == 'd' || input == 'D' )
        {
            position_y++;
            left = position_y - ridus;
            right = position_y + ridus;
        }
    }
}
int main(void)
{
    startup();//数据的初始化
    while(1)
    {
        show();//显示画面
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
    }
    return 0;
}

效果图如下:

四、反弹小球

代码如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>

//全局变量
int high,width; //游戏画面大小
int ball_x,ball_y;//小球的坐标
int ball_vx,ball_vy;//小球的速度
int position_x,position_y;//挡板的中心坐标
int ridus;//挡板的半径大小
int left,right;//挡板的左右位置
int ball_number;//反弹小球的次数

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 startup()//数据的初始化
{
    high =  15;
    width = 20;
    ball_x = 0;
    ball_y = width/2;
    ball_vx = 1;
    ball_vy = 1;
    ridus = 5;
    position_x = high;
    position_y = width/2;
    left = position_y -ridus;
    right = position_y + ridus;
    ball_number=0;
}

void show()//显示画面
{
    gotoxy(0,0);//光标移动到原点位置,以下重画清屏
    int i,j;
    for(i=0;i<=high+1;i++)
    {
        for(j=0;j<=width;j++)
        {
            if( ( i == ball_x) && ( j == ball_y ) )
                printf("O");//输出小球
            else if( j == width)
                printf("+");//输出右边框
            else if( i == high+1)
                printf("-");//输出下边框
            else if ( (i==high)&&(j>=left)&&(j<=right))
                printf("*");
            else
                printf(" ");//输出空格
        }
        printf("\n");
    }
    printf("反弹小球数:%d\n",ball_number);
}

void updateWithoutInput()//与用户输入无关的更新
{
    if( ball_x == high -1)
    {
        if( (ball_y>=left) && (ball_y<=right) )
        {
            ball_number++;
            printf("\a");//响铃
        }
        else
        {
            printf("游戏失败\n");
            system("pause");
            exit(0);
        }
    }
    
    ball_x = ball_x + ball_vx;
    ball_y = ball_y + ball_vy;

    if( (ball_x == 0 ) || (ball_x == high-1 ))
        ball_vx = -ball_vx;
    if( (ball_y == 0 ) || (ball_y == width-1 ))
        ball_vy = -ball_vy;

    Sleep(50);
}

void updateWithInput()//与用户输入有关的更新
{
    char input;
    if(kbhit())
    {
        input = getch();
        if( input == 'a' || input == 'A' )
        {
            position_y--;//位置左移
            left = position_y-ridus;
            right = position_y+ridus;
        }
        if( input == 'd' || input == 'D' )
        {
            position_y++;
            left = position_y - ridus;
            right = position_y + ridus;
        }
    }
}
int main(void)
{
    startup();//数据的初始化
    while(1)
    {
        show();//显示画面
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
    }
    return 0;
}

效果图如下:

五、添加砖块并实现打砖块操作

代码如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>

//全局变量
int high,width; //游戏画面大小
int ball_x,ball_y;//小球的坐标
int ball_vx,ball_vy;//小球的速度
int position_x,position_y;//挡板的中心坐标
int ridus;//挡板的半径大小
int left,right;//挡板的左右位置
int ball_number;//反弹小球的次数
int block_x1,block_y1;//砖块1的位置
int block_x2,block_y2;//砖块2的位置
int block_x3,block_y3;//砖块3的位置
int 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 startup()//数据的初始化
{
    high =  15;
    width = 20;
    ball_x = 0;
    ball_y = width/2;
    ball_vx = 1;
    ball_vy = 1;
    ridus = 5;
    position_x = high;
    position_y = width/2;
    left = position_y -ridus;
    right = position_y + ridus;
    ball_number=0;
    block_x1 = 0;
    block_y1 = 1;
    block_x2 = 0;
    block_y2 = 2;
    block_x3 = 0;
    block_y3 = 3;
    score=0;
}

void show()//显示画面
{
    gotoxy(0,0);//光标移动到原点位置,以下重画清屏
    int i,j;
    for(i=0;i<=high+1;i++)
    {
        for(j=0;j<=width;j++)
        {
            if( ( i == ball_x) && ( j == ball_y ) )
                printf("O");//输出小球
            else if( j == width)
                printf("+");//输出右边框
            else if( i == high+1)
                printf("-");//输出下边框
            else if ( (i==high)&&(j>=left)&&(j<=right))
                printf("*");
            else if( (i==block_x1) && (j==block_y1) )
                printf("A");//输出砖块1
            else if( (i==block_x2) && (j==block_y2) )
                printf("B");//输出砖块2
            else if( (i==block_x3) && (j==block_y3) )
                printf("C");//输出砖块3
            else
                printf(" ");//输出空格
        }
        printf("\n");
    }
    printf("反弹小球数:%d\n",ball_number);
    printf("消掉的砖块数: %d\n",score);
}

void updateWithoutInput()//与用户输入无关的更新
{
    if( ball_x == high -1)
    {
        if( (ball_y>=left) && (ball_y<=right) )//被挡板挡住了
        {
            ball_number++;
            printf("\a");//响铃
        }
        else
        {
            printf("游戏失败\n");
            system("pause");
            exit(0);
        }
    }
    
    if( (ball_x == block_x1) && (ball_y ==block_y1) )//小球击中砖块1
    {
        score++;//分数加1
        block_y1=rand()%width;//产生新的砖块
        while((block_y1==block_y2) || ( block_y1==block_y3))
        //当新产生的砖块和其他砖块重合时
        {
            block_y1=rand()%width;//产生新的砖块
        }
    }

    if( (ball_x == block_x2) && (ball_y ==block_y2) )//小球击中砖块2
    {
        score++;//分数加1
        block_y2=rand()%width;//产生新的砖块
        while((block_y2==block_y1) || ( block_y2==block_y3))
        //当新产生的砖块和其他砖块重合时
        {
            block_y2=rand()%width;//产生新的砖块
        }
    }

    if( (ball_x == block_x3) && (ball_y ==block_y3) )//小球击中砖块3
    {
        score++;//分数加1
        block_y3=rand()%width;//产生新的砖块
        while((block_y3==block_y1) || ( block_y3==block_y2))
            //当新产生的砖块和其他砖块重合时
        {
            block_y3=rand()%width;//产生新的砖块
        }
    }

    ball_x = ball_x + ball_vx;
    ball_y = ball_y + ball_vy;

    if( (ball_x == 0 ) || (ball_x == high-1 ))
        ball_vx = -ball_vx;
    if( (ball_y == 0 ) || (ball_y == width-1 ))
        ball_vy = -ball_vy;

    Sleep(66);
}

void updateWithInput()//与用户输入有关的更新
{
    char input;
    if(kbhit())
    {
        input = getch();
        if( input == 'a' || input == 'A' )
        {
            position_y--;//位置左移
            left = position_y-ridus;
            right = position_y+ridus;
        }
        if( input == 'd' || input == 'D' )
        {
            position_y++;
            left = position_y - ridus;
            right = position_y + ridus;
        }
    }
}
int main(void)
{
    startup();//数据的初始化
    while(1)
    {
        show();//显示画面
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
    }
    return 0;
}

效果图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C语言修炼之路函数篇真题训练下

    C语言修炼之路函数篇真题训练下

    函数是一组一起执行一个任务的语句。每个 C 程序都至少有一个函数,即主函数 main() ,所有简单的程序都可以定义其他额外的函数
    2022-03-03
  • 详细分析C++ 数据封装和数据抽象

    详细分析C++ 数据封装和数据抽象

    这篇文章主要介绍了C++ 数据封装和数据抽象的的相关资料,文中代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • C语言编程C++自定义个性化类型

    C语言编程C++自定义个性化类型

    这篇文章主要介绍了C语言编程中如何来自定义C++个性化类型,文中附含详细的示例代码,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-09-09
  • 浅谈C++中char型变量的地址输出

    浅谈C++中char型变量的地址输出

    下面小编就为大家带来一篇浅谈C++中char 型变量的地址输出。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Pipes实现LeetCode(194.转置文件)

    Pipes实现LeetCode(194.转置文件)

    这篇文章主要介绍了Pipes实现LeetCode(194.转置文件),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • C++11并发编程:多线程std::thread

    C++11并发编程:多线程std::thread

    今天小编就为大家分享一篇关于C++11并发编程:多线程std::thread,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 利用C语言玩转魔方阵实例教程

    利用C语言玩转魔方阵实例教程

    这篇文章主要给大家介绍了关于利用C语言玩转魔方阵的相关资料,文中详细介绍了关于奇数魔方阵和4N 魔方阵的实现方法,通过示例代码让大家更好的参考学习,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • C语言中函数返回字符串的方法汇总

    C语言中函数返回字符串的方法汇总

    C语言返回字符串函数共有四种方式,分别如下:使用堆空间,返回申请的堆地址,注意释放、函数参数传递指针,返回该指针、返回函数内定义的静态变量(共享)、返回全局变量
    2017-05-05
  • C语言异常处理机制案例讲解

    C语言异常处理机制案例讲解

    这篇文章主要介绍了C语言异常处理机制案例讲解,本文讲解了异常处理机制所用的函数和具体的代码实现等,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C++浅析虚函数使用方法

    C++浅析虚函数使用方法

    对C++了解的人都应该知道虚函数(Virtual Function)是通过一张虚函数表(Virtual Table)来实现的。简称为V-Table。本文就将详细讲讲虚函数表的原理与使用,需要的可以参考一下
    2022-08-08

最新评论