C语言消砖块游戏

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C语言消砖块游戏

辉小歌   2022-06-03 我要评论

本文项目为大家分享了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();//显示新画面
    }
}

效果图如下:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们