消砖块游戏(EasyX重构终极版)

编程入门 行业动态 更新时间:2024-10-07 08:20:04

消<a href=https://www.elefans.com/category/jswz/34/1728854.html style=砖块游戏(EasyX重构终极版)"/>

消砖块游戏(EasyX重构终极版)

如果没看过这篇文章的朋友可以先去这里反弹球消砖块C语言重构函数封装
因为这篇是C语言和EasyX结合的版本,也不是很难理解和实现

一个“屏保”


详细代码如下:

#include<conio.h>
#include<graphics.h>#define high 480
#define width 640int x, y;
int vx, vy;
int radius;void startup()
{x = width / 2;y = high / 2;vx = 1;vy = 1;radius = 20;initgraph(width, high);BeginBatchDraw();
}void clean()
{setcolor(BLACK);setfillcolor(BLACK);fillcircle(x, y, radius);
}void show()
{setcolor(BLUE);setfillcolor(YELLOW);fillcircle(x, y, radius);FlushBatchDraw();Sleep(3);
}void updateWithoutInput()
{x += vx;y += vy;if(x <= radius || x >= width - radius)vx = -vx;if(y <= radius || y >= high - radius)vy = -vy;
}void updateWithInput()
{
}void gameover()
{EndBatchDraw();closegraph();
}int main()
{startup();while(1){clean();updateWithoutInput();updateWithInput();show();}gameover();return 0;
}

添加一块挡板

效果演示如下:

详细代码如下:

#include<conio.h>
#include<graphics.h>#define high 480
#define width 640int x, y;					//小球's situation
int vx, vy;
int radius;
int x2, y2;					//挡板's central situation
int high2, width2;
int left, right, top, bottom;		void startup()
{x = width / 2;				y = high / 2;vx = 1;vy = 1;radius = 20;high2 = high / 20;width2 = width / 5;x2 = width / 2;y2 = high - high2 / 2;left = x2 - width2 / 2;right = x2 + width2 / 2;top = y2 - high2 / 2;bottom = y2 + high2 / 2;initgraph(width, high);BeginBatchDraw();
}void clean()			//擦除的效果
{setcolor(BLACK);setfillcolor(BLACK);fillcircle(x, y, radius);bar(left, top, right, bottom);
}void show()
{setcolor(BLUE);setfillcolor(YELLOW);fillcircle(x, y, radius);setfillcolor(BROWN);bar(left, top, right, bottom);FlushBatchDraw();Sleep(3);
}void updateWithoutInput()
{x += vx;y += vy;if(x <= radius || x >= width - radius)vx = -vx;if(y <= radius || y >= high - radius)vy = -vy;
}void updateWithInput()
{
}void gameover()
{EndBatchDraw();closegraph();
}int main()
{startup();while(1){clean();updateWithoutInput();updateWithInput();show();}gameover();return 0;
}

控制挡板接球

效果演示如下:


这一块只是完善了用户输入部分的函数

详细代码:

#include<conio.h>
#include<graphics.h>#define high 480
#define width 640int x, y;					//小球's situation
int vx, vy;
int radius;
int x2, y2;					//挡板's central situation
int high2, width2;
int left, right, top, bottom;		void startup()
{x = width / 2;				y = high / 2;vx = 1;vy = 1;radius = 20;high2 = high / 20;width2 = width / 5;x2 = width / 2;y2 = high - high2 / 2;left = x2 - width2 / 2;right = x2 + width2 / 2;top = y2 - high2 / 2;bottom = y2 + high2 / 2;initgraph(width, high);BeginBatchDraw();
}void clean()			//擦除的效果
{setcolor(BLACK);setfillcolor(BLACK);fillcircle(x, y, radius);bar(left, top, right, bottom);
}void show()
{setcolor(BLUE);setfillcolor(YELLOW);fillcircle(x, y, radius);setcolor(BLUE);setfillcolor(BROWN);bar(left, top, right, bottom);FlushBatchDraw();Sleep(3);
}void updateWithoutInput()
{		//考虑到球位于挡板上或挡板下的情况if(((y + radius >= top) && (y + radius < bottom - high2 / 3)) || (( y - radius <= bottom) && (y - radius > top - high2 / 3)))if((x >= left) && (x <= right))vy = -vy;x += vx;y += vy;if(x <= radius || x >= width - radius)vx = -vx;if(y <= radius || y >= high - radius)vy = -vy;
}void updateWithInput()
{char input;if(kbhit()){input = getch();if(input == 'a' && left > 0){x2 -= 15;left = x2 - width2 / 2;right = x2 + width2 / 2;}if(input == 'd' && right < width){x2 += 15;left = x2 - width2 / 2;right = x2 + width2 / 2;}if(input == 'w' && top > 0){y2 -= 15;top = y2 - high2 / 2;bottom = y2 + high2 / 2;}if(input == 's' && bottom < high){y2 += 15;top = y2 - high2 / 2;bottom = y2 + high2 / 2;}}
}void gameover()
{EndBatchDraw();closegraph();
}int main()
{startup();while(1){clean();updateWithoutInput();updateWithInput();show();}gameover();return 0;
}

