Help on Mini Game, Please^^

I'm new to programming in general, still a beginner, so please excuse the "rough look" of my code and how nonsensical it might appear...still learning....

I'm trying to create my own mini game, and keep it as simple as possible. I'm trying to make the ball that's falling from the top of the screen collide with my image (myDinoHat), where it touches the plate. Basically, I'm trying to make it so that if the ball falling touches the plate, it will disappear and count as a point. How can I do this? Also, I'm trying to make the ball falling from the sky become an image instead, like the image of a skull for example. I got the ball to fall alright, and my image to act as a mouse moving to catch something, but everything else is not working in my favor. May I have some help on this, please? Any help would be greatly appreciated! The code is attached:

pastebin.com/p8fB0vbf

Tagged:

Answers

  • make it so that if the ball falling touches the plate, it will disappear and count as a point.

    ok, use dist() to check for collision

    if so say reset y-pos to random (-44,-22); and x to random(0,width);

    also say score ++; then - make a global var score

    ===

    to have an image instead of the ball just load the image and display it where the ball is

  • dist:::

    if (dist(ballx, bally, platex,platey)<51) {

    score++;

    bally=random(-62, -14);

    }

    Place this eg. Where you move the ball

  • edited April 2016

    Thank you for taking the time to respond! Well, I decided that I need to try and just make everything simple by just having the ball make contact with the plate as a collision. Simply put, I'm trying to make the ball hit the plate as if it were like ping pong. I thought that would be easy to do, by making my image (myDinoHat) become the paddle. But my screen keeps going blank for some reason. Any help on this please?

    pastebin.com/p8fB0vbf

  • we had it that you used paddleX instead of dinox

    the same goes for the entire function ballHitPaddle !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    new version (since I don't have the images, there are some changes regarding this, e.g. the function image1 and some checks for null

    sorry for answering late, I was busy

    PImage myDinoHat;
    PImage myBackground;
    
    int dinox = 0;
    int ballSize;
    
    // Paddle Variables
    /// float paddleX; // NOOOOOO !!!!!
    
    int paddleSize = 70;
    
    int x = 0;
    int y = 0;
    
    int ySpeed = 5;
    int xSpeed = 5;
    
    // -------------------------------------------
    
    // delete this: 
    
    void image1(PImage i1, float x, float y ) {
      fill(255, 2, 2); 
      if (i1==null)
        rect(x, y, 124, 24);
    }
    
    // -------------------------------------------
    
    void setup() {
      size(500, 500);
    
      myDinoHat = loadImage("DinoHat.png");
      myBackground = loadImage("MiddleEast.png");
    
      background(255);
    
      // Reset game
      newGame();
    }
    
    
    void draw ()
    {
    
      x = x + xSpeed;
      y = y + ySpeed;
    
      // Check if the ball hits any wall
      if (x > width) {
        xSpeed = -5;
        //soundWall.rewind();
        //soundWall.play();
      }
      if (x < 0) {
        xSpeed = 5;
        //soundWall.rewind();
        //soundWall.play();
      }
      if (y < 0) {
        ySpeed = 5;
        //soundWall.rewind();
        //soundWall.play();
      }
      if (y > (height - 35)) {
        // GAME OVER MAN
        // Ball hit bottom of the screen
        // Restart game
        newGame();  
        //soundLose.rewind();
        //soundLose.play();
      }
    
      // Check if ball hit paddle
      if (ballHitPaddle()) {
        ySpeed = -abs(ySpeed);
    
        // This line resets the ball position just above the paddle
        y = (height - 50) - (ballSize / 2);  
        //soundPaddle.rewind();
        //soundPaddle.play();
      }
    
      //dinox = mouseX; // this just has the picture of the dinosaur become the mouse, and the new one at the bottom just has the speed of the mouse slow down
      dinox += (mouseX - dinox) / 2;   //the higher the number 20 the slower it is, the lower the number the faster it is
    
      if (myBackground!=null)
        image(myBackground, 0, 0, 900, 800); // (x, y, width, height)
      else  background(255); // rect(0, 0, 900, 800); // (x, y, width, height)
    
      // paddle 
      // image instead of image1
      image1(myDinoHat, dinox, height - 50);
    
    
      //Drawing the ball
      noStroke();
      ballSize = 18;
      //background(255, 153, 204);
      ellipse(x, y, ballSize, ballSize);
    }
    
    // This functions checks if a ball has hit the paddle
    boolean ballHitPaddle() {
    
      // We set up a bunch of helper variables
      int ballLeft = x - (ballSize / 2);
      int ballRight = x + (ballSize / 2);
      int ballTop = y - (ballSize / 2);
      int ballBottom = y + (ballSize / 2);
    
      // println ( ballLeft) ; 
    
      float  padLeft = (dinox);
      float  padRight = (dinox) + paddleSize;
      float padTop = height - 50;
    
      // println ( padLeft) ; 
    
      // Eliminating all cases where a collision would be impossible
      if (ballBottom < padTop+5) {
        // println ("1!");
        return false;
      }
      if (ballRight < padLeft) {
        // println ("2!");
        return false;
      }
      if (ballLeft > padRight) {
        //  println ("3!");
        return false;
      }
    
      // We checked against all negative cases
      // A collision has probably occured
      return true;
    }
    
    //Restarts Game
    void newGame() {
      x = int(random(50, width - 50));
      y = 50;
      xSpeed = 5;
      ySpeed = 5;
    }
    
Sign In or Register to comment.