Colored blocks

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 61354a8497
commit cd95a546bd
  1. 18
      Block.h
  2. 12
      BlockClump.cpp
  3. 7
      BlockClump.h
  4. 48
      Board.cpp
  5. 29
      Board.h
  6. 5
      C++/scripts/build.sh
  7. 5
      C++/scripts/debug.sh
  8. 8
      C++/scripts/md5
  9. 5
      C++/scripts/release.sh
  10. 5
      C++/scripts/web.sh
  11. BIN
      C++ProjectTemplate
  12. 1
      Meteos.h
  13. BIN
      assets/blocks_test.png
  14. 13
      main.cpp

@ -2,11 +2,25 @@
#define BLOCK_H
#include "pixelGameEngine.h"
enum class BlockColor{
WHITE,
GREEN,
RED,
ORANGE,
BLUE,
PINK,
LIGHT,
DARK,
SOUL,
TIME
};
class Block{
public:
vf2d pos;
BlockColor col;
bool markedForDeletion=false;
Block(vf2d pos)
:pos(pos){}
Block(vf2d pos,BlockColor col)
:pos(pos),col(col){}
};
#endif

@ -4,16 +4,16 @@ std::vector<Block>&BlockClump::getBlocks(){
return blocks;
}
void BlockClump::addBlock(int col){
blocks.push_back(Block({(float)col*12,0}));
void BlockClump::addBlock(int col,BlockColor color){
blocks.push_back(Block({(float)col*12,0},color));
}
void BlockClump::addBlock(int col,int row){
blocks.push_back(Block({(float)col*12,(float)row*12}));
void BlockClump::addBlock(int col,int row,BlockColor color){
blocks.push_back(Block({(float)col*12,(float)row*12},color));
}
void BlockClump::addBlock(vf2d offset){
blocks.push_back(Block(offset));
void BlockClump::addBlock(vf2d offset,BlockColor color){
blocks.push_back(Block(offset,color));
}
vf2d BlockClump::getBlockPosition(Block&b){

@ -3,6 +3,7 @@
#include "pixelGameEngine.h"
class Block;
enum class BlockColor;
class BlockClump{
std::vector<Block>blocks;
@ -12,9 +13,9 @@ class BlockClump{
float y=0;
BlockClump(){}
std::vector<Block>&getBlocks();
void addBlock(int col);
void addBlock(int col,int row);
void addBlock(vf2d offset);
void addBlock(int col,BlockColor color);
void addBlock(int col,int row,BlockColor color);
void addBlock(vf2d offset,BlockColor color);
//Get a block's position relative to this block clump.
vf2d getBlockPosition(Block&b);
};

@ -2,49 +2,37 @@
extern Meteos*game;
Board::Board(vi2d boardSize,float gravity,float spawnRate)
:boardSize(boardSize),gravity(gravity),spawnRate(spawnRate){
Board::Board()
:colorHandler({}){}
Board::Board(vi2d boardSize,float gravity,float spawnRate,std::array<int,10> colorRates,Renderable&tileset)
:boardSize(boardSize),gravity(gravity),spawnRate(spawnRate),colorHandler(colorRates),tileset(tileset.Decal()){
drawOffset={(float)game->ScreenWidth()/2-boardSize.x/2*12,(float)game->ScreenHeight()/2-boardSize.y/2*12};
yBottom=(boardSize.y-1)*12;
for (int i=0;i<boardSize.x;i++) {
cols.push_back(std::vector<Block>());
}
BlockClump c=BlockClump();
c.addBlock(0,0);
c.addBlock(1,0);
c.addBlock(2,0);
c.vspeed=-20;
c.y=yBottom-12;
BlockClump c2=BlockClump();
c2.addBlock(0,0);
c2.addBlock(1,0);
c2.addBlock(1,-1);
c2.addBlock(2,0);
c2.vspeed=20;
c2.y=24;
clumps.push_back(c);
clumps.push_back(c2);
}
void Board::spawnBlock(int col){
BlockClump c=BlockClump();
c.addBlock(col);
c.addBlock(col,colorHandler.getRandomColor());
for(int counter=1;game->coinFlip(game->gen)==0;counter++) {
c.addBlock(col,-counter);
c.addBlock(col,-counter,colorHandler.getRandomColor());
}
if (game->coinFlip(game->gen)==0) {
if (col>0) {
c.addBlock(col-1);
c.addBlock(col-1,colorHandler.getRandomColor());
for(int counter=1;game->coinFlip(game->gen)==0;counter++) {
c.addBlock(col-1,-counter);
c.addBlock(col-1,-counter,colorHandler.getRandomColor());
}
}
}
if (game->coinFlip(game->gen)==0) {
if (col<boardSize.x-1) {
c.addBlock(col+1);
c.addBlock(col+1,colorHandler.getRandomColor());
for(int counter=1;game->coinFlip(game->gen)==0;counter++) {
c.addBlock(col+1,-counter);
c.addBlock(col+1,-counter,colorHandler.getRandomColor());
}
}
}
@ -84,7 +72,7 @@ void Board::convertClump(int ind){
Block&b2=c.getBlocks()[i];
if (c.getBlockPosition(b).x==b2.pos.x) {
b2.markedForDeletion=true;
c2.addBlock(b2.pos);
c2.addBlock(b2.pos,b2.col);
}
c2.y=c.y;
}
@ -95,4 +83,16 @@ void Board::convertClump(int ind){
}
}
removeClump(ind);
}
BlockColor ColorDistributor::getRandomColor(){
int val=range(game->gen);
for (int i=0;i<10;i++) {
if (rangeTable[i].first!=-1&&
rangeTable[i].first<=val&&
rangeTable[i].second>val) {
return (BlockColor)i;
}
}
return BlockColor::WHITE;
}

@ -5,17 +5,42 @@
class Block;
class BlockClump;
enum class BlockColor;
class ColorDistributor{
std::array<std::pair<int,int>,10>rangeTable;
int maxRange=0;
std::uniform_int_distribution<>range;
public:
ColorDistributor(std::array<int,10>colorRates){
int sum=0;
for (int i=0;i<colorRates.size();i++) {
if (colorRates[i]>0) {
rangeTable[i]={sum,sum+colorRates[i]};
sum+=colorRates[i];
maxRange=sum;
} else {
rangeTable[i]={-1,-1};
}
}
range=std::uniform_int_distribution<>(0, maxRange);
}
BlockColor getRandomColor();
};
class Board{
std::vector<std::vector<Block>> cols;
std::vector<BlockClump> clumps;
public:
ColorDistributor colorHandler;
float yBottom;
Decal*tileset;
vf2d drawOffset;
vi2d boardSize;
float gravity;
float spawnRate;
Board(){}
Board(vi2d boardSize,float gravity,float spawnRate);
Board();
Board(vi2d boardSize,float gravity,float spawnRate,std::array<int,10> colorRates,Renderable&tileset);
void spawnBlock(int col);
std::vector<Block>&getBlocks(int col);
std::vector<BlockClump>&getBlockClumps();

@ -1,6 +1,11 @@
#Compiles the entire program then runs it, producing an executable. If the "test" argument is included, will try and run tests too (in the test folder)
#C++
printf "Running program...\n\n\n"
output=$(dpkg -l | grep libx11-dev)
if [[ -z $output ]]
then
sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev
fi
if [ "$1" = "test" ]
then
printf "Running tests...\n"

@ -1,6 +1,11 @@
#Compiles the entire program with debug flags then runs it in gdb. If the "test" argument is included, will try and run tests too (in the test folder)
#C++
printf "Running program...\n\n\n"
output=$(dpkg -l | grep libx11-dev)
if [[ -z $output ]]
then
sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev
fi
if [ "$1" = "test" ]
then
printf "Running tests...\n"

@ -1,7 +1,7 @@
build.sh:6fbec04ed16be42bab846f1eed603133 -
build.sh:d5e4e98a6f677aa0a9759ba0afb2fb77 -
commit.sh:d03a46e721060c22ccb146e19d27e70a -
debug.sh:7f57c6640be5f262988961c3b45dce97 -
debug.sh:849488515cab075948653c15eec4177b -
lines.sh:3b907786f7fc9204025993016c9080de -
release.sh:0a525311cc14b9c8aefc6f2b816129a1 -
release.sh:6a96fb84ba64ed60d31be436ec069f05 -
temp:d41d8cd98f00b204e9800998ecf8427e -
web.sh:96f2c316536011a3defac50aecae487d -
web.sh:b982212b88b80a59ad607b47d2ff3e94 -

@ -1,6 +1,11 @@
#Creates a release build that focuses on high runtime performance.
#C++
printf "Running program...\n\n\n"
output=$(dpkg -l | grep libx11-dev)
if [[ -z $output ]]
then
sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev
fi
if g++ $(find . -type f -name "*.cpp" -not -path "./test/*") ${CUSTOM_PARAMS} -O3 -s -DNDEBUG -o ${PROJECT_NAME}; then
./${PROJECT_NAME} "$@"
fi

@ -1,5 +1,10 @@
#Compiles emscripten instance of this project for the web.
#C++
output=$(dpkg -l | grep libx11-dev)
if [[ -z $output ]]
then
sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev
fi
if [ -d "assets" ]; then
em++ -std=c++17 -O2 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 -s USE_SDL_MIXER=2 -s USE_LIBPNG=1 $(find . -type f -name "*.cpp" -not -path "./test/*") -o ${PROJECT_NAME}.html -I pixelGameEngine.h --preload-file ./assets
else

Binary file not shown.

@ -13,6 +13,7 @@ class Meteos : public olc::PixelGameEngine{
std::uniform_int_distribution<> randBlockPos,coinFlip;
std::mt19937 gen;
Board gameBoard;
std::map<std::string,Renderable> SPRITES;
bool OnUserCreate()override;
bool OnUserUpdate(float fElapsedTime)override;
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 759 B

@ -9,12 +9,15 @@ bool Meteos::OnUserCreate()
{
game=this;
SPRITES["blocks_test.png"].Load("assets/blocks_test.png");
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);
coinFlip=std::uniform_int_distribution<>(0, 1);
gameBoard=Board({10,14},12.f,3.0f);
gameBoard=Board({10,14},12.f,3.0f,{3,3,2,3,2,3,0,0,0,0},SPRITES["blocks_test.png"]);
return true;
}
@ -57,7 +60,7 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
//Copy every block from one clump to the other
for (int m=0;m<c2.getBlocks().size();m++) {
Block&b4=c2.getBlocks()[m];
c.addBlock(b4.pos.x/12,(c2.getBlockPosition(b4).y-c.y)/12);
c.addBlock(b4.pos.x/12,(c2.getBlockPosition(b4).y-c.y)/12,b4.col);
}
if (c.vspeed>0) {
c.vspeed/=4;
@ -92,17 +95,17 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
goto nextClump;
}
}
c.y+=c.vspeed*fElapsedTime;
for (int j=0;j<c.getBlocks().size();j++){
Block&b=c.getBlocks()[j];
c.y+=c.vspeed*fElapsedTime;
FillRectDecal(c.getBlockPosition(b)+gameBoard.drawOffset,{12,12},DARK_GREEN);
DrawPartialDecal(c.getBlockPosition(b)+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12});
}
nextClump:;
}
for (int i=0;i<gameBoard.boardSize.x;i++){
for (int y=0;y<gameBoard.getBlocks(i).size();y++){
Block&b=gameBoard.getBlocks(i)[y];
FillRectDecal(b.pos+gameBoard.drawOffset,{12,12},GREEN);
DrawPartialDecal(b.pos+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12});
}
}
return true;

Loading…
Cancel
Save