EasyX与C++的反弹球消砖块


C++反弹球消砖块


游戏描述
一款用C++语言加上EasyX写的小游戏

游戏效果:
EasyX与C++的反弹球消砖块_第1张图片EasyX与C++的反弹球消砖块_第2张图片
定义变量

#define width 400
#define high 600

int ball_x, ball_y;  //小球中心坐标
int vx, vy;    //小球速度
int radius;   //小球半径
int wood_x, wood_y;  //木板中心坐标
int brick_count;   //砖块数量
int brick_x, brick_y;  //砖块长宽
int tag[5][10];    //标记数组

初始化

void startup()
{
	
	initgraph(width, high);

	ball_x = 200;
	ball_y = 580;
	radius = 10;
	vx = 1;
	vy = -1;
	wood_x = 200;
	wood_y = 595;
	brick_count = 50;
	brick_x = 40;
	brick_y = 20;

	//做标记
	for (int i = 0; i < 5; i++)
		for (int j = 0; j < 10; j++)
			tag[i][j] = 0;

	BeginBatchDraw();
}


与用户无关的更新

void updataWithoutInput()
{
	Sleep(5);
	//擦出圆
	setfillcolor(BLACK);
	solidcircle(ball_x, ball_y, radius);
	ball_x = ball_x + vx;
	ball_y = ball_y + vy;
	
	//小球碰壁后反弹
	if (ball_x <= radius || ball_x >= width - radius)
	{
		vx = -vx;
	}
	if (ball_y <= radius || ball_y >= high - radius)
	{
		vy = -vy;
	}

	//小球碰到挡板后反弹
	if (ball_y + 3 + radius >= wood_y-5&&ball_x + radius + 1 >= wood_x - 40&&ball_x - radius - 1 <= wood_x +40)
	{
		if (ball_x + radius + 1 == wood_x - 40&&ball_x - radius - 1 == wood_x +40)
		{
			vx = -vx;
			vy = -vy;
		}
		else
			vy = -vy;
	}

	//小球碰到砖块后反弹,增标记
	for(int i=0;i<5;i++)
		for (int j = 0; j < 10; j++)
		{
			//此处有砖块 且小球在该砖块的碰撞范围内
			if (tag[i][j] == 0 && ball_x + radius+1 >= j*brick_x&&ball_x - radius-1 <= (j + 1)*brick_x&&ball_y + radius+1 >= i*brick_y&&ball_y - radius-1 <= (i + 1)*brick_y)
			{
				if (ball_x + radius+1 >= j*brick_x&&ball_x - radius-1 <= (j + 1)*brick_x)
				{
					vx = -1 * vx;
				}
				if (ball_y + radius+1 >= i*brick_y&&ball_y - radius-1 <= (i + 1)*brick_y)
				{
					vy = -1 * vy;
				}
				
				tag[i][j] = 1;
				brick_count--;
				setfillcolor(BLACK);    //将击中的砖块用黑色覆盖掉
				fillrectangle(j*brick_x,i*brick_y,(j+1)*brick_x,(i+1)*brick_y);
				break;
			}
		}

	//结束游戏,砖块为零时,成功;挡板坐标大于高度,失败
	if (brick_count == 0||ball_y+radius>=high)
	{
		if (brick_count > 0)
			 MessageBox(NULL, L"You Lose!", L"打砖块", MB_RETRYCANCEL);
		else
		     MessageBox(NULL, L"You Win!", L"打砖块", MB_RETRYCANCEL);
	}

	FlushBatchDraw();
}

显示部分

void show()
{
	
	//画圆
	setfillcolor(GREEN);
	solidcircle(ball_x, ball_y, radius);
	
	//画挡板
	setfillcolor(BLUE);
	solidrectangle(wood_x - 40, wood_y - 5, wood_x + 40, wood_y + 5);
	
	//画砖块
	setfillcolor(YELLOW);
	setlinecolor(BLACK);
	for(int i=0;i<5;i++)
		for (int j = 0; j < 10; j++)
		{
			if (tag[i][j] == 0)
			{
				fillrectangle(brick_x * j, brick_y * i, brick_x * (j + 1), brick_y * (i + 1));
			}
		}
	FlushBatchDraw();
}

