欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

消砖块游戏(EasyX重构进阶版)

程序员文章站 2022-04-07 17:30:16
...

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

一个“屏保”

消砖块游戏(EasyX重构进阶版)
详细代码如下:

#include<conio.h>
#include<graphics.h>

#define high 480
#define width 640

int 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;
}

添加一块挡板

效果演示如下:
消砖块游戏(EasyX重构进阶版)
详细代码如下:

#include<conio.h>
#include<graphics.h>

#define high 480
#define width 640

int 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;
}


控制挡板接球

效果演示如下:

消砖块游戏(EasyX重构进阶版)
这一块只是完善了用户输入部分的函数

详细代码:

#include<conio.h>
#include<graphics.h>

#define high 480
#define width 640

int 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;
}

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

消砖块游戏(EasyX重构进阶版)

详细代码如下:

#include<conio.h>
#include<graphics.h>

#define high 480
#define width 640
#define num 10

int 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;
}

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