• fullscreen
  • Pong.pde
  • Upgrade.pde
  • //public int upgradeInterval = int(random(3000,4000));
    //int upgradeCounter = upgradeInterval-1000;
    PVector vel = new PVector();
    PVector loc = new PVector(30,50);
    boolean playing = false;
    boolean titleScreen = true;
    boolean falling = true;
    float score;
    int lifes = 5;
    PFont font;
    //ArrayList upgrades = new ArrayList();
    void setup()
    {
      frameRate(200);
      falling = true;
      lifes = 5;
      score = 0;
      vel.x = random(1,3);
      vel.y = random(5,7);
      font = createFont("Algerian",40);
      noStroke();
      fill(0);
      textFont(font);
      size(300,400);
      background(210,110,10);
      playing = false;
      titleScreen = true;
      cursor(ARROW);
      loc.x = 30;
      loc.y = 50;  
    }
    void draw()
    {
      background(210,110,10);
      if (titleScreen)
        text("Play Pong",40,200);
      else if (playing)
        play();
      else if (!playing&&!titleScreen)
        background(210,110,10);
    }
    void ellipse()
    {
      noStroke();
      fill(0);
      ellipse(loc.x,loc.y,40,40);
    }
    void rect()
    {
      noStroke();
      fill(140,70,20);
      rect(mouseX-40,height-40,80,20);
    }
    void update()
    {
      loc.x+=vel.x;
      loc.y+=vel.y;  
    }
    void play()
    {
      /*if (upgradeCounter <= upgradeInterval)
      {  
        upgradeCounter++;
      }
      else
      {
        upgradeCounter = 0;
        upgrades.add(new Upgrade(random(30,270)));
        for (int num = 0; num < upgrades.size(); num++)
        {
          ((Upgrade)upgrades.get(num)).fall();  
        }
      }*/
      font = loadFont("ArialMT-20.vlw");
      textFont(font);
      text("Score: " + int(score),170,30);
      text("Lifes: " + lifes,20,30);
      noCursor();
      ellipse();
      rect();
      update();
      if (get(int(loc.x),int(loc.y+20)) == color(140,70,20))
      {
        if (falling)
        {
          score+=(6*abs(vel.y));
          vel.y*=-1.02;
          if (vel.x < 0)
            vel.x = -(random(1,10));
          else if (vel.x > 0)
            vel.x = random(1,10);
          falling = false;
        }
      }
      else if (loc.y-20<=0)
      {
        vel.y*=-1;
        falling = true;
      }
      else if (loc.x-20<=0)
        vel.x*=-1;
      else if (loc.x+20>=width)
        vel.x*=-1;
      else if (loc.y-20>=height)
        if (lifes>1)
        {  
          lifes--;
          loc.x = 30;
          loc.y = 50;
          vel.x = random(1,3);
          vel.y = random(5,7);
          score-=score/2;
        }
        else
        {
          playing = false;
          titleScreen = false;
          redraw();
          delay(2000);
          setup();
        }
    }
    void mousePressed()
    {
      if (titleScreen)
      {
        if (mouseX >= 40 && mouseY >= 170 && mouseX <= 250 && mouseY <= 200)
        {
          titleScreen = false;
          playing = true;
        }
      }
      if (!titleScreen)
      {
        if (vel.y > 0)
          vel.y+=2;
        else if (vel.y < 0)
          vel.y-=2;    
      }
    }
    void keyPressed()
    {
      if (key == 'r')
        setup();
    }
    
    /*class Upgrade
    {
      private int maxtypes = 3;
      int type;
      private PVector location;
      public Upgrade(float x)
      {
        type = int(random(1,maxtypes));
        location.x = x;
        location.y = -20;
        stroke(100);
        strokeWeight(2);
        fill(50,50,type*(255/maxtypes));
        ellipse(loc.x,loc.y,15,15);
      }
      void fall()
      {
        location.y+=2;
        stroke(100);
        strokeWeight(2);
        fill(50,50,type*65);
        ellipse(loc.x,loc.y,15,15);
      }
      void remove()
      {
        upgrades.remove(this);
        upgradeInterval = int(random(3000,4000));
      }  
    }*/
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Jashaszun

    Pong

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    Made it completely myself.

    I'm sorry if this acts weirdly sometimes, like when the ball hits a wall, starts shaking, then goes down and you lose. I'm sorry 'bout that, but this is my first game.

    If it DOES act weirdly, press 'r' to restart.

    This is my version of the classic Pong. You have five lives. If you lose all your lives, the program restarts. You can click to make the ball go faster, and get more points.

    Try to get 50,000 points. Good luck with that!...

    Dan Bernier
    1 Oct 2009
    Not positive, but when the ball sticks to the wall, it's probably because you're re-reversing too soon...the ball hasn't had a chance to "escape" from the wall, before it gets turned around again.

    Where you have:
    else if (loc.x-20&lt;=0) { vel.x*=-1; }

    ...if you reverse the velocity and move the ball, but the ball STILL is inside that area (loc.x-20 &lt;= 0), then you'll reverse (again), and go back towards the wall.
    You need to login/register to comment.