Finally got it- connecting tilemaps & inventory (explained)

Understanding the language, error messages, etc.

Re: Finally got it- connecting tilemaps & inventory (explain

Postby naed » Tue Oct 11, 2016 8:49 am

Yes the function needs to run every time the room changes.

The function tells you which tilemap to open when the room changes so is very important
User avatar
naed
 
Posts: 140
Joined: Tue May 31, 2016 3:18 pm

Re: Finally got it- connecting tilemaps & inventory (explain

Postby naed » Thu Oct 13, 2016 12:10 am

ADMIN: External link to picture removed. Please use "Upload attachment" to avoid broken links.

just a bit of an update to my project - I've added animation to the inventory screen to give it a better feel than the popup screen i had before - its relatively smooth and looks pretty good - i do get a little glitch between the animations but for now it looks ok until i can refine the code a little bit more
User avatar
naed
 
Posts: 140
Joined: Tue May 31, 2016 3:18 pm

Re: Finally got it- connecting tilemaps & inventory (explain

Postby naed » Tue Oct 25, 2016 10:02 pm

did some work on my inventory today

i can now pick up items and they will place in the furthest left free slot

although i'm not amazingly happy with my coding skills, if anyone can help me refine the code and teach me something whilst doing so i'd be grateful

there are 3 items you can pick up (2 keys and a sock?) and i've taken away the need for the first key to get into the house for testing puposes

any advice appreciated

http://pastebin.com/ftxz38jw

please don't forget i'm learning as i go :D

EDIT - too many characters to post full code so i pastebinned it
User avatar
naed
 
Posts: 140
Joined: Tue May 31, 2016 3:18 pm

Re: Finally got it- connecting tilemaps & inventory (explain

Postby Duhjoker » Fri Jul 21, 2017 11:12 pm

Im trying to implement a change of maps but I keep getting these errors when I try to compile.......

Code: Select all
Arduino: 1.8.2 (Windows 7), TD: 1.36, Board: "Teensy 3.6, Serial, 180 MHz, Faster, US English"

Dune_RPG: In function 'void setup()':
Dune_RPG:122: warning: unused variable 'start_time'
      uint32_t start_time = millis();

               ^

Dune_RPG: In function 'void loop()':
Dune_RPG:201: warning: large integer implicitly truncated to unsigned type
                                     palette[6] = PINK;

                                                ^

Dune_RPG: In function 'bool checkcolision()':
Dune_RPG:323: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
       else if(tft.solid[i].spritecol == cave) && room == 1) {room = 2; player_x = 32; player_y = 204; cameraX = 170; cameraY = 110;} //return true;

                                                          ^

Dune_RPG:323: error: expected ';' before ')' token
       else if(tft.solid[i].spritecol == cave) && room == 1) {room = 2; player_x = 32; player_y = 204; cameraX = 170; cameraY = 110;} //return true;

                                                           ^

Dune_RPG:323: warning: statement has no effect
       else if(tft.solid[i].spritecol == cave) && room == 1) {room = 2; player_x = 32; player_y = 204; cameraX = 170; cameraY = 110;} //return true;

             

                                          ^

Dune_RPG:323: error: label 'room' used but not defined
       else if(tft.solid[i].spritecol == cave) && room == 1) {room = 2; player_x = 32; player_y = 204; cameraX = 170; cameraY = 110;} //return true;

                                                  ^

ISO C++ forbids comparison between pointer and integer [-fpermissive]

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.



Here is my code........
Code: Select all

/*This is the Main function file for the GameR-Iot Multiple MCU Arduino driven
 * game system and DIY console. This is a demo game RPG set in the Dune Universe
 * created by Frank Herbert.
 */
#include <GrafxT3.h>
#include <SPIN.h>
#include "SPI.h"
#include <Bounce.h>
#include "Player.h"
#include "World.h"
#include "Monsters.h"
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#define TFT_DC  9
#define TFT_CS 10
#define TFT_RST 7
#define TFT_SCK 13
#define TFT_MISO 12
#define TFT_MOSI 11
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
uint8_t use_fb = 1;
uint8_t use_clip_rect = 0;
uint8_t use_set_origin = 0;
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
GrafxT3 tft = GrafxT3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCK, TFT_MISO, &SPIN);
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
 int player_x = 240;
 int player_y = 320;
 int player_direction = 2;
 int x=-0,y=0;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Camera offset
int cameraX = -160;  /////starting position X 0f camera on tilemap
int cameraY = -120;  /////starting position Y of camera on tilemap

// Camera speed in pixels per update
int cameraXSpeed = 3;
int cameraYSpeed = 3;

// Camera offset boundaries
const int cameraXMin = -2080;
const int cameraXMax = 0;
const int cameraYMin = -2144;
const int cameraYMax = 0;

