Problem on my Game

Its my main code

Player witch;

boolean left, up, right,jump,test;
PImage playerArt;
PImage fondImage,cloud1,cloud2,rainbow1,rainbow2,tree1,tree2,tree3,tree4,grass,cobble,fondgris,fondmar,fondmar2;
int [] maprow= {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int [][] tilemap = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},  
  {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, 1},
  {1, 0, 0, 0, 0, 0, 2, 3, 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, 0, 0, 0, 0, 0, 4, 5, 0, 0, 0, 0, 1},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 13, 14, 10, 10, 10, 10, 10},
  {11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 11, 11, 11, 11, 11},
  {11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11},
  {11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11},
  {11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11},
  {11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11},
  {11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11},
  {11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}
};
int row,cols;
int cellWidth, cellHeight;

void setup(){
 fullScreen(P2D); 
 size(640,640);
 //row = 10;
 //cols = 10;
 row = tilemap.length;
 cols = tilemap[0].length;
 cellWidth = 64;//pour augmenter taille faire plus 64 et ajouter row ou cols Ă  la tilemap
 cellHeight = 64;
 witch = new Player();
 left = false;
 right=false;
 up = false;
 test = false;
 playerArt = loadImage("witch3.png");
 fondImage = loadImage("fond.png");
 cloud1 = loadImage("cloud1.png");
 cloud2 = loadImage("cloud2.png");
 rainbow1 = loadImage("rainbow1.png");
 rainbow2 = loadImage("rainbow2.png");
 tree1 = loadImage("tree1.png");
 tree2 = loadImage("tree2.png");
 tree3 = loadImage("tree3.png");
 tree4 = loadImage("tree4.png");
 grass = loadImage("grass.png");
 cobble = loadImage("cobble.png");
 fondmar = loadImage("fondmar.png");
 fondmar2 = loadImage("fondmar2.png");
 fondgris = loadImage("fondgris.png");
}

void draw(){
  background(120);
  renderMap();
  witch.update();
  checkWallCollisions(witch);
  witch.display();
}
void checkWallCollisions(Player witch){
  String collisionSide = "none";
  for(int i=0; i<row; i++){
    for(int j=0; j<cols; j++){
      if(tilemap[i][j] == 1 || tilemap[i][j] == 10 || tilemap[i][j] == 11){//check si c'est un mur//pour plus tard
        int distX = floor((witch.x + witch.w/2)-(j*cellWidth+cellWidth/2));
        int distY = floor((witch.y + witch.h/2)-(i*cellHeight+cellHeight/2));
        int combinedHalfW = floor(witch.w/2+cellWidth/2);
        int combinedHalfH = floor(witch.h/2+cellHeight/2);
        if(abs(distX) < combinedHalfW){
          if(abs(distY) < combinedHalfH){
            int overlapX = combinedHalfW - abs(distX);
            int overlapY = combinedHalfH - abs(distY);
            if(overlapX >= overlapY){
                if(distY > 0){
                  collisionSide = "TOP";
                  witch.y += overlapY;
                }else{
                  collisionSide = "BOTTOM";
                 witch.y -= overlapY;
                 witch.land(witch.y);
                }}else{
                  witch.connected=false;
                  if(distX > 0){
                      collisionSide ="Left";
                      witch.x += overlapX;
                  }else{
                    collisionSide = "Right";
                    witch.x -= overlapX;
                  }
                }
            }
          }
        }
      }//end tilemap
    }//end cols
  }//end rows   
void keyPressed(){
 //fill(255);
 //textSize(24);
 //text("keyCode: " + keyCode, 50, 50);
 //ASCII character codes
 
 switch(keyCode){
  case 37:
    left = true;
    break;
  case 38:
    up = true;
    break;
  case 39:
    right = true;
    break;
  case 32:
    jump = true;
    break;
 }
}
void keyReleased(){
 //fill(255);
 //textSize(24);
 //text("keyCode: " + keyCode, 50, 50);
 //ASCII character codes
 
 switch(keyCode){
  case 37:
    left = false;
    break;
  case 38:
    up = false;
    break;
  case 39:
    right = false;
    break;
  case 32:
    jump = false;
    break;  
 }
}

