Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Cant get random speed for the ellipse

$
0
0

Hello everyone,

I have programmed a very simple ping pong-ish but the ball does not bounce back randomly. I want the Y speed to be random. (for example -4 and -8).

Here is the whole code

import ddf.minim.*;

AudioPlayer player;
Minim minim;//audio context
int base=30;
int x,y,gameScore=0;
int changeX=-8;
int changeY=-6;
int gameOver=0;
PImage img;
void setup()
{
  size(760, 640);
  x=(int)random(width);
  y=height-base;
  minim = new Minim(this);
  player = minim.loadFile("work.mp3", 2048);
  player.play();
  img = loadImage("work.jpg");

}
void draw()
{
  if(gameOver==0)
  {
  background(img);
  text("SCORE:"+gameScore+"",width/2,height/2);
  rect(mouseX,height-base,160,base);
  ellipse(x,y,15,15);
  x=x+changeX;
  y=y+changeY;
  if(x<0 | x>width)
  {
    changeX=-changeX;
  }
  if(y<0)
  {
    changeY=-changeY;
  }
  if(y>height-base)
  {
    //check whether it is falling inside the rectangle or not
    if(x>mouseX && x<mouseX+200)
    {
      changeY=-changeY; //bounce back
      gameScore++;
    }
    else
    {
      gameOverSplash();
    }
  }
  }
  else
  {
    background(100,100,200);
    text("Game Over!",width/2,height/2);
    text("CLICK TO RESTART",width/2,height/2+20);
  }
}
void gameOverSplash()
{
  gameOver=1;
}
void mouseClicked()
{
  changeY=-changeY;
  gameScore=0;
  gameOver=0;
}
void stop()
{
  player.close();
  minim.stop();
  super.stop();
}

I really would appreciate your help. Thanks


Viewing all articles
Browse latest Browse all 2896

Trending Articles