消磚塊遊戲(EasyX重構進階版)

2020-08-12 18:03:25

如果沒看過這篇文章的朋友可以先去這裏反彈球消磚塊C語言重構函數封裝
因爲這篇是C語言和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;
}

新增一塊擋板

效果演示如下:
在这里插入图片描述
詳細程式碼如下:

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


控制擋板接球

效果演示如下:

在这里插入图片描述
這一塊只是完善了使用者輸入部分的函數

詳細程式碼:

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

終極版消磚塊(難度:簡易)

在这里插入图片描述

詳細程式碼如下:

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

如果喜歡我的文章,請記得一鍵三連哦,點贊關注收藏,你的每一個贊每一份關注每一次收藏都將是我前進路上的無限動力 !!!↖(▔▽▔)↗感謝支援!