目录
头文件
#include <graphics.h> //引入EasyX库
#include <conio.h>
#include <time.h>
#include <stdio.h>
#include <mmsystem.h>
#include <Windows.h>
#pragma comment(lib, "Winmm.lib")
定义产量
// 定义常量
#define WIDTH 640 // 窗口宽度
#define HEIGHT 480 // 窗口高度
#define BALL_RADIUS 8 // 小球半径
#define BRICK_WIDTH 40 // 砖块宽度
#define BRICK_HEIGHT 20 // 砖块高度
#define BRICK_ROWS 8 // 砖块行数
#define BRICK_COLS 17 // 砖块列数
#define BOARD_WIDTH 80 // 板子宽度
#define BOARD_HEIGHT 10 // 板子高度
#define BOARD_SPEED 15 // 板子移动速度
初始化数据
// 砖块的状态,0 表示存在,1 表示被打掉了
int bricks[BRICK_ROWS][BRICK_COLS] = { 0 };
// 小球的位置和速度
double ball_x = WIDTH / 2, ball_y = HEIGHT / 2;
double ball_speed_x = 10, ball_speed_y = 10;
// 板子的位置和速度
double board_x = WIDTH / 2 - BOARD_WIDTH / 2, board_y = HEIGHT - BOARD_HEIGHT;
double board_speed = BOARD_SPEED;
函数的封装
// 添加背景音乐
void playMusic() {
mciSendString(_T("open 123.mp3 alias bkmusic"), NULL, 0, NULL);
mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);
}
// 停止播放背景音乐
void stopMusic() {
mciSendString(_T("stop bkmusic"), NULL, 0, NULL);
mciSendString(_T("close bkmusic"), NULL, 0, NULL);
}
// 画一个矩形(砖块)
void drawBrick(int x, int y, int w, int h, int color) {
setfillcolor(color); // 设置填充颜色
fillrectangle(x, y, x + w, y + h); // 画矩形
setlinecolor(BLACK); // 设置线条颜色
rectangle(x, y, x + w, y + h); // 画矩形框
}
// 画所有砖块
void drawBricks() {
for (int i = 0; i < BRICK_ROWS; i++) {
for (int j = 0; j < BRICK_COLS; j++) {
if (bricks[i][j] == 0) { // 只画存在的砖块
int color = RGB(200 - i * 30, 200 - j * 20, 200 - i * 20); // 计算砖块颜色
drawBrick(j * BRICK_WIDTH, i * BRICK_HEIGHT, BRICK_WIDTH, BRICK_HEIGHT, color); // 画砖块
}
}
}
}
// 画小球
void drawBall() {
setfillcolor(YELLOW); // 设置填充颜色
setlinecolor(WHITE); // 设置线条颜色
fillcircle(ball_x, ball_y, BALL_RADIUS); // 画圆形
}
// 画板子
void drawBoard() {
setfillcolor(RGB(100, 100, 100)); // 设置填充颜色
setlinecolor(BLACK); // 设置线条颜色
fillrectangle(board_x, board_y, board_x + BOARD_WIDTH, board_y + BOARD_HEIGHT); // 画矩形
}
// 碰撞检测
void checkCollision() {
// 检测是否碰到砖块
int brick_row = (ball_y / BRICK_HEIGHT);
int brick_col = (ball_x / BRICK_WIDTH);
if (brick_row >= 0 && brick_row < BRICK_ROWS && brick_col >= 0 && brick_col < BRICK_COLS && bricks[brick_row][brick_col] == 0) {
bricks[brick_row][brick_col] = 1; // 标记砖块被打掉
ball_speed_y = -ball_speed_y; // 小球反弹
score += 10;//加10分
}
// 检测是否碰到墙壁
if (ball_x < BALL_RADIUS || ball_x > WIDTH - BALL_RADIUS) {
ball_speed_x = -ball_speed_x; // 小球反弹
}
if (ball_y < BALL_RADIUS) {
ball_speed_y = -ball_speed_y; // 小球反弹
}
// 检测是否碰到板子
if (ball_y > HEIGHT - BOARD_HEIGHT - BALL_RADIUS && ball_x >= board_x && ball_x <= board_x + BOARD_WIDTH) {
ball_speed_y = -ball_speed_y; // 小球反弹
}
// 检测游戏是否结束
if (ball_y > HEIGHT + BALL_RADIUS) {
TCHAR s[20];//定义字符串数组
swprintf_s(s, _T("%d"), score);//将整数score转化为字符串
settextstyle(40, 0, _T("宋体"));//设置字体大小,字体
outtextxy((int)WIDTH/2,(int)HEIGHT/2, s);//输出得分
MessageBox(NULL, _T("Game Over!"), _T("提示"), MB_OK); // 提示游戏结束
stopMusic(); // 停止播放背景音乐
exit(0); // 退出程序
}
}
// 移动板子
void moveBoard() {
if (GetAsyncKeyState(VK_LEFT) && board_x > 0) { // 按下左箭头键并且板子还没有到窗口左边缘
board_x -= board_speed; // 板子向左移动
}
if (GetAsyncKeyState(VK_RIGHT) && board_x < WIDTH - BOARD_WIDTH) { // 按下右箭头键并且板子还没有到窗口右边缘
board_x += board_speed; // 板子向右移动
}
}
主函数
int main() {
IMAGE bgimg;//定义背景图片
//加载背景图片
loadimage(&bgimg, _T("bak.png"));
initgraph(WIDTH, HEIGHT); // 初始化窗口
putimage(0, 0, &bgimg);
BeginBatchDraw();//开启双缓冲机制
srand((unsigned int)time(NULL)); // 初始化随机数生成器
playMusic(); // 播放背景音乐
bool started = false; // 游戏是否已经开始
printf("************欢迎来到自制方块游戏*******\n");
printf("************输入空格开始游戏********\n");
printf("************使用左右方向键来移动木板***********\n");
//随机小球速度
ball_speed_x = (double)(rand() % 25 + 1) / 10 * 10;
ball_speed_y = (double)(rand() % 25 + 1) / 10 * 10;
while (1) {
// 绘制图形
cleardevice(); // 清空窗口
drawBricks(); // 画所有砖块
drawBall(); // 画小球
drawBoard(); // 画板子
// 移动小球和板子
if (started) { // 游戏已经开始
ball_x += ball_speed_x; // 小球水平方向移动
ball_y += ball_speed_y; // 小球竖直方向移动
moveBoard(); // 移动板子
// 碰撞检测
checkCollision(); // 检测小球碰撞情况
}
Sleep(50); // 暂停一段时间,减缓游戏速度
if (!started) { // 游戏还没有开始
settextcolor(BLUE); // 设置文字颜色
settextstyle(30, 0, _T("微软雅黑")); // 设置文字样式
outtextxy(WIDTH / 2 - 100, HEIGHT / 2 +100, _T("按空格键开始游戏")); // 输出文字
if (GetAsyncKeyState(VK_SPACE)) { // 按下空格键
started = true; // 标记游戏已经开始
}
FlushBatchDraw(); // 刷新缓冲区
EndBatchDraw();
// 刷新画面
}
}
getchar();
stopMusic(); // 停止播放背景音乐
closegraph(); // 关闭窗口,释放资源
return 0;
}
源代码
#include <graphics.h> //引入EasyX库
#include <conio.h>
#include <time.h>
#include <stdio.h>
#include <mmsystem.h>
#include <Windows.h>
#pragma comment(lib, "Winmm.lib")
// 定义常量
#define WIDTH 640 // 窗口宽度
#define HEIGHT 480 // 窗口高度
#define BALL_RADIUS 8 // 小球半径
#define BRICK_WIDTH 40 // 砖块宽度
#define BRICK_HEIGHT 20 // 砖块高度
#define BRICK_ROWS 8 // 砖块行数
#define BRICK_COLS 17 // 砖块列数
#define BOARD_WIDTH 80 // 板子宽度
#define BOARD_HEIGHT 10 // 板子高度
#define BOARD_SPEED 15 // 板子移动速度
// 砖块的状态,0 表示存在,1 表示被打掉了
int bricks[BRICK_ROWS][BRICK_COLS] = { 0 };
// 小球的位置和速度
double ball_x = WIDTH / 2, ball_y = HEIGHT / 2;
double ball_speed_x = 10, ball_speed_y = 10;
// 板子的位置和速度
double board_x = WIDTH / 2 - BOARD_WIDTH / 2, board_y = HEIGHT - BOARD_HEIGHT;
double board_speed = BOARD_SPEED;
//添加得分
int score = 0;//初始化得分
// 添加背景音乐
void playMusic() {
mciSendString(_T("open 123.mp3 alias bkmusic"), NULL, 0, NULL);
mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);
}
// 停止播放背景音乐
void stopMusic() {
mciSendString(_T("stop bkmusic"), NULL, 0, NULL);
mciSendString(_T("close bkmusic"), NULL, 0, NULL);
}
// 画一个矩形(砖块)
void drawBrick(int x, int y, int w, int h, int color) {
setfillcolor(color); // 设置填充颜色
fillrectangle(x, y, x + w, y + h); // 画矩形
setlinecolor(BLACK); // 设置线条颜色
rectangle(x, y, x + w, y + h); // 画矩形框
}
// 画所有砖块
void drawBricks() {
for (int i = 0; i < BRICK_ROWS; i++) {
for (int j = 0; j < BRICK_COLS; j++) {
if (bricks[i][j] == 0) { // 只画存在的砖块
int color = RGB(200 - i * 30, 200 - j * 20, 200 - i * 20); // 计算砖块颜色
drawBrick(j * BRICK_WIDTH, i * BRICK_HEIGHT, BRICK_WIDTH, BRICK_HEIGHT, color); // 画砖块
}
}
}
}
// 画小球
void drawBall() {
setfillcolor(YELLOW); // 设置填充颜色
setlinecolor(WHITE); // 设置线条颜色
fillcircle(ball_x, ball_y, BALL_RADIUS); // 画圆形
}
// 画板子
void drawBoard() {
setfillcolor(RGB(100, 100, 100)); // 设置填充颜色
setlinecolor(BLACK); // 设置线条颜色
fillrectangle(board_x, board_y, board_x + BOARD_WIDTH, board_y + BOARD_HEIGHT); // 画矩形
}
// 碰撞检测
void checkCollision() {
// 检测是否碰到砖块
int brick_row = (ball_y / BRICK_HEIGHT);
int brick_col = (ball_x / BRICK_WIDTH);
if (brick_row >= 0 && brick_row < BRICK_ROWS && brick_col >= 0 && brick_col < BRICK_COLS && bricks[brick_row][brick_col] == 0) {
bricks[brick_row][brick_col] = 1; // 标记砖块被打掉
ball_speed_y = -ball_speed_y; // 小球反弹
score += 10;//加10分
}
// 检测是否碰到墙壁
if (ball_x < BALL_RADIUS || ball_x > WIDTH - BALL_RADIUS) {
ball_speed_x = -ball_speed_x; // 小球反弹
}
if (ball_y < BALL_RADIUS) {
ball_speed_y = -ball_speed_y; // 小球反弹
}
// 检测是否碰到板子
if (ball_y > HEIGHT - BOARD_HEIGHT - BALL_RADIUS && ball_x >= board_x && ball_x <= board_x + BOARD_WIDTH) {
ball_speed_y = -ball_speed_y; // 小球反弹
}
// 检测游戏是否结束
if (ball_y > HEIGHT + BALL_RADIUS) {
TCHAR s[20];//定义字符串数组
swprintf_s(s, _T("%d"), score);//将整数score转化为字符串
settextstyle(40, 0, _T("宋体"));//设置字体大小,字体
outtextxy((int)WIDTH/2,(int)HEIGHT/2, s);//输出得分
MessageBox(NULL, _T("Game Over!"), _T("提示"), MB_OK); // 提示游戏结束
stopMusic(); // 停止播放背景音乐
exit(0); // 退出程序
}
}
// 移动板子
void moveBoard() {
if (GetAsyncKeyState(VK_LEFT) && board_x > 0) { // 按下左箭头键并且板子还没有到窗口左边缘
board_x -= board_speed; // 板子向左移动
}
if (GetAsyncKeyState(VK_RIGHT) && board_x < WIDTH - BOARD_WIDTH) { // 按下右箭头键并且板子还没有到窗口右边缘
board_x += board_speed; // 板子向右移动
}
}
int main() {
IMAGE bgimg;//定义背景图片
//加载背景图片
loadimage(&bgimg, _T("bak.png"));
initgraph(WIDTH, HEIGHT); // 初始化窗口
putimage(0, 0, &bgimg);
BeginBatchDraw();//开启双缓冲机制
srand((unsigned int)time(NULL)); // 初始化随机数生成器
playMusic(); // 播放背景音乐
bool started = false; // 游戏是否已经开始
printf("************欢迎来到自制方块游戏*******\n");
printf("************输入空格开始游戏********\n");
printf("************使用左右方向键来移动木板***********\n");
//随机小球速度
ball_speed_x = (double)(rand() % 25 + 1) / 10 * 10;
ball_speed_y = (double)(rand() % 25 + 1) / 10 * 10;
while (1) {
// 绘制图形
cleardevice(); // 清空窗口
drawBricks(); // 画所有砖块
drawBall(); // 画小球
drawBoard(); // 画板子
// 移动小球和板子
if (started) { // 游戏已经开始
ball_x += ball_speed_x; // 小球水平方向移动
ball_y += ball_speed_y; // 小球竖直方向移动
moveBoard(); // 移动板子
// 碰撞检测
checkCollision(); // 检测小球碰撞情况
}
Sleep(50); // 暂停一段时间,减缓游戏速度
if (!started) { // 游戏还没有开始
settextcolor(BLUE); // 设置文字颜色
settextstyle(30, 0, _T("微软雅黑")); // 设置文字样式
outtextxy(WIDTH / 2 - 100, HEIGHT / 2 +100, _T("按空格键开始游戏")); // 输出文字
if (GetAsyncKeyState(VK_SPACE)) { // 按下空格键
started = true; // 标记游戏已经开始
}
FlushBatchDraw(); // 刷新缓冲区
EndBatchDraw();
// 刷新画面
}
}
getchar();
stopMusic(); // 停止播放背景音乐
closegraph(); // 关闭窗口,释放资源
return 0;
}