How to Link generated objects with a moving character?

I am creating a code in which we are trying to link a character, in this case Mario, with a sequence of randomly generated blocks. We have created a system of generation for the blocks, but are having trouble having Mario interact with them and stop on the blocks. Any suggestions? Currently the blocks are generated on a series of timers, and his interactions with them are controlled by a series of if statements determining Mario's position. Any suggestions?

Answers

  • None of which we can see working, because you haven't posted your code.

    Post your code.

  • edited May 2017
    int jumpcounter = 0;
    int counter = 1200;
    int counter2 = 0;
    int counter3 = 0;
    int speed = 4;
    int moveright = 200;
    int movedown = 164;
      float block_move = 0;
      float block_xpos = 1200;
      float block_ypos = random(250,500);
      float block_xlength = random(150,350);
      float block_ylength = random(75,125);
      float block_xpos2 = 1600;
      float block_ypos2 = random(250,500);
      float block_xlength2 = random(150,350);
      float block_ylength2 = random(75,125);
      float block_xpos3 = 2000;
      float block_ypos3 = random(250,500);
      float block_xlength3 = random(150,350);
      float block_ylength3 = random(75,125);
      float block_xpos4 = 2400;
      float block_ypos4 = random(250,500);
      float block_xlength4 = random(150,350);
      float block_ylength4 = random(75,125);
      float block_xpos5 = 2800;
      float block_ypos5 = random(250,500);
      float block_xlength5 = random(150,350);
      float block_ylength5 = random(75,125);
      float block_xpos6 = 3200;
      float block_ypos6 = random(250,500);
      float block_xlength6 = random(150,350);
      float block_ylength6 = random(75,125);
     boolean mario = true;
    boolean mariojump = false;
    boolean counterstart = false;
    boolean stop = true;
    boolean goright = false;
    boolean goleft = false;
    boolean start = false;
    boolean fall = true;
        PFont typeface1;
    
    void setup(){
      size(1200,600);
      background(0,200,255);
      noStroke();
      frameRate(60);
       typeface1 = loadFont("GillSans-48.vlw");
    }
    
    void draw(){
      background(0,200,255);
      fill(100,255,100);
      textSize(25);
      text("SCORE:" + counter3 ,500,40);
    
      rect(0+block_move,300,800,150);
    
    
     if(block_move-moveright >= -775){
      fall = false;
     }
     else{ fall = true;}
    
     if(moveright+64 >= block_xpos+block_move && moveright <= block_xpos+block_move+block_xlength){
       fall = false; }
    if(moveright+64>= block_xpos2+block_move && moveright <= block_xpos2+block_move+block_xlength2){
       fall = false; }
    if(moveright+64 >= block_xpos3+block_move && moveright <= block_xpos3+block_move+block_xlength3){
       fall = false; }
    if(moveright+64 >= block_xpos4+block_move && moveright <= block_xpos4+block_move+block_xlength4){
       fall = false; }
    if(moveright+64 >= block_xpos5+block_move && moveright <= block_xpos5+block_move+block_xlength5){
       fall = false; }
    if(moveright+64 >= block_xpos6+block_move && moveright <= block_xpos6+block_move+block_xlength6){
       fall = false; }
    
    
    
        if(fall == true && counterstart == false){
       movedown = movedown + 12;
     }
    
      if(counterstart == true){
        jumpcounter = jumpcounter +2;
      }
       if(counterstart == true && jumpcounter <= 20){
         movedown = movedown -12;
       }
        if(counterstart == true && jumpcounter > 20 &&  jumpcounter <=40){
         movedown = movedown -8;
       }
       if(counterstart == true && jumpcounter > 40 && jumpcounter <=50){
       movedown = movedown -4;
    }
    if(counterstart == true && jumpcounter > 50 && jumpcounter <=60){
       movedown = movedown +4;
    }
       if(counterstart == true && jumpcounter <= 80 && jumpcounter > 60){
         movedown = movedown +8;
       }
       if(counterstart == true && jumpcounter > 80 && jumpcounter <=100){
         movedown = movedown +12;
       }
        if(counterstart == true && jumpcounter > 100 && fall == true){
         movedown = movedown +12;
       }
    
       else if (counterstart == true && jumpcounter > 100 && fall == false){
       mariojump = false; 
       mario = true; 
       jumpcounter = 0; 
       counterstart = false;
     }
    
    
       if(keyPressed && key == 'o'){
         start = true;
       }
     if(start == true){  
       block_move = block_move -speed;
     counter = counter +speed;       //changes speed
      counter2 = counter2 +speed;
      counter3 = counter3 + speed;
     }
         if(keyPressed && key == 'w'){
      mariojump = true;
      mario = false;
      counterstart = true;
    
    }
        if(keyPressed && key == 'a'){
        goleft = true;
      }
      if(keyPressed && key == 'd'){
        goright = true;
    }
    if(goright == true){moveright = moveright +6;}
    if(goleft == true){moveright = moveright -6;}
    
    
      if(counter2 >= 2400){
    block_xlength = random(150,350);
    block_ylength = random(75,125);
    block_ypos = random(250,500);
    block_xlength2 = random(150,350);
    block_ylength2 = random(75,125);
    block_ypos2 = random(250,500);
    block_xlength3 = random(150,350);
    block_ylength3 = random(75,125);
    block_ypos3 = random(250,500);
    counter2 = 0;
      }
      if(counter >= 2400){
        block_xlength4 = random(150,350);
    block_ylength4 = random(75,125);
    block_ypos4 = random(250,500);
     block_xlength5 = random(150,350);
    block_ylength5 = random(75,125);
    block_ypos5 = random(250,500);
     block_xlength6 = random(150,350);
    block_ylength6 = random(75,125);
    block_ypos6 = random(250,500);
        counter = 0;
      }
    
    
     course(); 
     course2();
     course3();
     course4();
     course5();
     course6();
    }
    
    void course() {
      fill(100,255,100);
     for(int i = 0; i < 1000; i++){
      rect(block_xpos+block_move+i*2400, block_ypos, block_xlength, block_ylength);
      }
    }
    void course2() {
     for(int i = 0; i < 1000; i++){
      rect(block_xpos2+block_move+i*2400, block_ypos2, block_xlength2, block_ylength2);
      }
    }
    void course3() {
     for(int i = 0; i < 1000; i++){
      rect(block_xpos3+block_move+i*2400, block_ypos3, block_xlength3, block_ylength3);
      }
    }
    void course4() {
     for(int i = 0; i < 1000; i++){
      rect(block_xpos4+block_move+i*2400, block_ypos4, block_xlength4, block_ylength4);
      }
    }
    void course5() {
     for(int i = 0; i < 1000; i++){
      rect(block_xpos5+block_move+i*2400, block_ypos5, block_xlength5, block_ylength5);
      }
    }
    void course6() {
     for(int i = 0; i < 1000; i++){
      rect(block_xpos6+block_move+i*2400, block_ypos6, block_xlength6, block_ylength6);
      }
      if(mario == true){
         mario();
       }
       else{ mariojump();}
    
    
    
    
    
    }
    
    
    
    
    void mariojump(){
       fill(255,0,0);
      rect(72+moveright,24+movedown,24,24);
      rect(72+moveright,48+movedown,16,16);
        rect(72+moveright,64+movedown,8,8);
    
         fill(230,150,90);
      rect(8+moveright,16+movedown,56,40);
      rect(16+moveright,56+movedown,56,8);
      rect(64+moveright,32+movedown,16,16);
      rect(80+moveright,40+movedown,8,8);
      rect(-32+moveright,64+movedown,24,16);
      rect(-24+moveright,80+movedown,8,8);
      rect(72+moveright,8+movedown,24,16);
    
       fill(255,0,0);
      rect(16+moveright,8+movedown,40,8);
       rect(8+moveright,16+movedown,72,8);
    
       rect(-8+moveright,64+movedown,80,16);
          rect(8+moveright,72+movedown,64,16);
    
    
        fill(0);
      rect(48+moveright,24+movedown,8,16);
    
      fill(90,40,10);
       rect(8+moveright,24+movedown,24,8);
       rect(16+moveright,32+movedown,8,16);
       rect(24+moveright,40+movedown,8,8);
       rect(0+moveright,32+movedown,8,24);
       rect(8+moveright,48+movedown,8,8);
       rect(48+moveright,48+movedown,32,8);
       rect(56+moveright,40+movedown,8,8);
       rect(0+moveright,96+movedown,16,16);
        rect(-8+moveright,104+movedown,16,16);
        rect(88+moveright,72+movedown,16,32);
        rect(96+moveright,64+movedown,8,8);
    
       fill(0,0,80);
       rect(32+moveright,80+movedown,56,24);
       rect(16+moveright,88+movedown,48,24);
        rect(32+moveright,72+movedown,8,8);
          rect(24+moveright,64+movedown,8,8);
          rect(64+moveright,72+movedown,8,8);
          rect(56+moveright,64+movedown,8,8);
    
          fill(240,240,40);
    rect(64+moveright,80+movedown,8,8);
    rect(40+moveright,88+movedown,8,8);
    }
    
    void mario(){
      fill(230,150,90);
      rect(8+moveright,16+movedown,56,40);
      rect(16+moveright,56+movedown,56,8);
      rect(64+moveright,32+movedown,16,16);
      rect(80+moveright,40+movedown,8,8);
      rect(-8+moveright,88+movedown,16,24);
       rect(72+moveright,88+movedown,16,24);
       rect(64+moveright,96+movedown,8,8);
       rect(8+moveright,96+movedown,8,8);
    
      fill(0);
      rect(48+moveright,24+movedown,8,16);
    
      fill(255,0,0);
      rect(16+moveright,8+movedown,40,8);
       rect(8+moveright,16+movedown,72,8);
    
       rect(8+moveright,64+movedown,48,32);
       rect(0+moveright,72+movedown,8,16);
       rect(-8+moveright,80+movedown,8,8);
       rect(56+moveright,72+movedown,24,16);
        rect(80+moveright,80+movedown,8,8);
          rect(64+moveright,88+movedown,8,8);
    
    
    
       fill(90,40,10);
       rect(8+moveright,24+movedown,24,8);
       rect(16+moveright,32+movedown,8,16);
       rect(24+moveright,40+movedown,8,8);
       rect(0+moveright,32+movedown,8,24);
       rect(8+moveright,48+movedown,8,8);
       rect(48+moveright,48+movedown,32,8);
       rect(56+moveright,40+movedown,8,8);
       rect(0+moveright,120+movedown,24,16);
       rect(56+moveright,120+movedown,24,16);
       rect(-8+moveright,128+movedown,8,8);
       rect(80+moveright,128+movedown,8,8);
    
      fill(0,0,80);
      rect(24+moveright,64+movedown,8,56);
       rect(48+moveright,72+movedown,8,40);
       rect(24+moveright,80+movedown,32,8);
       rect(16+moveright,88+movedown,48,16);
       rect(8+moveright,104+movedown,24,16);
       rect(48+moveright,104+movedown,24,16);
       rect(32+moveright,104+movedown,16,8);
    
       fill(240,240,40);
       rect(24+moveright,88+movedown,8,8);
        rect(48+moveright,88+movedown,8,8);
    
    
    
     }
     void keyReleased(){
       if(key == 'a'){
         goleft = false;
       }
        if(key == 'd'){
         goright = false;
       }
       }
    
  • I had some statements determining Y-position, but they seemed to be doing moe harm than good. The X-position seems to be working, but stop after the first generation of the blocks is through (after 6 blocks)

Sign In or Register to comment.