#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;
int main()
{
sf::Vector2f rightBoundary(430.0f, 440.0f);
sf::Vector2f leftBoundary(0.0f, 440.0f);
int endGame = 0;
srand(time(0));
RenderWindow app(VideoMode(520, 450), "Arkanoid!");
app.setFramerateLimit(60);
Texture t1, t2, t3, t4, t5, t6;
String s1 = "images/block0" + std::to_string((rand() % 5) + 1) + ".png";
t1.loadFromFile(s1);
t2.loadFromFile("images/background.jpg");
t3.loadFromFile("images/ball.png");
t4.loadFromFile("images/paddle.png");
t5.loadFromFile("images/congrats.jpg");
t6.loadFromFile("images/lost.png");
Sprite sBackground(t2), sBall(t3), sPaddle(t4), scongrats(t5),slost(t6);
sPaddle.setPosition(400, 440);
Sprite block[100];
int n = 0;
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++)
{
if (i == j || i + j == 11)
continue;
block[n].setTexture(t1);
block[n].setPosition(i * 43, j * 20);
n++;
}
float dx = 6, dy = 5;
float x = 300, y = 300;
while (app.isOpen())
{
Event e;
while (app.pollEvent(e))
{
if (e.type == Event::Closed)
app.close();
}
if (endGame == n)
{
app.clear();
app.draw(scongrats);
app.display();
}
else if(y >= 440){
app.clear();
app.draw(slost);
app.display();
}
else
{
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;
endGame++;
}
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;
endGame++;
}
if (x < 0 || x > 520)
dx = -dx;
if (y < 0 || y > 450)
dy = -dy;
if (Keyboard::isKeyPressed(Keyboard::Right) && sPaddle.getPosition() != rightBoundary)
sPaddle.move(10, 0);
if (Keyboard::isKeyPressed(Keyboard::Left) && sPaddle.getPosition() != leftBoundary)
sPaddle.move(-10, 0);
if (FloatRect(x, y, 12, 12).intersects(sPaddle.getGlobalBounds()))
dy = -(rand() % 5 + 2);
sBall.setPosition(x, y);
app.clear();
app.draw(sBackground);
app.draw(sBall);
app.draw(sPaddle);
for (int i = 0; i < n; i++)
app.draw(block[i]);
app.display();
}
}
return 0;
}
0 Comments