跟着童晶老师学c语言//利用easyx实现弹射弹珠

本文介绍了使用C++编程语言和图形库实现的一个基本游戏,涉及小球移动、碰撞检测、挡板控制以及用户输入响应。代码展示了如何初始化游戏元素,更新游戏状态,并在屏幕上显示结果。
摘要由CSDN通过智能技术生成

#include <conio.h>
#include <graphics.h>

#define High 480  // 游戏画面尺寸
#define Width 640
#define Brick_num 10 // 砖块个数
#define R 10        // 小球的半径
#define Speed 30

// 全局变量
int ball_x,ball_y; // 小球的坐标
int ball_vx,ball_vy; // 小球的速度
int bar_x,bar_y; // 挡板中心坐标
int bar_high,bar_width;  // 挡板的高度和宽度
int bar_left,bar_right,bar_top,bar_bottom; // 挡板的上下左右位置坐标

int isBrickExisted[Brick_num]; // 每个砖块是否存在,1为存在,0为没有了
int brick_high,brick_width; // 每个砖块的高度和宽度
int speed;

void startup()  // 数据初始化
{
    ball_x = Width/2;
    ball_y = High/2;
    ball_vx = 1;
    ball_vy = 1;
    
    bar_high = High/20;
    bar_width = Width/2;
    bar_x = Width/2;
    bar_y = High - bar_high/2;
    bar_left = bar_x - bar_width/2;
    bar_right = bar_x + bar_width/2;
    bar_top = bar_y - bar_high/2;
    bar_bottom = bar_y + bar_high/2;

    brick_width = Width/Brick_num;
    brick_high = High/Brick_num;
    speed=0;
    int i;
    for (i=0;i<Brick_num;i++)
        isBrickExisted[i] = 1;
    
    initgraph(Width, High);
    BeginBatchDraw();
}

void gameover()
{
    EndBatchDraw();
    closegraph();
}


void clean()  // 消除画面
{
    setcolor(BLACK);    //线条颜色 
    cleardevice();
    setfillcolor(WHITE);
    if(speed>=Speed)
    {
        fillcircle(ball_x, ball_y, R);     // 绘制圆
        speed=0;
    }
    
    bar(bar_left,bar_top,bar_right,bar_bottom);    
    // 绘制一个填充矩形

    int i,brick_left,brick_right,brick_top,brick_bottom;    
    for (i=0;i<Brick_num;i++)
    {
        brick_left = i*brick_width;
        brick_right = brick_left + brick_width;
        brick_top = 0;
        brick_bottom = brick_high;
        if (!isBrickExisted[i])     // 砖块没有了,绘制黑色
            fillrectangle(brick_left,brick_top,brick_right,brick_bottom);
            
    }
}    

void show()  // 显示画面
{
    setcolor(RED);
    setfillcolor(BLUE);
    fillcircle(ball_x, ball_y, R);    // 绘制圆
    bar(bar_left,bar_top,bar_right,bar_bottom);
    // 绘制挡板
    int i,brick_left,brick_right,brick_top,brick_bottom;

    for (i=0;i<Brick_num;i++)
    {
        brick_left = i*brick_width;
        brick_right = brick_left + brick_width;
        brick_top = 0;
        brick_bottom = brick_high;

        if (isBrickExisted[i])     // 砖块存在,绘制砖块
        {
            setcolor(GREEN);
            setfillcolor(YELLOW);
            fillrectangle(brick_left,brick_top,brick_right,brick_bottom);    // 绘制砖块
        }
    }

    FlushBatchDraw();
    // 延时
//    Sleep(3);    
}    

void updateWithoutInput()  // 与用户输入无关的更新
{
    // 更新小圆坐标
    if(speed<Speed)
    {
        speed++;    
    }
    
    if(speed>=Speed)
    {
        ball_x = ball_x + ball_vx;
        ball_y = ball_y + ball_vy;
    }
    else return;
    // 挡板和小圆碰撞,小圆反弹
    if ( ( ball_y+R >= bar_top-1) 
        &&( ball_y-R <= bar_bottom+1) )
        if ( (ball_x>=bar_left) && (ball_x<=bar_right) )
                {
                    ball_y=bar_top-R-1;
                    ball_vy = -ball_vy;
                }

        // 小圆和边界碰撞
    if(ball_x>=Width-R )
    
    {
        ball_vx = -ball_vx;
        ball_x=Width-R;
    }

    if (ball_x<=R)
    {
        ball_vx = -ball_vx;
        ball_x=R;
    }
    if(ball_y>=High-R )
    
    {
        ball_vy = -ball_vy;
        ball_y=High-R;
    }

    if (ball_y<=R)
    {
        ball_vy = -ball_vy;
        ball_y=R;
    }

    // 判断小圆是否和某个砖块碰撞
    int i,brick_left,brick_right,brick_bottom;
    for (i=0;i<Brick_num;i++)
    {
        if (isBrickExisted[i])     // 砖块存在,才判断
        {
            brick_left = i*brick_width;
            brick_right = brick_left + brick_width;
            brick_bottom = brick_high;
            if ( (ball_y-R<=brick_bottom) && (ball_x>=brick_left)
             && (ball_x<=brick_right) )
            {
                isBrickExisted[i] = 0;
                ball_vy = -ball_vy;    
            }
        }
    }
    int out=1;
    for (i=0;i<Brick_num;i++)
    {
        if (isBrickExisted[i])     // 砖块存在,才判断
        {
            out=0;
        }
    }
    if(out==1) gameover();
}

void updateWithInput()  // 与用户输入有关的更新
{    
    MOUSEMSG m;        // 定义鼠标消息
    if (MouseHit())  //这个函数用于检测当前是否有鼠标消息
    {
        m = GetMouseMsg();      // 获取一条鼠标消息
        if(m.uMsg == WM_MOUSEMOVE)
        {
            // 鼠标移动时,挡板的位置等于鼠标所在的位置
            bar_x = m.x;
            bar_y = m.y;
            bar_left = bar_x - bar_width/2;
            bar_right = bar_x + bar_width/2;
            bar_top = bar_y - bar_high/2;
            bar_bottom = bar_y + bar_high/2;
        }
        else if (m.uMsg == WM_LBUTTONDOWN)
        {
            // 按下鼠标左键,初始化小球的位置为挡板上面中心
            ball_x = bar_x;
            ball_y = bar_top - R;
        }
        else if (m.uMsg == WM_RBUTTONDOWN)
        {
            // 按下鼠标右键,初始化小球的位置为挡板上面中心
            //并使方向反向 
            ball_x = bar_x;
            ball_y = bar_top - R;
            ball_vx*=-1;
        }
    }
}


int main()
{
    startup();  // 数据初始化    
    while (1)  //  游戏循环执行
    {
        clean();  // 把之前绘制的内容取消
        updateWithoutInput();  // 与用户输入无关的更新
        updateWithInput();     // 与用户输入有关的更新
        show();  // 显示新画面
    }
    gameover();     // 游戏结束、后续处理
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值