I need help with chests


(Cody) #21

I can open the chest as many times as I want… Is there a way of only being able to do it once


(Pharap) #22

Oops, didn’t think about that.

Yep, just check to make sure the chest hasn’t been opened already:

if(Chests[i].x == x && Chests[i].y == y && !Chests[i].opened)
{
	score += 10;
	Chests[i].opened = true;
	break;
}

(Cody) #23

Thanks @pharap I will add this in the mornin
Thanks all!
Now…enemy Ai I wonder…
Just kiddin! Or am I?


(Pharap) #24

Walk before you run Cody - make sure you understand the code you’ve got before you go adding more.

Get yourself a rubber duck and explain the code to the duck.


[WIP] Maze of Doom - 1 bit FPS
(Scott) #25

I did. That’s why I changed it to
constexpr size_t ChestCount = sizeof(chests) / sizeof(chests[0]);
in my size calculation example.


(Scott) #26

@Pharap and @filmote,
Sorry for being critical but, for the most part, I think you are basically writing the code for @Cody17 each time he asks a question. It may be better to just provide pseudo-code and force him to learn how to understand, translate and write the actual code. If he asks how to translate something, for instance a control structure, provide a link or instructions on the generic syntax. Just cutting and pasting then complaining about errors isn’t accomplishing much.

Again, sorry if you feel I’m out of line here.


(Cody) #27

:smirk: I read that artical.


(Simon) #28

I kind of agree with you @MLXXXp but people learn in different ways. This isn’t school homework we are doing for @Cody17 and you hope when helping someone they absorb the detail. If the same questions get asked again and again then I tend to tune out :grinning:

As a parent of a child with high-functioning ASD, I know that sometimes you need to be explicit and referring someone to a c++ manual will not necessarily work. Why is it that all of the c++ references on the web are so ‘academic’?

@Cody17 do you mind me asking how old you are? I am guessing 17 but could be wrong.


(Cody) #29

I only had that name because the Cody was already taken it think I would have to check. And my age must stay confidential sorry.:grimacing:


(Simon) #30

No problem … just checking up on the homework question :grinning:


(Cody) #31

@pharap your code did not work I can still open the chest to infinite.


(Pharap) #32

This is precisely why I hate mathematicians.
Mathematicians love writing overly complicated bumf instead of explaining things simply. (And I know it can be explained in simple terms because places like (for example) mathsisfun manage it.)


Are you sure? It should be working.

If you post what code you’ve got so far and I’ll see if there’s something missing or backwards or something.


(Cody) #33
#include <Arduboy2.h>
#include <ATMlib.h>
Sprites sprites;
#include "Map.h"
Arduboy2 arduboy;


int gamestate = 0;
int FRAMES = 40;
int vol = 8;
int wait = 0;
String GameName = "Risk of Pain";
uint8_t Floor = 50;
bool jump = false;
uint8_t playerHieght = 6;
uint8_t playerWidth = 4;
bool player_walking_left = false;
bool player_walking_right = false;
int score  = 0;

int count = 0;
constexpr const static uint8_t ChestCount = 4;

