Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- #include "olcConsoleGameEngine.h"
- class AsteroidsMain : public olcConsoleGameEngine
- {
- public:
- AsteroidsMain()
- {
- m_sAppName = L"Asteroids";
- }
- private:
- struct sSpaceObject {
- float x;
- float y;
- float dx;
- float dy;
- int nSize;
- };
- vector<sSpaceObject> vecAsteroids;
- virtual bool OnUserCreate() {
- vecAsteroids.push_back({20.0f, 20.0f, 8.0, -6.0f, (int)16 });
- return true;
- }
- virtual bool OnUserUpdate(float fElapsedTime) {
- // clear console
- Fill(0, 0, ScreenWidth(), ScreenHeight(), PIXEL_SOLID, 0);
- for (auto &a : vecAsteroids) {
- a.x += a.dx * fElapsedTime;
- a.y += a.dy * fElapsedTime;
- WrapCoordinates(a.x, a.y, a.x, a.y);
- for (int x = 0; x < a.nSize; x++) {
- for (int y = 0; y < a.nSize; y++) {
- Draw(a.x + x, a.y + y, PIXEL_QUARTER, FG_RED);
- }
- }
- }
- return true;
- }
- void WrapCoordinates(float ix, float iy, float &ox, float &oy) {
- ox = ix;
- oy = oy;
- if (ix < 0.0f) ox = ix + (float)ScreenWidth();
- if (ix > (float)ScreenWidth()) ox = ix - (float)ScreenWidth();
- if (iy < 0.0f) oy = iy + (float)ScreenHeight();
- if (iy > (float)ScreenHeight()) oy = iy - (float)ScreenHeight();
- }
- virtual void Draw(int x, int y, short c = 0x2588, short col = 0x000F)
- {
- float fx, fy;
- WrapCoordinates(x, y, fx, fy);
- olcConsoleGameEngine::Draw(fx, fy, c, col);
- }
- };
- int main() {
- AsteroidsMain game;
- game.ConstructConsole(160, 100, 8, 8);
- game.Start();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement