C语言用easyx实现消砖块游戏

 更新时间:2022年05月12日 16:03:32   作者:辉小歌  
这篇文章主要为大家详细介绍了C语言消砖块游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文项目为大家分享了C语言用easyx实现消砖块游戏的具体代码,供大家参考,具体内容如下

一、最终效果展示

效果图如下:

这个项目还是有很多的细节漏洞的。例如: 边界控制这里还是有点问题的。

二、绘制静态的挡板

代码如下:

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

#define High 480 //游戏画面尺寸
#define Width 640

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


void startup()//数据的初始化
{
    ball_x=Width/2;
    ball_y=High/2;
    ball_vx=1;
    ball_vy=1;
    radius=20;

    bar_high=High/20;
    bar_width=Width/5;
    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;

    initgraph(Width,High);
    BeginBatchDraw();
}

void clean()//显示画面
{
    setcolor(BLACK);//绘制黑线,黑色填充的圆
    setfillcolor(BLACK);
    fillcircle(ball_x,ball_y,radius);
    bar(bar_left,bar_top,bar_right,bar_bottom);//绘制黑色,黑色填充的挡板
}

void show()//显示画面
{
    setcolor(YELLOW);//绘制黄线,绿色填充的圆
    setfillcolor(GREEN);
    fillcircle(ball_x,ball_y,radius);
    bar(bar_left,bar_top,bar_right,bar_bottom);//绘制黄色,绿色填充的挡板

    FlushBatchDraw();
    Sleep(3);
}

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

        if( (ball_x<=radius)||(ball_x>=Width-radius))
            ball_vx=-ball_vx;
        if( (ball_y<=radius)||(ball_y>=High-radius))
            ball_vy=-ball_vy;
}

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

}

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

int main()
{
    startup();//数据的初始化
    while(1)
    {
        clean();//把之前绘制的内容清除
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
        show();//显示新画面
    }
}

效果图如下:

三、控制挡板

代码如下:

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

#define High 480 //游戏画面尺寸
#define Width 640

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


void startup()//数据的初始化
{
    ball_x=Width/2;
    ball_y=High/2;
    ball_vx=1;
    ball_vy=1;
    radius=20;

    bar_high=High/20;
    bar_width=Width/5;
    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;

    initgraph(Width,High);
    BeginBatchDraw();
}

void clean()//显示画面
{
    setcolor(BLACK);//绘制黑线,黑色填充的圆
    setfillcolor(BLACK);
    fillcircle(ball_x,ball_y,radius);
    bar(bar_left,bar_top,bar_right,bar_bottom);//绘制黑色,黑色填充的挡板
}

void show()//显示画面
{
    setcolor(YELLOW);//绘制黄线,绿色填充的圆
    setfillcolor(GREEN);
    fillcircle(ball_x,ball_y,radius);
    bar(bar_left,bar_top,bar_right,bar_bottom);//绘制黄色,绿色填充的挡板

    FlushBatchDraw();
    Sleep(3);
}

void updateWithoutInput()//与用户输入无关的更新
{
    //挡板和小球碰撞,小球反弹
    if(((ball_y+radius>=bar_top)&&(ball_y+radius<bar_bottom-bar_high/3))
        ||((ball_y-radius<=bar_bottom)&&(ball_y-radius>bar_top-bar_high/3)))
        if((ball_x>=bar_left)&&(ball_x<=bar_right))
            ball_vy=-ball_vy;

    ball_x=ball_x+ball_vx;
    ball_y=ball_y,ball_vy;//更新小球的坐标

    if( (ball_x<=radius)||(ball_x>=Width-radius))
        ball_vx=-ball_vx;
    if( (ball_y<=radius)||(ball_y>=High-radius))
        ball_vy=-ball_vy;
}

void updateWithInput()//与用户输入有关的更新
{
    char input;
    if(kbhit())
    {
        input=getch();
        if(input=='a'&&bar_left>0)
        {
            bar_x=bar_x-15;//位置左移
            bar_left=bar_x-bar_width/2;
            bar_right=bar_x+bar_width/2;
        }
        if(input=='d'&&bar_right<Width)
        {
            bar_x=bar_x+15;//位置左移
            bar_left=bar_x-bar_width/2;
            bar_right=bar_x+bar_width/2;
        }
        if(input=='w'&&bar_top>0)
        {
            bar_y=bar_y-15;//位置左移
            bar_top=bar_y-bar_high/2;
            bar_bottom=bar_y+bar_high/2;
        }
        if(input=='s'&&bar_bottom<High)
        {
            bar_y=bar_y+15;//位置右移
            bar_top=bar_y-bar_high/2;
            bar_bottom=bar_y+bar_high/2;
        }
    }
}

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

