/
main.cpp
77 lines (58 loc) · 1.94 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "config.h"
#include "structures.hpp"
#include "collision.hpp"
sf::Color brickColors[] {sf::Color::Red, sf::Color::Green, sf::Color::Blue, sf::Color::Yellow};
int main()
{
std::cout << appName << " Version " << Arkanoid11_VERSION_MAJOR << "." << Arkanoid11_VERSION_MINOR << std::endl;
// setup game objects
Ball ball{windowWidth / 2, windowHeight / 2};
Paddle paddle{windowWidth / 2, windowHeight - paddleHeight * 2};
std::vector<Brick> bricks;
bricks.reserve(countBlocksX * countBlocksY);
for(int ix{0}; ix < countBlocksX; ++ix)
for(int iy{0}; iy < countBlocksY; ++iy)
bricks.emplace_back( (ix + 1) * (brickWidth + 4) + 20,
(iy + 1) * (brickHeight + 4),
brickColors[iy / 2]);
sf::VideoMode vmode(windowWidth, windowHeight);
sf::RenderWindow window(vmode, appName);
window.setFramerateLimit(60);
while(window.isOpen())
{
window.clear();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
window.close();
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
}
// update
ball.update();
paddle.update();
testCollision(paddle, ball);
for(auto& brick : bricks)
testCollision(brick, ball);
// remove all destroyed bricks
bricks.erase(std::remove_if(std::begin(bricks), std::end(bricks),
[](const Brick& brick) { return brick.destroyed; }),
std::end(bricks));
// winning conditions
bool won = std::all_of(std::begin(bricks), std::end(bricks),
[](const Brick& brick) { return brick.destroyed; });
if(won)
window.close();
// Draw
window.draw(paddle.shape);
window.draw(ball.shape);
for(auto& brick : bricks)
window.draw(brick.shape);
window.display();
}
return 0;
}