|
| 1 | +#include <SFML/Graphics.hpp> |
| 2 | +#include <time.h> |
| 3 | +#include <list> |
| 4 | +using namespace sf; |
| 5 | + |
| 6 | +const int W = 1200; |
| 7 | +const int H = 800; |
| 8 | + |
| 9 | +float DEGTORAD = 0.017453f; |
| 10 | + |
| 11 | +class Animation |
| 12 | +{ |
| 13 | +public: |
| 14 | + float Frame, speed; |
| 15 | + Sprite sprite; |
| 16 | + std::vector<IntRect> frames; |
| 17 | + |
| 18 | + Animation(){} |
| 19 | + |
| 20 | + Animation (Texture &t, int x, int y, int w, int h, int count, float Speed) |
| 21 | + { |
| 22 | + Frame = 0; |
| 23 | + speed = Speed; |
| 24 | + |
| 25 | + for (int i=0;i<count;i++) |
| 26 | + frames.push_back( IntRect(x+i*w, y, w, h) ); |
| 27 | + |
| 28 | + sprite.setTexture(t); |
| 29 | + sprite.setOrigin(w/2,h/2); |
| 30 | + sprite.setTextureRect(frames[0]); |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + void update() |
| 35 | + { |
| 36 | + Frame += speed; |
| 37 | + int n = frames.size(); |
| 38 | + if (Frame >= n) Frame -= n; |
| 39 | + if (n>0) sprite.setTextureRect( frames[int(Frame)] ); |
| 40 | + } |
| 41 | + |
| 42 | + bool isEnd() |
| 43 | + { |
| 44 | + return Frame+speed>=frames.size(); |
| 45 | + } |
| 46 | + |
| 47 | +}; |
| 48 | + |
| 49 | + |
| 50 | +class Entity |
| 51 | +{ |
| 52 | +public: |
| 53 | +float x,y,dx,dy,R,angle; |
| 54 | +bool life; |
| 55 | +std::string name; |
| 56 | +Animation anim; |
| 57 | + |
| 58 | +Entity() |
| 59 | +{ |
| 60 | + life=1; |
| 61 | +} |
| 62 | + |
| 63 | +void settings(Animation &a,int X,int Y,float Angle=0,int radius=1) |
| 64 | +{ |
| 65 | + anim = a; |
| 66 | + x=X; y=Y; |
| 67 | + angle = Angle; |
| 68 | + R = radius; |
| 69 | +} |
| 70 | + |
| 71 | +virtual void update(){}; |
| 72 | + |
| 73 | +void draw(RenderWindow &app) |
| 74 | +{ |
| 75 | + anim.sprite.setPosition(x,y); |
| 76 | + anim.sprite.setRotation(angle+90); |
| 77 | + app.draw(anim.sprite); |
| 78 | + |
| 79 | + CircleShape circle(R); |
| 80 | + circle.setFillColor(Color(255,0,0,170)); |
| 81 | + circle.setPosition(x,y); |
| 82 | + circle.setOrigin(R,R); |
| 83 | + //app.draw(circle); |
| 84 | +} |
| 85 | + |
| 86 | +virtual ~Entity(){}; |
| 87 | +}; |
| 88 | + |
| 89 | + |
| 90 | +class asteroid: public Entity |
| 91 | +{ |
| 92 | +public: |
| 93 | + asteroid() |
| 94 | + { |
| 95 | + dx=rand()%8-4; |
| 96 | + dy=rand()%8-4; |
| 97 | + name="asteroid"; |
| 98 | + } |
| 99 | + |
| 100 | +void update() |
| 101 | + { |
| 102 | + x+=dx; |
| 103 | + y+=dy; |
| 104 | + |
| 105 | + if (x>W) x=0; if (x<0) x=W; |
| 106 | + if (y>H) y=0; if (y<0) y=H; |
| 107 | + } |
| 108 | + |
| 109 | +}; |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +class bullet: public Entity |
| 114 | +{ |
| 115 | +public: |
| 116 | + bullet() |
| 117 | + { |
| 118 | + name="bullet"; |
| 119 | + } |
| 120 | + |
| 121 | +void update() |
| 122 | + { |
| 123 | + dx=cos(angle*DEGTORAD)*6; |
| 124 | + dy=sin(angle*DEGTORAD)*6; |
| 125 | + // angle+=rand()%6-3; |
| 126 | + x+=dx; |
| 127 | + y+=dy; |
| 128 | + |
| 129 | + if (x>W || x<0 || y>H || y<0) life=0; |
| 130 | + } |
| 131 | + |
| 132 | +}; |
| 133 | + |
| 134 | + |
| 135 | +class player: public Entity |
| 136 | +{ |
| 137 | +public: |
| 138 | + bool thrust; |
| 139 | + |
| 140 | + player() |
| 141 | + { |
| 142 | + name="player"; |
| 143 | + } |
| 144 | + |
| 145 | + void update() |
| 146 | + { |
| 147 | + if (thrust) |
| 148 | + { dx+=cos(angle*DEGTORAD)*0.2; |
| 149 | + dy+=sin(angle*DEGTORAD)*0.2; } |
| 150 | + else |
| 151 | + { dx*=0.99; |
| 152 | + dy*=0.99; } |
| 153 | + |
| 154 | + int maxSpeed=15; |
| 155 | + float speed = sqrt(dx*dx+dy*dy); |
| 156 | + if (speed>maxSpeed) |
| 157 | + { dx *= maxSpeed/speed; |
| 158 | + dy *= maxSpeed/speed; } |
| 159 | + |
| 160 | + x+=dx; |
| 161 | + y+=dy; |
| 162 | + |
| 163 | + if (x>W) x=0; if (x<0) x=W; |
| 164 | + if (y>H) y=0; if (y<0) y=H; |
| 165 | + } |
| 166 | + |
| 167 | +}; |
| 168 | + |
| 169 | + |
| 170 | +bool isCollide(Entity *a,Entity *b) |
| 171 | +{ |
| 172 | + return (b->x - a->x)*(b->x - a->x)+ |
| 173 | + (b->y - a->y)*(b->y - a->y)< |
| 174 | + (a->R + b->R)*(a->R + b->R); |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +int main() |
| 179 | +{ |
| 180 | + srand(time(0)); |
| 181 | + |
| 182 | + RenderWindow app(VideoMode(W, H), "Asteroids!"); |
| 183 | + app.setFramerateLimit(60); |
| 184 | + |
| 185 | + Texture t1,t2,t3,t4,t5,t6,t7; |
| 186 | + t1.loadFromFile("images/spaceship.png"); |
| 187 | + t2.loadFromFile("images/background.jpg"); |
| 188 | + t3.loadFromFile("images/explosions/type_C.png"); |
| 189 | + t4.loadFromFile("images/rock.png"); |
| 190 | + t5.loadFromFile("images/fire_blue.png"); |
| 191 | + t6.loadFromFile("images/rock_small.png"); |
| 192 | + t7.loadFromFile("images/explosions/type_B.png"); |
| 193 | + |
| 194 | + t1.setSmooth(true); |
| 195 | + t2.setSmooth(true); |
| 196 | + |
| 197 | + Sprite background(t2); |
| 198 | + |
| 199 | + Animation sExplosion(t3, 0,0,256,256, 48, 0.5); |
| 200 | + Animation sRock(t4, 0,0,64,64, 16, 0.2); |
| 201 | + Animation sRock_small(t6, 0,0,64,64, 16, 0.2); |
| 202 | + Animation sBullet(t5, 0,0,32,64, 16, 0.8); |
| 203 | + Animation sPlayer(t1, 40,0,40,40, 1, 0); |
| 204 | + Animation sPlayer_go(t1, 40,40,40,40, 1, 0); |
| 205 | + Animation sExplosion_ship(t7, 0,0,192,192, 64, 0.5); |
| 206 | + |
| 207 | + |
| 208 | + std::list<Entity*> entities; |
| 209 | + |
| 210 | + for(int i=0;i<15;i++) |
| 211 | + { |
| 212 | + asteroid *a = new asteroid(); |
| 213 | + a->settings(sRock, rand()%W, rand()%H, rand()%360, 25); |
| 214 | + entities.push_back(a); |
| 215 | + } |
| 216 | + |
| 217 | + player *p = new player(); |
| 218 | + p->settings(sPlayer,200,200,0,20); |
| 219 | + entities.push_back(p); |
| 220 | + |
| 221 | + /////main loop///// |
| 222 | + while (app.isOpen()) |
| 223 | + { |
| 224 | + Event event; |
| 225 | + while (app.pollEvent(event)) |
| 226 | + { |
| 227 | + if (event.type == Event::Closed) |
| 228 | + app.close(); |
| 229 | + |
| 230 | + if (event.type == Event::KeyPressed) |
| 231 | + if (event.key.code == Keyboard::Space) |
| 232 | + { |
| 233 | + bullet *b = new bullet(); |
| 234 | + b->settings(sBullet,p->x,p->y,p->angle,10); |
| 235 | + entities.push_back(b); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + if (Keyboard::isKeyPressed(Keyboard::Right)) p->angle+=3; |
| 240 | + if (Keyboard::isKeyPressed(Keyboard::Left)) p->angle-=3; |
| 241 | + if (Keyboard::isKeyPressed(Keyboard::Up)) p->thrust=true; |
| 242 | + else p->thrust=false; |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | + for(auto a:entities) |
| 247 | + for(auto b:entities) |
| 248 | + { |
| 249 | + if (a->name=="asteroid" && b->name=="bullet") |
| 250 | + if ( isCollide(a,b) ) |
| 251 | + { |
| 252 | + a->life=false; |
| 253 | + b->life=false; |
| 254 | + |
| 255 | + Entity *e = new Entity(); |
| 256 | + e->settings(sExplosion,a->x,a->y); |
| 257 | + e->name="explosion"; |
| 258 | + entities.push_back(e); |
| 259 | + |
| 260 | + |
| 261 | + for(int i=0;i<2;i++) |
| 262 | + { |
| 263 | + if (a->R==15) continue; |
| 264 | + Entity *e = new asteroid(); |
| 265 | + e->settings(sRock_small,a->x,a->y,rand()%360,15); |
| 266 | + entities.push_back(e); |
| 267 | + } |
| 268 | + |
| 269 | + } |
| 270 | + |
| 271 | + if (a->name=="player" && b->name=="asteroid") |
| 272 | + if ( isCollide(a,b) ) |
| 273 | + { |
| 274 | + b->life=false; |
| 275 | + |
| 276 | + Entity *e = new Entity(); |
| 277 | + e->settings(sExplosion_ship,a->x,a->y); |
| 278 | + e->name="explosion"; |
| 279 | + entities.push_back(e); |
| 280 | + |
| 281 | + p->settings(sPlayer,W/2,H/2,0,20); |
| 282 | + p->dx=0; p->dy=0; |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + |
| 287 | + if (p->thrust) p->anim = sPlayer_go; |
| 288 | + else p->anim = sPlayer; |
| 289 | + |
| 290 | + |
| 291 | + for(auto e:entities) |
| 292 | + if (e->name=="explosion") |
| 293 | + if (e->anim.isEnd()) e->life=0; |
| 294 | + |
| 295 | + if (rand()%150==0) |
| 296 | + { |
| 297 | + asteroid *a = new asteroid(); |
| 298 | + a->settings(sRock, 0,rand()%H, rand()%360, 25); |
| 299 | + entities.push_back(a); |
| 300 | + } |
| 301 | + |
| 302 | + for(auto i=entities.begin();i!=entities.end();) |
| 303 | + { |
| 304 | + Entity *e = *i; |
| 305 | + |
| 306 | + e->update(); |
| 307 | + e->anim.update(); |
| 308 | + |
| 309 | + if (e->life==false) {i=entities.erase(i); delete e;} |
| 310 | + else i++; |
| 311 | + } |
| 312 | + |
| 313 | + |
| 314 | + |
| 315 | + //////draw////// |
| 316 | + app.draw(background); |
| 317 | + |
| 318 | + for(auto i:entities) |
| 319 | + i->draw(app); |
| 320 | + |
| 321 | + app.display(); |
| 322 | + } |
| 323 | + |
| 324 | + return 0; |
| 325 | +} |
0 commit comments