与用户有关的更新

void updataWithInput()
{
	    MOUSEMSG m;       //记录鼠标消息
		while (MouseHit())
		{
			m = GetMouseMsg();
			if (m.uMsg == WM_MOUSEMOVE)
			{
				//擦除挡板
				setfillcolor(BLACK);
				solidrectangle(wood_x - 40, wood_y - 5, wood_x + 40, wood_y + 5);
				//鼠标移动的时候方块移动
				if (m.x - 40 < 0)
				{
					wood_x = 40;
					wood_y = m.y;
				}
				else if (m.x + 40 > width)
				{
					wood_x = width - 40;
					wood_y = m.y;
				}
				else if (m.x - 40 >= 0 && m.x + 40 <= width)
				{
					wood_x = m.x;
					wood_y = m.y;
				}
				//设置小球Y坐标的边界
				if (m.y - 5 >= high)
					wood_y = high-5;
				if (m.y - 5 <= ball_y + radius)
					wood_y = ball_y + radius + 8;
			}
			if (m.uMsg == WM_LBUTTONDOWN)
			{
				//鼠标左键按下时小球归位
				ball_x = wood_x;
				ball_y = wood_y-20;
				vx = 1;
				vy = -1;
			}
			if (m.uMsg == WM_RBUTTONUP)
			{
				//鼠标右键按下又抬起后小球归位
				ball_x = wood_x;
				ball_y = wood_y - 20;
				vx = 1;
				vy = -1;
			}
		}
}

总部分

int main()
{
	startup();
	while (1)
	{
		show();
		updataWithoutInput();
		updataWithInput();

	}
	EndBatchDraw();
	_getch();
	closegraph();
	
    return 0;
}

总结
在CSDN中,很少能够看到用C++和EasyX写的小程序,感觉对新手来说不是很友好

完整代码

#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;

#define width 400
#define high 600

int ball_x, ball_y;  //小球中心坐标
int vx, vy;    //小球速度
int radius;   //小球半径
int wood_x, wood_y;  //木板中心坐标
int brick_count;   //砖块数量
int brick_x, brick_y;  //砖块长宽
int tag[5][10];    //标记数组

