How to bounce ball with borders ?

edited December 2015 in Questions about Code

Hello pro brothers, I modified one of the game from proccessing game. But,now I want borders like this. http://i76.photobucket.com/albums/j5/Darius25/Borders problem_zpsbbwhzq2i.jpg But,how? I'm so noob and I just started to learn processing.Can you pro brothers help me out? Thank you.

Here are my lines in processing:

boolean gameover= false, right = false, left = false, up = false,down = false, d = false, a = false, w = false, s = false;
int topgoals=0;
int bottomgoals=0;
float changespeed=0;
Paddle bottom;
Ball football;
Paddle top;
void setup()
{
  frameRate(100);
  noStroke();
  football= new Ball();
  bottom=new Paddle();
  top=new Paddle();
  top.y=0;
  bottom.y = 754;
  size(720,760);
}
void keyPressed()
{
  if (keyCode == LEFT)
  {
    left = true;
  }
  if (keyCode == RIGHT)
  {
    right = true;
  }
  if(keyCode == UP)
  {
    up = true;
  }
  if(keyCode == DOWN)
  {
    down = true;
  }
  if (key == 'a')
  {
    a=true;
  }
  if (key == 'd' )
  {
    d=true;
  }
  if (key == 'w')
  {
    w = true;
  }
  if (key == 's')
  {
    s = true;
  }
}
void keyReleased()
{
  if (keyCode == LEFT)
  {
    left = false;
  }
  if (keyCode==RIGHT)
  {
    right = false;
  }
  if (keyCode == UP)
  {
    up = false;
  }
  if (keyCode == DOWN)
  {
    down = false;
  }
  if (key=='a')
  {
    a=false;
  }
  if (key=='d')
  {
    d=false;
  }
  if (key == 'w')
  {
    w = false;
  }
  if (key == 's')
  {
    s = false;
  }
}
void draw()
{
  if (gameover==false)
  {
    background(#00760F);
    fill(#FFFFFF);
    rect(0,0,4,850);
    rect(716,0,4,850);
    bottom.show();
    top.show();
    if (left==true)
    {
      bottom.moveleft();
    }
    if (right==true)
    {
      bottom.moveright();
    }
    if (a==true)
    {
      top.moveleft();
    }
    if (d==true)
    {
      top.moveright();
    }
    football.move();
    football.bounce();
    football.show();
    if (football.n<-8)
    {
      gameover=true;
      bottomgoals++;
    }
    if (football.n>850)
    {
      gameover=true;
      topgoals++;
    }
  }
  else //gameover==true
  {
    background(0);
    fill(255, 0, 0);
    changespeed=0;
    textSize(18);
    text("Top Player's goals: "+topgoals, 15, 290);
    text("Bottom Player's goals: "+bottomgoals, 15, 330);
    textSize(36);
    text("Game over! Click to restart.", 15, 250);
    if (mousePressed==true)
    {
      football.m=int(random(200, 301));
      football.n=int(random(200, 301));
      int xdirection=int(random(2));
      int ydirection=int(random(2));
      if (xdirection==0)
      {
        football.right=true;
      }
      else //xidrection==1
      {
        football.right=false;
      }
      if (ydirection==0)
      {
        football.up=true;
      }
      else //ydirection==1
      {
        football.up=false;
      }
      gameover=false;
    }
  }
}
class Paddle
{
  int x, y;
  Paddle()
  {
    x=360;
  }
  void show()
  {
    fill(#038BFF);
    rect(x, y, 80, 6);
  }
  void moveleft()
  {
    if (x>=0)
    {
      x -= 5;//speed
    }
  }
  void moveright()
  {
    if (x<=635)
    {
      x += 5;//speed
    }
  }
}
class Ball
{
  int m, n;
  boolean up, right;
  Ball()
  {
    m=425;
    n=360;
    up=true;
    right=true;
  }
  void move()
  {
    if (up==true)
    {
      n=int(n-2-changespeed/2);
    }
    else  //up==false
    {
      n=int(n+2+changespeed/2);
    }
    if (right==true)
    {
      m=int(m+2+changespeed/2);
    }
    else  //right==false
    {
      m=int(m-2-changespeed/2);
    }
  }
  void bounce()
  {
    if (get(int(m)-8, int(n))!=color(#00760F))
    {
      right=true;
    }
    if (get(int(m)+8, int(n))!=color(#00760F))
    {
      right=false;
    }
    if (get(int(m), int(n)-8)==color(#038BFF))
    {
      up=false;
    }
    if (get(int(m), int(n)+8)==color(#038BFF))
    {
      up=true;
      changespeed+=1.0/4;
    }
  }
  void show()
  {
    fill(#FF0324);
    ellipse(m, n, 16, 16);
  }
}
Tagged:

Answers

  • please format your code.

    highlight, hit ctrl-o.

  • I didn't know that. Thank you. Now,can please help me?

  • edited December 2015

    I made an example for you. I changed the mechanics logic so there will be less code, but you can bring it back.

    Ball ball;
    BorderBox[] borders;
    
    void setup()
    {
      size(600,400);
      frameRate(100);
      fill(255);
      noStroke();
      ball= new Ball(width/2, height/2, 20);
    
      borders = new borders[4];
      borders[0] = new BorderBox(0,0, 250, 100);
      borders[1] = new BorderBox(width-100,0, 100, 250);
      borders[2] = new BorderBox(0,height-200, 100, 200);
      borders[3] = new BorderBox(width-200,height-100, 200, 100);
    }
    
    void draw()
    {
      background(0);
      ball.move();
      ball.show();
      for (int i = 0; i < borders.length; i++){
      borders[i].show();
      borders[i].repeal(ball);
      }
    }
    
    class Ball
    {
      float x, y;
      float speedX, speedY;
      int size;
    
      Ball(float x_, float y_, int size_)
      {
       x = x_;
       y = y_;
       size = size_;
       speedX = 1;
       speedY = 1;
      }
      void move()
      {
      x += speedX;
      y += speedY;
      }
      void show(){
      ellipse(x, y, size, size);
      }
    }
    
    class BorderBox{
    int x, y, w, h;
    BorderBox(int x_, int y_, int w_, int h_){
        x = x_;
        y = y_;
        w = w_;
        h = h_;
        }
    
        void repeal(Ball b){
        if (b.x + b.size/2 > x && b.x - b.size/2 < x+w && b.y + b.size/2 > y && b.y - b.size/2 < y+h){
                if( b.x + b.size/2 > x + 1 && b.x - b.size/2 < x+w - 1) {
                b.speedY *= -1;
                } else if (b.y + b.size/2 > y + 1 && b.y - b.size/2 < y+h - 1){
                b.speedX *= -1;
                } else {
                b.speedY *= -1;
                b.speedX *= -1;
                }
            }
        }
    
        void show(){
        rect(x, y, w, h);
        }
    }
    

    In order to understang what's going on here, I striktly recommend you to get into object-oriented programming. Here, we have 2 classes - for ball and borders they can interact with each other. Try adding more boxes or modifiying existing - you will see that we have a universal object that makes a ball bounce regardless of it's size and position.

    Also feel free to ask any questions.

Sign In or Register to comment.