心血来潮想要学习一下easyX(明明还有一大堆其他的东西要学,有点不务正业了(ಥ_ಥ) )
首先需要去官网下载并安装:EasyX官网
官网里面有安装和使用教程,还有很多案例程序(对于只能在黑框里输出文字的我很有吸引力,没办法太菜了啊(ಥ_ಥ))
然后找到了一篇入门教程:
https://blog.youkuaiyun.com/sandalphon4869/article/details/80862023
照着里面的小例子打了一遍,大致明白了怎么用,就想找个小游戏做做
找到了一个打砖块小游戏:
https://blog.youkuaiyun.com/xMaii/article/details/78154870
看起来还挺简单的,照着打了一遍(哈哈自己写不出来)
#include <graphics.h>//图形库
#include <conio.h> //用于进行键盘操作
const int WINDOW_HEIGHT = 500;
const int WINDOW_WIDE = 400;
class Bricks //定义砖块类
{
public:
int bricks[12][12]; //用二维数组保存所有砖块
int count; //记录砖块总数
const int x = 10, y = 5; //定义砖块有y排,x列
const int length = WINDOW_WIDE / x; //计算每个砖块的长
const int wide = 20; //定义每个砖块的宽
//构造函数
Bricks()
{
//memset(数组名,ch,n) 给数组前n个值赋为ch
//砖的状态:0--存在 1--被打掉
memset(bricks, 0, sizeof(bricks));//所有砖块初始化为0
count = x * y; //计算砖块总数
}
//画出所有的砖块
void drawAllbricks()
{
setfillcolor(YELLOW); //设置砖块颜色
setlinecolor(BLACK); //设置边框颜色
for (int i = 0; i < y; i++)
for (int j = 0; j < x; j++)
fillrectangle(j*length, i*wide, (j + 1)*length, (i + 1)*wide);//有边框填充颜色
}
};
class Board //定义木板类
{
public:
int x, y; // 定义木板的坐标(为左上角定点的坐标)
const int length = 80; //定义木板的长度
const int wide = 15; // 定义木板的宽度
//将木板初始化在中心位置
Board()
{
y = WINDOW_HEIGHT - wide;
x = WINDOW_WIDE / 2 - length / 2;
setfillcolor(BLUE);
solidrectangle(x, y, x + length, y + wide);
}
//木板移动函数
void Move()
{
//键盘的每个按键都对应一个确定的键值,-> :77 <- :75
int ch; //接收键值
ch = _getch();
setfillcolor(BLACK);//将木板当前位置用背景色覆盖
solidrectangle(x, y, x + length, y + wide);
switch (ch)
{
//左移:木板长度的1/3
case 75:
case 'A':
case 'a':
x = x - length / 3;
break;
//右移:
case 77:
case 'd':
case 'D':
x = x + length / 3;
break;
}
//木板左右移动受到边界的控制
if (x <= 0)
x = 0;
if (x >= WINDOW_WIDE - length)
x = WINDOW_WIDE - length;
//更新坐标后画出新的木板
setfillcolor(BLUE);
solidrectangle(x, y, x + length, y + wide);
}
};
class Ball//木板类
{
public:
const int r = 20;
const int speed = 2;// 小球的飞行速度
int ballx, bally;//球的坐标
int addx, addy; //小球飞行的方向
//设置两个标志量
bool go;//小球是否发射
bool iscatch; //木板是否接住了小球
//初始化小球位置,位于木板中心,参数是木板的宽
Ball(int board_wide)
{
ballx = WINDOW_WIDE / 2;
bally = WINDOW_HEIGHT - board_wide - r - 1;
//初始时小球向右发射
addx = 1;
addy = -1;
go = 0;//初始状态 小球未发射出去
iscatch = 1; //初始状态 球被木板接住
setfillcolor(RED);
solidcircle(ballx, bally, r);
}
void move(Bricks &bricks,Board &board)
{
setfillcolor(BLACK);//用背景色覆盖掉小球
solidcircle(ballx, bally, r);
BeginBatchDraw(); //开启批量画图,消除闪烁
//如果小球碰到窗口边界:(左,右,上) 反弹
if (ballx >= WINDOW_WIDE - r || ballx <= r)
addx = -addx;
if (bally <= r)
addy = -addy;
//如果小球碰到下边界,退出
if (bally >= WINDOW_HEIGHT - r)
{
iscatch = 0;
return;
}
//如果木板接住小球
if (ballx >= board.x - r &&ballx <= board.x + board.length + r&&bally >= board.y -r)
{
//x不变,y反弹
addy = -addy;
iscatch = 1; //表示接住
}
//如果小球碰到砖块
int flag = 0; //表示没有碰到砖块
for (int i = 0; i < bricks.y&&flag == 0; i++)
{
for (int j = 0; j < bricks.x && flag == 0; j++)
{
//如果此处有砖块,且在碰撞范围之内
if (bricks.bricks[i][j] == 0 && ballx >= j*bricks.length - r && ballx <= (j + 1)*bricks.length + r &&bally >= i*bricks.wide - r&& bally <= (i + 1)*bricks.wide + r)
{
//碰到左右两边:y不变,x反弹
if(bally >= i*bricks.wide - r&& bally <= (i + 1)*bricks.wide + r)
addx = -addx;
//碰到上下两边,x不变,y反弹
else if (ballx >= j*bricks.length - r && ballx <= (j + 1)*bricks.length + r)
addy = -addy;
//四个顶角处
else
continue;
bricks.bricks[i][j] = 1; //表示这块砖被打掉
bricks.count--;
flag = 1; //击中了不用继续遍历
setfillcolor(BLACK); //奖击中的砖用黑色覆盖
solidrectangle(j*bricks.length, i*bricks.wide, (j + 1)*bricks.length, (i + 1)*bricks.wide);
}
}
}
ballx += addx *speed;//更新位置
bally += addy *speed;
if (bally + 1 < board.y - r)
go = 1; //小球发射成功
setfillcolor(RED); //在新的位置画小球
solidcircle(ballx, bally, r);
FlushBatchDraw(); //把之前绘制的内容显示出来
Sleep(5);
}
};
void Gaming()
{
Bricks brick;
brick.drawAllbricks();
Board board;
Ball ball(board.wide);
while (1)
{
if (ball.iscatch == 0 || brick.count == 0)
{
//本局结束,把当前小球和木板清理
setfillcolor(BLACK);
solidcircle(ball.ballx, ball.bally, ball.r);
solidrectangle(board.x, board.y, board.x + board.x + board.length, board.y + board.y + board.wide);
if (brick.count > 0)
MessageBox(NULL, L"You Lose!", L"打砖块", MB_RETRYCANCEL);
else
MessageBox(NULL, L"You Win!", L"打砖块", MB_RETRYCANCEL);
}
if (_kbhit()) //用该函数判断是否按下某个键,按下返回1,否则返回0
{
board.Move();
}
ball.move(brick, board);
}
}
int main()
{
initgraph(WINDOW_WIDE, WINDOW_HEIGHT);
Gaming();
return 0;
}
最后的结果截图:
小球和木板的移动实际就是用黑色覆盖掉原来的,在新的位置显示一个新的!砖块的消失同理,用背景色覆盖掉。其中涉及到一些位置的计算,自己画画图能够理解。
虽然是照着别人打的,但是收获还是挺多的。