Multiple help beginner completely new

please help me, I am making a project and I want a rectangle be at a random position but will stay here until the game closes, not that it will still generate everywhere. help.
I will insert the code I need to fix. Thank you.

int px, py;
float len;
int value = 0;

void setup(){
  size(800, 600);
  cursor(CROSS);
  len = max(width + 12121, height + 12121);
  px = 255;
  py = 199;
  stroke(0);
  strokeWeight(2.7);
}

void draw(){
  background(255);
  drawLine(px, py, mouseX, mouseY);
  fill(0);
  stroke(2.77);
  fill(255);
  ellipse(px, py, 20, 20);
  rect(random(800), random(600), 30, 70);
  
  if(px > width - 10) {
    px = width - 10;
  }
  if(px < 10) {
    px = 10;
  }
  if(py > height - 10) {
    py = height - 10;
  }
  if(py < 10) {
    py = 10;
  }
}

void drawLine(float fx, float fy, float tX, float tY){
  float ang = atan2(tY - fy, tX - fx);
  float x = fx + cos(ang) * len;
  float y = fy + sin(ang) * len;
  stroke(255,0,0);
  strokeWeight(3);
  line(fx, fy, x, y);
}

void keyPressed() {
  if(keyPressed == true && key == 'd' || key == 'D') {
    px = px + 4;
  }
    if(keyPressed == true && key == 'a' || key == 'A') {
    px = px - 4;
  }
    if(keyPressed == true && key == 's' || key == 'S') {
    py = py + 4;
  }
    if(keyPressed == true && key == 'w' || key == 'W') {
    py = py - 4;
  }
}

1 Like

that’s the problem.

Please make a variable rx, ry and use them here instead of random.

Set both values to random values in setup ()

3 Likes