-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
127 lines (109 loc) · 3.37 KB
/
main.cpp
File metadata and controls
127 lines (109 loc) · 3.37 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <SFML/Graphics.hpp>
#include <math.h>
#include "inc/Spaceship.h"
#include "inc/Animation.h"
#include "inc/Entity.h"
#include "inc/Asteroid.h"
#include "inc/Explosion.h"
#include <list>
#include <iostream>
const int WIDTH = 1200;
const int HEIGHT = 800;
Spaceship *spaceship = new Spaceship("../images/spaceship.png", WIDTH - 10, HEIGHT - 10, 15);
std::list<Entity *> entities;
void createAsteroid()
{
entities.push_back(new Asteroid(rand() % WIDTH, rand() % HEIGHT, WIDTH, HEIGHT));
}
void handleInputEvents()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
spaceship->incAngle();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
spaceship->decAngle();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && spaceship->availableToShot())
entities.push_back(spaceship->shoot());
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
spaceship->throttleFull();
else
spaceship->throttleRelease();
}
int main()
{
// Create a window
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Asteroids");
window.setFramerateLimit(60);
sf::Texture tBackground, tExplosion;
tBackground.loadFromFile("../images/background.jpg");
tExplosion.loadFromFile("../images/explosions/type_C.png");
sf::Sprite sBackground(tBackground), sExplosion(tExplosion);
entities.push_back(spaceship);
for (int i = 0; i < 15; i++)
createAsteroid();
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
handleInputEvents();
window.clear();
window.draw(sBackground);
for (auto a : entities)
{
for (auto b : entities)
{
if (a->name == "bullet" && b->name == "asteroid")
{
if (a->collision(b))
{
a->life = false;
b->life = false;
entities.push_back(new Explosion(tExplosion, sExplosion, a->x, b->y));
}
}
}
}
for (auto i = entities.begin(); i != entities.end();)
{
Entity *entity = *i;
if (!entity->life)
{
if (entity->name == "asteroid")
createAsteroid();
i = entities.erase(i);
}
else
i++;
}
for (auto ent : entities)
{
if (ent->name == std::string("spaceship"))
{
Spaceship *spaceship = (Spaceship *)ent;
window.draw(spaceship->draw());
}
if (ent->name == std::string("bullet"))
{
Bullet *bullet = (Bullet *)ent;
window.draw(bullet->draw());
}
if (ent->name == std::string("asteroid"))
{
Asteroid *asteroid = (Asteroid *)ent;
window.draw(asteroid->draw());
}
if (ent->name == std::string("explosion"))
{
Explosion *explosion = (Explosion *)ent;
window.draw(explosion->draw());
}
}
spaceship->cadenceDown();
window.display();
}
return 0;
}