Loading...
Logo
Processing Forum
Hello, you all know the story. We're almost done, but I can't figure out how to make Santa react to hitting the deer. What's the simplest way to do this? Here's the code: (it's long, sorry.)

PImage Santa;
PImage Sled;
PImage Reindeer;
PImage Dirt;
PImage Ice;
PImage Water;
PImage Logs;
PImage bg;
PImage House;
PImage Gift;

PImage reindeer;
PImage reindeer2;
PImage reindeer3;
PImage reindeer4;
PImage reindeer5;
PImage reindeer6;
PImage reindeer7;
PImage reindeer8;
PImage reindeer9;
PImage reindeer10;
PImage reindeer11;
PImage reindeer12;
PImage reindeer13;

PImage sleigh;
PImage sleigh2;
PImage sleigh3;
PImage sleigh4;
PImage sleigh5;
PImage sleigh6;
PImage sleigh7;
PImage sleigh8;
PImage sleigh9;
PImage sleigh10;
PImage sleigh11;
PImage sleigh12;
PImage sleigh13;
PImage sleigh14;
PImage sleigh15;
PImage sleigh16;
PImage sleigh17;
PImage sleigh18;

PImage penguin;

PFont FontA;

int santaX = 185, santaY = 442;
int house1X= 30, house1Y = 0;
int house2X= 123, house2Y = 0;
int house3X= 216, house3Y = 0;
int house4X= 309, house4Y = 0;
int house5X= 402, house5Y = 0;
int santaMP = santaX;
int Score= 0;

float x;
float x2;
float x3;
float x4;
float x5;
float x6;
float x7;
float x8;
float x9;
float x10;
float x11;
float x12;
float x13;
float x14;
float x15;
float x16;


float a;
float a2;
float a3;
float a4;
float a5;
float a6;
float a7;
float a8;
float a9;
float a10;
float a11;
float a12;
float a13;
float a14;
float a15;
float a16;
float a17;
float a18;

float z;

Integer count = 0;
Integer count2 = 0;
Integer count3 = 0;
Integer startR2 = 0;
Integer startR3 = 0;
Integer startR5 = 0;
Integer startR6 = 0;
Integer startR7 = 0;
Integer startR8 = 0;
Integer startR10 = 0;
Integer startR11 = 0;
Integer startR12 = 0;

Integer startS2 = 0;
Integer startS3 = 0;
Integer startS4 = 0;
Integer startS6 = 0;
Integer startS7 = 0;
Integer startS8 = 0;
Integer startS9 = 0;
Integer startS10 = 0;
Integer startS11 = 0;
Integer startS12 = 0;
Integer startS13 = 0;
Integer startS14 = 0;
Integer startS15 = 0;
Integer startS16 = 0;
Integer startS17 = 0;
Integer startS18 = 0;

boolean left, right, up, down;
boolean runOnce=true;
//boolean intersect=false;
//boolean deerintersect=false;
boolean rightblock=false;
boolean leftblock=false;
boolean downblock=false;
boolean upblock=false;
boolean scoreblockdown=false;
boolean scoreblockup=false;
boolean house1collision=false;
boolean gift1Delivered=false;
boolean house2collision=false;
boolean gift2Delivered=false;
boolean house3collision=false;
boolean gift3Delivered=false;
boolean house4collision=false;
boolean gift4Delivered=false;
boolean house5collision=false;
boolean gift5Delivered=false;
boolean allgifts=false;
boolean resetgifts=false;

void setup() {
  size (450, 510);
  FontA = loadFont ("ArcadeNormal-48.vlw");
  Santa = loadImage ("santasmall.png");
  Reindeer = loadImage ("reindeer.png");
  bg = loadImage ("black.jpg");
  Dirt = loadImage ("8-bit_dirt.png");
  Ice = loadImage ("8-bit_ice.png");
  Water = loadImage ("8-bit_water.png");
  Logs = loadImage ("8-bit_log.png");
  House = loadImage ("house.png");
  Gift = loadImage ("gift1.png");
  
  reindeer = loadImage ("reindeer2.png");
  reindeer2 = loadImage ("reindeer2.png");
  reindeer3 = loadImage ("reindeer2.png");
  reindeer4 = loadImage ("reindeer.png");
  reindeer5 = loadImage ("reindeer.png");
  reindeer6 = loadImage ("reindeer.png");
  reindeer7 = loadImage ("reindeer2.png");
  reindeer8 = loadImage ("reindeer2.png");
  reindeer9 = loadImage ("reindeer.png");
  reindeer10 = loadImage ("reindeer.png");
  reindeer11 = loadImage ("reindeer.png");
  reindeer12 = loadImage ("reindeer.png");
  reindeer13 = loadImage ("reindeer2.png");
  
  sleigh = loadImage ("sleigh.png");
  sleigh2 = loadImage ("sleigh.png");
  sleigh3 = loadImage ("sleigh.png");
  sleigh4 = loadImage ("sleigh.png");
  sleigh5 = loadImage ("sleigh.png");
  sleigh6 = loadImage ("sleigh.png");
  sleigh7 = loadImage ("sleigh.png");
  sleigh8 = loadImage ("sleigh.png");
  sleigh9 = loadImage ("sleigh.png");
  sleigh10 = loadImage ("sleigh.png");
  sleigh11 = loadImage ("sleigh.png");
  sleigh12 = loadImage ("sleigh.png");
  sleigh13 = loadImage ("sleigh.png");
  sleigh14 = loadImage ("sleigh.png");
  sleigh15 = loadImage ("sleigh.png");
  sleigh16 = loadImage ("sleigh.png");
  sleigh17 = loadImage ("sleigh.png");
  sleigh18 = loadImage ("sleigh.png");
  
  penguin = loadImage ("penguin.png");
}

void draw () 
{
  background(bg); 
  image(Water, 0, 0);
  image(Ice, 0, 45, Ice.width, Ice.height/1.3);
  image(Dirt, 0, 287, Dirt.width, Dirt.height/2.55);
  image(Logs, -5, 131, Logs.width/.8, Logs.height/.8);
  image(Logs, 145, 131, Logs.width/.8, Logs.height/.8);
  image(Logs, 295, 131, Logs.width/.8, Logs.height/.8);
  image(Logs, -5, 251, Logs.width/.8, Logs.height/.8);
  image(Logs, 145, 251, Logs.width/.8, Logs.height/.8);
  image(Logs, 295, 251, Logs.width/.8, Logs.height/.8);
  image(Logs, -5, 441, Logs.width/.8, Logs.height/.8);
  image(Logs, 145, 441, Logs.width/.8, Logs.height/.8);
  image(Logs, 295, 441, Logs.width/.8, Logs.height/.8);
  image(House, house1X, house1Y);  
  image(House, house2X, house2Y);  
  image(House, house3X, house3Y);  
  image(House, house4X, house4Y);  
  image(House, house5X, house5Y);
  //image(Logs, 300, height-490);
  //image(Logs, 200, height-400);
  

  if (gift1Delivered==true) {
    image(Gift, 35, 10);
  }

  if (gift2Delivered==true) {
    image(Gift, 129, 10);
  }

  if (gift3Delivered==true) {
    image(Gift, 222, 10);
  }

  if (gift4Delivered==true) {
    image(Gift, 314, 10);
  }

  if (gift5Delivered==true) {
    image(Gift, 408, 10);
  }

  image(Santa, santaX, santaY);  
  textFont(FontA);
  fill(255);
  textSize(28);
  text("Score", 3, 505);
  text(Score, 160, 505);

  {
    if (runOnce)
      if (up) {
        if (upblock==false)  
          santaY -=31;
        runOnce = false;
        //intersect = false;  
        if (scoreblockup==false)
          Score= Score +10;
      }
      else
        if (down) {
          if (downblock==false)  
            santaY +=31;          
          runOnce = false;
          //intersect = false;
          if (scoreblockdown==false)
            Score = Score -10;
        }
    if (runOnce)
      if (left) {
        if (leftblock==false)  
          santaX -= 31;
        runOnce = false;
        //intersect = false;
      }
      else if (right) { 
        if (rightblock==false)       
          santaX +=31;
        runOnce = false;
        //intersect = false;
      }
    //if (intersect==false) {
    //image (Sled, 175, 200 );
    //image (Reindeer, 200, 350);
    //image (Santa, santaX, santaY);
    // }
    //else
    //{
    //fill (255, 2, 2);
    //image (Sled, santaX, santaY);
    //image (Santa, santaX, santaY-10);
    // image (Reindeer, 200, 350);
    // }
    //if (deerintersect==false) {
    //   image (Sled, 175, 200 );
    //  image (Reindeer, 200, 350);
    //  image (Santa, santaX, santaY);
    //  }
    //  else
    {
      //  fill (255, 2, 2);
      //  image (Santa, 185, 460);
      //   image (Reindeer, 200, 350);
    }
  }


  //------------------------------------  
  {
    if (santaX==house1X && santaY<=30)
      house1collision=true;
  }
  {
    if (santaX==house2X && santaY<=30)
      house2collision=true;
  }  
  {
    if (santaX==house3X && santaY<=30)
      house3collision=true;
  }  
  {
    if (santaX==house4X && santaY<=30)
      house4collision=true;
  }  
  {
    if (santaX==house5X && santaY<=30)
      house5collision=true;
  }  

  {
    if (house1collision==true)
    {
      set(santaX=185, santaY=442, Santa);
      Score = Score + 50;
      house1collision=false;
      gift1Delivered=true;
    }
  }
  {
    if (house2collision==true)
    {
      set(santaX=185, santaY=442, Santa);
      Score = Score + 50;
      house2collision=false;
      gift2Delivered=true;
    }
  }
  {
    if (house3collision==true)
    {
      set(santaX=185, santaY=442, Santa);
      Score = Score + 50;
      house3collision=false;
      gift3Delivered=true;
    }
  }
  {
    if (house4collision==true)
    {
      set(santaX=185, santaY=442, Santa);
      Score = Score + 50;
      house4collision=false;
      gift4Delivered=true;
    }
  }
  {
    if (house5collision==true)
    {
      set(santaX=185, santaY=442, Santa);
      Score = Score + 50;
      house5collision=false;
      gift5Delivered=true;
    }
  }
  if (gift1Delivered==true) {
    if (gift2Delivered==true) {
      if (gift3Delivered==true) {
        if (gift4Delivered==true) {
          if (gift5Delivered==true) {
            allgifts=true;
          }
        }
      }
    }
  }

  if (allgifts==true) {
    Score = Score + 1000;
    allgifts=false;
    resetgifts=true;
  }

  if (resetgifts==true) {
    gift1Delivered=false;
    gift2Delivered=false;
    gift3Delivered=false;
    gift4Delivered=false;
    gift5Delivered=false;
    resetgifts=false;
  }

  {
    {
      if (santaX >= width-65) 
        rightblock=true;   
      if (santaX <= width-65) 
        rightblock=false;
    }
    {
      if (santaX <= width-440) 
        leftblock=true;    
      if (santaX >= width-440) 
        leftblock=false;
    }
    {
      if (santaY <= height-490)
        upblock=true;
      if (santaY >= height-490) 
        upblock=false;
    }
    if (gift1Delivered==true) {
      if (santaY <= 50) {
        if (santaX==house1X) {
          upblock=true;
          scoreblockup=true;
        }
      }
    }
    
    if (gift2Delivered==true) {
      if (santaY <= 50) {
        if (santaX==house2X) {
          upblock=true;
          scoreblockup=true;
        }
      }
    }

    if (gift3Delivered==true) {
      if (santaY <= 50) {
        if (santaX==house3X) {
          upblock=true;
          scoreblockup=true;
        }
      }
    }

    if (gift4Delivered==true) {
      if (santaY <= 50) {
        if (santaX==house4X) {
          upblock=true;
          scoreblockup=true;
        }
      }
    }

    if (gift5Delivered==true) {
      if (santaY <= 50) {
        if (santaX==house5X) {
          upblock=true;
          scoreblockup=true;
        }
      }
    }

    if (santaY >= height-80)
      downblock=true;
    if (santaY <= height-80)
      downblock=false;
  }
  {
    if (santaY >= height-80)
      scoreblockdown=true;
    if (santaY <= height-80)
      scoreblockdown=false;

    if (santaY <= height-490)
      scoreblockup=true;
    if (santaY >= height-490) {      
        scoreblockup=false;
      }
      if (gift1Delivered==false) {
      }
      //if (abs(santaX-175)< 30 && abs(santaY-200)< 30) {
      //intersect=true;
    }
    
    
    
    
    // reindeer and sleigh
    
    image(reindeer, (x), 273, reindeer.width/2, reindeer.height/2);
  
  x = x + 2;
  
  if ((x > 200) && (startR2 == 0)){
    startR2 = 1;
  }
  
  if (x > 450){
    x = 0;
  }
  
  if (startR2 == 1) {
      image(reindeer2, (x2), 273, reindeer2.width/2, reindeer2.height/2);
      
      x2 = x2 + 2;
      
      if ((x2 > 550) && (count2 == 0)) {
        x2 = 0;
        count2 = 1;
      }
      if ((x2 > 550) && (count2 == 1)) {
        x2 = 0;
      }
      
      if ((x2 > 200) && (startR3 == 0)) {
        startR3 = 1;
      }
  }
  
  if (startR3 == 1) {
    image(reindeer3, x3, 273, reindeer3.width/2, reindeer3.height/2);
    
    x3 = x3 +2;
    
    if ((x3 > 550) && (count3 == 0)) {
      x3 = 0;
      count3 = 1;
    }
    if ((x3 > 550) && (count3 == 1)) {
      x3 = 0;
    }
  }
  
//2nd row

  image(reindeer4, x4, 375, reindeer4.width/2, reindeer4.height/2);
  
  
  x4 = x4 - 1.2;
  
  if ((x4 < 330) && (x4 > 325) && (startR5 == 0)) {
    startR5 = 1;
  }
  
  if (x4 < 0) {
   x4 = 450;
  }
  
 if(startR5 == 1){
   image(reindeer5, x5, 375, reindeer5.width/2, reindeer5.height/2);
   
   x5 = x5 - 1.2;
   
   if ((x5 < 330) && (x5 > 325) && (startR6 == 0)) {
     startR6 = 1;
   }
   
   if (x5 < 0) {
     x5 = 450;
   }
 }

if(startR6 == 1){
   image(reindeer6, x6, 375, reindeer6.width/2, reindeer6.height/2);
   
   x6 = x6 - 1.2;
   
   if ((x6 < 330) && (x6 > 325) && (startR7 == 0)) {
     startR7 = 1;
   }
   
   if (x6 < 0) {
     x6 = 450;
   }
}

// 3rd row

image(reindeer7, x7, 341, reindeer7.width/2, reindeer7.height/2);

x7 = x7 + 0.8;

if ((x7 > 240) && (startR8 == 0)) {
  startR8 = 1;
}

if (x7 > 450){
  x7 = -50;
}

if(startR8 == 1) {
  image(reindeer8, x8, 341, reindeer8.width/2, reindeer8.height/2);
  
  x8 = x8 + 0.8;
  
  if (x8 > 450){
    x8 = -50;
  }
}

//4th row

image(reindeer9, x9, 307, reindeer9.width/2, reindeer9.height/2);

x9 = x9 - 1;

if((x9 < 425) && (x9 > 420) && (startR10 == 00)){
  startR10 = 1;
}

if(x9 < 0){
  x9 = 450;
}

if(startR10 == 1){
  image(reindeer10, x10, 307, reindeer10.width/2, reindeer10.height/2);
  
  x10 = x10 - 1;
  
  if ((x10 < 425) && (x10 > 420) && (startR11 == 0)){
    startR11 = 1;
  }
  
  if(x10 < 0){
    x10 = 450;
  }
}

if(startR11 == 1){
  image(reindeer11, x11, 307, reindeer11.width/2, reindeer11.height/2);
  
  x11 = x11 - 1;
  
  if ((x11 < 425) && (x11 > 420) && (startR12 == 0)){
    startR12 = 1;
  }
  
  if (x11 < 0){
    x11 = 450;
  }
}

if(startR12 == 1){
  image(reindeer12, x12, 307, reindeer12.width/2, reindeer12.height/2);
  
  x12 = x12 - 1;
  
  if(x12 < 0){
    x12 = 450;
  }
}

//5th row

image(reindeer13, x13, 409, reindeer13.width/2, reindeer13.height/2);

x13 = x13 + 3;

if(x13 > 450){
  x13 = 0;
}





//sleds

image(sleigh, a, 235, sleigh.width/3, sleigh.height/3);

a = a + 2;

if((a > 110) && (startS2 == 0)){
  startS2 = 1;
}

if (a > 450){
  a = 0;
}

if(startS2 == 1){
  image(sleigh2, a2, 235, sleigh2.width/3,sleigh2.height/3);
  a2 = a2 + 2;
  
  if((a2 > 110) && (startS3 == 0)) {
    startS3 = 1;
  }
  
  if (a2 >450){
    a2 = 0;
  }
}

if(startS3 == 1){
  image(sleigh3 , a3, 235, sleigh3.width/3, sleigh3.height/3);
  a3 = a3 + 2;
  
  if ((a3 > 110) && (startS4 == 0)) {
    startS4 = 1;
  }
  
  if(a3 >450) {
    a3 = 0;
  }
}

if(startS4 == 1){
  image(sleigh4, a4, 235, sleigh4.width/3, sleigh.height/3);
  a4 = a4 + 2;
  
  if(a4 > 450){
    a4 = 0;
  }
}

//2nd row sleds

image(sleigh5, a5, 206, sleigh5.width/3, sleigh5.height/3);
a5 = a5 - 1;

if((a5 < 410) && (a5 > 405) && (startS6 == 0)){
  startS6 = 1;
}

if(a5 < 0){
  a5 = 450;
}

if(startS6 == 1){
  image(sleigh6, a6, 206, sleigh6.width/3, sleigh6.height/3);
a6 = a6 - 1;

if((a6 < 410) && (a6 > 405) && (startS7 == 0)){
  startS7 = 1;
}

if(a6 < 0){
  a6 = 450;
}
}

if(startS7 == 1){
  image(sleigh7, a7, 206, sleigh7.width/3, sleigh7.height/3);
a7 = a7 - 1;

if((a7 < 310) && (a7 > 305) && (startS8 == 0)){
  startS8 = 1;
}

if(a7 < 0){
  a7 = 450;
}

if(startS8 == 1){
  image(sleigh8, a8, 206, sleigh8.width/3, sleigh8.height/3);
a8 = a8 - 1;

if((a8 < 410) && (a8 > 405) && (startS9 == 0)){
  startS9 = 1;
}

if(a8 < 0){
  a8 = 450;
}
}

if(startS9 == 1){
  image(sleigh9, a9, 206, sleigh9.width/3, sleigh9.height/3);
a9 = a9 - 1;

if((a9 < 410) && (a9 > 405) && (startS10 == 0)){
  startS10 = 1;
}

if(a9 < 0){
  a9 = 450;
}
}

if(startS10 == 1){
  image(sleigh10, a10, 206, sleigh10.width/3, sleigh10.height/3);
a10 = a10 - 1;


if(a10 < 0){
  a10 = 450;
}
}

//3rd row sleds

image(sleigh11, a11, 172, sleigh11.width/3, sleigh11.height/3);
a11 = a11 + 5;

if((a11 > 225) && (startS12 == 0)){
  startS12 = 1;
}

if(a11 > 450){
  a11 = 0;
}

if(startS12 == 1){
  image(sleigh12, a12, 172, sleigh12.width/3, sleigh12.height/3);
  a12 = a12 + 5;
  
  if(a12 >450){
    a12 = 0;
  }

}
}

// 4th row sleds

image(sleigh13, a13, 113, sleigh13.width/3, sleigh13.height/3);
a13 = a13 - 0.8;

if((a13 < 345) && (a13 > 340) && (startS14 == 0)){
  startS14 = 1;
}

if(a13 < 0){
a13 = 450;
}

if(startS14 == 1){
  image(sleigh14, a14, 113, sleigh14.width/3, sleigh14.height/3);
a14 = a14 - 0.8;

if((a14 < 295) && (a14 > 290) && (startS15 == 0)){
  startS15 = 1;
}

if(a14 < 0){
a14 = 450;
}
}

if(startS15 == 1){
  image(sleigh15, a15, 113, sleigh15.width/3, sleigh15.height/3);
a15 = a15 - 0.8;

if((a15 < 345) && (a15 > 340) && (startS16 == 0)){
  startS16 = 1;
}

if(a15 < 0){
a15 = 450;
}
}

if(startS16 == 1){
  image(sleigh16, a16, 113, sleigh16.width/3, sleigh16.height/3);
a16 = a16 - 0.8;

if(a16 < 0){
a16 = 450;
}
}

//5th row sleds
image(sleigh17, a17, 84, sleigh17.width/3, sleigh17.height/3);
a17 = a17 + 5;

if((a17 > 225) && (startS18 == 0)){
  startS18 = 1;
}

if(a17 > 450){
  a17 = 0;
}

if(startS18 == 1){
  image(sleigh18, a18, 84, sleigh18.width/3, sleigh18.height/3);
  a18 = a18 + 5;
  
  if(a18 >450){
    a18 = 0;
  }

}

// penguin

image(penguin, z, 38, penguin.width/2, penguin.height/2);
z = z - 8;

if(z < 0){
  z = 450;
}
    
  }
    
 

Replies(1)

Ouch! I haven't followed the "whole story", but when I see code like that, I have only one urge, to paste this link: From several variables to arrays
Your code is prone to be repetitive and hard to read, understand and fix...

To answer your question, you probably need to do rectangle intersection check, as shown in the wiki.