void renderMap(){
  for (int i=0; i< row; i++){
    for(int j=0; j < cols; j++){
      switch(tilemap[i][j]){
          case 0:
            image(fondImage,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;
          case 2:
            image(cloud1,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;
          case 3:
            image(cloud2,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;
          case 4:
            image(rainbow1,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;
          case 5:
            image(rainbow2,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;  
          case 6:
            image(tree1,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;  
          case 7:
            image(tree2,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;  
          case 8:
            image(tree3,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;  
          case 9:
            image(tree4,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;    
          case 1: 
            image(fondImage,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;
          case 10: 
            image(grass,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;  
          case 11: 
            image(cobble,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;
          case 12: 
            image(fondgris,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break; 
          case 13: 
            image(fondmar,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;
          case 14: 
            image(fondmar2,j*cellWidth,i*cellHeight,cellWidth,cellHeight);
            break;   
          default:
            println("Problème de map");
    }// end for cols
  }//end for rows
}//end rendernMap
}

And the code of my player class :

class Player{
  int x,y,w,h;
  int speedX, speedY;
  int currentFrame, offsetX, offsetY, totalFrames, row, sx, sy;
  int hold, delay,time;
  int g;
  boolean connected;
  //constructeur
  Player(){
   x= 10;
   y = 200;
   w = 64;
   h= 64;
   
   currentFrame = 0;
   offsetX = 0*w;
   offsetY = 0*h;
   row=0;
   sx = 0;
   sy=0;
   hold =0;
   delay=10;
   totalFrames =4;
   connected = false;
   g= 1;
  }
//methods
void land(int destY){
 speedY =0;
 connected = true;
}
void update(){
//horizontal
if(left&&!right){
  speedX = -4;
  totalFrames = 2;
  row = 4;
}
if(right&&!left){
  speedX = 4;
  totalFrames = 2;
  row = 3;
}
if(!left&&!right){
 speedX =0;
 totalFrames = 2;
 row =0;
}
//vertical
if(up && connected == true){
  totalFrames = 5;
  row =5;
  speedY = -20; 
  connected = false;
}
if (connected == false){
 speedY += g; 
}
if(y > height){
  y =0;
  speedY =0;
}

x += speedX;
y += speedY;
//update sprite art
sx = currentFrame *w;
sy = row * h;
hold = (hold +1) % delay;
if(hold ==0){
currentFrame =(currentFrame + 1)% totalFrames;
}}
void display(){
 //fill(237,121,255);
 //rect(x,y,w,h);
 //fill(0);
 //rect(x+w/2,y+h/2,1,1);
 copy(playerArt, sx + offsetX, sy + offsetY, w, h, x, y, w, h);
}
}

My problem is that when my character is on a platform that is not solid it does not fall I have to jump before it can fall.

1 Like

Welcome to this forum.
In most programming languages errors fall into one of three categories

  1. Syntax error - the program fails to compile so will not run.
  2. Runtime error - the program crashes at some point during execution.
  3. Logic error - the program compiles and executes but it doesn’t do what you expect.

The first two are usually easy to fix because error messages are generated and presented to the user to help debug the code.

The last one the logic error, is by far the hardest to debug because there are no error messages you simply have to work out the problem from its behaviour.

My problem is that when my character is on a platform that is not solid it does not fall I have to jump before it can fall.

This is a logic error :frowning_face:

To solve this type error we need a program that doesn’t produce syntax or runtime errors. Unfortunately the code you provide cannot be executed by forum members because we don’t have the image files, which probably explains the lack of responses.

To help you I have modified your program by creating dummy images at runtime. The changes are in the setup method and 2 new methods called makeTile. I have also merged the code into one file to make it easier for forum members to test your code.

NOTE: This code below does not solve the logic problem but will make it easier for other forum members to help you.

Player witch;

boolean left, up, right, jump, test;
PImage playerArt;
PImage fondImage, cloud1, cloud2, rainbow1, rainbow2, tree1, tree2, tree3, tree4, grass, cobble, fondgris, fondmar, fondmar2;
int [] maprow= {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int [][] tilemap = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {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, 1},
  {1, 0, 0, 0, 0, 0, 2, 3, 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, 0, 0, 0, 0, 0, 4, 5, 0, 0, 0, 0, 1},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 13, 14, 10, 10, 10, 10, 10},
  {11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 11, 11, 11, 11, 11},
  {11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11},
  {11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11},
  {11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11},
  {11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11},
  {11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11},
  {11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}
};
int row, cols;
int cellWidth, cellHeight;

void setup() {
  fullScreen(P2D);
  size(640, 640);
  //row = 10;
  //cols = 10;
  row = tilemap.length;
  cols = tilemap[0].length;
  cellWidth = 64;//pour augmenter taille faire plus 64 et ajouter row ou cols Ă  la tilemap
  cellHeight = 64;
  witch = new Player();
  left = false;
  right=false;
  up = false;
  test = false;
  
  playerArt = makeTile("witch3", 0xFF666666, 0xFFFFFF00);
  fondImage = makeTile("fond", 0xFFFF7777, 0xFFFFFF00);
  cloud1 = makeTile("cloud1", 0xFFAAAAFF);
  cloud2 = makeTile("cloud2", 0xFFAAAAEE);
  rainbow1 = makeTile("rainbow1", 0xFFDDBB00);
  rainbow2 = makeTile("rainbow2", 0xFFDDBB00);
  tree1 = makeTile("tree1", 0xFF00AA00);
  tree2 = makeTile("tree2", 0xFF00BB00);
  tree3 = makeTile("tree3", 0xFF00CC00);
  tree4 = makeTile("tree1", 0xFF008800);
  grass = makeTile("grass", 0xFF00CC00, 0xFFFFFFFF);
  cobble = makeTile("cobble", 0xFFCCDD00);
  fondmar = makeTile("fondmar", 0xFFFFAAAA, 0xFF660000);
  fondmar2 = makeTile("fondmar2",0xFFFF9999, 0xFF660000);
  fondgris = makeTile("fondgris", 0xFFFF8888, 0xFF660000);
}

PImage makeTile(String text, int bgcol){
  return makeTile(text, bgcol, 0xFF000000);
}

PImage makeTile(String text, int bgcol, int tcol ){
  PGraphics pg = createGraphics(64,64);
  pg.beginDraw();
  pg.background(bgcol);
  pg.stroke(0); pg.strokeWeight(3); pg.noFill();
  pg.rect(3,3,58,58);
  pg.fill(tcol);
  pg.textAlign(CENTER,CENTER);
  pg.textSize(12);
  pg.text(text.toUpperCase(),0,0,64,64);
  pg.endDraw();
  return pg.get();
}


void draw() {
  background(120);
  renderMap();
  witch.update();
  checkWallCollisions(witch);
  witch.display();
}
void checkWallCollisions(Player witch) {
  String collisionSide = "none";
  for (int i=0; i<row; i++) {
    for (int j=0; j<cols; j++) {
      if (tilemap[i][j] == 1 || tilemap[i][j] == 10 || tilemap[i][j] == 11) {//check si c'est un mur//pour plus tard
        int distX = floor((witch.x + witch.w/2)-(j*cellWidth+cellWidth/2));
        int distY = floor((witch.y + witch.h/2)-(i*cellHeight+cellHeight/2));
        int combinedHalfW = floor(witch.w/2+cellWidth/2);
        int combinedHalfH = floor(witch.h/2+cellHeight/2);
        if (abs(distX) < combinedHalfW) {
          if (abs(distY) < combinedHalfH) {
            int overlapX = combinedHalfW - abs(distX);
            int overlapY = combinedHalfH - abs(distY);
            if (overlapX >= overlapY) {
              if (distY > 0) {
                collisionSide = "TOP";
                witch.y += overlapY;
              } else {
                collisionSide = "BOTTOM";
                witch.y -= overlapY;
                witch.land(witch.y);
              }
            } else {
              witch.connected=false;
              if (distX > 0) {
                collisionSide ="Left";
                witch.x += overlapX;
              } else {
                collisionSide = "Right";
                witch.x -= overlapX;
              }
            }
          }
        }
      }
    }//end tilemap
  }//end cols
}//end rows

void keyPressed() {
  //fill(255);
  //textSize(24);
  //text("keyCode: " + keyCode, 50, 50);
  //ASCII character codes

  switch(keyCode) {
  case 37:
    left = true;
    break;
  case 38:
    up = true;
    break;
  case 39:
    right = true;
    break;
  case 32:
    jump = true;
    break;
  }
}
void keyReleased() {
  //fill(255);
  //textSize(24);
  //text("keyCode: " + keyCode, 50, 50);
  //ASCII character codes

  switch(keyCode) {
  case 37:
    left = false;
    break;
  case 38:
    up = false;
    break;
  case 39:
    right = false;
    break;
  case 32:
    jump = false;
    break;
  }
}

void renderMap() {
  for (int i=0; i< row; i++) {
    for (int j=0; j < cols; j++) {
      switch(tilemap[i][j]) {
      case 0:
        image(fondImage, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 2:
        image(cloud1, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 3:
        image(cloud2, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 4:
        image(rainbow1, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 5:
        image(rainbow2, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 6:
        image(tree1, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 7:
        image(tree2, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 8:
        image(tree3, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 9:
        image(tree4, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 1:
        image(fondImage, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 10:
        image(grass, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 11:
        image(cobble, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 12:
        image(fondgris, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 13:
        image(fondmar, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 14:
        image(fondmar2, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      default:
        println("Problème de map");
      }// end for cols
    }//end for rows
  }//end rendernMap
}


class Player {
  int x, y, w, h;
  int speedX, speedY;
  int currentFrame, offsetX, offsetY, totalFrames, row, sx, sy;
  int hold, delay, time;
  int g;
  boolean connected;
  //constructeur
  Player() {
    x= 10;
    y = 200;
    w = 64;
    h= 64;

    currentFrame = 0;
    offsetX = 0*w;
    offsetY = 0*h;
    row=0;
    sx = 0;
    sy=0;
    hold =0;
    delay=10;
    totalFrames =4;
    connected = false;
    g= 1;
  }
  //methods
  void land(int destY) {
    speedY =0;
    connected = true;
  }
  void update() {
    //horizontal
    if (left&&!right) {
      speedX = -4;
      totalFrames = 2;
      row = 4;
    }
    if (right&&!left) {
      speedX = 4;
      totalFrames = 2;
      row = 3;
    }
    if (!left&&!right) {
      speedX =0;
      totalFrames = 2;
      row =0;
    }
    //vertical
    if (up && connected == true) {
      totalFrames = 5;
      row =5;
      speedY = -20;
      connected = false;
    }
    if (connected == false) {
      speedY += g;
    }
    if (y > height) {
      y =0;
      speedY =0;
    }

    x += speedX;
    y += speedY;
    //update sprite art
    sx = currentFrame *w;
    sy = row * h;
    hold = (hold +1) % delay;
    if (hold ==0) {
      currentFrame =(currentFrame + 1)% totalFrames;
    }
  }
  void display() {
    //fill(237,121,255);
    //rect(x,y,w,h);
    //fill(0);
    //rect(x+w/2,y+h/2,1,1);
    copy(playerArt, sx + offsetX, sy + offsetY, w, h, x, y, w, h);
  }
}
1 Like

SOLUTION FOUND so please read on

After running the sketch and examining the code the issue had to be in the checkWallCollisions method. This method didn’t work because you scanned the whole array looking for a wall tile and then testing it against the witch’s position, but this could never find the situation where there was NO WALL BELOW the witch so it could drop through.

Also since the witch is the same size as a tile then the maximum number if tiles it could intersect with is 4 so is very inefficient to check all 510 tiles when we can work out which 4 we are interested in.

So I have completely rewritten the checkWallCollisions method which now identifies

  • the tile containing the top-left corner of the witch, and
  • the position of the corner within the tile

and uses that to checks the adjacent tiles for collisions

I have also added a separate method to test a tile position to see if it is a wall because this will be used several times.

You will have to modify the sketch to include your images.

Player witch;

boolean left, up, right, jump, test;
PImage playerArt;
PImage fondImage, cloud1, cloud2, rainbow1, rainbow2, tree1, tree2, tree3, tree4, grass, cobble, fondgris, fondmar, fondmar2;
int [] maprow= {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int [][] tilemap = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {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, 1},
  {1, 0, 0, 0, 0, 0, 2, 3, 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, 0, 0, 0, 0, 0, 4, 5, 0, 0, 0, 0, 1},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 13, 14, 10, 10, 10, 10, 10},
  {11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 11, 11, 11, 11, 11},
  {11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11},
  {11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11},
  {11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11},
  {11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11},
  {11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11},
  {11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}
};
int row, cols;
int cellWidth, cellHeight;

void setup() {
  fullScreen(P2D);
  //size(640, 640);
  //row = 10;
  //cols = 10;
  row = tilemap.length;
  cols = tilemap[0].length;
  println(row,cols);
  cellWidth = 64;//pour augmenter taille faire plus 64 et ajouter row ou cols Ă  la tilemap
  cellHeight = 64;
  witch = new Player();
  left = false;
  right=false;
  up = false;
  test = false;
  
  playerArt = makeTile("witch3", 0xFF666666, 0xFFFFFF00);
  fondImage = makeTile("fond", 0xFFFF7777, 0xFFFFFF00);
  cloud1 = makeTile("cloud1", 0xFFAAAAFF);
  cloud2 = makeTile("cloud2", 0xFFAAAAEE);
  rainbow1 = makeTile("rainbow1", 0xFFDDBB00);
  rainbow2 = makeTile("rainbow2", 0xFFDDBB00);
  tree1 = makeTile("tree1", 0xFF00AA00);
  tree2 = makeTile("tree2", 0xFF00BB00);
  tree3 = makeTile("tree3", 0xFF00CC00);
  tree4 = makeTile("tree1", 0xFF008800);
  grass = makeTile("grass", 0xFF00CC00, 0xFFFFFFFF);
  cobble = makeTile("cobble", 0xFFCCDD00);
  fondmar = makeTile("fondmar", 0xFFFFAAAA, 0xFF660000);
  fondmar2 = makeTile("fondmar2",0xFFFF9999, 0xFF660000);
  fondgris = makeTile("fondgris", 0xFFFF8888, 0xFF660000);
}

PImage makeTile(String text, int bgcol){
  return makeTile(text, bgcol, 0xFF000000);
}

PImage makeTile(String text, int bgcol, int tcol ){
  PGraphics pg = createGraphics(64,64);
  pg.beginDraw();
  pg.background(bgcol);
  pg.stroke(0); pg.strokeWeight(3); pg.noFill();
  pg.rect(3,3,58,58);
  pg.fill(tcol);
  pg.textAlign(CENTER,CENTER);
  pg.textSize(12);
  pg.text(text.toUpperCase(),0,0,64,64);
  pg.endDraw();
  return pg.get();
}

void draw() {
  background(120);
  renderMap();
  witch.update();
  checkWallCollisions(witch);
  //checkCollisionWithWall(witch);
  witch.display();
}

void checkWallCollisions(Player witch) {
  int cw = cellWidth, ch = cellHeight, cw2 = cw/2, ch2 = ch / 2;
  int tx = witch.x / cw;
  int ox = witch.x % cw;
  int ty = witch.y / ch;
  int oy = witch.y % ch;
  boolean connected = false;
  if (ox >= cw2 && isWall(ty, tx)) {
    witch.x += cw - ox;
    ox = 0;
    tx ++;
  } else if (ox < cw2 && isWall(ty, tx+1)) {
    witch.x -= ox;
    ox = 0;
  }
  if (oy >= ch2) {
    // Might be moving up check if stopper
    boolean stuck = isWall(ty, tx) || (ox > 0 && isWall(ty, tx + 1));
    if (stuck) {
      witch.y += ch - oy;
    } else
      witch.connected = false;
  } else  if (oy < ch2) {
    // Might be moving down check for stopper below
    boolean stuck = isWall(ty + 1, tx) || (ox > 0 && isWall(ty + 1, tx + 1));
    if (stuck) {
      witch.y -= oy;
      witch.land(witch.y);
    } else
      witch.connected = false;
  } 
}

// Return true if the tile is a 'wall'
boolean isWall(int r, int c) {
  if (r<0 || r >= row || c < 0 || c >= cols)
    return true; // Must be a wall because it is outside array limits
  return tilemap[r][c] == 1 || tilemap[r][c] == 10 || tilemap[r][c] == 11;
}

void keyPressed() {
  //fill(255);
  //textSize(24);
  //text("keyCode: " + keyCode, 50, 50);
  //ASCII character codes

  switch(keyCode) {
  case 37:
    left = true;
    break;
  case 38:
    up = true;
    break;
  case 39:
    right = true;
    break;
  case 32:
    jump = true;
    break;
  }
}
void keyReleased() {
  //fill(255);
  //textSize(24);
  //text("keyCode: " + keyCode, 50, 50);
  //ASCII character codes

  switch(keyCode) {
  case 37:
    left = false;
    break;
  case 38:
    up = false;
    break;
  case 39:
    right = false;
    break;
  case 32:
    jump = false;
    break;
  }
}

void renderMap() {
  for (int i=0; i< row; i++) {
    for (int j=0; j < cols; j++) {
      switch(tilemap[i][j]) {
      case 0:
        image(fondImage, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 2:
        image(cloud1, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 3:
        image(cloud2, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 4:
        image(rainbow1, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 5:
        image(rainbow2, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 6:
        image(tree1, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 7:
        image(tree2, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 8:
        image(tree3, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 9:
        image(tree4, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 1:
        image(fondImage, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 10:
        image(grass, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 11:
        image(cobble, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 12:
        image(fondgris, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 13:
        image(fondmar, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      case 14:
        image(fondmar2, j*cellWidth, i*cellHeight, cellWidth, cellHeight);
        break;
      default:
        println("Problème de map");
      }// end for cols
    }//end for rows
  }//end rendernMap
}


class Player {
  int x, y, w, h;
  int speedX, speedY;
  int currentFrame, offsetX, offsetY, totalFrames, row, sx, sy;
  int hold, delay, time;
  int g;
  boolean connected;
  //constructeur
  Player() {
    x= 96;
    y = 200;
    w = 64;
    h= 64;

    currentFrame = 0;
    offsetX = 0*w;
    offsetY = 0*h;
    row=0;
    sx = 0;
    sy=0;
    hold =0;
    delay=10;
    totalFrames =4;
    connected = false;
    g= 1;
  }
  //methods
  
  void land(int destY) {
    speedY = 0;
    connected = true;
  }
  void update() {
    //horizontal
    if (left&&!right) {
      speedX = -4;
      totalFrames = 2;
      row = 4;
    }
    if (right&&!left) {
      speedX = 4;
      totalFrames = 2;
      row = 3;
    }
    if (!left&&!right) {
      speedX =0;
      totalFrames = 2;
      row =0;
    }
    //vertical
    if (up && connected == true) {
      totalFrames = 5;
      row =5;
      speedY = -20;
      connected = false;
    }
    if (connected == false) {
      speedY += g;
    }
    if (y > height) {
      y =0;
      speedY =0;
    }

    x += speedX;
    y += speedY;
    //update sprite art
    sx = currentFrame *w;
    sy = row * h;
    hold = (hold +1) % delay;
    if (hold ==0) {
      currentFrame =(currentFrame + 1)% totalFrames;
    }
  }
  void display() {
    //fill(237,121,255);
    //rect(x,y,w,h);
    //fill(0);
    //rect(x+w/2,y+h/2,1,1);
    copy(playerArt, sx + offsetX, sy + offsetY, w, h, x, y, w, h);
  }
}
2 Likes