这篇文章主要为大家详细介绍了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();//显示新画面
}
}
效果图如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:C语言用easyx实现消砖块游戏


猜你喜欢
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- C语言详解float类型在内存中的存储方式 2023-03-27
- Easyx实现扫雷游戏 2023-02-06
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- Qt计时器使用方法详解 2023-05-30
- C语言qsort()函数的使用方法详解 2023-04-26
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- ubuntu下C/C++获取剩余内存 2023-09-18
- C++ 数据结构超详细讲解顺序表 2023-03-25
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30