Switch to full style
Understanding the language, error messages, etc.
Post a reply

Drawing Map from arrays

Wed Oct 05, 2016 1:01 am

Hey!
I'm a beginner with all the C's byte stuff because I was creating games for PC and Mac in C# with unity....
Anyway, what I want is to draw maps on screen using ID code, let me explain, here's my map :

Code:
const byte Map[] PROGMEM = {13,9,
8,8,

0,0,0,0,0,0,0,0,0,0,0,0,2,
2,1,2,5,5,8,0,0,0,0,0,0,7,
3,0,0,0,0,8,0,0,0,0,0,0,2,
3,0,0,0,0,0,4,5,2,0,0,0,3,
2,0,2,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,2,5,5,0,5,5,2,
3,0,3,0,9,0,3,0,9,0,0,0,7,
2,0,2,1,1,1,2,1,1,1,1,1,2,
};


Each id represents a block or an ennemie.
For example, 0 is nothing and 1 is

Code:
const byte PROGMEM Brick[] = //ID: 0
{
  8,8,
  B11111111,
  B00100001,
  B11111111,
  B10001000,
  B11111111,
  B00100001,
  B11111111,
  B00000000,
};


What i want to do: i want to be able to scroll and see the level using arrow. I don't know what to do but i tried this:

Code:
#include <Backlight.h>
#include <Battery.h>
#include <Buttons.h>
#include <Display.h>
#include <Gamebuino.h>
#include <Sound.h>
#include <SPI.h>

Gamebuino gb;
byte PlayerPositionX;
byte PlayerPositionY;

byte CurrentMapSizeX;
byte CurrentMapSizeY;

const byte PROGMEM Brick[] = //ID: 0
{
  8,8,
  B11111111,
  B00100001,
  B11111111,
  B10001000,
  B11111111,
  B00100001,
  B11111111,
  B00000000,
};

const byte PROGMEM SharpBrick[]= //ID: 2
{
  8,8,
  B11111111,
  B11000011,
  B10100101,
  B10011001,
  B10011001,
  B10100101,
  B11000011,
  B11111111,
};
const byte PROGMEM TowerBrick[]= //ID: 3
{
  8,8,
  B10101101,
  B10110101,
  B10101101,
  B10110101,
  B10101101,
  B10110101,
  B10101101,
  B10110101,
};

const byte PROGMEM Piston[]= //ID: 4
{
  8,8,
  B00000000,
  B01111110,
  B00100100,
  B00100100,
  B01000010,
  B00011000,
  B01111110,
  B00000000,
};

const byte PROGMEM Holder[]= //ID: 5
{
  8,8,
  B11111111,
  B10100101,
  B11111111,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
};

const byte PROGMEM  Teleporter1[]= //ID: 6
{
  8,8,
  B11111111,
  B10101011,
  B11010110,
  B10101100,
  B10101100,
  B11010110,
  B10101011,
  B11111111,
};

const byte PROGMEM Teleporter0[]= //ID: 7
{
  8,8,
  B11111111,
  B11010101,
  B01101011,
  B00110101,
  B00110101,
  B01101011,
  B11010101,
  B11111111,
};

const byte PROGMEM Wall[]= //ID: 8
{
  8,8,
  B11110000,
  B11110000,
  B10010000,
  B10010000,
  B10010000,
  B10010000,
  B10010000,
  B11110000,
};

const byte Map[] PROGMEM = {13,9,
8,8,

0,0,0,0,0,0,0,0,0,0,0,0,2,
2,1,2,5,5,8,0,0,0,0,0,0,7,
3,0,0,0,0,8,0,0,0,0,0,0,2,
3,0,0,0,0,0,4,5,2,0,0,0,3,
2,0,2,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,2,5,5,0,5,5,2,
3,0,3,0,9,0,3,0,9,0,0,0,7,
2,0,2,1,1,1,2,1,1,1,1,1,2,
};

//For info about map:
//http://gamebuino.com/forum/viewtopic.php?f=8&t=3492&p=12227&hilit=map#p12227

void setup() {
  // put your setup code here, to run once:
  gb.begin();
  gb.titleScreen(F("BIGBLACKBOX Testing"));
  CurrentMapSizeX = 13;
  CurrentMapSizeY = 9;
}

void loop() {
  // put your main code here, to run repeatedly:
  if(gb.update()) {
   
   if(gb.buttons.pressed(BTN_UP)) {
    PlayerPositionY--;
   }
   if(gb.buttons.pressed(BTN_DOWN)) {
    PlayerPositionY++;
   }
   
   if(gb.buttons.pressed(BTN_RIGHT)) {
     PlayerPositionX++;
   }
   if(gb.buttons.pressed(BTN_LEFT)) {
     PlayerPositionX--;
   }

  //Drawing
   for(int x = 0; x < CurrentMapSizeX; x++) {
     for(int y = 0; y < CurrentMapSizeY; y++) {
       byte Id;
       Id = Map[x+y*CurrentMapSizeX];
       //gb.display.print(Id);
       if(Id == 1) {
         gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Brick);
       }
       if(Id == 2) {
         gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, SharpBrick);
       }
       if(Id == 3) {
         gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, TowerBrick);
       }
       if(Id == 4) {
         gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Piston);
       }
       if(Id == 5) {
         gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Holder);
       }
       if(Id == 6) {
          gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Teleporter1);
       }
       if(Id == 7) {
        gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Teleporter0);
       }
       if(Id == 8) {
         gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Wall);
       }
      }
    }
    //gb.display.drawBitmap(10,10,Brick);
  }
}


How can i make my code work. I don't do it for to awnser but for the knowleght i can get.

By the way: I speak french and my english is vary bad :lol:

Re: Drawing Map from arrays

Wed Oct 05, 2016 1:45 am

You might want to check out this thread.........

viewtopic.php?f=8&t=3440

It got everything your asking about and a lil more.

Re: Drawing Map from arrays

Wed Oct 05, 2016 7:45 am

Hello and welcome :)

You should check out the source code of the SSCCE (Short, Self Contained, Correct Examples) provided with the library ;)
TileMapRAM shows how do draw a tile map stored in RAM.

But RAM is a scarce ressource, so it's better to store your bitmaps and tilemaps in flash memory (aka PROGMEM) if you don't need to change them at run time (as it's read-only memory). So you could look at UFO race, especially the file world.ino, it's a simple game I made with maps stored in PROGMEM.

Re: Drawing Map from arrays

Wed Oct 05, 2016 4:31 pm

Thanks it work!

Re: Drawing Map from arrays

Wed Oct 05, 2016 7:02 pm

What did you use to help you figure it out, also would love to hear a little bit about your project if you fancy updating us

Re: Drawing Map from arrays

Wed Oct 05, 2016 8:57 pm

I just realize that the code half works... I display blocks and the blocks move when i use arrow but the map is only few random blocks placed randomly... For now, i'm experimenty with how gamebuino work, display maps and physic. For testing, before i start my projet, what i want to do is a scrollable map (explore the map using arrows) here's the half working code:
Code:
#include <Backlight.h>
#include <Battery.h>
#include <Buttons.h>
#include <Display.h>
#include <Gamebuino.h>
#include <Sound.h>
#include <SPI.h>

Gamebuino gb;
byte PlayerPositionX;
byte PlayerPositionY;

byte CurrentMapSizeX;
byte CurrentMapSizeY;

const byte PROGMEM Brick[] = //ID: 1
{
  8,8,
  B00000000,
  B11011110,
  B00000000,
  B01110111,
  B00000000,
  B11011110,
  B00000000,
  B11111111,
};

const byte PROGMEM SharpBrick[]= //ID: 2
{
  8,8,
  B00000000,
  B00111100,
  B01011010,
  B01100110,
  B01100110,
  B01011010,
  B00111100,
  B00000000,
};
const byte PROGMEM TowerBrick[]= //ID: 3
{
  8,8,
  B01010010,
  B01001010,
  B01010010,
  B01001010,
  B01010010,
  B01001010,
  B01010010,
  B01001010,
};

const byte PROGMEM Piston[]= //ID: 4
{
  8,8,
  B11111111,
  B10000001,
  B11011011,
  B11011011,
  B10111101,
  B11100111,
  B10000001,
  B11111111,
};

const byte PROGMEM Holder[]= //ID: 5
{
  8,8,
  B00000000,
  B01011010,
  B00000000,
  B11111111,
  B11111111,
  B11111111,
  B11111111,
  B11111111,
};

const byte PROGMEM  Teleporter0[]= //ID: 6
{
  8,8,
  B00000000,
  B01010100,
  B00101001,
  B01010011,
  B01010011,
  B00101001,
  B01010100,
  B00000000,
};

const byte PROGMEM Teleporter1[]= //ID: 7
{
  8,8,
  B00000000,
  B00101010,
  B10010100,
  B11001010,
  B11001010,
  B10010100,
  B00101010,
  B00000000,
};

const byte PROGMEM Wall[]= //ID: 8
{
  8,8,
  B00001111,
  B00001111,
  B01101111,
  B01101111,
  B01101111,
  B01101111,
  B01101111,
  B00001111,
};

const byte Map[] PROGMEM = {13,9,
8,8,

0,0,0,0,0,0,0,0,0,0,0,0,2,
2,1,2,5,5,8,0,0,0,0,0,0,7,
3,0,0,0,0,8,0,0,0,0,0,0,2,
3,0,0,0,0,0,4,5,2,0,0,0,3,
2,0,2,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,2,5,5,0,5,5,2,
3,0,3,0,9,0,3,0,9,0,0,0,7,
2,0,2,1,1,1,2,1,1,1,1,1,2,
};

//For info about map:
//http://gamebuino.com/forum/viewtopic.php?f=8&t=3492&p=12227&hilit=map#p12227

void setup() {
  // put your setup code here, to run once:
  gb.begin();
  gb.titleScreen(F("BIGBLACKBOX Testing"));
  CurrentMapSizeX = 13;
  CurrentMapSizeY = 9;
}

void loop() {
  // put your main code here, to run repeatedly:
  if(gb.update()) {
   if(gb.buttons.pressed(BTN_UP)) {
    PlayerPositionY = PlayerPositionY + 1;
   }
   if(gb.buttons.pressed(BTN_DOWN)) {
    PlayerPositionY = PlayerPositionY - 1;
   }
   
   if(gb.buttons.pressed(BTN_RIGHT)) {
     PlayerPositionX = PlayerPositionX + 1;
   }
   if(gb.buttons.pressed(BTN_LEFT)) {
     PlayerPositionX = PlayerPositionX - 1;
   }

  //Drawing
   for(int x = 0; x < CurrentMapSizeX; x++) {
     for(int y = 0; y < CurrentMapSizeY; y++) {
       byte Id;
       Id = Map[x+y*CurrentMapSizeX];
       if(Id == 1) {
         gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Brick);
       }
       if(Id == 2) {
        gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, SharpBrick);
       }
       if(Id == 3) {
          gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, TowerBrick);
       }
       if(Id == 4) {
         gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Piston);
       }
       if(Id == 5) {
         gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Holder);
       }
       if(Id == 6) {
          gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Teleporter1);
       }
       if(Id == 7) {
        gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Teleporter0);
       }
       if(Id == 8) {
         gb.display.drawBitmap(x*8-PlayerPositionX,y*8-PlayerPositionY, Wall);
       }
      }
    }
  }
}


The real game is... a secret! The name is BIG BLACK BOX, i will start the developpement when i will have engouh knowlegt about gamebuino stuff.

Re: Drawing Map from arrays

Wed Oct 05, 2016 9:09 pm

I'm not at a computer right now to have a look at your code properly, but will take a look as soon as I can access one.

In the meantime, did you look at the posts Duhjoker and Rodot linked you to, there is so much info on this site around tilemaps... In fact it's possibly the most talked about topic in recent times

Re: Drawing Map from arrays

Thu Oct 06, 2016 4:37 pm

So, i've fix the code and it work better than last time! I'm close to what i want. http://pastebin.com/phFbHa7L There's one problem. First two line are wierds... They should be at the end [url]pic.twitter.com/YBsQ8DKW2R[/url] [url]pic.twitter.com/YBsQ8DKW2R[/url]

Any idea? (I can't wait to get my Gamebuino (I'm currenlty using an emulator))

Re: Drawing Map from arrays

Fri Oct 07, 2016 12:59 am

I fix the error myself

Re: Drawing Map from arrays

Fri Oct 07, 2016 7:06 am

Hey, you can attach images to your post, please don't link to external pictures as they will eventually be down. Click on "upload attachement" when you're writing/editing a post and select your picture ;)
Post a reply