Easyx-----c语言实现图形化打砖块

#include<stdio.h> 
#include<conio.h>
#include<easyx.h>
#include<Windows.h>
#define WINDOW_Width  768
#define WINDOW_Height 1073 
#define BrickWidth 81
#define BrickHeight 27
#define ROWS 5     //行
#define COLS 8     //列
enum bk { null, red, orange, yellow, green, blue };
#define Broad_Width 110
#define Broad_Height 20
int brick[ROWS][COLS] = { 0 };
IMAGE img[6], img_ball[2]; IMAGE img_bk, img_board;

int Ball_x = 6; //球的x方向向量   
int Ball_y = -6;//球的y方向向量  
#define Radius 5
int Broad_x = (WINDOW_Width - Broad_Width) / 2 +25; //Broad初始化
int Broad_y = WINDOW_Height - Broad_Height-100;

int circle_x = WINDOW_Width / 2-16;               //circle初始化
int circle_y = WINDOW_Height - Radius - Broad_Height-122 ;
void loadRescource()
{
	loadimage(img + 1, "./res/redblock.png",78,25);
	loadimage(img + 2, "./res/orangeblock.png",78,25);
	loadimage(img + 3, "./res/yellowblock.png",78,25);
	loadimage(img + 4, "./res/greenblock.png",78,25);
	loadimage(img + 5, "./res/blueblock.png",78,25);

	loadimage(&img_bk, "./res/bk.png");

	loadimage(img_ball + 0, "./res/ball0.png");
	loadimage(img_ball + 1, "./res/ball.png");

	loadimage(&img_board, "./res/board.png");

}
void drawBall()
{
	putimage(circle_x, circle_y, &img_ball[0]);
	putimage(circle_x, circle_y, &img_ball[1]);

}


void ballMove()
{
	circle_x += Ball_x;
	circle_y += Ball_y;

}
int  DisappearBrick()
{
	int ZK_COL =( circle_x-60) / BrickWidth;
	int ZK_ROW = (circle_y-250) / BrickHeight;

	if (ZK_ROW < 5 && ZK_COL < 8 && brick[ZK_ROW][ZK_COL] != -1)
	{
		brick[ZK_ROW][ZK_COL] = -1;
		return 1;

	}return 0;
}
//碰撞检测int circle_x, int circle_y
void CollisionDetection()
{
	//球如果往右边移动,检测球是否撞上右边沿
	if (Ball_x > 0 && circle_x >= WINDOW_Width - Radius-55)
	{
		Ball_x = -6;
	}
	//球如果往上边移动,检测球是否撞上上边沿
	if (Ball_y < 0 && circle_y <= Radius || DisappearBrick())
	{
		Ball_y = 6;
	}
	//球如果往左边移动,检测球是否撞上左边沿
	if (Ball_x < 0 && circle_x <= Radius +35)
	{
		Ball_x = 6;
	}
	//球如果往下边移动,检测球是否撞上下边沿
	if (Ball_y > 0 && circle_y >= WINDOW_Height - Radius)
	{
		Ball_y = 0;
		Ball_x = 0;

	}
	//检测球是否撞上板子,只能从上往下撞,只有两种极限情况
	if ((Ball_y > 0) && (circle_y >= (Broad_y - Radius) - 25) &&//球y坐标
		(circle_x >= Broad_x) && (circle_x <= (Broad_x + Broad_Width)))
	{
		Ball_y = -6;
	}

}
//键盘控制
void KeyControl()
{
	CollisionDetection();	//不管有没有按键都要碰撞检测
	int ch;
	if (true == _kbhit())
	{                       //检测是否有按键
		ch = _getch();//获取按键的值
		switch (ch)
		{
		case 'a':
		case 'A':
		case 75:
			if(Broad_x >= Radius + 35+38)
			Broad_x -= 50;
			break;
		case 'd':
		case 'D':
		case 77:
			if (Broad_x <= WINDOW_Width - Radius - 55-149)
			Broad_x += 50;
			break;
		}
	}
}
void DrawAllBrick()
{
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
			switch (brick[i][j])
			{
			case 1:
				putimage(j * 81 + 60, i * 27 + 250, &img[1]);
				break;
			case 2:
				putimage(j * 81 + 60, i * 27 + 250, &img[2]);
				break;
			case 3:
				putimage(j * 81 + 60, i * 27 + 250, &img[3]);
				break;
			case 4:
				putimage(j * 81 + 60, i * 27 + 250, &img[4]);
				break;
			case 5:
				putimage(j * 81 + 60, i * 27 + 250, &img[5]);
			}
	}

}
void init(int  brick[][COLS])
{
	int k = 1;
	for (int i = 0; i < ROWS; i++)
	{

		for (int j = 0; j < COLS; j++)
		{

			brick[i][j] = k;

		}
		k++;
	}

}

int main()
{
	initgraph(WINDOW_Width, WINDOW_Height/*,EW_SHOWCONSOLE*/);
	init(brick);
	loadRescource();

	while (true)
	{
		BeginBatchDraw();

		putimage(0, 0, &img_bk);

		putimage(Broad_x, Broad_y, &img_board);

		DrawAllBrick();

		drawBall();
		ballMove();
		KeyControl();
		DisappearBrick();

		EndBatchDraw();
		Sleep(20);
	}
	while (1);
	return 0;
}

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qiuqiuyaq

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

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

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

打赏作者

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

抵扣说明:

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

余额充值