// Player offset boundaries
const int playerXMin = 80;
const int playerXMax = 240;
const int playerYMin = 80;
const int playerYMax = 160;

int animTransition;
int room = 1;
int co_ords = 1;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////Pixel Color Includes////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
uint16_t palette[16];  // Should probably be 256, but I don't use many colors...
uint16_t pixel_data[2500];

//Extra integers for color palette
int a = 0xa; int b = 0xb; int c = 0xc;
int d = 0xd; int e = 0xe; int f = 0xf;

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////Button assignments//////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//////dpad + select buttons
const int buttonUp = 33; //up button
Bounce ButtonUp = Bounce(buttonUp, 10);  // 10 ms debounce
const int buttonDown = 38; //down_button
Bounce ButtonDown = Bounce(buttonDown, 10);  // 10 ms debounce
const int buttonLeft = 35; //left button
Bounce ButtonLeft = Bounce(buttonLeft, 10);  // 10 ms debounce
const int buttonRight = 17; //right button
Bounce ButtonRight = Bounce(buttonRight, 10);  // 10 ms debounce
const int buttonS = 21; //select button
Bounce ButtonS = Bounce(buttonS, 10);  // 10 ms debounce

//////action + start buttons
const int buttonX = 32; // X button up
Bounce ButtonX = Bounce(buttonX, 10);  // 10 ms debounce
const int buttonY = 26; // Y button left
Bounce ButtonY = Bounce(buttonY, 10);  // 10 ms debounce
const int buttonA = 21; // A button right
Bounce ButtonA = Bounce(buttonA, 10);  // 10 ms debounce
const int buttonB = 28; // B buttun down
Bounce ButtonB = Bounce(buttonB, 10);  // 10 ms debounce
const int buttonT = 4; // Start button
Bounce ButtonT = Bounce(buttonT, 10);  // 10 ms debounce

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////Set-up//////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void setup() {
  while (!Serial && (millis() < 4000)) ;
  Serial.begin(115200);
  tft.begin();
  tft.setRotation(1);
  tft.fillScreen(BLACK);
  //tft.setFrameRate(60);
  tft.persistence = false;
   pinMode(buttonUp, INPUT_PULLUP);
   pinMode(buttonDown, INPUT_PULLUP);
   pinMode(buttonLeft, INPUT_PULLUP);
   pinMode(buttonRight, INPUT_PULLUP);
   pinMode(buttonS, INPUT_PULLUP);
   pinMode(buttonX, INPUT_PULLUP);
   pinMode(buttonY, INPUT_PULLUP);
   pinMode(buttonA, INPUT_PULLUP);
   pinMode(buttonB, INPUT_PULLUP);
   pinMode(buttonT, INPUT_PULLUP);
 
   tft.useFrameBuffer(use_fb);
     uint32_t start_time = millis();
//       use_fb = !use_fb;
 
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////Loop////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void loop(void) {
   //updates the GameRIot (the display, the sound, the buttons, everyyhing)
  //returns true when it's time to render a new frame (20 times/second)
//   if(tft.updateAll()){

///////////////////////////////////////////////////////////////////////////////
////////////////////////////////camera controls////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Clamp cameraX
if(cameraX < cameraXMin)
{
  cameraX = cameraXMin;
}
else if(cameraX > cameraXMax)
{
   cameraX = cameraXMax;
}

// Clamp cameraY
if(cameraY < cameraYMin)
{
  cameraY = cameraYMin;
}
else if(cameraY > cameraYMax)
{
   cameraY = cameraYMax; 
}

// Check if player is beyond X boundary
if(player_x < playerXMin)
{
  cameraX += cameraXSpeed;
  if(cameraX > cameraXMin && cameraX < cameraXMax)
  {
    player_x = playerXMin;
  }
}
else if(player_x > playerXMax)
{
  cameraX -= cameraXSpeed;
  if(cameraX > cameraXMin && cameraX < cameraXMax)
  {
    player_x = playerXMax;
  }
}

// Check if player is beyond Y boundary
if(player_y < playerYMin)
{
  cameraY += cameraYSpeed;
  if(cameraY > cameraYMin && cameraY < cameraYMax)
  {
    player_y = playerYMin;
  }
}
else if(player_y > playerYMax)
{
  cameraY -= cameraYSpeed;
  if(cameraY > cameraYMin && cameraY < cameraYMax)
  {
    player_y = playerYMax;
  }
}
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////Palette////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
 palette[0] = 0;
       palette[1] = BLACK;
             palette[2] = BLUE;
                   palette[3] = BROWN;
                         palette[4] = DARKGREEN;
                              palette[5] = GREY;
                                    palette[6] = PINK;
                                          palette[7] = RED;
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////                                               
                                           palette[8] = LIGHTBROWN;
                                     palette[9] = GREEN;
                               palette[a]= DARKGREY;
                         palette[b] = LIGHTGREY;
                   palette[c] = YELLOW;
             palette[d] = PURPLE;
       palette[e] = WHITE;
 palette[f] = NAVY;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////Tilemap/////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
     if(room == 1){
  tft.drawTilemap(cameraX, cameraY, dune_demo, spritesheet, palette);}
     if(room == 2){
  tft.drawTilemap(cameraX = 0, cameraY = 0, room1, spritesheet, palette);}
 
