I have created a table in Lua for Pico-8 in order to draw a row of invaders every frame ! (see attached images - code/result)
I’m trying to do the same thing with a map (as it seems to be the closest data type to a table in Lua) in C++ and I’m having trouble understanding/figuring out the syntax to do this, I also need to create the for loop to iterate through it and display my sprites.
Here’s my code so far:
// Map, C++, PicoSystem
#include “picosystem.hpp”
#include
#include
#include
#include
using namespace picosystem;
using namespace std;
int px = 57;
int py = 57;
int score = 0;
// for sound
voice_t blip;
// Custom sprite sheet
const color_t custom_sprite_sheet_data[256] = {
0x0000, 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000, 0x0000,
0xffff, 0x0000, 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000,
0xffff, 0xffff, 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0xffff, 0x0000, 0xffff, 0x0000, 0xffff, 0x0000,
0xffff, 0xffff, 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000,
0xffff, 0xffff, 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000,
0xffff, 0xffff, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000, 0x0000,
0xffff, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
};
// spritesheet setup
buffer_t *CUSTOM_SPRITESHEET = buffer(16, 16, (void *)custom_sprite_sheet_data);
buffer_t *custom_sprite_sheet = CUSTOM_SPRITESHEET;
// initialize function
void init() {
blip = voice(10, 10, 10, 10, 40, 2);
// empty map container
map<string, int> coin;
// elements
coin.insert[0](pair<string, int>(“x”, 75, “y”, 78);
coin.insert[1](pair<string, int>(“x”, 30, “y”, 33);
coin.insert[2](pair<string, int>(“x”, 55, “y”, 18);
coin.insert[3](pair<string, int>(“x”, 10, “y”, 73);
coin.insert[4](pair<string, int>(“x”, 80, “y”, 28);
}
// update function (called 50 times a second)
void update(uint32_t tick) {
if(button(LEFT)) {
if(px > 2) {
px -= 2;
}
}
if(button(RIGHT)) {
if(px < 113) {
px += 2;
}
}
if(button(UP)) {
if(py > 1) {
py -= 2;
}
}
if(button(DOWN)) {
if(py < 113) {
py += 2;
}
}
// check player collision with coins later
}
// draw function
void draw(uint32_t tick) {
// set current color
pen(0, 0, 0);
// clear screen with current color
clear();
pen(15, 15, 15); // color white
text(“SCORE:”, 2, 3);
text(str((int)score, 0), 41, 3);
// load custom spritesheet
spritesheet(custom_sprite_sheet);
// draw player
sprite(1, px, py);
// draw coins using data from map in these spots (see map above)
//sprite(0, 75, 78);
//sprite(0, 30, 33);
//sprite(0, 55, 18);
//sprite(0, 10, 73);
//sprite(0, 80, 28);
}
As you can see, I need help to fill in the for loop and the sprite number?, x?, y?
I have a pretty good C++ book and have read many samples online of how this should work but I haven’t been able to come up with a solution that works, any help would be appreciated.
Thanks,
Brian