我用SFML写了一个打砖块的小游戏,在写声音之前一切正常,证明我的配置和语法都没问题,但是写了一个发声模块之后程序不能工作了,我把发声的逻辑一点点删掉,直到SoundBuffer buffer,也就是第一句话,有他就不能编译,没他就可以,怎么回事



#include <iostream>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <SFML/audio.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
int main()
{
srand(time(0)); // 使用当前时间作为随机数种子
double screenWidth = 576 * 1.7, screenHeight = 324 * 1.7;
RenderWindow app(VideoMode(screenWidth, screenHeight), L"打砖块!"); // 创建窗口
app.setFramerateLimit(60);
Texture t1, t2, t3, t4, t5; // 创建纹理对象
t1.loadFromFile("images/stone.png"); // 加载纹理图片
t2.loadFromFile("images/city/1.png");//背景
t3.loadFromFile("images/balls.png");//球
t4.loadFromFile("images/bat_small.png");//板子
t5.loadFromFile("images/game over.png");//游戏结束
Sprite sBackground(t2), sBall(t3), sPaddle(t4), gameOver(t5); // 创建精灵对象,并将纹理应用到精灵上
sBall.setTextureRect(IntRect(rand()%4*16, 0, 16, 16));
sBackground.setScale(2.0, 2.0);
sPaddle.setScale(1.5, 1.5);
sPaddle.setPosition(screenWidth / 2 - sPaddle.getLocalBounds().width / 2, screenHeight / 10*9 - sPaddle.getLocalBounds().height / 2); // 设置挡板的初始位置
Sprite block[1000]; // 创建方块精灵对象数组
int n = 0;
for (int i = 1; i <= 14; i++) // 创建方块对象,并设置位置
for (int j = 1; j <= 10; j++)
{
block[n].setTexture(t1);
block[n].setTextureRect(IntRect(0,rand() % 7 * 32, 64, 32));
block[n].setPosition(i * 64, j * 32);
n++;
}
float dx = 3, dy = 2;
float x = 300, y = 300;
bool gameState = true;
gameOver.setPosition(screenWidth / 2 - gameOver.getLocalBounds().width / 2, screenHeight / 2 - gameOver.getLocalBounds().height / 2);
SoundBuffer buffer;
while (app.isOpen())
{
Event e;
while (app.pollEvent(e))
{
if (e.type == Event::Closed)
app.close();
}
if (gameState) {
x += dx;
for (int i = 0; i < n; i++) // 检测球与方块的碰撞
if (FloatRect(x + 3, y + 3, 6, 6).intersects(block[i].getGlobalBounds()))
{
block[i].setPosition(-100, 0);
dx = -dx;
}
y += dy;
for (int i = 0; i < n; i++) // 检测球与方块的碰撞
if (FloatRect(x + 3, y + 3, 6, 6).intersects(block[i].getGlobalBounds()))
{
block[i].setPosition(-100, 0); // 将碰撞的方块移出屏幕
dy = -dy; // 球的运动方向取反
}
if (x < 0 || x>screenWidth) dx = -dx; // 球与窗口边界的碰撞检测
if (y < 0) dy = -dy;
if (y > screenHeight) gameState = false;
//if (Keyboard::isKeyPressed(Keyboard::Right)) sPaddle.move(6, 0); // 键盘输入,控制挡板的移动
//if (Keyboard::isKeyPressed(Keyboard::Left)) sPaddle.move(-6, 0);
sPaddle.setPosition(Mouse::getPosition(app).x - sPaddle.getLocalBounds().width / 2, sPaddle.getPosition().y);
if (sBall.getGlobalBounds().intersects(sPaddle.getGlobalBounds())) dy = -(rand() % 5 + 2); // 球与挡板的碰撞检测
}
else {
if (Keyboard::isKeyPressed(Keyboard::P)) {
dx = 3, dy = 2;
x = 300, y = 300;
sBall.setPosition(x, y); // 设置球的位置
sBall.setTextureRect(IntRect(rand() % 4 * 16, 0, 16, 16));
gameState = true;
}
}
sBall.setPosition(x, y); // 设置球的位置
app.clear(); // 清空窗口
app.draw(sBackground); // 绘制背景
app.draw(sBall); // 绘制球
app.draw(sPaddle); // 绘制挡板
if (gameState) {
for (int i = 0; i < n; i++) // 绘制方块
app.draw(block[i]);
}
else
app.draw(gameOver);
app.display(); // 显示窗口内容
}
return 0;
}