void setup()
{
  arduboy.begin();
  arduboy.setFrameRate(FRAMES);
  playerY = Floor;
 
  
}
 void loop()
 {
  if(!arduboy.nextFrame())
  return;
  switch( gamestate){
    case 0:
    arduboy.clear();
    arduboy.pollButtons();
    arduboy.setCursor(30,40);
    arduboy.print(GameName);
    arduboy.setCursor(40,50);
    arduboy.print(F("Press A"));
    arduboy.display();

    if(arduboy.justPressed(A_BUTTON))
    {
      gamestate = 1;
    }
    
    break;
    case 1:
    
    arduboy.clear();
    arduboy.pollButtons();




 uint8_t x= (playerX / TileWidth);
uint8_t y= (playerY / TileHeight);
    uint8_t tile = getMapTile(x,y);
switch((TileTypes)tile)
{
 
case TileTypes::Nothing: 
{

}
break;
case TileTypes::Ladder:

{
if(arduboy.pressed(UP_BUTTON))
{
  Floor = 16;
  vol = 3;
  jump = true;
 
}
if(arduboy.pressed(DOWN_BUTTON))
{
  Floor = 50;
  vol = 3;
  jump = true;
}
}
break;
case TileTypes::Chest:
{

  if(arduboy.justPressed(B_BUTTON))
 {
  for(byte i = 0; i < ChestCount; i++)
  {
  if(Chests[i].x == x && Chests[i].y == y && !Chests[i].opened)
  {
    
      score+=10;
     
    count+=1;
    Chests[i].opened = true;
    break;
     
  }
 }
}
 



}
}
        for(uint8_t y = 0; y < MapHeight; ++y)
{
  for(uint8_t x = 0; x < MapWidth; ++x)
  {
    sprites.drawOverwrite(x * TileWidth, y * TileHeight, tileSheet, getMapTile(x,y));
  }
}

   if(!(arduboy.pressed(LEFT_BUTTON)||arduboy.pressed(RIGHT_BUTTON)))
   {
    arduboy.drawBitmap(playerX,playerY,Idle,16,16,WHITE);
   }
    if(arduboy.justPressed(A_BUTTON) and playerY == Floor and !(Floor == 16))
    {
      jump = true;
      vol = 6;
    }
  
   
   
if(jump == true)
    {
      playerY=playerY-vol;
      vol--;
      
    }
    if(vol == 0)
    {
      jump = false;
    }
    if(jump == false)
    {
     playerY=playerY+vol; 
     vol++;
    
    }
    if(vol > 10)vol = 10;
    if(vol < 0)vol = 0;
    if(playerY > Floor)playerY=Floor;
    if(arduboy.pressed(LEFT_BUTTON))
    {
      player_walking_left = true;
    }
    else 
    {
      player_walking_left = false;
    }
    if(arduboy.pressed(RIGHT_BUTTON))
    {
      player_walking_right = true;
    }
    else
    {
      player_walking_right = false;
    }
  
    if(player_walking_left == true)
    {
      playerX--;
      wait++;
      if(wait == 1)
      {
           arduboy.drawBitmap(playerX,playerY,WalkLeft,16,16,WHITE);
      }
      if(wait > 1)wait = 0;
      if(wait < 1)
      {
        arduboy.drawBitmap(playerX,playerY,WalkRight,16,16,WHITE);
      }
      
    }
    if(player_walking_right == true)
    {
      playerX++;
      wait++;
     
      if(wait == 1)
      {
           arduboy.drawBitmap(playerX,playerY,WalkLeft,16,16,WHITE);
      }
      if(wait > 1)wait = 0;
      if(wait < 1)
      {
        arduboy.drawBitmap(playerX,playerY,WalkRight,16,16,WHITE);
      }
      
    }
    if(arduboy.justPressed(B_BUTTON))
    {
      
    }
   arduboy.print(score);
Serial.println(uint8_t(tile));
    arduboy.display();
 }}

(Pharap) #34

I can’t see where Chests is defined. Is that in Map.h?


(Cody) #35

Yep! Why do I need to move it?


(Pharap) #36

You don’t need to move it but it would be helpful to see it to make sure there aren’t any typos or some other reason why this wouldn’t be working.


(Cody) #37
#pragma once
const unsigned char PROGMEM tileSheet[] =
{
  
  
  8, 4,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
   
0xFF,0x24,0x24,0xFF,
0x3F,0x09,0x09,0x3F,

0x0F,0x09,0x09,0x0F,
0x00,0x00,0x00,0x00,

0x0F,0x09,0x09,0x0F,
0x00,0x00,0x00,0x00,
};
const unsigned char PROGMEM mapArray[] =
{
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,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,0,0,0,0,0,0,
1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,3,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,
1,2,2,2,2,1,2,2,2,2,1,2,2,1,2,2,2,2,1,2,2,2,2,2,2,1,2,1,2,2,2,0,
1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
1,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,3,0,1,0,0,0,0,0,0,
};
const unsigned char Idle[] PROGMEM =
{
  0x1C,0xFF,0xFF,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
const unsigned char WalkLeft[] PROGMEM =
{

 0x1C,0xFF,0x7F,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

};
const unsigned char WalkRight[] PROGMEM =
{
0x0C,0x7F,0xFF,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
enum class TileTypes : uint8_t {
  
  Nothing,
  Ladder,
  Floor,
  Chest,
};
static const uint8_t MapWidth = 32;
static const uint8_t MapHeight = 7;

static const uint8_t TileWidth = 4;
static const uint8_t TileHeight = 8;
uint8_t playerX = 0;
uint8_t playerY = 0;



struct Chest
{
  uint8_t x;
  uint8_t y;
  bool opened;
};

Chest Chests[] =
{
  { 3, 4, false}
};



uint8_t getMapTile(uint8_t x,uint8_t y)
{
  return pgm_read_word(&mapArray[x + (MapWidth * y)]);
}

(Pharap) #38

Ah, glad I asked that.
The problem is that ChestCount (in your .ino file) is 4, but the Chests array actually only has 1 Chest in it, which means you’re reading memory that belongs to something else.

Chest Chests[] =
{
  { 3, 4, false}
};

You can either lower ChestCount to 1:

constexpr const static uint8_t ChestCount = 4;

Or you can add more chests:

Chest Chests[] =
{
  { 3, 4, false }, // x, y, opened
  { 2, 2, false },
  { 4, 4, false },
  { 5, 4, false }
};

Also don’t forget that the chests in your Chests array have to have their x and y coordinates match up to the coordinates of tiles in your map.
At the moment it looks like they don’t match up.
It looks like in your current map the chests are located at (16, 2), (5, 6), (19, 6) and (23, 6).


(Cody) #39

I’ve changed it to one but it refuses to open or add my score


(Simon) #40

Have you put some serial.printlns in to debug it?