vs2019利用EasyX实现反弹球消砖块

这里是引用
Hello,大家好我是“是令狐冲啊”来自安徽,现在在西安读书,很高兴能够利用csdn这个平台分享自己编程时的感想与问题,我热爱编程,热爱代码,努力学习,希望自己变得更好。

vs2019利用EasyX实现反弹球消砖块

目前还是一名小白,下面这个代码还存在一些bug,但是还在陆续改进中,希望大家给予批评和指正,我会不断改进滴。

#include<graphics.h>
#include<conio.h>
#define Width 640//初始化游戏画面的尺寸
#define High 480
#define Brick_num 10//砖头的个数
int isBrickExisted[Brick_num];//判断每个砖块是否存在,1为存在,0为不存在
int brick_high, brick_width;//定义砖头的高和宽度
int ball_x = Width / 2;//小球的中心坐标
int ball_y = High / 2;//小球的中心坐标
int radius = 20;//小球的半径
int bar_x = Width / 2;//挡板的中心坐标
int bar_high = High / 20;//挡板的高度和宽度
int bar_width = Width / 5;
int bar_y = High - bar_high / 2;
int bar_left = bar_x - bar_width / 2;//挡板左端点x
int bar_right = bar_x + bar_width / 2;//挡板右端点x
int bar_top = bar_y - bar_high / 2;//挡板顶点
int bar_bottom = bar_y + bar_high / 2;//挡板最低处
int ball_vx = 1, ball_vy = 1;//小球的速度
void startup()//数据的初始化
{
	brick_high = High / Brick_num;
	brick_width = Width / 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;
	int brick_left, brick_right, brick_top, brick_bottom;
	for (i = 0; i < Brick_num; i++)
	{
		brick_left = i * brick_width;
		brick_top = 0;
		brick_right = brick_left + brick_width;
		brick_bottom = brick_high;
		if (isBrickExisted[i] == 0)//如果砖块等于0,砖块绘制成黑色
			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 brick_left, brick_right, brick_top, brick_bottom;
	int i;
	for (i = 0; i < Brick_num; i++)
	{
		brick_left = i * brick_width;
		brick_top = 0;
		brick_right = brick_left + brick_width;
		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_right) && (ball_x >= bar_left))
			ball_vy = -ball_vy;
	//更新小圆的坐标
	ball_x = ball_x + ball_vx;
	ball_y = ball_y + ball_vy;
	//小球和边界碰撞
	if ((ball_x <= radius) || (ball_x + radius >= Width))
		ball_vx = -ball_vx;
	if ((ball_y <= radius) || (ball_y + radius >= High))
		ball_vy = -ball_vy;
	//判断小球是否和砖块碰撞
	int brick_left, brick_right, brick_top, brick_bottom;
	int i;
	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 == 's' && bar_bottom <High)
		{
			bar_y = bar_y + 15;
			bar_top = bar_y - bar_high / 2;
			bar_bottom = bar_y+ bar_bottom / 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_bottom / 2;
		}
	}
}
void gameover()
{
	EndBatchDraw();
	closegraph();
}
int main()
{
	startup();
		while (1)
		{
			clean();
			updateWithoutInput();
			updateWithInput();
			show();

		}
		gameover();
		return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是令狐冲啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值