///////////////////////////////////////////////////////////////////////////////
///////////////////////////Buttons/////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// 
       if (ButtonUp.update());
               if (ButtonDown.update());
                       if (ButtonLeft.update());
                             if (ButtonRight.update());
                                       if (ButtonA.update());
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
                                       ButtonUp.rebounce(10);
                               ButtonDown.rebounce(10);
                       ButtonLeft.rebounce(10);
            ButtonRight.rebounce(10);
     ButtonA.rebounce(10);
///////////////////////////////////////////////////////////////////////////////
////////////////////////////Up/////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
 if (ButtonUp.fallingEdge()){
     tft.writeRectNBPP(player_x, player_y,16,16,4,paulrearw,palette);
     player_direction = 1;
     player_y -= 3;
     if(checkcolision())
     {
      player_y += 3;}
     }
     if(player_y <= 32){
        player_y = 32;}
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////Down///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
 if (ButtonDown.fallingEdge()){
   tft.writeRectNBPP(player_x, player_y,16,16,4,paulfrontw,palette);
   player_direction = 2;
   player_y += 3;
    if(checkcolision())
    {
    player_y -=3;}
    }
    if(player_y >= 216){
       player_y = 216;}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////Left////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
 if (ButtonLeft.fallingEdge()){
   tft.writeRectNBPP(player_x, player_y,16,16,4,paulleftw,palette);
   player_direction = 3;
   player_x -= 3;
   if(checkcolision())
   {
      player_x += 3;} 
   }
   if(player_x >= 288){
      player_x = 288;}
//////////////////////////////////////////////////////////////////////////////
////////////////////////////Right////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
if (ButtonRight.fallingEdge()){
  tft.writeRectNBPP(player_x, player_y,16,16,4,paulrightw,palette);
  player_direction = 4;
  player_x += 3;
  if(checkcolision())
  {
    player_x -= 3;}
  }
            if(player_x <= 32){
              player_x = 32;}
///////////////////////////////////////////////////////////////////////////////     
//////////////////////////////PLAYER DIRECTION/////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
if (player_direction == 1){
  tft.writeRectNBPP(player_x, player_y,16,16,4,paulrear,palette);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
else if (player_direction == 2){
   tft.writeRectNBPP(player_x, player_y,16,16,4,paulfront,palette);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
else if (player_direction == 3){
    tft.writeRectNBPP(player_x, player_y,16,16,4,paulleft,palette);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
else if (player_direction == 4){
     tft.writeRectNBPP(player_x, player_y,16,16,4,paulright,palette);
        }
       tft.updateScreen();
     }
//}
/////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////collision/////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
bool checkcolision(void) // Transformed it into a function
{
  for(uint16_t i=0; i < tft.numcolision + 1; i++)
  {
    if(tft.collideRectRect(player_x, player_y,16,16,tft.solid[i].x,tft.solid[i].y,16,16))
    {
     
           if(tft.solid[i].spritecol == rock){tft.popup(F(" ""Rock"" "),1); return true;} //Return True if character have touched the wall//return true;
      else if(tft.solid[i].spritecol == cave) && room == 1) {room = 2; player_x = 32; player_y = 204; cameraX = 170; cameraY = 110;} //return true;
      else if(tft.solid[i].spritecol == rockbl)return true;
      else if(tft.solid[i].spritecol == rockbr)return true;
      else if(tft.solid[i].spritecol == rocktl)return true;
      else if(tft.solid[i].spritecol == rocktr)return true;
      else if(tft.solid[i].spritecol == seitch) return true;
      else if(tft.solid[i].spritecol == stairsl) return true;
      else if(tft.solid[i].spritecol == stairsr) return true;
    }
  }
  return false; // Return false if don't touch anything
}

//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
 



Edit::::::

This works!!! Thank you naed for your explanation of how this works. Once I understood that I was able to use it. The problem with the above just needed a second pair of eyes. I was missing an extra ( in the collision functions call to switch rooms. Easy fix.
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Previous

Return to Programming Questions

Who is online

Users browsing this forum: Google [Bot] and 9 guests

cron