int main()
{
    startup();//数据的初始化
    while(1)
    {
        clean();//把之前绘制的内容清除
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
        show();//显示新画面
    }
}

效果图如下:

四、消砖块

代码如下:

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

#define High 480 //游戏画面尺寸
#define Width 640
#define Brick_num 10

//全局变量
int ball_x,ball_y;//小球的坐标
int ball_vx,ball_vy;//小球的速度
int radius;//小球的半径
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;//每个砖块的高度和宽度

void startup()//数据的初始化
{
    ball_x=Width/2;
    ball_y=High/2;
    ball_vx=1;
    ball_vy=1;
    radius=20;

    bar_high=High/20;
    bar_width=Width/5;
    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;

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

void clean()//显示画面
{
    setcolor(BLACK);//绘制黑线,黑色填充的圆
    setfillcolor(BLACK);
    fillcircle(ball_x,ball_y,radius);
    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(YELLOW);//绘制黄线,绿色填充的圆
    setfillcolor(GREEN);
    fillcircle(ball_x,ball_y,radius);
    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(WHITE);
            setfillcolor(RED);
            fillrectangle(brick_left,brick_top,brick_right,brick_bottom);//绘制砖块
        }
    }
    FlushBatchDraw();
    Sleep(3);
}

void updateWithoutInput()//与用户输入无关的更新
{
    //挡板和小球碰撞,小球反弹
    if(((ball_y+radius>=bar_top)&&(ball_y+radius<bar_bottom-bar_high/3))
        ||((ball_y-radius<=bar_bottom)&&(ball_y-radius>bar_top-bar_high/3)))
        if((ball_x>=bar_left)&&(ball_x<=bar_right))
            ball_vy=-ball_vy;

    ball_x=ball_x+ball_vx;
    ball_y=ball_y+ball_vy;//更新小球的坐标

    //小球和边界碰撞
    if( (ball_x<=radius)||(ball_x>=Width-radius))
        ball_vx=-ball_vx;
    if( (ball_y<=radius)||(ball_y>=High-radius))
        ball_vy=-ball_vy;

    //判断小球是否和某个砖块碰撞
    int i,brick_left,brick_right,brick_top,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==brick_bottom+radius)&&(ball_x>=brick_left)&&(ball_x<=
                brick_right))
            {
                isBrickExisted[i]=0;
                ball_vy=-ball_vy;
            }
        }
    }
}

void updateWithInput()//与用户输入有关的更新
{
    char input;
    if(kbhit())
    {
        input=getch();
        if(input=='a'&&bar_left>0)
        {
            bar_x=bar_x-15;//位置左移
            bar_left=bar_x-bar_width/2;
            bar_right=bar_x+bar_width/2;
        }
        if(input=='d'&&bar_right<Width)
        {
            bar_x=bar_x+15;//位置左移
            bar_left=bar_x-bar_width/2;
            bar_right=bar_x+bar_width/2;
        }
    }
}

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

int main()
{
    startup();//数据的初始化
    while(1)
    {
        clean();//把之前绘制的内容清除
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
        show();//显示新画面
    }
}

效果图如下:

五、鼠标交互

先看一个关于鼠标交互的实例

#include<graphics.h>
#include<conio.h>
int main(void)
{
    initgraph(640,480);//初始化图形窗口
    MOUSEMSG m;//定义鼠标消息
    while(1)
    {
        m=GetMouseMsg();//获取一条鼠标消息
        if(m.uMsg==WM_MOUSEMOVE)
        {
            putpixel(m.x,m.y,WHITE);//鼠标移动的时候画小白点
        }
        else if(m.uMsg==WM_LBUTTONDOWN)
        {
            rectangle(m.x-5,m.y-5,m.x+5,m.y+5);
            //鼠标左键按下时在鼠标位置画一个方块
        }
        else if(m.uMsg==WM_RBUTTONUP)
        {
            circle(m.x,m.y,10);
            //鼠标右键按下时在鼠标位置画一个圆
        }
    }
    return 0;
}

用鼠标控制挡板移动,按鼠标左键初始化小球位置

代码如下:

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

#define High 480 //游戏画面尺寸
#define Width 640
#define Brick_num 10

//全局变量
int ball_x,ball_y;//小球的坐标
int ball_vx,ball_vy;//小球的速度
int radius;//小球的半径
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;//每个砖块的高度和宽度

