Hello.
I’ve just Added three flowers.
My goal, obviously is to avoid player, rocks and flowers spawns colliding with themselves.
I would be kind enouth to call for example the spawnRocks() function and then call two funtions checkPlayerCollisions() and checkRocksCollisions().
Also, do the same with spawnFlowers() but this time with three functions, checkPlayerCollisions() , checkRocksCollisions() and checkFlowersCollisions() .
spawnRocks() => checkPlayerCollisions()
checkRocksCollisions()
spawnFlowers() => checkPlayerCollisions()
checkRocksCollisions()
checkFlowersCollisions()
I know ‘while’ condition is practical but as wrote in the code it checks only one verification.
I think also Collisions functions must be rewrote.
Perhaps you could help me with that ?
In theory would that be possible ?
Here is My actual code :
#include <Arduboy2.h>
Arduboy2 arduboy;
const int playerWidth = 16;
const int playerHeight = 16;
const int rockWidth = 16;
const int rockHeight = 16;
const int flowerWidth = 16;
const int flowerHeight = 16;
Point player;
Point rockLocation[3];
Point flowerLocation[3];
const unsigned char background[] PROGMEM =
{
0x84, 0x20, 0x9, 0x00, 0x24, 0x00, 0x10, 0x80,
};
const unsigned char playerbitmap[] PROGMEM =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff, 0xf7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xf0, 0x38, 0xf, 0x7, 0xff, 0xf0, 0x80, 0x00, 0x00, 0x00, 0x00,
};
const unsigned char rock[] PROGMEM =
{
0x00, 0x00, 0x00, 0x80, 0xf0, 0xfc, 0xff, 0xff, 0xff, 0xfe, 0xf8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x1f, 0x1f, 0x3f, 0x3f, 0x1f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3e, 0x18, 0x00, 0x00,
};
const unsigned char flower[] PROGMEM =
{
0x00, 0x00, 0x00, 0x10, 0xfe, 0xfe, 0xff, 0xcf, 0xcf, 0xfe, 0xfe, 0x7c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc, 0x1c, 0x18, 0xff, 0x1d, 0x1e, 0xe, 0x6, 0x00, 0x00, 0x00, 0x00,
};
void spawnPlayer()
{
player.x = 50;
player.y = 20;
}
void spawnRock(size_t index)
{
rockLocation[index].x = random(128 - rockWidth);
rockLocation[index].y = random(64 - rockHeight);
}
void spawnFlower(size_t index)
{
flowerLocation[index].x = random(128 - flowerWidth);
flowerLocation[index].y = random(64 - flowerHeight);
}
void spawnRocks()
{
for (size_t i = 0; i < 3; ++i)
{
bool colliding = true;
while (colliding)
{
spawnRock(i);
colliding = checkRockCollisions(i);
}
}
}
void spawnFlowers()
{
for (size_t i = 0; i < 3; ++i)
{
bool colliding = true;
while (colliding)
{
spawnFlower(i);
colliding = checkFlowerCollisions(i);
}
}
}
bool checkPlayerCollisions(size_t index)
{
Rect rockRect = { rockLocation[index].x, rockLocation[index].y, rockWidth, rockHeight };
Rect playerRect = { player.x, player.y, playerWidth, playerHeight };
if (arduboy.collide(rockRect, playerRect))
{
return true;
}
return false;
}
bool checkRockCollisions(size_t index)
{
// Only check collisions against existing rocks
Rect rockRect = { rockLocation[index].x, rockLocation[index].y, rockWidth, rockHeight };
for (size_t i = 0; i < index; ++i)
{
Rect otherRockRect = { rockLocation[i].x, rockLocation[i].y, rockWidth, rockHeight };
if (arduboy.collide(rockRect, otherRockRect))
{
return true;
}
}
return false;
}
bool checkFlowerCollisions(size_t index)
{
Rect flowerRect = { flowerLocation[index].x, flowerLocation[index].y, flowerWidth, flowerHeight };
// Only check collisions against existing flowers
for (size_t i = 0; i < index; ++i)
{
Rect otherFlowerRect = { flowerLocation[i].x, flowerLocation[i].y, flowerWidth, flowerHeight };
if (arduboy.collide(flowerRect, otherFlowerRect))
{
return true;
}
}
return false;
}
bool isCollidingWithAnyRock(Rect rect)
{
for (size_t i = 0; i < 3; ++i)
{
Rect rockRect = { rockLocation[i].x, rockLocation[i].y, rockWidth, rockHeight };
if (arduboy.collide(rect, rockRect))
{
return true;
}
}
return false;
}
void setup()
{
arduboy.begin();
arduboy.clear();
arduboy.initRandomSeed();
spawnPlayer();
spawnRocks();
spawnFlowers();
}
void loop()
{
arduboy.clear();
if (arduboy.pressed(LEFT_BUTTON)) {
Rect playerRect = { player.x - 1, player.y, playerWidth, playerHeight };
bool hitARock = isCollidingWithAnyRock(playerRect);
if (!hitARock && player.x > 0) {
player.x = player.x - 1;
}
}
if (arduboy.pressed(RIGHT_BUTTON)) {
Rect playerRect = { player.x + 1, player.y, playerWidth, playerHeight };
bool hitARock = isCollidingWithAnyRock(playerRect);
if (!hitARock && player.x + playerWidth < WIDTH ) {
player.x = player.x + 1;
}
}
if (arduboy.pressed(UP_BUTTON)) {
Rect playerRect = { player.x, player.y - 1, playerWidth, playerHeight };
bool hitARock = isCollidingWithAnyRock(playerRect);
if (!hitARock && player.y > 0) {
player.y = player.y - 1;
}
}
if (arduboy.pressed(DOWN_BUTTON)) {
Rect playerRect = { player.x, player.y + 1, playerWidth, playerHeight };
bool hitARock = isCollidingWithAnyRock(playerRect);
if (!hitARock && player.y + playerHeight < HEIGHT ) {
player.y = player.y + 1;
}
}
//For each column on the screen
for ( int backgroundx = 0; backgroundx < arduboy.width(); backgroundx = backgroundx + 8 ) {
//For each row in the column
for ( int backgroundy = 0; backgroundy < arduboy.height(); backgroundy = backgroundy + 8 ) {
//Draw a background tile
arduboy.drawBitmap( backgroundx, backgroundy, background, 8, 8, WHITE );
}
}
//arduboy.fillRect(player.x, player.y, playerWidth, playerHeight, BLACK);
arduboy.drawBitmap(player.x, player.y, playerbitmap, playerWidth, playerHeight, WHITE);
for ( int i = 0; i < 3; ++i) {
//arduboy.fillRect(rockLocation[i].x, rockLocation[i].y, rockWidth, rockHeight, BLACK);
arduboy.drawBitmap(rockLocation[i].x, rockLocation[i].y, rock, rockWidth, rockHeight, WHITE);
}
for ( int i = 0; i < 3; ++i) {
//arduboy.fillRect(flowerLocation[i].x, flowerLocation[i].y, flowerWidth, flowerHeight, BLACK);
arduboy.drawBitmap(flowerLocation[i].x, flowerLocation[i].y, flower, flowerWidth, flowerHeight, WHITE);
}
arduboy.display();
}