终极版消砖块(难度:简易)

详细代码如下:

#include<conio.h>
#include<graphics.h>#define high 480
#define width 640
#define num 10int x, y;					//小球's situation
int vx, vy;
int radius;
int x2, y2;					//挡板's central situation
int high2, width2;			//挡板高宽
int left, right, top, bottom;		
int exist[num];				//记录砖块是否exist,1表exist,0表消失
int high3, width3;			//每个砖块的高宽void startup()
{x = width / 2;				y = high / 2;vx = 1;vy = 1;radius = 20;high2 = high / 20;width2 = width / 3;x2 = width / 2;y2 = high - high2 / 2;left = x2 - width2 / 2;right = x2 + width2 / 2;top = y2 - high2 / 2;bottom = y2 + high2 / 2;width3 = width / num;			//砖块宽高high3 = high / (num + 2);int i;for(i = 0; i < num; i++)exist[i] = 1;			//砖块exist Array初始化initgraph(width, high);BeginBatchDraw();
}void clean()			//擦除的效果
{setcolor(BLACK);setfillcolor(BLACK);fillcircle(x, y, radius);bar(left, top, right, bottom);int i, left3, right3, top3, bottom3;for(i = 0; i < num; i++){left3 = i * width3;right3 = left3 + width3;top3 = 0;bottom3 = high3;if(!exist[i])			//砖块被消了fillrectangle(left3, top3, right3, bottom3);}
}void show()
{setcolor(BLUE);setfillcolor(YELLOW);fillcircle(x, y, radius);setcolor(BLUE);setfillcolor(BROWN);bar(left, top, right, bottom);int i, left3, right3, top3, bottom3;for(i = 0; i < num; i++){left3 = i * width3;right3 = left3 + width3;top3 = 0;bottom3 = high3;if(exist[i]){setcolor(WHITE);setfillcolor(CYAN);fillrectangle(left3, top3, right3, bottom3);}}FlushBatchDraw();Sleep(1);
}void updateWithoutInput()
{		//考虑到球位于挡板上或挡板下的情况if(((y + radius >= top) && (y + radius < bottom - high2 / 2)) || (( y - radius <= bottom) && (y - radius > top - high2 / 2)))if((x >= left) && (x <= right))vy = -vy;x += vx;y += vy;if(x <= radius || x >= width - radius)vx = -vx;if(y <= radius || y >= high - radius)vy = -vy;int i, left3, right3, top3, bottom3;for(i = 0; i < num; i++){if(exist[i]){left3 = i * width3;right3 = left3 + width3;bottom3 = high3;if((y == bottom3 + radius) && (x >= left3) && (x <= right3)){exist[i] = 0;y += 1;vy = -vy;}} }
}void updateWithInput()
{char input;if(kbhit()){input = getch();if(input == 'a' && left > 0){x2 -= 15;left = x2 - width2 / 2;right = x2 + width2 / 2;}if(input == 'd' && right < width){x2 += 15;left = x2 - width2 / 2;right = x2 + width2 / 2;}if(input == 'w' && top > 0){y2 -= 15;top = y2 - high2 / 2;bottom = y2 + high2 / 2;}if(input == 's' && bottom < high){y2 += 15;top = y2 - high2 / 2;bottom = y2 + high2 / 2;}}
}void gameover()
{EndBatchDraw();closegraph();
}int main()
{startup();while(1){clean();updateWithoutInput();updateWithInput();show();}gameover();return 0;
}

加入鼠标交互先进版

#include<conio.h>
#include<graphics.h>#define high 480
#define width 640
#define num 10int x, y;					//小球's situation
int vx, vy;
int radius;
int x2, y2;					//挡板's central situation
int high2, width2;			//挡板高宽
int left, right, top, bottom;		
int exist[num];				//记录砖块是否exist,1表exist,0表消失
int high3, width3;			//每个砖块的高宽void startup()
{x = width / 2;				y = high / 2;vx = 1;vy = 1;radius = 20;high2 = high / 20;width2 = width / 3;x2 = width / 2;y2 = high - high2 / 2;left = x2 - width2 / 2;right = x2 + width2 / 2;top = y2 - high2 / 2;bottom = y2 + high2 / 2;width3 = width / num;			//砖块宽高high3 = high / (num + 2);int i;for(i = 0; i < num; i++)exist[i] = 1;			//砖块exist Array初始化initgraph(width, high);BeginBatchDraw();
}void clean()			//擦除的效果
{setcolor(BLACK);setfillcolor(BLACK);fillcircle(x, y, radius);bar(left, top, right, bottom);int i, left3, right3, top3, bottom3;for(i = 0; i < num; i++){left3 = i * width3;right3 = left3 + width3;top3 = 0;bottom3 = high3;if(!exist[i])			//砖块被消了fillrectangle(left3, top3, right3, bottom3);}
}void show()
{setcolor(BLUE);setfillcolor(YELLOW);fillcircle(x, y, radius);setcolor(BLUE);setfillcolor(BROWN);bar(left, top, right, bottom);int i, left3, right3, top3, bottom3;for(i = 0; i < num; i++){left3 = i * width3;right3 = left3 + width3;top3 = 0;bottom3 = high3;if(exist[i]){setcolor(WHITE);setfillcolor(CYAN);fillrectangle(left3, top3, right3, bottom3);}}FlushBatchDraw();Sleep(1);
}void updateWithoutInput()
{		//考虑到球位于挡板上或挡板下的情况if(((y + radius >= top) && (y + radius < bottom - high2 / 2)) || (( y - radius <= bottom) && (y - radius > top - high2 / 2)))if((x >= left) && (x <= right))vy = -vy;x += vx;y += vy;if(x <= radius || x >= width - radius)vx = -vx;if(y <= radius || y >= high - radius)vy = -vy;int i, left3, right3, top3, bottom3;for(i = 0; i < num; i++){if(exist[i]){left3 = i * width3;right3 = left3 + width3;bottom3 = high3;if((y == bottom3 + radius) && (x >= left3) && (x <= right3)){exist[i] = 0;y += 1;vy = -vy;}} }
}void updateWithInput()
{
//	char input;
//	if(kbhit())
//	{
//		input = getch();
//		if(input == 'a' && left > 0)
//		{
//			x2 -= 15;
//			left = x2 - width2 / 2;
//			right = x2 + width2 / 2;
//		}
//		if(input == 'd' && right < width)
//		{
//			x2 += 15;
//			left = x2 - width2 / 2;
//			right = x2 + width2 / 2;
//		}
//		if(input == 'w' && top > 0)
//		{
//			y2 -= 15;
//			top = y2 - high2 / 2;
//			bottom = y2 + high2 / 2;
//		}
//		if(input == 's' && bottom < high)
//		{
//			y2 += 15;
//			top = y2 - high2 / 2;
//			bottom = y2 + high2 / 2;
//		}
//	}MOUSEMSG m;if(MouseHit())				//检测当前是否有鼠标信息{m = GetMouseMsg();if(m.uMsg == WM_MOUSEMOVE){x2 = m.x;y2 = m.y;left = x2 - width2 / 2;right = x2 + width2 / 2;top = y2 - high2 / 2;bottom = y2 + high2 / 2;}}
}void gameover()
{EndBatchDraw();closegraph();
}int main()
{startup();while(1){clean();updateWithoutInput();updateWithInput();show();}gameover();return 0;
}

如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

更多推荐

消砖块游戏(EasyX重构终极版)

本文发布于:2024-03-10 19:34:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1728855.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:砖块   重构   终极版   游戏   EasyX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!