void startup()
{
	
	initgraph(width, high);

	ball_x = 200;
	ball_y = 580;
	radius = 10;
	vx = 1;
	vy = -1;
	wood_x = 200;
	wood_y = 595;
	brick_count = 50;
	brick_x = 40;
	brick_y = 20;

	//做标记
	for (int i = 0; i < 5; i++)
		for (int j = 0; j < 10; j++)
			tag[i][j] = 0;

	BeginBatchDraw();
}
void show()
{
	
	//画圆
	setfillcolor(GREEN);
	solidcircle(ball_x, ball_y, radius);
	
	//画挡板
	setfillcolor(BLUE);
	solidrectangle(wood_x - 40, wood_y - 5, wood_x + 40, wood_y + 5);
	
	//画砖块
	setfillcolor(YELLOW);
	setlinecolor(BLACK);
	for(int i=0;i<5;i++)
		for (int j = 0; j < 10; j++)
		{
			if (tag[i][j] == 0)
			{
				fillrectangle(brick_x * j, brick_y * i, brick_x * (j + 1), brick_y * (i + 1));
			}
		}
	FlushBatchDraw();
}
void updataWithoutInput()
{
	Sleep(5);
	//擦出圆
	setfillcolor(BLACK);
	solidcircle(ball_x, ball_y, radius);
	ball_x = ball_x + vx;
	ball_y = ball_y + vy;
	
	//小球碰壁后反弹
	if (ball_x <= radius || ball_x >= width - radius)
	{
		vx = -vx;
	}
	if (ball_y <= radius || ball_y >= high - radius)
	{
		vy = -vy;
	}

	//小球碰到挡板后反弹
	if (ball_y + 3 + radius >= wood_y-5&&ball_x + radius + 1 >= wood_x - 40&&ball_x - radius - 1 <= wood_x +40)
	{
		if (ball_x + radius + 1 == wood_x - 40&&ball_x - radius - 1 == wood_x +40)
		{
			vx = -vx;
			vy = -vy;
		}
		else
			vy = -vy;
	}

	//小球碰到砖块后反弹,增标记
	for(int i=0;i<5;i++)
		for (int j = 0; j < 10; j++)
		{
			//此处有砖块 且小球在该砖块的碰撞范围内
			if (tag[i][j] == 0 && ball_x + radius+1 >= j*brick_x&&ball_x - radius-1 <= (j + 1)*brick_x&&ball_y + radius+1 >= i*brick_y&&ball_y - radius-1 <= (i + 1)*brick_y)
			{
				if (ball_x + radius+1 >= j*brick_x&&ball_x - radius-1 <= (j + 1)*brick_x)
				{
					vx = -1 * vx;
				}
				if (ball_y + radius+1 >= i*brick_y&&ball_y - radius-1 <= (i + 1)*brick_y)
				{
					vy = -1 * vy;
				}
				
				tag[i][j] = 1;
				brick_count--;
				setfillcolor(BLACK);    //将击中的砖块用黑色覆盖掉
				fillrectangle(j*brick_x,i*brick_y,(j+1)*brick_x,(i+1)*brick_y);
				break;
			}
		}

	//结束游戏,砖块为零时,成功;挡板坐标大于高度,失败
	if (brick_count == 0||ball_y+radius>=high)
	{
		if (brick_count > 0)
			 MessageBox(NULL, L"You Lose!", L"打砖块", MB_RETRYCANCEL);
		else
		     MessageBox(NULL, L"You Win!", L"打砖块", MB_RETRYCANCEL);
	}

	FlushBatchDraw();
}
void updataWithInput()
{
	    MOUSEMSG m;       //记录鼠标消息
		while (MouseHit())
		{
			m = GetMouseMsg();
			if (m.uMsg == WM_MOUSEMOVE)
			{
				//擦除挡板
				setfillcolor(BLACK);
				solidrectangle(wood_x - 40, wood_y - 5, wood_x + 40, wood_y + 5);
				//鼠标移动的时候方块移动
				if (m.x - 40 < 0)
				{
					wood_x = 40;
					wood_y = m.y;
				}
				else if (m.x + 40 > width)
				{
					wood_x = width - 40;
					wood_y = m.y;
				}
				else if (m.x - 40 >= 0 && m.x + 40 <= width)
				{
					wood_x = m.x;
					wood_y = m.y;
				}
				//设置小球Y坐标的边界
				if (m.y - 5 >= high)
					wood_y = high-5;
				if (m.y - 5 <= ball_y + radius)
					wood_y = ball_y + radius + 8;

			}
			if (m.uMsg == WM_LBUTTONDOWN)
			{
				//鼠标左键按下时小球归位
				ball_x = wood_x;
				ball_y = wood_y-20;
				vx = 1;
				vy = -1;
			}
			if (m.uMsg == WM_RBUTTONUP)
			{
				//鼠标右键按下又抬起后小球归位
				ball_x = wood_x;
				ball_y = wood_y - 20;
				vx = 1;
				vy = -1;
			}

		}
		
}
int main()
{
	startup();
	while (1)
	{
		show();
		updataWithoutInput();
		updataWithInput();

	}
	EndBatchDraw();
	_getch();
	closegraph();
	
    return 0;
}

你可能感兴趣的:(萌新)