Hi Guys & Gals!
Disclaimer: please note that I am new to Arduboy and C++ programming therefore my questions and code are on a begginer level.
I am trying to make procedurally generated forest. Things I have:
- Trees sprites
- Idea to randomly generate one (128 x 64) background: create an array of random x, y positions, draw trees accoridingly to that array
- Idea to generate more than one background: create many (128 x 64) backgrounds and draw them with shift of 128 px and 64 px.
- Working pice of code
The problem:
If I would like to have more backgrounds, using abovementioned approach, I would need a larger array to keep x,y positions. I was thiniking of adding second dimenstion to an array, however such array would not fit in the memory. My approach is probably quite badâŚany advices would be more than welcomed. Please find below full code:
#include <Arduboy2.h>
Arduboy2 arduboy;
Sprites sprites;
byte frame;
//buffers
byte abuffer;
byte bbuffer;
byte upbuffer = 0;
byte downbuffer = 0;
byte leftbuffer = 0;
byte rightbuffer = 0;
byte moving = 0;
byte mstate = 1;
//cursor variables
int crs_x = 0;
int crs_y = 0;
// Structure for x, y, frame
struct xy_point {
byte x;
byte y;
byte z;
};
// Structure will keep 20 trees, however it seems that to procedurally generate more backgrounds I would need bigger array? (and I do not have memory for that)
xy_point xy_points[20];
const unsigned char PROGMEM trees[] =
{
// width, height,
8, 16,
// FRAME 00
0x80, 0x80, 0xd0, 0xfe, 0xb0, 0x08, 0x80, 0x00,
0x08, 0x1d, 0x5d, 0xff, 0x1b, 0x1d, 0x0e, 0x02,
// FRAME 01
0x00, 0x80, 0xc0, 0xfe, 0xf0, 0x80, 0x00, 0x00,
0x0e, 0x1f, 0x1f, 0xff, 0x1f, 0x1f, 0x0e, 0x00,
// FRAME 02
0x00, 0xb0, 0xf8, 0xfc, 0xfe, 0x6e, 0x07, 0x00,
0x03, 0x07, 0x0f, 0xff, 0x0f, 0x07, 0x03, 0x00,
// FRAME 03
0x80, 0xd8, 0xb0, 0xfc, 0xff, 0xf2, 0xc0, 0x60,
0x05, 0x0d, 0x0f, 0xff, 0x0f, 0x07, 0x06, 0x02,
};
const unsigned char PROGMEM foxr[] =
{
// width, height,
8, 8,
// FRAME 00
0x00, 0x00, 0x7c, 0x20, 0x60, 0x38, 0x10, 0x00,
// FRAME 01
0x00, 0x0c, 0x70, 0x20, 0x20, 0x78, 0x10, 0x00,
// FRAME 02
0x00, 0x0c, 0x30, 0x60, 0x20, 0x78, 0x10, 0x00,
// FRAME 03
0x00, 0x00, 0x3c, 0x60, 0x20, 0x78, 0x10, 0x00,
// FRAME 04
0x00, 0x00, 0x3c, 0x60, 0x60, 0x38, 0x10, 0x00,
// FRAME 05
0x00, 0x00, 0x7c, 0x20, 0x60, 0x38, 0x10, 0x00,
};
const unsigned char PROGMEM foxl[] =
{
// width, height,
8, 8,
// FRAME 00
0x00, 0x10, 0x38, 0x60, 0x20, 0x7c, 0x00, 0x00,
// FRAME 01
0x00, 0x10, 0x78, 0x20, 0x20, 0x70, 0x0c, 0x00,
// FRAME 02
0x00, 0x10, 0x78, 0x20, 0x60, 0x30, 0x0c, 0x00,
// FRAME 03
0x00, 0x10, 0x78, 0x20, 0x60, 0x3c, 0x00, 0x00,
// FRAME 04
0x00, 0x10, 0x38, 0x60, 0x60, 0x3c, 0x00, 0x00,
// FRAME 05
0x00, 0x10, 0x38, 0x60, 0x20, 0x7c, 0x00, 0x00,
};
void random_tree_pos() {
for ( int i = 1; i < 20; i = i + 1 ) {
xy_points[i].x = random(0, 128);
xy_points[i].y = random(0, 64);
xy_points[i].z = random(0, 4);
}
}
void tree(int a, int b) {
for ( int i = 1; i < 20; i = i + 1 ) {
sprites.drawSelfMasked(xy_points[i].x + a, xy_points[i].y + b, trees, xy_points[i].z);
}
}
void buttons() {
if (arduboy.pressed(LEFT_BUTTON) and leftbuffer == 0) {
moving = 1;
crs_x = crs_x + 1;
mstate = 0;
}
if (arduboy.pressed(RIGHT_BUTTON) and rightbuffer == 0 ) {
moving = 1;
crs_x = crs_x - 1;
mstate = 1;
}
if (arduboy.pressed(UP_BUTTON) and upbuffer == 0) {
moving = 1;
crs_y = crs_y + 1;
}
if (arduboy.pressed(DOWN_BUTTON) and downbuffer == 0) {
moving = 1;
crs_y = crs_y - 1;
}
if (arduboy.pressed(B_BUTTON) and bbuffer == 0) {
}
if (arduboy.pressed(A_BUTTON) and abuffer == 0) {
abuffer = 1;
random_tree_pos();
}
}
void setup() {
// put your setup code here, to run once:
arduboy.begin();
arduboy.clear();
arduboy.setFrameRate(60);
random_tree_pos();
arduboy.initRandomSeed()
;
}
void loop() {
if (!arduboy.nextFrame()) {
return; // go back to the start of the loop
}
arduboy.clear();
tree(crs_x, crs_y);
if (mstate == 1) {
sprites.drawSelfMasked(60, 28, foxr, frame);
} else {
sprites.drawSelfMasked(60, 28, foxl, frame);
};
if (arduboy.everyXFrames(2)) {
buttons();
if (moving == 1 or frame > 0) {
frame = frame + 1;
}
if (frame > 5) {
frame = 0;
};
}
if ( arduboy.notPressed(A_BUTTON) == true) {
abuffer = 0;
}
if ( arduboy.notPressed(B_BUTTON) == true) {
bbuffer = 0;
}
if ( arduboy.notPressed(DOWN_BUTTON) == true ) {
downbuffer = 0;
moving = 0;
}
if ( arduboy.notPressed(UP_BUTTON) == true ) {
upbuffer = 0;
moving = 0;
}
if ( arduboy.notPressed(RIGHT_BUTTON) == true ) {
rightbuffer = 0;
moving = 0;
}
if ( arduboy.notPressed(LEFT_BUTTON) == true ) {
leftbuffer = 0;
moving = 0;
}
arduboy.display();
}