[SOLVED] Scrolling problem

Advice on general approaches or feasibility and discussions about game design

[SOLVED] Scrolling problem

Postby awesome101 » Fri Jul 17, 2015 10:09 pm

After finishing super maruino land, I wanted to keep up with my Nintendo series and create a game similar to pokemon. My side scrolling is kinda screwed up, when I walk up or left all the tiles go away. But if I go down and right, it works fine.

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

#include "bitmaps.h"
#include "towns.h"
#include "player.h"

#define TOWN_W 18
#define TOWN_H 16

Gamebuino gb;
Player player(24, 8);

byte twndata[TOWN_W * TOWN_H];
int camera_x = 0, camera_y = 0;

void loadTown(byte curTown) {
  for(int i = 0; i < TOWN_W * TOWN_H; i++){
    twndata[i] = pgm_read_byte(towns + (TOWN_W * TOWN_H) * (curTown - 1) + i);
  }
}

void drawTile(byte id, int x, int y) {
  byte *tileToDraw = NULL;

            switch(id) {
              case 2: {tileToDraw = tileset002; break;}
              case 3: {tileToDraw = tileset003; break;}
              case 4: {tileToDraw = tileset004; break;}
              case 5: {tileToDraw = tileset005; break;}
              case 6: {tileToDraw = tileset006; break;}
              case 7: {tileToDraw = tileset007; break;}
              case 8: {tileToDraw = tileset008; break;}
              case 9: {tileToDraw = tileset009; break;}
              case 10: {tileToDraw = tileset010; break;}
              case 11: {tileToDraw = tileset011; break;}
              case 12: {tileToDraw = tileset012; break;}
              case 13: {tileToDraw = tileset013; break;}
              case 14: {tileToDraw = tileset014; break;}
              case 15: {tileToDraw = tileset015; break;}
              case 16: {tileToDraw = tileset016; break;}
              case 17: {tileToDraw = tileset017; break;}
              case 18: {tileToDraw = tileset018; break;}
              case 19: {tileToDraw = tileset019; break;}
              case 20: {tileToDraw = tileset020; break;}
              case 21: {tileToDraw = tileset021; break;}
              case 22: {tileToDraw = tileset022; break;}
            }
           
  if(tileToDraw != NULL) {
    if(x >= 0 && x <= LCDWIDTH && y >= 0 && y <= LCDHEIGHT) {
      gb.display.drawBitmap(x, y, tileToDraw, NOROT, NOFLIP);
    }
  }
}

void drawTown() {
  camera_x = player.x;
  camera_y = player.y;
  for(int y = 0;y < TOWN_H;y++) {
    for(int x = 0;x < TOWN_W;x++) {
      drawTile(twndata[x + y * TOWN_W], (x << 3) - camera_x, (y << 3) - camera_y);
    }
  }
}

void setup() {
  gb.begin();
  gb.titleScreen(F("PIKEMONZ"));
  loadTown(1);
}

void loop() {
  if(gb.update()) {
    gb.display.clear();
    drawTown();
    player.render(gb);
  }
}



Player.h:
Code: Select all
#ifndef PLAYER_H
#define PLAYER_H

class Player {
  public:
  Player(int x, int y) {
    this->x = x;
    this->y = y;
    this->velX = 0;
    this->velY = 0;
    this->flip = false;
    this->bitMap = nash_frame_0;
  }
 
  int x, y;
  byte velX, velY;
  boolean flip;
  byte *bitMap;
 
  void render(Gamebuino gb) {
    if(gb.buttons.repeat(BTN_RIGHT, 1)) {
      velX = 1;
      flip = false;
      if(gb.frameCount % 6 >= 0 && gb.frameCount % 6 < 3) {
        bitMap = nash_frame_3;
      } else if(gb.frameCount % 10 >= 0 && gb.frameCount % 10 < 3) {
        bitMap = nash_frame_2;
      } else {
        bitMap = nash_frame_4;
      }
    } else if(gb.buttons.repeat(BTN_LEFT, 1)) {
      velX = -1;
      flip = true;
      if(gb.frameCount % 6 >= 0 && gb.frameCount % 6 < 3) {
        bitMap = nash_frame_3;
      } else if(gb.frameCount % 10 >= 0 && gb.frameCount % 10 < 3) {
        bitMap = nash_frame_2;
      } else {
        bitMap = nash_frame_4;
      }
    }
    else {
      velX = 0;
    }
   
    if(gb.buttons.repeat(BTN_UP, 1)) {
      velY = -1;
      if(gb.frameCount % 6 >= 0 && gb.frameCount % 6 < 3) {
        flip = false;
        bitMap = nash_frame_0;
      } else if(gb.frameCount % 10 >= 0 && gb.frameCount % 10 < 3) {
        flip = false;
        bitMap = nash_frame_1;
      } else {
        flip = true;
        bitMap = nash_frame_1;
      }
    } else if(gb.buttons.repeat(BTN_DOWN, 1)) {
      velY = 1;
      if(gb.frameCount % 6 >= 0 && gb.frameCount % 6 < 3) {
        flip = false;
        bitMap = nash_frame_5;
      } else if(gb.frameCount % 10 >= 0 && gb.frameCount % 10 < 3) {
        flip = false;
        bitMap = nash_frame_6;
      } else {
        flip = true;
        bitMap = nash_frame_6;
      }
    }
    else {
      velY = 0;
    }
   
    x += velX;
    y += velY;
   
    if(!flip) {
      gb.display.drawBitmap(x, y, bitMap, NOROT, NOFLIP);
    } else {
      gb.display.drawBitmap(x, y, bitMap, NOROT, FLIPH);
    }
  }
};

#endif


Thanks :D
Attachments
pokeuino.gif
pokeuino.gif (157.62 KiB) Viewed 4103 times
Last edited by awesome101 on Sat Jul 18, 2015 2:18 pm, edited 1 time in total.
awesome101
 
Posts: 159
Joined: Sat Jun 20, 2015 6:56 pm

Re: Scrolling problem

Postby awesome101 » Sat Jul 18, 2015 2:07 pm

If anyone could tell me how to scroll the game vertically and horizontally, that would be great. Thanks!
awesome101
 
Posts: 159
Joined: Sat Jun 20, 2015 6:56 pm

Re: [SOLVED] Scrolling problem

Postby awesome101 » Sat Jul 18, 2015 2:19 pm

Nevermind guys, it was because my velX and velY variables were bytes so when I set them to -1, it crashed.
awesome101
 
Posts: 159
Joined: Sat Jun 20, 2015 6:56 pm

Re: [SOLVED] Scrolling problem

Postby Sorunome » Sat Jul 18, 2015 3:06 pm

if it is inside the [-128,127] range you might wanne try 'char' instead of 'int' to get more speed.
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: [SOLVED] Scrolling problem

Postby awesome101 » Sat Jul 18, 2015 6:49 pm

Yeah I'm using int8_t.
awesome101
 
Posts: 159
Joined: Sat Jun 20, 2015 6:56 pm

Re: [SOLVED] Scrolling problem

Postby Sorunome » Sat Jul 18, 2015 9:53 pm

Ok, that should work, too
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm


Return to Project Guidance & Game development

Who is online

Users browsing this forum: No registered users and 95 guests