Help with my own Game

How can i fix it?

Code:

float xg;
float rx = 500;
float ry = 1300;
float rw = 100;
float rh = 100;
float linksX = 100;
float linksY = 1300;
float linksW = 100;
float linksH = 100;
void setup() {
  xg = 325;
  orientation(PORTRAIT);
  fullScreen();
  background(0);
  stroke(0);
  noFill();
}
void draw() {
  fill(#FFFFFF);
  rect(xg, 1200, 50, 50, 2);
  rect(rx, ry, rw, rh, 5);
  rect(linksX, linksY, linksW, linksH, 5);
  fill(255);
  
  if (mousePressed) {
    if (mouseX>rx && mouseX <rx+rw && mouseY>ry && mouseY <ry+rh) {
      if(xg < 650) {
         xg = xg + 3;
      }
    }
    if (mouseX>linksX && mouseX <linksX+linksW && mouseY>linksY && mouseY <linksY+linksH) {
      if (xg > 5) {
          xg = xg - 3;
      }
    }
  }
}

Can you please describe what happens falsely now and what you want to happen instead?

Look my Screenshot :joy:

I Like only a rect

it’s difficult to find out what’s wrong. If you tell us what you tried to do and what will be the game we could help you by either showing you the issue or resolving it by changing the code.

1 Like

I want to do a 2D jump and run game I need: run right & left and a jump

float spieler_x;
float spieler_y = 200;
float rx = 200;
float ry = 500;
float rw = 100;
float rh = 100;
float linksX = 50;
float linksY = 500;
float linksW = 100;
float linksH = 100;
float jumpX = 1250;
float jumpY = 500;
float jumpW = 100;
float jumpH = 100;
void setup() {
  orientation(LANDSCAPE);
  background(0);
  stroke(0);
  noFill();
}
void draw() {
  fill(#FFFFFF);
  rect(spieler_x, spieler_y, 50, 50, 2);
  rect(rx, ry, rw, rh, 5);
  rect(jumpX, jumpY, jumpW, jumpH, 5);
  rect(linksX, linksY, linksW, linksH, 5);
  fill(255);
  if (mousePressed) {
    if(mouseX>rx && mouseX <rx+rw && mouseY>ry && mouseY <ry+rh) {
      fill(#818181);
      rect(rx, ry, rw, rh, 5);
        if (spieler_x < 1400) {
          spieler_x++;
      }
    }
    if (mouseX>linksX && mouseX <linksX+linksW && mouseY>linksY && mouseY <linksY+linksH) {
      fill(#818181);
      rect(linksX, linksY, linksW, linksH, 5);
      if (spieler_x > 5) {
        spieler_x--;
      }
    }
    if(mouseX>jumpX && mouseX <jumpX+jumpW && mouseY>jumpY && mouseY <jumpY+jumpH) {
      fill(#818181);
      rect(jumpX, jumpY, jumpW, jumpH, 5);
      redraw();
      spieler_y--;
    }
  }
  fullScreen();
}

so a 2D platformer? Its the name for what you are describing

1 Like

Yes sorry my english is so Bad :confused:

no problem. Mine used to be bad too. Well, I continued learning it and here I am with an ok understanding of it.

Can you Help me? Please😂

I’ll try : P I’ll share my progress and feel free to take over : )

Maybe you could try box2D its a library meant for that (2D physics). I’ll try to make it in normal processing for now

my progress:

float x=300,y=100,w=50,h=50, xs = 0, ys = 0, g = 0.5, speed = 0.5, friction = 0.9;
float ox1 = 200, oy1 = 500, ow1 = 200, oh1 = 20;

void setup() {
  size(600,600);
}

void draw() {
  background(0);
  rect(x,y,w,h);
  rect(ox1,oy1,ow1,oh1);
  
  ys += g;
  y  += ys;
  xs *= friction;
  x += xs;
  if(y + h > oy1) {
    ys = 0;
    y = oy1 - h;
  }
  
  if(keyPressed) {
    if(key == 'w') {
      if(ys == 0) {
        ys -= 10;
      }
    }
    if(key == 'a') {
      xs -= speed;
    }
    if(key == 'd') {
      xs += speed;
    }
  }
}

I programming for Android

I know but as I said before I suck at it : P

@noel can you Help me?

I’ve adapted @CodeMasterX’s code a bit. As you can see I take every point/measurement in relation to width and height. That is necessary for Android because you want to use fullscreen, and there exist an enormous variety of screen sizes. This code has a fixed canvas size and you need to change it. The idea is to use three fingers to steer. The ring finger for moving to the right, the index finger for moving to the left, and the middle finger to jump by tapping above the buttons. To jump and move simuataniously you need the Android’s buildin touches array. But the sketch is too small on my phone to test it. So change it to fullscreen and we’ll see.

float x = 300, y = 100, w = 50, h = 50, xs = 0; 
float ys = 0, g = 0.5, speed = 0.5, ox1 = 200; 
float oy1 = 500, ow1 = 200, oh1 = 20, friction = 0.97; 
int cv; 
boolean jump = true; 

void setup() { 
  size(600, 600); 
  textSize(30); 
  cv = height-height/12; 
  fill(0, 220, 220); 
  rect(0, cv, width, height/12); 
  fill(0); 
  line(width/2, cv, width/2, height); 
  fill(100); 
  textSize(20); 
  text("Left", width/5, height- height/30); 
  text("Right", 11*width/16, height-height/30);
} 

void draw() { 
  fill(0); 
  rect(0, 0, width, cv); 
  fill(255); 
  rect(x, y, w, h); 
  rect(ox1, oy1, ow1, oh1); 
  if (jump) { 
    ys += g; 
    y += ys; 
    xs *= friction; 
    x += xs; 
    if (y+h > oy1) { 
      ys = 0; 
      y = oy1-h; 
      jump = false;
    }
  } 
  if (mousePressed) { 
    if (mouseY > cv) { 
      if (mouseX > width/2) x += 5; 
      if (mouseX < width/2) x -= 5;
    } else { 
      jump = true; 
      if (ys == 0) { 
        ys -= 10;
      }
    }
  }
}

I need to create this

As the image is a landscape use that orientation. You’ll have to take the image to GIMP or similar and cut out the dinosaur-rider and save it as a transparent png image. Then correct the hole in the background image with appropriate colors. Load the image into the sketch and use it in the background. You need to search the button positions relative to width and height. (To make it work on every phone) Post it here (!!formatted in a code-block!!), then I’ll help you further.
Edit: Where is the jump button? Or has it to jump when you shake your phone?

I have so far…

float spieler_x;
float spieler_y = 200;
float rx = 200;
float ry = 500;
float rw = 100;
float rh = 100;
float linksX = 50;
float linksY = 500;
float linksW = 100;
float linksH = 100;
float jumpX = 1250;
float jumpY = 500;
float jumpW = 100;
float jumpH = 100;
void setup() {
  orientation(LANDSCAPE);
  background(0);
  stroke(0);
  noFill();
}
void draw() {
  fill(#FFFFFF);
  rect(spieler_x, spieler_y, 50, 50, 2);
  rect(rx, ry, rw, rh, 5);
  rect(jumpX, jumpY, jumpW, jumpH, 5);
  rect(linksX, linksY, linksW, linksH, 5);
  fill(255);
  if (mousePressed) {
    if(mouseX>rx && mouseX <rx+rw && mouseY>ry && mouseY <ry+rh) {
      fill(#818181);
      rect(rx, ry, rw, rh, 5);
        if (spieler_x < 1400) {
          spieler_x++;
      }
    }
    if (mouseX>linksX && mouseX <linksX+linksW && mouseY>linksY && mouseY <linksY+linksH) {
      fill(#818181);
      rect(linksX, linksY, linksW, linksH, 5);
      if (spieler_x > 5) {
        spieler_x--;
      }
    }
    if(mouseX>jumpX && mouseX <jumpX+jumpW && mouseY>jumpY && mouseY <jumpY+jumpH) {
      fill(#818181);
      rect(jumpX, jumpY, jumpW, jumpH, 5);
      redraw();
      spieler_y--;
    }
  }
  fullScreen();
}

now do i have to adjust it to the cell phone size?

Edit: the player is still a little too slow for me and the jump is a little flawed

Yes for example instead of writing rx = 200; you write something like rx = width/4;
Then if it works on your phone screen resolution, it will also work on other sizes.