这篇文章主要讲解了“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()//与用户输入有关的更新
{
/*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语言中怎么用easyx实现消砖块游戏这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。