generated from sigonasr2/CPlusPlusProjectTemplate
Colored blocks
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
61354a8497
commit
cd95a546bd
18
Block.h
18
Block.h
@ -2,11 +2,25 @@
|
|||||||
#define BLOCK_H
|
#define BLOCK_H
|
||||||
#include "pixelGameEngine.h"
|
#include "pixelGameEngine.h"
|
||||||
|
|
||||||
|
enum class BlockColor{
|
||||||
|
WHITE,
|
||||||
|
GREEN,
|
||||||
|
RED,
|
||||||
|
ORANGE,
|
||||||
|
BLUE,
|
||||||
|
PINK,
|
||||||
|
LIGHT,
|
||||||
|
DARK,
|
||||||
|
SOUL,
|
||||||
|
TIME
|
||||||
|
};
|
||||||
|
|
||||||
class Block{
|
class Block{
|
||||||
public:
|
public:
|
||||||
vf2d pos;
|
vf2d pos;
|
||||||
|
BlockColor col;
|
||||||
bool markedForDeletion=false;
|
bool markedForDeletion=false;
|
||||||
Block(vf2d pos)
|
Block(vf2d pos,BlockColor col)
|
||||||
:pos(pos){}
|
:pos(pos),col(col){}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
@ -4,16 +4,16 @@ std::vector<Block>&BlockClump::getBlocks(){
|
|||||||
return blocks;
|
return blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BlockClump::addBlock(int col){
|
void BlockClump::addBlock(int col,BlockColor color){
|
||||||
blocks.push_back(Block({(float)col*12,0}));
|
blocks.push_back(Block({(float)col*12,0},color));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BlockClump::addBlock(int col,int row){
|
void BlockClump::addBlock(int col,int row,BlockColor color){
|
||||||
blocks.push_back(Block({(float)col*12,(float)row*12}));
|
blocks.push_back(Block({(float)col*12,(float)row*12},color));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BlockClump::addBlock(vf2d offset){
|
void BlockClump::addBlock(vf2d offset,BlockColor color){
|
||||||
blocks.push_back(Block(offset));
|
blocks.push_back(Block(offset,color));
|
||||||
}
|
}
|
||||||
|
|
||||||
vf2d BlockClump::getBlockPosition(Block&b){
|
vf2d BlockClump::getBlockPosition(Block&b){
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include "pixelGameEngine.h"
|
#include "pixelGameEngine.h"
|
||||||
|
|
||||||
class Block;
|
class Block;
|
||||||
|
enum class BlockColor;
|
||||||
|
|
||||||
class BlockClump{
|
class BlockClump{
|
||||||
std::vector<Block>blocks;
|
std::vector<Block>blocks;
|
||||||
@ -12,9 +13,9 @@ class BlockClump{
|
|||||||
float y=0;
|
float y=0;
|
||||||
BlockClump(){}
|
BlockClump(){}
|
||||||
std::vector<Block>&getBlocks();
|
std::vector<Block>&getBlocks();
|
||||||
void addBlock(int col);
|
void addBlock(int col,BlockColor color);
|
||||||
void addBlock(int col,int row);
|
void addBlock(int col,int row,BlockColor color);
|
||||||
void addBlock(vf2d offset);
|
void addBlock(vf2d offset,BlockColor color);
|
||||||
//Get a block's position relative to this block clump.
|
//Get a block's position relative to this block clump.
|
||||||
vf2d getBlockPosition(Block&b);
|
vf2d getBlockPosition(Block&b);
|
||||||
};
|
};
|
||||||
|
48
Board.cpp
48
Board.cpp
@ -2,49 +2,37 @@
|
|||||||
|
|
||||||
extern Meteos*game;
|
extern Meteos*game;
|
||||||
|
|
||||||
Board::Board(vi2d boardSize,float gravity,float spawnRate)
|
Board::Board()
|
||||||
:boardSize(boardSize),gravity(gravity),spawnRate(spawnRate){
|
: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};
|
drawOffset={(float)game->ScreenWidth()/2-boardSize.x/2*12,(float)game->ScreenHeight()/2-boardSize.y/2*12};
|
||||||
yBottom=(boardSize.y-1)*12;
|
yBottom=(boardSize.y-1)*12;
|
||||||
for (int i=0;i<boardSize.x;i++) {
|
for (int i=0;i<boardSize.x;i++) {
|
||||||
cols.push_back(std::vector<Block>());
|
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){
|
void Board::spawnBlock(int col){
|
||||||
BlockClump c=BlockClump();
|
BlockClump c=BlockClump();
|
||||||
c.addBlock(col);
|
c.addBlock(col,colorHandler.getRandomColor());
|
||||||
for(int counter=1;game->coinFlip(game->gen)==0;counter++) {
|
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 (game->coinFlip(game->gen)==0) {
|
||||||
if (col>0) {
|
if (col>0) {
|
||||||
c.addBlock(col-1);
|
c.addBlock(col-1,colorHandler.getRandomColor());
|
||||||
for(int counter=1;game->coinFlip(game->gen)==0;counter++) {
|
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 (game->coinFlip(game->gen)==0) {
|
||||||
if (col<boardSize.x-1) {
|
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++) {
|
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];
|
Block&b2=c.getBlocks()[i];
|
||||||
if (c.getBlockPosition(b).x==b2.pos.x) {
|
if (c.getBlockPosition(b).x==b2.pos.x) {
|
||||||
b2.markedForDeletion=true;
|
b2.markedForDeletion=true;
|
||||||
c2.addBlock(b2.pos);
|
c2.addBlock(b2.pos,b2.col);
|
||||||
}
|
}
|
||||||
c2.y=c.y;
|
c2.y=c.y;
|
||||||
}
|
}
|
||||||
@ -96,3 +84,15 @@ void Board::convertClump(int ind){
|
|||||||
}
|
}
|
||||||
removeClump(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;
|
||||||
|
}
|
29
Board.h
29
Board.h
@ -5,17 +5,42 @@
|
|||||||
class Block;
|
class Block;
|
||||||
class BlockClump;
|
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{
|
class Board{
|
||||||
std::vector<std::vector<Block>> cols;
|
std::vector<std::vector<Block>> cols;
|
||||||
std::vector<BlockClump> clumps;
|
std::vector<BlockClump> clumps;
|
||||||
public:
|
public:
|
||||||
|
ColorDistributor colorHandler;
|
||||||
float yBottom;
|
float yBottom;
|
||||||
|
Decal*tileset;
|
||||||
vf2d drawOffset;
|
vf2d drawOffset;
|
||||||
vi2d boardSize;
|
vi2d boardSize;
|
||||||
float gravity;
|
float gravity;
|
||||||
float spawnRate;
|
float spawnRate;
|
||||||
Board(){}
|
Board();
|
||||||
Board(vi2d boardSize,float gravity,float spawnRate);
|
Board(vi2d boardSize,float gravity,float spawnRate,std::array<int,10> colorRates,Renderable&tileset);
|
||||||
void spawnBlock(int col);
|
void spawnBlock(int col);
|
||||||
std::vector<Block>&getBlocks(int col);
|
std::vector<Block>&getBlocks(int col);
|
||||||
std::vector<BlockClump>&getBlockClumps();
|
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)
|
#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++
|
#C++
|
||||||
printf "Running program...\n\n\n"
|
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" ]
|
if [ "$1" = "test" ]
|
||||||
then
|
then
|
||||||
printf "Running tests...\n"
|
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)
|
#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++
|
#C++
|
||||||
printf "Running program...\n\n\n"
|
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" ]
|
if [ "$1" = "test" ]
|
||||||
then
|
then
|
||||||
printf "Running tests...\n"
|
printf "Running tests...\n"
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
build.sh:6fbec04ed16be42bab846f1eed603133 -
|
build.sh:d5e4e98a6f677aa0a9759ba0afb2fb77 -
|
||||||
commit.sh:d03a46e721060c22ccb146e19d27e70a -
|
commit.sh:d03a46e721060c22ccb146e19d27e70a -
|
||||||
debug.sh:7f57c6640be5f262988961c3b45dce97 -
|
debug.sh:849488515cab075948653c15eec4177b -
|
||||||
lines.sh:3b907786f7fc9204025993016c9080de -
|
lines.sh:3b907786f7fc9204025993016c9080de -
|
||||||
release.sh:0a525311cc14b9c8aefc6f2b816129a1 -
|
release.sh:6a96fb84ba64ed60d31be436ec069f05 -
|
||||||
temp:d41d8cd98f00b204e9800998ecf8427e -
|
temp:d41d8cd98f00b204e9800998ecf8427e -
|
||||||
web.sh:96f2c316536011a3defac50aecae487d -
|
web.sh:b982212b88b80a59ad607b47d2ff3e94 -
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
#Creates a release build that focuses on high runtime performance.
|
#Creates a release build that focuses on high runtime performance.
|
||||||
#C++
|
#C++
|
||||||
printf "Running program...\n\n\n"
|
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
|
if g++ $(find . -type f -name "*.cpp" -not -path "./test/*") ${CUSTOM_PARAMS} -O3 -s -DNDEBUG -o ${PROJECT_NAME}; then
|
||||||
./${PROJECT_NAME} "$@"
|
./${PROJECT_NAME} "$@"
|
||||||
fi
|
fi
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
#Compiles emscripten instance of this project for the web.
|
#Compiles emscripten instance of this project for the web.
|
||||||
#C++
|
#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
|
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
|
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
|
else
|
||||||
|
Binary file not shown.
1
Meteos.h
1
Meteos.h
@ -13,6 +13,7 @@ class Meteos : public olc::PixelGameEngine{
|
|||||||
std::uniform_int_distribution<> randBlockPos,coinFlip;
|
std::uniform_int_distribution<> randBlockPos,coinFlip;
|
||||||
std::mt19937 gen;
|
std::mt19937 gen;
|
||||||
Board gameBoard;
|
Board gameBoard;
|
||||||
|
std::map<std::string,Renderable> SPRITES;
|
||||||
bool OnUserCreate()override;
|
bool OnUserCreate()override;
|
||||||
bool OnUserUpdate(float fElapsedTime)override;
|
bool OnUserUpdate(float fElapsedTime)override;
|
||||||
};
|
};
|
||||||
|
BIN
assets/blocks_test.png
Normal file
BIN
assets/blocks_test.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 759 B |
13
main.cpp
13
main.cpp
@ -9,12 +9,15 @@ bool Meteos::OnUserCreate()
|
|||||||
{
|
{
|
||||||
game=this;
|
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
|
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()
|
gen=std::mt19937(rd()); //Standard mersenne_twister_engine seeded with rd()
|
||||||
randBlockPos=std::uniform_int_distribution<>(0, 9);
|
randBlockPos=std::uniform_int_distribution<>(0, 9);
|
||||||
coinFlip=std::uniform_int_distribution<>(0, 1);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +60,7 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
|
|||||||
//Copy every block from one clump to the other
|
//Copy every block from one clump to the other
|
||||||
for (int m=0;m<c2.getBlocks().size();m++) {
|
for (int m=0;m<c2.getBlocks().size();m++) {
|
||||||
Block&b4=c2.getBlocks()[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) {
|
if (c.vspeed>0) {
|
||||||
c.vspeed/=4;
|
c.vspeed/=4;
|
||||||
@ -92,17 +95,17 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
|
|||||||
goto nextClump;
|
goto nextClump;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.y+=c.vspeed*fElapsedTime;
|
||||||
for (int j=0;j<c.getBlocks().size();j++){
|
for (int j=0;j<c.getBlocks().size();j++){
|
||||||
Block&b=c.getBlocks()[j];
|
Block&b=c.getBlocks()[j];
|
||||||
c.y+=c.vspeed*fElapsedTime;
|
DrawPartialDecal(c.getBlockPosition(b)+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12});
|
||||||
FillRectDecal(c.getBlockPosition(b)+gameBoard.drawOffset,{12,12},DARK_GREEN);
|
|
||||||
}
|
}
|
||||||
nextClump:;
|
nextClump:;
|
||||||
}
|
}
|
||||||
for (int i=0;i<gameBoard.boardSize.x;i++){
|
for (int i=0;i<gameBoard.boardSize.x;i++){
|
||||||
for (int y=0;y<gameBoard.getBlocks(i).size();y++){
|
for (int y=0;y<gameBoard.getBlocks(i).size();y++){
|
||||||
Block&b=gameBoard.getBlocks(i)[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;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user