Draw basic board and setup classes

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent e77690f7e0
commit 0a5ebfd622
  1. 11
      Block.h
  2. 9
      Board.cpp
  3. 13
      Board.h
  4. BIN
      C++ProjectTemplate
  5. 21
      Meteos.h
  6. 56
      main.cpp
  7. 2
      pixelGameEngine.h

@ -0,0 +1,11 @@
#ifndef BLOCK_H
#define BLOCK_H
#include "pixelGameEngine.h"
class Block{
public:
vf2d pos;
Block(vf2d pos)
:pos(pos){}
};
#endif

@ -0,0 +1,9 @@
#include "Board.h"
#include "Meteos.h"
extern Meteos*game;
Board::Board(vi2d boardSize,float gravity)
:boardSize(boardSize),gravity(gravity){
drawOffset={(float)game->ScreenWidth()/2-boardSize.x/2*12,(float)game->ScreenHeight()/2-boardSize.y/2*12};
}

@ -0,0 +1,13 @@
#ifndef BOARD_H
#define BOARD_H
#include "pixelGameEngine.h"
class Board{
public:
vf2d drawOffset;
vi2d boardSize;
float gravity;
Board(){}
Board(vi2d boardSize,float gravity);
};
#endif

Binary file not shown.

@ -0,0 +1,21 @@
#ifndef METEOS_H
#define METEOS_H
#include "pixelGameEngine.h"
#include "Block.h"
#include "Board.h"
#include <random>
class Meteos : public olc::PixelGameEngine{
public:
Meteos(){
sAppName="Meteos";
}
std::vector<Block> blocks;
float lastBlockSpawn=0.0f;
std::uniform_int_distribution<> randBlockPos;
std::mt19937 gen;
Board gameBoard;
bool OnUserCreate()override;
bool OnUserUpdate(float fElapsedTime)override;
};
#endif

@ -1,31 +1,49 @@
#define OLC_PGE_APPLICATION #define OLC_PGE_APPLICATION
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include "Meteos.h"
#include <random>
class Meteos : public olc::PixelGameEngine Meteos*game;
bool Meteos::OnUserCreate()
{ {
public: game=this;
Meteos()
{
sAppName = "Meteos";
}
bool OnUserCreate() override std::random_device rd; //Will be used to obtain a seed for the random number engine
{ gen=std::mt19937(rd()); //Standard mersenne_twister_engine seeded with rd()
randBlockPos=std::uniform_int_distribution<>(0, 9);
return true;
} gameBoard=Board({10,14},0.1f);
return true;
}
bool OnUserUpdate(float fElapsedTime) override bool Meteos::OnUserUpdate(float fElapsedTime)
{ {
lastBlockSpawn+=fElapsedTime;
return true; if (lastBlockSpawn>=5.0f){
lastBlockSpawn-=5.0f;
blocks.push_back(Block({(float)randBlockPos(gen)*12,0}));
}
Clear(Pixel(32,32,255));
for (int x=-1;x<=gameBoard.boardSize.x;x++) {
for (int y=0;y<=gameBoard.boardSize.y;y++) {
if (x==-1||x==10||y==14) {
FillRectDecal({(float)(gameBoard.drawOffset.x+x*12),(float)(gameBoard.drawOffset.y+y*12)},{12,12},Pixel(0,0,0,255));
} else {
DrawRectDecal({(float)(gameBoard.drawOffset.x+x*12),(float)(gameBoard.drawOffset.y+y*12)},{12,12},Pixel(255,255,255,64));
}
}
}
for (Block&b:blocks) {
FillRectDecal(b.pos+gameBoard.drawOffset,{12,12},GREEN);
} }
}; return true;
}
int main() int main()
{ {
Meteos game; Meteos instance;
if (game.Construct(256, 240, 4, 4)) if (instance.Construct(256, 240, 4, 4))
game.Start(); instance.Start();
return 0; return 0;
} }

@ -6243,3 +6243,5 @@ namespace olc
// | END OF OLC_PGE_APPLICATION | // | END OF OLC_PGE_APPLICATION |
// O------------------------------------------------------------------------------O // O------------------------------------------------------------------------------O
using namespace olc;

Loading…
Cancel
Save