Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. #include "olcConsoleGameEngine.h"
  7.  
  8. class AsteroidsMain : public olcConsoleGameEngine
  9. {
  10. public:
  11.     AsteroidsMain()
  12.     {
  13.         m_sAppName = L"Asteroids";
  14.     }
  15.  
  16. private:
  17.     struct sSpaceObject {
  18.         float x;
  19.         float y;
  20.         float dx;
  21.         float dy;
  22.         int nSize;
  23.     };
  24.  
  25.     vector<sSpaceObject> vecAsteroids;
  26.  
  27.     virtual bool OnUserCreate() {
  28.  
  29.         vecAsteroids.push_back({20.0f, 20.0f, 8.0, -6.0f, (int)16 });
  30.  
  31.  
  32.         return true;
  33.     }
  34.  
  35.     virtual bool OnUserUpdate(float fElapsedTime) {
  36.  
  37.         // clear console
  38.         Fill(0, 0, ScreenWidth(), ScreenHeight(), PIXEL_SOLID, 0);
  39.  
  40.         for (auto &a : vecAsteroids) {
  41.             a.x += a.dx * fElapsedTime;
  42.             a.y += a.dy * fElapsedTime;
  43.             WrapCoordinates(a.x, a.y, a.x, a.y);
  44.  
  45.             for (int x = 0; x < a.nSize; x++) {
  46.                 for (int y = 0; y < a.nSize; y++) {
  47.                     Draw(a.x + x, a.y + y, PIXEL_QUARTER, FG_RED);
  48.                 }
  49.             }
  50.  
  51.         }
  52.         return true;
  53.     }
  54.  
  55.     void WrapCoordinates(float ix, float iy, float &ox, float &oy) {
  56.         ox = ix;
  57.         oy = oy;
  58.  
  59.         if (ix < 0.0f) ox = ix + (float)ScreenWidth();
  60.         if (ix > (float)ScreenWidth()) ox = ix - (float)ScreenWidth();
  61.  
  62.         if (iy < 0.0f) oy = iy + (float)ScreenHeight();
  63.         if (iy > (float)ScreenHeight()) oy = iy - (float)ScreenHeight();
  64.     }
  65.  
  66.     virtual void Draw(int x, int y, short c = 0x2588, short col = 0x000F)
  67.     {
  68.         float fx, fy;
  69.         WrapCoordinates(x, y, fx, fy);
  70.         olcConsoleGameEngine::Draw(fx, fy, c, col);
  71.     }
  72.  
  73. };
  74.  
  75. int main() {
  76.    
  77.     AsteroidsMain game;
  78.     game.ConstructConsole(160, 100, 8, 8);
  79.     game.Start();
  80.  
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement