Newbs need help with collision detection.

Understanding the language, error messages, etc.

Re: Newbs need help with collision detection.

Postby Duhjoker » Sat Aug 06, 2016 4:52 am

Nothing relevant to collision, display glitch problem solved
Last edited by Duhjoker on Sat Aug 06, 2016 9:28 am, edited 2 times in total.
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby Duhjoker » Sat Aug 06, 2016 7:21 am

Gobbledygook
Last edited by Duhjoker on Sat Aug 06, 2016 9:26 am, edited 1 time in total.
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby Duhjoker » Sat Aug 06, 2016 8:55 am

I logging off for the night but I now that I know im at least displaying the player sprite and the tile map, I decided to try adding the collision bits from the example. Ofcourse its not stopping the character when it hits a wall but its compiling. im sure im missing something but im still trying to learn the commands.

Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;

int i;

const byte tilemap[] PROGMEM = {14,8,
6,6,
3,3,3,3,3,3,6,6,3,3,3,3,3,3,
3,3,3,5,3,6,6,6,3,3,3,3,3,3,
3,3,6,6,6,6,6,6,6,3,3,3,3,3,
3,6,6,6,6,6,6,6,6,6,3,3,3,3,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,
3,3,6,6,6,6,6,6,6,6,6,6,3,3,
3,3,6,6,6,6,6,6,6,6,6,6,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3};

const byte bush_bottom[] PROGMEM = {B01001000,B00110000,B01011000,B00000000,B00000000, B00000000 };
const byte bush_top[] PROGMEM = {B00000000, B00000000, B00000000, B00110000,B01001000,B10110100 };
const byte bush[] PROGMEM = {B0010000, B01001000, B10110100, B01001000, B00110000, B01011000 };
const byte rock_terrain[] PROGMEM = {B01011000, B10100100, B00010100, B10100000, B10000100, B01011000 } ;
const byte turtle_rock[] PROGMEM = {B00000000, B01110000, B00101000, B01001000, B01110000, B00000000 };
const byte port_noir[] PROGMEM = {B11111100, B11111100, B11111100, B11111100, B11111100,B11111100 };
const byte port_blanc[] PROGMEM = {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 } ;

const byte *spritesheet[] = {bush_bottom,bush_top,bush,rock_terrain,turtle_rock,port_noir,port_blanc};

const byte sever_front_1[] PROGMEM = {6,6,0x78,0x0,0x0,0x30,0x0,0xcc,};
const byte sever_front_2[] PROGMEM = {6,6,0x84,0x48,0x78,0x84,0x78,0x0,};
const byte sever_right_1[] PROGMEM = {6,6,0x78,0x80,0xc8,0xb4,0x0,0xcc,};
const byte sever_right_2[] PROGMEM = {6,6,0x4,0x58,0x30,0x0,0x78,0x0,};
const byte sever_rear_1[] PROGMEM = {6,6,0X78,0X78,0X0,0X30,0X0,0XCC,};
const byte sever_rear_2[] PROGMEM = {6,6,0x84,0x0,0x78,0x84,0x78,0x0,};
const byte sever_left_1[] PROGMEM = {6,6,0x78,0x4,0x4c,0xb4,0x0,0xcc,};
const byte sever_left_2[] PROGMEM = {6,6,0x80,0x68,0x30,0x0,0x78,0x0,};

  int player_x = 15;
  int player_y = 15;
  int player_direction = 0;

int x=-50,y=50;


void setup() {
  gb.begin();
  gb.titleScreen(F("test"));
  gb.display.persistence = false;
}


void loop() {
  if(gb.update()){
    if (gb.buttons.repeat(BTN_RIGHT,1)){x--;}
    if (gb.buttons.repeat(BTN_LEFT,1)){x++;}
    if (gb.buttons.repeat(BTN_DOWN,1)){y--;}
    if (gb.buttons.repeat(BTN_UP,1)){y++;}
    gb.display.drawTilemap(x,y,tilemap,spritesheet); // draw the tilemap
//    gb.display.cursorY = 12; gb.display.println( x );gb.display.println( y );

 if(gb.buttons.repeat(BTN_UP,1)){
       gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_rear_1);
       }
    gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_rear_2);
    }
      player_direction = 1;
      player_y = player_y - 1;}
    if(player_y <= 0){
      player_y = 0;}



    if(gb.buttons.repeat(BTN_DOWN,1)){
      gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_front_1);
    }
     gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_front_2);
    }
      player_direction = 2;
      player_y = player_y + 1;}
    if(player_y >= 40){
      player_y = 40;}
 


    if(gb.buttons.repeat(BTN_RIGHT,1)){
       gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_right_1);
    }
     gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_right_2);
    }
      player_direction = 3;
      player_x = player_x + 1;}
    if(player_x >= 77){
      player_x = 77;}


    if(gb.buttons.repeat(BTN_LEFT,1)){
       gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_left_1);
    }
    gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_left_2);
    }
      player_direction = 4;
      player_x = player_x - 1;}
    if(player_x <= -2){
      player_x = -2;}
     
      ////////////PLAYER DIRECTION/////////////

 if (player_direction == 1){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_rear_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_rear_2);
        }
 }
     
      else if (player_direction == 2){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_front_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_front_2);
        }
      }
else if (player_direction == 3){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_left_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_left_2);
        }
}
      else if (player_direction == 4){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_right_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_right_2);
        }
      }
      else {  gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_front_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_front_2);
        }
      }
     for(i=0; i < gb.display.numcolision + 1; i++)
   {
    if(gb.collideRectRect(player_x,player_y,8,8,gb.display.solid[i].x,gb.display.solid[i].y,8,8))
    {
          if(gb.display.solid[i].spritecol == port_blanc); //Do nothing because it's floor
else      if(gb.display.solid[i].spritecol == port_noir);
else      if(gb.display.solid[i].spritecol == rock_terrain);
else      if(gb.display.solid[i].spritecol == turtle_rock);
else      if(gb.display.solid[i].spritecol == bush);
else      if(gb.display.solid[i].spritecol == bush_bottom);
else      if(gb.display.solid[i].spritecol == bush_top);
      }   
  }     
  }
 
}






Thank you summoner123 for helping us.
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby naed » Sat Aug 06, 2016 11:53 am

Going to see if I can take a look at this later today if I find the time, I do wonder why it doesn't like the x/y coordinates to be set at zero and freezes if they are?

Thanks Summoner123 for the help
User avatar
naed
 
Posts: 140
Joined: Tue May 31, 2016 3:18 pm

Re: Newbs need help with collision detection.

Postby Summoner123 » Sat Aug 06, 2016 3:35 pm

Duhjoker wrote:Ok I think we need to start over I'm starting to get confused and I can't understand why my tilemaps refuse to show now.


I have discovered that is related with the size of your tiles...if you can change them to 8x8 it should work better. maybe with the 6x6 size the "Drawtilemap" is trying to performe ilegal operations (like division by zero or other similar things) Resulting in a Crash.

************** ..::EDIT::.. ************

I have modified you code to 8x8 tiles just adding to empyt lines to each tile, it solved the crashing problem, now I can show you a example of use my colision "rotine", there is the code:

Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;



const byte tilemap[] PROGMEM = {14,8,
8,8,
3,3,3,3,3,3,6,6,3,3,3,3,3,3,
3,3,3,5,3,6,6,6,3,3,3,3,3,3,
3,3,6,6,6,6,6,6,6,3,3,3,3,3,
3,6,6,6,6,6,6,6,6,6,3,3,3,3,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,
3,3,6,6,6,6,6,6,6,6,6,6,3,3,
3,3,6,6,6,6,6,6,6,6,6,6,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3};

const byte bush_bottom[] PROGMEM = {B01001000,B00110000,B01011000,B00000000,B00000000, B00000000, B00000000, B00000000 };
const byte bush_top[] PROGMEM = {B00000000, B00000000, B00000000, B00110000,B01001000,B10110100, B00000000, B00000000 };
const byte bush[] PROGMEM = {B0010000, B01001000, B10110100, B01001000, B00110000, B01011000, B00000000, B00000000 };
const byte rock_terrain[] PROGMEM = {B01011000, B10100100, B00010100, B10100000, B10000100, B01011000, B00000000, B00000000 } ;
const byte turtle_rock[] PROGMEM = {B00000000, B01110000, B00101000, B01001000, B01110000, B00000000, B00000000, B00000000 };
const byte port_noir[] PROGMEM = {B11111100, B11111100, B11111100, B11111100, B11111100,B11111100, B00000000, B00000000 };
const byte port_blanc[] PROGMEM = {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 } ;

const byte *spritesheet[] = {bush_bottom,bush_top,bush,rock_terrain,turtle_rock,port_noir,port_blanc};

const byte sever_front_1[] PROGMEM = {6,6,0x78,0x0,0x0,0x30,0x0,0xcc,};
const byte sever_front_2[] PROGMEM = {6,6,0x84,0x48,0x78,0x84,0x78,0x0,};
const byte sever_right_1[] PROGMEM = {6,6,0x78,0x80,0xc8,0xb4,0x0,0xcc,};
const byte sever_right_2[] PROGMEM = {6,6,0x4,0x58,0x30,0x0,0x78,0x0,};
const byte sever_rear_1[] PROGMEM = {6,6,0X78,0X78,0X0,0X30,0X0,0XCC,};
const byte sever_rear_2[] PROGMEM = {6,6,0x84,0x0,0x78,0x84,0x78,0x0,};
const byte sever_left_1[] PROGMEM = {6,6,0x78,0x4,0x4c,0xb4,0x0,0xcc,};
const byte sever_left_2[] PROGMEM = {6,6,0x80,0x68,0x30,0x0,0x78,0x0,};

  int player_x = 20;
  int player_y = 20;
  int player_direction = 0;

int x=0,y=0;


void setup() {
  gb.begin();
  gb.titleScreen(F("test"));
  gb.display.persistence = false;
}


void loop() {
  if(gb.update()){
    if (gb.buttons.repeat(BTN_RIGHT,1));//{x--;}
    if (gb.buttons.repeat(BTN_LEFT,1));//{x++;}
    if (gb.buttons.repeat(BTN_DOWN,1));//{y--;}
    if (gb.buttons.repeat(BTN_UP,1));//{y++;}
    gb.display.drawTilemap(x,y,tilemap,spritesheet); // draw the tilemap
//    gb.display.cursorY = 12; gb.display.println( x );gb.display.println( y );

 if(gb.buttons.repeat(BTN_UP,1)){
       gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_rear_1);
       }
    gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_rear_2);
    }
      player_direction = 1;
      player_y = player_y - 1;
      if(checkcolision())player_y++;  // ADD by Summoner123 - If have colision on the new position regreat one Pixel
      }

     
    if(player_y <= 0){
      player_y = 0;}



    if(gb.buttons.repeat(BTN_DOWN,1)){
      gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_front_1);
    }
     gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_front_2);
    }
      player_direction = 2;
      player_y = player_y + 1;
      if(checkcolision())player_y--;  // ADD by Summoner123 - If have colision on the new position regreat one Pixel
      }
     
    if(player_y >= 40){
      player_y = 40;}
 


    if(gb.buttons.repeat(BTN_RIGHT,1)){
       gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_right_1);
    }
     gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_right_2);
    }
      player_direction = 3;
      player_x = player_x + 1;
      if(checkcolision())player_x--;  // ADD by Summoner123 - If have colision on the new position regreat one Pixel
      }
     
     
    if(player_x >= 77){
      player_x = 77;}


    if(gb.buttons.repeat(BTN_LEFT,1)){
       gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_left_1);
    }
    gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_left_2);
    }
      player_direction = 4;
      player_x = player_x - 1;
     
      if(checkcolision())player_x++;  // ADD by Summoner123 - If have colision on the new position regreat one Pixel
      }
     
    if(player_x <= -2){
      player_x = -2;}
     
      ////////////PLAYER DIRECTION/////////////

 if (player_direction == 1){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_rear_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_rear_2);
        }
 }
     
      else if (player_direction == 2){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_front_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_front_2);
        }
      }
else if (player_direction == 3){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_left_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_left_2);
        }
}
      else if (player_direction == 4){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_right_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_right_2);
        }
      }
      else {  gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_front_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_front_2);
        }
      }
}
 
}
     bool checkcolision() // Transformed it into a function
   {
    uint16_t i;
     for(i=0; i < gb.display.numcolision + 1; i++)
   {
    if(gb.collideRectRect(player_x,player_y,6,6,gb.display.solid[i].x,gb.display.solid[i].y,8,8))
    {
          if(gb.display.solid[i].spritecol == port_blanc); //Do nothing because it's floor - This line not needed
          if(gb.display.solid[i].spritecol == port_noir);
else      if(gb.display.solid[i].spritecol == rock_terrain){gb.popup(F(" ""Rock"" "),1); return true;} //Return True if character have touched the wall
else      if(gb.display.solid[i].spritecol == turtle_rock);
else      if(gb.display.solid[i].spritecol == bush);
else      if(gb.display.solid[i].spritecol == bush_bottom);
else      if(gb.display.solid[i].spritecol == bush_top);
      }
   }
    return false; // Return false if don't touch anything
  }     


This is the result:

Image

But I dont have added a code to move the camera... :mrgreen:
User avatar
Summoner123
 
Posts: 26
Joined: Fri Mar 20, 2015 12:35 am

Re: Newbs need help with collision detection.

Postby naed » Sat Aug 06, 2016 9:05 pm

That's awesome, looks like Duhjoker will need to create some more sprites at 8x8 if he's going to use your collision code

I've still not taken a proper look at this to see how it works as been struggling to get to a computer in the last few days

Hopefully I'll find time soon

EDIT

so i got to a computer this evening and took a look at Summoner123's collision code for tilemaps, and i must say it works amazingly well

although i got a little sidetracked playing with his demo code (the one he never released until now).... i simply had to make it so that his players head wasn't see-through :lol: :lol:

the demo, along with my extra bits added in came to 56% storage space used and i did notice some slowdown on the emulator whilst walking around (although nothing that would upset gameplay too much) and seeing as the map that was loaded was rather large 30x30 tiles (at 8x8 sprites) i think you could fit quite a bit into this game your trying to make

EDIT #2

also @Summoner123 i'm surprised it does not work with 6x6 tiles because it did work and display the map until we added your library for collision detection, not that its a major issue its just that 6x6 tiles fit perfectly into the Gamebuino's screen if only displaying one full screen
User avatar
naed
 
Posts: 140
Joined: Tue May 31, 2016 3:18 pm

Re: Newbs need help with collision detection.

Postby Duhjoker » Sat Aug 06, 2016 11:15 pm

Awesome thank you I can work with that.

8 bit might be a bit of a problem though. At least for most players. The problem being the resolution. The 6 by 6 sprites allow for me to fit all the elements details of each room with the monsters and character sprites being able to move between obstacles like rock terrain and turtle rocks, with the current resolution. So that gives us two options.

1 Build a Gamebuino with an LCD screen res of 220 X 176

Or

2 add camera commands.

While 1 is a good option, it will limit the amount of players who can actually play the game. 2 is the better option for for obvious reasons so we will go with that unless any one objects?

Lol fortunately for me at least a lil is that I have both 8 and 16-bit sprites made that just need a lil doctoring.

Plus the camera only has to move like one step up or one step down depending on the movement of the player Sprite.

Ok thanks guys I'm gonna play around with this tonight.
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby naed » Sat Aug 06, 2016 11:25 pm

I don't mind helping with the sprites if you give me an idea of what your looking for or want to change to 8x8

and it doesn't seem like the camera commands would be too much to implement, i for one would prefer this option so i can see how this project ends up looking (and preferably i get to play the game ;) )

EDIT

if you don't want to add camera commands you could always do the following

screen.jpg
screen.jpg (4.82 KiB) Viewed 107462 times


if you have the first and last tile placed just off screen so you can just see the edge, it would save on writing camera controls and having unnecessary scrolling, the yellow line in the pic above represents the edge of the Gamebuino screen
User avatar
naed
 
Posts: 140
Joined: Tue May 31, 2016 3:18 pm

Re: Newbs need help with collision detection.

Postby Duhjoker » Sun Aug 07, 2016 12:34 am

its ok on the sprites wont take long plus it gives a chance to take in what ive learned.

actually I think camera controls would be better for now so we keep the elements. Later if gamebuino decides to upgrade it will be simple to switch out to a higher res.
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby Summoner123 » Sun Aug 07, 2016 1:33 am

naed wrote:so i got to a computer this evening and took a look at Summoner123's collision code for tilemaps, and i must say it works amazingly well


Thanks Naed...

Duhjoker wrote:actually I think camera controls would be better for now



I was Working in another game using the same code, but at this time a plataformer (a poor port ou Dangerou Dave hahaha)... I think the camera manegement of this code is much Easier to unsderstand... Take a Look at the "renderiza()" Routine:

Code: Select all
#include <SPI.h>
#include <Gamebuino.h>

#define nojump 0
#define jumping 1
#define faling 2


#define flr 1
#define doorway 2
#define dead 3

#define goleft 1
#define goright 2

#define maxjump 26


Gamebuino gb;





const byte dv[] PROGMEM = {8,12,0xF0,0xFE,0xE8,0xF0,0x60,0xF0,0xF8,0xF8,0xE8,0x70,0xD8,0xEC,};
const byte dvl[] PROGMEM = {8,12,0x1E,0xFE,0x2E,0x1E,0xC,0x1E,0x3E,0x3E,0x2E,0x1C,0x36,0x6E,};

const byte dvjp[] PROGMEM = {16,12,0x3C,0x0,0x3F,0x80,0x3A,0x0,0x3C,0x0,0x18,0x0,0x3D,0x80,0xFF,0x80,0xDB,0x0,0x18,0x0,0xFF,0x0,0xF7,0x0,0x83,0x80,};
const byte dvw1[] PROGMEM = {8,12,0x78,0x7F,0x74,0x78,0x30,0x78,0xFC,0xB7,0x33,0x78,0xCD,0x66,};
const byte dvw2[] PROGMEM = {8,12,0x78,0x7F,0x74,0x78,0x30,0x78,0xFC,0xB6,0x32,0x78,0xCD,0x66,};

const byte dvjpl[] PROGMEM = {16,12,0x1E,0x0,0xFE,0x0,0x2E,0x0,0x1E,0x0,0xC,0x0,0xDE,0x0,0xFF,0x80,0x6D,0x80,0xC,0x0,0x7F,0x80,0x77,0x80,0xE0,0x80,};
const byte dvwl1[] PROGMEM = {8,12,0x1E,0xFE,0x2E,0x1E,0xC,0x1E,0x3F,0xED,0xCC,0x1E,0xB3,0x66,};
const byte dvwl2[] PROGMEM = {8,12,0x1E,0xFE,0x2E,0x1E,0xC,0x1E,0x3F,0x6D,0x4C,0x1E,0xB3,0x66,};

const byte stage1[] PROGMEM = {19,10,
8,8,
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1,
 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 1,
 1, 6, 6, 0, 6, 6, 6, 0, 6, 6, 6, 0, 6, 6, 6, 0, 6, 6, 1,
 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1,
 1, 0, 6, 6, 6, 0, 6, 6, 6, 0, 6, 6, 6, 0, 6, 6, 6, 0, 1,
 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1,
 1, 6, 6, 6, 0, 0, 0, 0, 6, 6, 6, 1, 0, 0, 0, 0, 0, 6, 1,
 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 3, 6, 6, 6, 6, 6, 1,
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } ;


const byte stage2[] PROGMEM = {50,10,  //  if(stage = 0)  xmax = (-1 * (pgm_read(&stage1[0]) * 8)) + 84;
8,8,
 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 1,
 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 0, 0, 0, 0, 6, 1, 1, 6, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
 1, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 0, 6, 6, 6, 6, 6, 1, 1, 6, 6, 6, 6, 6, 1, 5, 5, 5, 6, 5, 5, 5, 6, 5, 5, 6, 6,
 1, 6, 6, 6, 6, 6, 6, 6, 0, 0, 0, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 1, 0, 6, 6, 1, 0, 0, 1, 0, 0, 0, 0, 1, 6, 6, 1, 5, 6, 5, 6, 5, 6, 6, 6, 5, 6, 5, 6,
 1, 6, 0, 0, 6, 6, 6, 6, 6, 1, 6, 6, 6, 4, 1, 6, 0, 0, 0, 0, 0, 6, 1, 6, 6, 1, 0, 6, 6, 1, 6, 6, 6, 6, 1, 6, 6, 1, 5, 5, 5, 6, 5, 6, 6, 6, 5, 5, 6, 6,
 1, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 0, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 1, 6, 1, 0, 6, 6, 6, 1, 6, 6, 1, 6, 0, 6, 0, 1, 5, 6, 6, 6, 5, 6, 6, 6, 5, 6, 5, 6,
 1, 6, 6, 6, 0, 0, 0, 6, 6, 1, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 1, 6, 0, 6, 6, 0, 6, 6, 6, 0, 1, 6, 6, 6, 6, 1, 5, 6, 6, 6, 5, 5, 5, 6, 5, 6, 5, 6,
 1, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 0, 1, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 1, 6, 6, 6, 6, 6, 1, 6, 6, 1, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
 1, 1, 1, 5, 5, 5, 5, 5, 5, 1, 5, 5, 5, 5, 1, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 } ;




const byte block[] PROGMEM = {   
0xff,//B11110011,
0xff,//B00011111,
0xff,//B11111100,
0xff,//B11000111,
B00000000,
B00000000,
B00000000,
B00000000 } ;

const byte plataform[] PROGMEM = {   
0xff,//B11111111,
0xff,//B11011011,
0xff,//B10100101,
0xff,//B11011011,
0xff,//B11011011,
0xff,//B10100101,
0xff,//B11011011,
0xff//B11111111
} ;

const byte diamond[] PROGMEM = {   
B00000000,
B00111100,
B01111110,
B11111111,
B01111110,
B00111100,
B00011000,
B00000000 } ;

const byte door[] PROGMEM = {   
B01111110,
B01111110,
B01000010,
B01111110,
B01111010,
B01111110,
B01000010,
B01111110 } ;

const byte tace[] PROGMEM = {   
B11111111,
B11111111,
B01111110,
B00111100,
B00011000,
B00011000,
B00011000,
B00111100 } ;

const byte alga[] PROGMEM = {   
B01000101,
B10001001,
B10010010,
B10010100,
B01010010,
B00101010,
B00101001,
B01011101 } ;

const byte voidtile[] PROGMEM = {   
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000 } ;


const byte *spritesheet[] = {   block,plataform,diamond,door,tace,alga,voidtile } ;


typedef struct{
 
  int posx=10;
  int posy=24;
  byte jump = nojump;
  byte hjump = 0;
 
 
 
  const byte *bitmap = dv;
}character;
character player;


int camerax = 0;
int cameray = -32;

byte stage = 0;

unsigned int score = 0;
int xmax = -68;

extern const byte font3x5[];

bool tkey = false;

const byte *stagesheet[2] = {stage1,stage2};



void setup()
{
  gb.begin();
  gb.titleScreen(F("DD Port"),dvjp);
  gb.display.setFont(font3x5);
 
}

void loop()
{
  if(gb.update())
  {   
   anda();   // Anda = Walk in Portuguese
   checkcolision();
   renderiza();  //  Renderiza = Reder in Protuguese
  }
}

void anda()  //walk
{
  byte walk = 0;
 
     if(gb.buttons.repeat(BTN_RIGHT,1))
     {
     player.posx++;
     walk=goright;
    if(colisao() == flr)player.posx--;
   
    }
     
else if(gb.buttons.repeat(BTN_LEFT ,1)){
        player.posx--;
        walk=goleft;
        if(colisao() == flr)player.posx++;
       
      }
     
     if(gb.buttons.pressed(BTN_B) && player.jump == nojump){player.jump = jumping; player.hjump = 0; gb.sound.playTick();}
     


if(walk && player.jump == nojump)
     {
if(walk == goright)
{
       if(gb.frameCount % 12 >= 0 && gb.frameCount % 12 < 2)player.bitmap = dvw1;
      else if(gb.frameCount % 12 >= 2 && gb.frameCount % 12 < 4)player.bitmap = dvw1;
      else if(gb.frameCount % 12 >= 4 && gb.frameCount % 12 < 6)player.bitmap = dv;
      else if(gb.frameCount % 12 >= 6 && gb.frameCount % 12 < 8)player.bitmap = dv;
      else if(gb.frameCount % 12 >= 8 && gb.frameCount % 12 < 10)player.bitmap = dvw2;
      else if(gb.frameCount % 12 >= 10 && gb.frameCount % 12 < 12)player.bitmap = dvw2;
}
if(walk == goleft)
{
       if(gb.frameCount % 12 >= 0 && gb.frameCount % 12 < 2)player.bitmap = dvwl1;
      else if(gb.frameCount % 12 >= 2 && gb.frameCount % 12 < 4)player.bitmap = dvwl1;
      else if(gb.frameCount % 12 >= 4 && gb.frameCount % 12 < 6)player.bitmap = dvl;
      else if(gb.frameCount % 12 >= 6 && gb.frameCount % 12 < 8)player.bitmap = dvl;
      else if(gb.frameCount % 12 >= 8 && gb.frameCount % 12 < 10)player.bitmap = dvwl2;
      else if(gb.frameCount % 12 >= 10 && gb.frameCount % 12 < 12)player.bitmap = dvwl2;
}

     }

else if(player.jump == jumping)
     {
      if(player.hjump < maxjump) {player.posy -=2; player.hjump +=2;}
      if(colisao() == flr || player.hjump == maxjump)player.jump = faling;
       
       player.bitmap = dvjp;
 if(walk == goleft)player.bitmap = dvjpl;     
     }
     
else if(player.jump == faling)
      { //
           
        player.posy+=2;
        player.bitmap = dvjp;
        if(walk == goleft)player.bitmap = dvjpl;
        if(colisao() == flr)       
        {
          player.jump=nojump;
          player.bitmap = dv;
          player.posy -=2;
         // gb.popup(F("colision Block"),15);
        } 
    }
}


void renderiza()  //render
{
 
        if(player.posx > 35 && camerax != 0 && camerax != xmax){player.posx = 3 ;  camerax--;} //X Scrolling to the Right
else  if(player.posx < 30 && camerax != 0 && camerax != xmax){player.posx = 30; camerax++;} //X Scrolling to the Left
else  if(player.posx > 35 && camerax <= 0 && camerax >= xmax)camerax--;
else  if(player.posx < 30 && camerax <= 0 && camerax >= xmax)camerax++;
   
  if(camerax >0)camerax=0;
  if(camerax < xmax) camerax = xmax;
 
 
        if(player.posy > 14 ) {cameray-=2; player.posy -=2;}    //Y Scrolling  Down
else  if(player.posy < 10 ){cameray+=2; player.posy +=2;}   //Y Scrolling  Up

 
 gb.display.drawTilemap(camerax,cameray,stagesheet[stage],spritesheet);
 gb.display.drawBitmap(player.posx,player.posy,player.bitmap);
 
 
 gb.display.cursorY = 43;
 
 if(tkey)
 {
   gb.display.cursorX = 0;
   gb.display.print(F("To the door"));
 }
 gb.display.cursorX = 48;
 
 gb.display.print(F(" Score: ")); gb.display.print(score * 10);
 if(gb.buttons.pressed(BTN_A))Serial.println(player.posy);
 
}


byte colisao()
{
  byte i =0;
 
  byte height = 8;
 

  for(i=0; i < gb.display.numcolision + 1; i++)
   {
     if(gb.display.solid[i].spritecol == block)height = 4;
     
    if(gb.collideRectRect(player.posx,player.posy,8,12,gb.display.solid[i].x,gb.display.solid[i].y,8,height))
      {
                   
              if(gb.display.solid[i].spritecol == door)return doorway;
    else      if(gb.display.solid[i].spritecol == plataform )return flr;
    else      if(gb.display.solid[i].spritecol == block )return flr;
    else      if(gb.display.solid[i].spritecol == alga)return dead;
    else      if(gb.display.solid[i].spritecol == tace)
             {
              spritesheet[4] = voidtile;
              tkey=true;
             }
             
       
      }   
  }
  return 0; 
}


void checkcolision()
{
 
  byte collisionstate = colisao();
 
  player.posy+=2;
  if(colisao() != flr  && player.jump == nojump)player.jump = faling;
  player.posy-=2;
 
  if(collisionstate == doorway && tkey == true)
  {
  player.posx = 10;
  player.posy = 24;
 
  camerax = 0;
  cameray = -32;
 
  if(stage == 1)stage = 0;
  else stage = 1;
 
  tkey = false;
 
  spritesheet[4] = tace;

  if(stage == 1)  xmax = (-1 * (pgm_read_byte(&stage2[0]) * 8)) + 84; 
  if(stage == 0)  xmax = (-1 * (pgm_read_byte(&stage1[0]) * 8)) + 84;
 // Serial.println(stage);
  }
 
  if(collisionstate == dead)
  {
  player.posx = 10;
  player.posy = 24;
 
  camerax = 0;
  cameray = -32;   
  }




This is the result:

Image

The Scrolling code work well, so think just removing the "gravity" it can work for you...
User avatar
Summoner123
 
Posts: 26
Joined: Fri Mar 20, 2015 12:35 am

PreviousNext

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 3 guests

cron