概述
打砖块
- easyx的使用
easyx的使用
#include <stdio.h>
#include <graphics.h>
#include <time.h>
#include<stdlib.h>
#include<conio.h>
#define graph_width 900
#define graph_height 600
int difficulty = 3; //难度,最高5
// 砖块模块
// 二维数组充当地图
int map[5][8];
//初始化地图
void init_map()
{
srand((unsigned)time(NULL));
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
map[i][j] = rand() % 3+1;
}
}
}
//画地图
void draw_map()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
int x = j* graph_width/8;
int y = i * 25;
switch (map[i][j])
{
case 1:
setfillcolor(LIGHTRED);
fillrectangle(x, y, x + graph_width / 8, y + 25);
break;
case 2:
setfillcolor(LIGHTBLUE);
fillrectangle(x, y, x + graph_width / 8, y + 25);
break;
case 3:
setfillcolor(LIGHTGREEN);
fillrectangle(x, y, x + graph_width / 8, y + 25);
break;
case 0:
break;
}
}
}
}
// 木板模块
//抽象木板属性
struct Board
{
int x;
int y;
int width;
int height;
COLORREF color;
int speed;
};
//初始化木板
struct Board board = { graph_width / 2 - 100,graph_height - 25,200,25,WHITE,5 };
//画木板
void draw_Board()
{
setfillcolor(board.color);
fillrectangle(board.x, board.y, board.x + board.width, board.y + board.height);
}
//木板移动
void move_board()
{
if ((GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A')) && board.x > 0)
{
board.x -= board.speed;
}
if ((GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D')) &&board.x < graph_width - board.width)
{
board.x += board.speed;
}
}
// 球模块
struct Ball
{
int x;
int y;
int dx;
int dy;
int r;
COLORREF color;
int speed;
};
//初始化球
struct Ball ball = {graph_width/2,board.y-25,1,-1,25,RED,5};
//画球
void draw_ball()
{
setfillcolor(ball.color);
fillcircle(ball.x, ball.y, ball.r);
}
//球移动
void move_ball()
{
ball.x += ball.dx;
ball.y += ball.dy;
}
//消除砖块
int clear_brick()
{
if ((ball.y -ball.r)/ 25 < 5)
{
if (map[(ball.y - ball.r) / 25][ball.x/ (graph_width / 8)] != 0)
{
map[(ball.y - ball.r) / 25][ball.x / (graph_width / 8)] = 0;
return 1;
}
}
return 0;
}
//碰撞模块
void rebound()
{
//左右墙壁碰撞
if (ball.x == ball.r || ball.x == graph_width - ball.r )
{
ball.dx *= -1;
}
//木板、砖块、上墙壁碰撞
if ((ball.x >= board.x && ball.x <= board.x + board.width && board.y - ball.y == ball.r) || ball.y == ball.r||clear_brick())
ball.dy *= -1;
}
//时间等待模块
int waittime(int t)
{
static int begin = clock();
int now = clock();
if (now - begin > t)
{
begin = now;
return 1;
}
else
return 0;
}
//判断胜负
int win()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
if(map[i][j] != 0)
return 0;
}
}
return 1;
}
int lose()
{
if (ball.y > board.y)
return 1;
else
return 0;
}
//玩游戏
void play()
{
init_map();
while (!win())
{
BeginBatchDraw();
draw_map();
draw_Board();
draw_ball();
FlushBatchDraw();
if (waittime(6 - difficulty))
{
move_board();
move_ball();
rebound();
}
cleardevice();
if (lose())
{
printf("你输了n");
break;
}
if (GetAsyncKeyState(' '))
{
system("pause");
}
}
if (win())
{
printf("恭喜你赢了n");
}
}
//主函数
int main()
{
initgraph(graph_width, graph_height);
play();
return 0;
}
最后
以上就是无奈水壶为你收集整理的c++之打砖块小游戏的全部内容,希望文章能够帮你解决c++之打砖块小游戏所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复