iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C语言用easyx实现消砖块游戏
  • 227
分享到

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

2024-04-02 19:04:59 227人浏览 八月长安
摘要

本文项目为大家分享了C语言用easyx实现消砖块游戏的具体代码,供大家参考,具体内容如下 一、最终效果展示 效果图如下: 这个项目还是有很多的细节漏洞的。例如: 边界控制这里还是有

本文项目为大家分享了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()//与用户输入有关的更新
{
    
    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();//显示新画面
    }
}

效果图如下:

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

--结束END--

本文标题: C语言用easyx实现消砖块游戏

本文链接: https://www.lsjlt.com/news/148672.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c语言怎么计算字符串长度
    在C语言中,可以使用内置的strlen()函数来计算字符串的长度。该函数位于string.h头文件中。以下是一个示例代码: #inc...
    99+
    2024-05-15
    c语言
  • c语言怎么判断三个数的大小
    可以通过比较三个数之间的大小关系来判断它们的大小。以下是一个示例代码: #include <stdio.h> int ...
    99+
    2024-05-15
    c语言
  • c++排序函数sort怎么使用
    在C++中,使用sort函数对容器中的元素进行排序,可以按照默认的升序排列,也可以自定义排序规则。 下面是一个简单的例子,对一个ve...
    99+
    2024-05-15
    c++
  • c++中string的compare函数怎么使用
    在C++中,可以使用string的compare函数来比较两个string对象的大小。该函数的原型如下: int compare(c...
    99+
    2024-05-15
    c++
  • c++中string的compare函数有什么用
    c++中string的compare函数用于比较两个字符串的大小关系。该函数返回一个整数值,表示两个字符串的比较结果。 具体来说,如...
    99+
    2024-05-15
    c++
  • c++中print函数的功能有哪些
    在C++中,通常使用std::cout来进行输出操作,而不是使用print函数。std::cout是C++标准库中的一个输出流对象,...
    99+
    2024-05-15
    c++
  • c语言数据结构怎么使用
    在C语言中,数据结构可以通过定义结构体来实现。以下是一些常见的数据结构的使用方法示例: 链表: #include <std...
    99+
    2024-05-15
    c语言
  • c++中print函数的用法是什么
    在C++中没有内置的print函数,但可以使用cout来输出内容到控制台。cout是C++标准库中的一个输出流对象,可以使用<...
    99+
    2024-05-15
    c++
  • c++中concept的用法是什么
    在C++20中,Concept是一种新的语言特性,用于定义类型要求和约束。Concept可以被用来约束函数模板、类模板和普通函数的参...
    99+
    2024-05-15
    c++
  • c++中concept的作用是什么
    在C++中,concept的作用是定义一种通用的约束,用于限制模板参数的类型范围。通过使用concept,可以在编译时对模板参数进行...
    99+
    2024-05-15
    c++
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作