void startup()//数据的初始化
{
    ball_x=Width/2;
    ball_y=High/2;
    ball_vx=1;
    ball_vy=1;
    radius=20;

    bar_high=High/20;
    bar_width=Width/5;
    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;

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

void clean()//显示画面
{
    setcolor(BLACK);//绘制黑线,黑色填充的圆
    setfillcolor(BLACK);
    fillcircle(ball_x,ball_y,radius);
    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(YELLOW);//绘制黄线,绿色填充的圆
    setfillcolor(GREEN);
    fillcircle(ball_x,ball_y,radius);
    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(WHITE);
            setfillcolor(RED);
            fillrectangle(brick_left,brick_top,brick_right,brick_bottom);//绘制砖块
        }
    }
    FlushBatchDraw();
    Sleep(3);
}

void updateWithoutInput()//与用户输入无关的更新
{
    //挡板和小球碰撞,小球反弹
    if(((ball_y+radius>=bar_top)&&(ball_y+radius<bar_bottom-bar_high/3))
        ||((ball_y-radius<=bar_bottom)&&(ball_y-radius>bar_top-bar_high/3)))
        if((ball_x>=bar_left)&&(ball_x<=bar_right))
            ball_vy=-ball_vy;

    ball_x=ball_x+ball_vx;
    ball_y=ball_y+ball_vy;//更新小球的坐标

    //小球和边界碰撞
    if( (ball_x<=radius)||(ball_x>=Width-radius))
        ball_vx=-ball_vx;
    if( (ball_y<=radius)||(ball_y>=High-radius))
        ball_vy=-ball_vy;

    //判断小球是否和某个砖块碰撞
    int i,brick_left,brick_right,brick_top,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==brick_bottom+radius)&&(ball_x>=brick_left)&&(ball_x<=
                brick_right))
            {
                isBrickExisted[i]=0;
                ball_vy=-ball_vy;
            }
        }
    }
}

void updateWithInput()//与用户输入有关的更新
{
    /*char input;
    if(kbhit())
    {
    input=getch();
    if(input=='a'&&bar_left>0)
    {
    bar_x=bar_x-15;//位置左移
    bar_left=bar_x-bar_width/2;
    bar_right=bar_x+bar_width/2;
    }
    if(input=='d'&&bar_right<Width)
    {
    bar_x=bar_x+15;//位置左移
    bar_left=bar_x-bar_width/2;
    bar_right=bar_x+bar_width/2;
    }
    }*/
    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-radius-3;
        }
    }
}

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

int main()
{
    startup();//数据的初始化
    while(1)
    {
        clean();//把之前绘制的内容清除
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
        show();//显示新画面
    }
}

效果图如下:

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

相关文章

  • 7种排序算法的实现示例

    7种排序算法的实现示例

    这篇文章主要介绍了7种排序算法的实现示例,需要的朋友可以参考下
    2014-05-05
  • 基于c++ ege图形库实现五子棋游戏

    基于c++ ege图形库实现五子棋游戏

    这篇文章主要为大家详细介绍了基于c++ ege图形库实现五子棋游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • 关于C++中二分法详解

    关于C++中二分法详解

    大家好,本篇文章主要讲的是关于C++中二分法详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-02-02
  • C语言入门篇--初识C语言及数据类型

    C语言入门篇--初识C语言及数据类型

    本篇文章是c语言基础篇,主要为大家介绍了C语言的基本类型,为大家介绍了什么是C语言,希望可以帮助大家快速入门c语言的世界,更好的理解c语言
    2021-08-08
  • c++迭代器失效的情况汇总

    c++迭代器失效的情况汇总

    这篇文章主要介绍了C++迭代器失效的几种情况总结,文中代码非常详细,帮助大家更好的了解学习,感兴趣的朋友可以参考下
    2020-06-06
  • 简单掌握C++中的函数模板

    简单掌握C++中的函数模板

    这篇文章主要介绍了C++中的函数模板,包括函数模板的声明和生成以及异常处理等基本知识,需要的朋友可以参考下
    2016-04-04
  • QT UDP网络编程实现简单消息传输

    QT UDP网络编程实现简单消息传输

    这篇文章主要为大家详细介绍了QT UDP网络编程实现简单消息传输,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • C语言一维数组初步学习笔记

    C语言一维数组初步学习笔记

    这篇文章主要介绍了C语言一维数组初步学习笔记,包括指针访问数组等重要知识点,需要的朋友可以参考下
    2016-05-05
  • C++哈希应用的位图和布隆过滤器

    C++哈希应用的位图和布隆过滤器

    这篇文章主要介绍了C++哈希应用的位图和布隆过滤器的相关资料,文章内容多以列举试题的方式讲解,感兴趣的朋友可以参考下面文章内容
    2021-09-09
  • 关于C++类的成员初始化列表的相关问题

    关于C++类的成员初始化列表的相关问题

    下面小编就为大家带来一篇关于C++类的成员初始化列表的相关问题。小编觉得挺
    2016-05-05

最新评论