How do I make objects stack in my game?

edited December 2015 in How To...

I am making a game where I have a moving ice cream cone at the bottom of the screen that can be controlled using the left and right arrow keys. The point of the game is to catch randomly spawning ice cream scoops that fall from the top of the window. When the cone checks if the ice cream has hit, it increases the score. If it misses, then it is game over.

Problem: When the ice cream falls from the top of the screen and the player successfully "catches" it, I want the ice cream to stay on the cone so that as the player continues to catch new ice cream scoops, they will stack on top of one another.

Tagged:

Answers

  • When the ice scoop is caught, mark it as caught. Draw caught scoops with the same X position as your cone.

    It is hard to help you beyond that because you have not posted some example code showing your issue.

  • edited December 2015

    Well, as has been said when you have a class iceCreamScoop and is marked as caught (boolean caught = false; set to true) then set it's X to the x of the cone (with a random offsetX = random(-7,7); maybe which is constant once it is set as property of the scoop)

    The cone can also have a yForCaughtScoop which decreases with every caught scoop: yForCaughtScoop = yForCaughtScoop - 7; which is passed to the scoop

    ;-)

  • @TFGuy44, how do I post example code, I was going to but I didn't know how.

  • edited December 2015
  • _vk_vk
    Answer ✓

    https://forum.processing.org/two/discussion/8045/how-to-format-code-and-text#latest

    In short. ctrl-T in PDE to ident the code. Copy, leave one empty line before and one after, paste it , select code and hit ctrl-o.

  • edited December 2015

    Code is below:

  •  
    Cone cone;
    Icecream icecream;
    PFont font;
    float speed = 10;
    String l;
    int time;
    int score;
    boolean moveRight= false;
    boolean moveLeft= false;
    boolean onCone = false;
    boolean canHit = true;
    boolean beforeGame;
    boolean endGame; 
    
    void setup () {
      background(255);
      size(600, 600);
      cone = new Cone();
      icecream = new Icecream();
      color c1 = color(#CEFBFF);
      color c2 = color(#FFFEF0);
      gradientBackground(c1, c2);
      beforeGame = true;
      endGame = false;
    }
    
    
    void draw() {
      if (keyPressed) beforeGame = false;
      if (beforeGame == true) {
        textAlign(CENTER);
        fill(#FF8EC6);
        textSize(70);
        text("Icecream Dash", 300, 300);
        textSize(26);
        text("Catch as many scoops as you can !", 300, 500);
        textSize(26);
        text("Press any key to begin", 300, 550);
      }
      if (beforeGame == false) {
        color c1 = color(#CEFBFF);
        color c2 = color(#FFFEF0);
        gradientBackground(c1, c2);
        score();
        cone.move();
        cone.display();
        icecream.display();
        hitCone();
        reachBottom();
      }
    
      if (endGame == true) {
        color c1 = color(#CEFBFF);
        color c2 = color(#FFFEF0);
        gradientBackground(c1, c2);
        textAlign(CENTER);
        fill(#FF8EC6);
        textSize(70);
        text("Game Over!", 300, 300);
      }
    
      if (icecream.l > score) {
        score = icecream.l;
      }
    }
    
    void keyPressed() {
      if (key == CODED) {
        if (keyCode == RIGHT) {
          moveRight = true;
        } else if (keyCode == LEFT) {
          moveLeft = true;
        }
      }
    }
    
    void keyReleased() {
      if (key == CODED) {
        if (keyCode == RIGHT) {
          moveRight = false;
        } else if (keyCode == LEFT) {
          moveLeft = false;
        }
      }
    }
    
    void score() {
      fill(0);
      textSize(20);
      text("Score: " + icecream.l, 50, 23);
      fill(0);
      textSize(20);
      text("High Score: " + score, 500, 26);
    }
    
    void hitCone() {
      if (canHit == true && (cone.x + 150 > icecream.x - 40) && (cone.x + 150 < icecream.x + 40) 
        && (icecream.y1 > cone.y +30) && icecream.y1 < cone.y + 60) {
        player1.loop();
        icecream.spawn();
        icecream.l++;
        score++;
      }
    }
    
    void reachBottom() {
      if (icecream.y1 > height - 20) {
        player.play();
        endGame = true;
      }
    }
    
    void gradientBackground(color c1, color c2) {
      for (int i=0; i<=width; i++) {
        for (int j = 0; j<=height; j++) {
          color c = color(
          (red(c1)+(j)*((red(c2)-red(c1))/height)), 
          (green(c1)+(j)*((green(c2)-green(c1))/height)), 
          (blue(c1)+(j)*((blue(c2)-blue(c1))/height))
            );
          set(i, j, c);
        }
      }
    }
    class Cone {
      int l;
      float x;
      float y;
      String move;
    
    Cone () {
      l = 1;
      x = 150;
      y = 400;
    }
    
    void display() {
      noStroke();
      fill(#FFD581);
      triangle(x + 100, y + 50, x + 200, y + 50, x + 150, y + 190);
    }
    
    void move() {
     if(moveRight && x < 350)
      {
        x += speed;
      }
     if(moveLeft && x > -50)
      {
        x -= speed;
      }
      }
    }
    class Icecream {
      float x;
      float y1;
      boolean checkHit;
      int s;
      int l;
    
    Icecream() {
        x = int(random(100,350));
        y1 = 0;
        s = 5;
        l = 0;
       } 
    
    void display() {
      noStroke();
      fill(random(255), random(255), random(255));
      arc (x, y1, 110, 110, -PI, 0);
      ellipse(x-45,y1, 40,40);
      ellipse(x-15,y1,40,40);
      ellipse(x+15,y1,40,40);
      ellipse(x+45,y1,40,40);
      y1 = y1 + s;
      
    }
    
    void spawn() {
      for (int i = 0; i< l; i++)
       { 
         player1.play();
         y1 = 0;
         x = int(random(50,350));
         canHit = true;
         
         }
       if (score % 5 == 0) {
           s++;  
       
       }
         }
         
    void stay() {
      if (canHit == true && (cone.x + 150 > icecream.x - 40) && (cone.x + 150 < icecream.x + 40) 
        && (icecream.y1 > cone.y +30) && icecream.y1 < cone.y + 60) {
          s = 0;
      }
    }
    }
    
  • and? have you done what we said? does it work?

  • please come back when you need help

Sign In or Register to comment.