【无标题】

这是花费两月半制作的c++小球打砖游戏

制作不易,勿喷~

cpp 复制代码
在这里插入代码片

#include

#include <conio.h>

#include <windows.h>

using namespace std;

bool gameOver;

const int width = 40;

const int height = 20;

int paddleX, ballX, ballY, ballDirX, ballDirY;

int bricks[5][10];

int score;

void Setup() {

gameOver = false;

paddleX = width/2;

ballX = width/2;

ballY = height-2;

ballDirX = -1;

ballDirY = -1;

score = 0;

复制代码
for(int i=0; i<5; i++) {
    for(int j=0; j<10; j++) {
        bricks[i][j] = 1;
    }
}

}

void Draw() {

system("cls");

for(int i=0; i<width+2; i++)

cout << "#";

cout << endl;

复制代码
for(int i=0; i<height; i++) {
    for(int j=0; j<width; j++) {
        if(j == 0) cout << "#";
        
        if(i == ballY && j == ballX) 
            cout << "O";
        else if(i == height-1 && j >= paddleX-2 && j <= paddleX+2)
            cout << "=";
        else {
            bool brick = false;
            for(int k=0; k<5; k++) {
                for(int l=0; l<10; l++) {
                    if(bricks[k][l] && i == k+1 && j >= l*4 && j <= l*4+3) {
                        cout << "[]";
                        j++; // 修正砖块显示错位
                        brick = true;
                    }
                }
            }
            if(!brick) cout << " ";
        }
        
        if(j == width-1) cout << "#";
    }
    cout << endl;
}

for(int i=0; i<width+2; i++)
    cout << "#";
cout << endl;
cout << "Score:" << score << endl;

}

void Input() {

if(_kbhit()) {

char key = _getch();

if(key == 'a' && paddleX > 3) paddleX--;

if(key == 'd' && paddleX < width-3) paddleX++;

if(key == 'x') gameOver = true;

}

}

void Logic() {

ballX += ballDirX;

ballY += ballDirY;

复制代码
if(ballX <= 0 || ballX >= width-1) ballDirX *= -1;
if(ballY <= 0) ballDirY *= -1;

if(ballY == height-2 && ballX >= paddleX-2 && ballX <= paddleX+2)
    ballDirY = -1;

if(ballY >= height) gameOver = true;

for(int i=0; i<5; i++) {
    for(int j=0; j<10; j++) {
        if(bricks[i][j] && ballY == i+1 && 
           ballX >= j*4 && ballX <= j*4+3) {
            bricks[i][j] = 0;
            ballDirY *= -1;
            score += 10;
        }
    }
}

}

int main() {

Setup();

int a;

cout<<"请输入速度";

cin>>a;

while(!gameOver) {

Draw();

Input();

Logic();

Sleep(a);

}

return 0;

}

相关推荐
昇腾CANN1 小时前
TileLang-Ascend 算子性能优化方法与实操
开发语言·javascript·性能优化·昇腾·cann
John_ToDebug1 小时前
WebHostView 与 TabStrip 交互机制深度解析
c++·chrome·windows
AGV算法笔记2 小时前
CVPR 2025 最新感知算法解读:GaussianLSS 如何用 Gaussian Splatting 重构 BEV 表示?
算法·重构·自动驾驶·3d视觉·感知算法·多视角视觉
沐知全栈开发2 小时前
ionic 手势事件详解
开发语言
lsx2024062 小时前
Bootstrap 按钮
开发语言
神仙别闹2 小时前
基于 Python 实现 BERT 的情感分析模型
开发语言·python·bert
禾叙_2 小时前
【langchain4j】结构化输出(六)
java·开发语言
NQBJT2 小时前
VS Code配置Python人工智能开发环境
开发语言·人工智能·vscode·python
byoass2 小时前
智巢AI知识库深度解析:企业文档管理从大海捞针到精准狙击的进化之路
开发语言·网络·人工智能·安全·c#·云计算