- 取石子
打砖块*c++o11*gzjun
- 2025-7-13 15:27:49 @
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <time.h>
using namespace std;
// 定义游戏区域大小
const int WIDTH = 80;
const int HEIGHT = 20;
// 定义挡板信息
int paddleX = WIDTH / 2 - 5;
const int PADDLE_WIDTH = 10;
// 定义小球信息
int ballX = WIDTH / 2;
int ballY = HEIGHT - 2;
int ballDirX = 1;
int ballDirY = -1;
// 定义砖块信息
bool bricks[HEIGHT / 2][WIDTH / 5] = {false};
// 初始化砖块
void initBricks() {
for (int i = 0; i < HEIGHT / 2; i++) {
for (int j = 0; j < WIDTH / 5; j++) {
bricks[i][j] = true;
}
}
}
// 绘制游戏界面
void draw() {
system("cls");
// 绘制顶部边界
for (int i = 0; i < WIDTH + 2; i++) {
cout << "#";
}
cout << endl;
// 绘制游戏区域
for (int y = 0; y < HEIGHT; y++) {
cout << "#";
for (int x = 0; x < WIDTH; x++) {
if (x >= paddleX && x < paddleX + PADDLE_WIDTH && y == HEIGHT - 1) {
cout << "=";
} else if (x == ballX && y == ballY) {
cout << "O";
} else if (y < HEIGHT / 2 && bricks[y][x / 5]) {
cout << "*";
} else {
cout << " ";
}
}
cout << "#" << endl;
}
// 绘制底部边界
for (int i = 0; i < WIDTH + 2; i++) {
cout << "#";
}
cout << endl;
}
// 处理用户输入
void input() {
if (_kbhit()) {
char ch = _getch();
if (ch == 'a' && paddleX > 0) {
paddleX--;
} else if (ch == 'd' && paddleX + PADDLE_WIDTH < WIDTH) {
paddleX++;
}
}
}
// 更新游戏逻辑
void logic() {
ballX += ballDirX;
ballY += ballDirY;
// 处理小球撞到左右边界
if (ballX == 0 || ballX == WIDTH - 1) {
ballDirX = -ballDirX;
}
// 处理小球撞到顶部边界
if (ballY == 0) {
ballDirY = -ballDirY;
}
// 处理小球撞到挡板
if (ballY == HEIGHT - 2 && ballX >= paddleX && ballX < paddleX + PADDLE_WIDTH) {
ballDirY = -ballDirY;
}
// 处理小球掉落
if (ballY == HEIGHT - 1) {
cout << "Game Over!" << endl;
exit(0);
}
// 处理小球撞到砖块
if (ballY < HEIGHT / 2 && bricks[ballY][ballX / 5]) {
bricks[ballY][ballX / 5] = false;
ballDirY = -ballDirY;
}
}
int main() {
srand(time(NULL));
initBricks();
while (true) {
draw();
input();
logic();
Sleep(500);
}
return 0;
}
4 条评论
-
曹莫凡 @ 2025-7-14 14:27:09
666啊!高梓均
-
2025-7-13 17:15:58@
代码可能有问题访问15253201282@163.com邮箱
-
2025-7-13 17:10:52@
#include <iostream 加上> #include <windows.h> #include <conio.h> #include <time.h>
using namespace std;
// 定义游戏区域大小 const int WIDTH = 80; const int HEIGHT = 20;
// 定义挡板信息 int paddleX = WIDTH / 2 - 5; const int PADDLE_WIDTH = 10;
// 定义小球信息 int ballX = WIDTH / 2; int ballY = HEIGHT - 2; int ballDirX = 1; int ballDirY = -1;
// 定义砖块信息 bool bricks[HEIGHT / 2][WIDTH / 5] = {false};
// 初始化砖块 void initBricks() { for (int i = 0; i < HEIGHT / 2; i++) { for (int j = 0; j < WIDTH / 5; j++) { bricks[i][j] = true; } } }
// 绘制游戏界面 void draw() { system("cls");
// 绘制顶部边界 for (int i = 0; i < WIDTH + 2; i++) { cout << "#"; } cout << endl; // 绘制游戏区域 for (int y = 0; y < HEIGHT; y++) { cout << "#"; for (int x = 0; x < WIDTH; x++) { if (x >= paddleX && x < paddleX + PADDLE_WIDTH && y == HEIGHT - 1) { cout << "="; } else if (x == ballX && y == ballY) { cout << "O"; } else if (y < HEIGHT / 2 && bricks[y][x / 5]) { cout << "*"; } else { cout << " "; } } cout << "#" << endl; } // 绘制底部边界 for (int i = 0; i < WIDTH + 2; i++) { cout << "#"; } cout << endl;
}
// 处理用户输入 void input() { if (_kbhit()) { char ch = _getch(); if (ch == 'a' && paddleX > 0) { paddleX--; } else if (ch == 'd' && paddleX + PADDLE_WIDTH < WIDTH) { paddleX++; } } }
// 更新游戏逻辑 void logic() { ballX += ballDirX; ballY += ballDirY;
// 处理小球撞到左右边界 if (ballX == 0 || ballX == WIDTH - 1) { ballDirX = -ballDirX; } // 处理小球撞到顶部边界 if (ballY == 0) { ballDirY = -ballDirY; } // 处理小球撞到挡板 if (ballY == HEIGHT - 2 && ballX >= paddleX && ballX < paddleX + PADDLE_WIDTH) { ballDirY = -ballDirY; } // 处理小球掉落 if (ballY == HEIGHT - 1) { cout << "Game Over!" << endl; exit(0); } // 处理小球撞到砖块 if (ballY < HEIGHT / 2 && bricks[ballY][ballX / 5]) { bricks[ballY][ballX / 5] = false; ballDirY = -ballDirY; }
}
int main() { srand(time(NULL)); initBricks();
while (true) { draw(); input(); logic(); Sleep(500); } return 0;
}
-
2025-7-13 17:00:22@
有问题请私信UP 操作方法: C++代码生成确保是C++O11 编译器版本:C++6.5、C++5.13、C++5.11 输入法确保是英文(全角) W,A,S,D 作者:高梓钧gzjun 邮箱:15253201282@163.com、gzjunde2013@163.com
- 1
信息
- ID
- 116
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- 递交数
- 56
- 已通过
- 9
- 上传者