4 Replies Latest reply: Apr 11, 2013 8:38 PM by EJP RSS

    My own version of pong

    1001180
      controlls UP DOWN, A to enable automatic movement F to disable automatic movement.






      import java.applet.Applet;
      import java.awt.Color;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.Rectangle;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.KeyEvent;
      import java.awt.event.KeyListener;
      import java.util.Random;

      import javax.swing.Timer;


      public class Pongmatic extends Applet implements KeyListener,ActionListener{


           private Rectangle rect;
           private int polx = 1;
           private int poly = 1;
           private int pont1 =0;
           private int pont2 = 0;
           private int badY = 0;

           private Rectangle police;
           Timer tm = new Timer(5, this);
           private Rectangle rect2;

           private boolean auto = false;
           private boolean nonstop = false;
           private boolean follows = true;
           private boolean notfol = false;
           private int move = 0;
           private int move2=0;
           private int fail;







           public void init(){
                tm.start();
                rect = new Rectangle(15,300,25,50);
                rect2 = new Rectangle(745,300,25,50);
                police = new Rectangle(500,300,25,25);
                fail();


                
                     
                          
                     
                     
                          
                     
                


                this.addKeyListener(this);
           }
           private void fail() {
                if (Math.random() < 0.50){
                     fail = 25;}
                else{
                     if (Math.random() < 0.50){
                          fail = 40;}
                     }
                
                
                
           }
           public void paint(Graphics g) {
                super.paint(g);
                g.fillRect(0,0,90000,90000);
                


                {
                     //frame
                     this.setSize(800,600);





                     Graphics2D g10 = (Graphics2D) g;
                     Graphics2D g1 = (Graphics2D) g;
                     g10.setColor(Color.red);
                     g10.fill(rect);
                     g10.fill(rect2);

                     g1.setColor(Color.BLUE);
                     g1.fill(police);


                }
           }

           @Override
           public void actionPerformed(ActionEvent e) {
                police.x = police.x -polx;
                police.y = police.y -poly;
                badY = police.y - 60;

                if(follows){
                     rect.setLocation(rect.x,police.y);
                }
                if(notfol){
                     rect.setLocation(rect.x,badY);
                }

                //bouncing of back wall
                if(nonstop){
                     if(police.x==0||police.x==780){
                          polx = -polx;
                     }
                }


                if(auto){
                     rect2.y = police.y;


                }
                if(fail == 50){
                          follows = false;
                          notfol = true;     
                }
                

                //bounces of characters
                if(police.intersects(rect)){     
                     polx=- polx;


                }
                if(police.intersects(rect2)){
                     polx=-polx;
                     fail = fail +1;
                }


                if(move2==0){
                     if(police.intersects(rect)){     
                          poly = -poly;     
                     }

                }
                if(move2==1){
                     if(police.intersects(rect)){     
                          poly = +poly;
                     }
                }





                if(move == 1){
                     if(police.intersects(rect2)){
                          poly = +poly;                                             

                     }
                }
                if(move == 0){
                     if(police.intersects(rect2)){
                          poly = -poly;                                             

                     }
                }

                //bouncing of walls
                if(police.y == 550||police.y==0){
                     poly = -poly;
                }
                repaint();
           }

           @Override
           public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

           }

           @Override
           public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub





                if (e.getKeyCode() == KeyEvent.VK_W) {
                     rect.setLocation(rect.x,rect.y-30);


                }
                if (e.getKeyCode() == KeyEvent.VK_S) {
                     rect.setLocation(rect.x,rect.y+30);

                }
                if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                     rect2.setLocation(rect2.x,rect2.y+30);
                     move = 1;
                     move2=0;

                }
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                     rect2.setLocation(rect2.x,rect2.y-30);
                     move = 0;
                     move2=1;

                }
                if (e.getKeyCode() == KeyEvent.VK_A) {
                     auto = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_F) {
                     auto = false;
                }
                if (e.getKeyCode() == KeyEvent.VK_B) {
                     nonstop = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_N) {
                     nonstop = false;
                }

                repaint();

           }



           @Override
           public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub

           }

      }