Well guys were back with some problems. After lots of coding and crap, we got the ball to move and kinda bounce off the wall but one problem. The paddles dont move. The movement of the ball somehow cancles out the movement of the paddles/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| import java.awt.*; import java.awt.Graphics; import java.applet.Applet; import java.awt.event.*;
public class Pong extends Applet implements ActionListener { Ball Ball1=new Ball(); pongpaddle Paddle1=new pongpaddle(); pongpaddle Paddle2=new pongpaddle();
Button btnStart=new Button("Start Game"); Button btnRestart=new Button("Restart Game");
int Counter1=0; int Counter2=0; int MovX; int MovY; int Ballx=Ball1.getBall_xLoc(); int Bally=Ball1.getBall_yLoc(); int Xinc=1; int Yinc=1; Image offscreen; Graphics buffer;
public void init() { requestFocus(); addKeyListener(new DirectionKeyListener());
Paddle1.setLocation(0,140); Paddle2.setLocation(680,140); btnStart.addActionListener(this); btnRestart.addActionListener(this); add(btnStart); add(btnRestart);
offscreen=createImage(700,350); buffer=offscreen.getGraphics(); }
private class DirectionKeyListener implements KeyListener { public void keyPressed(KeyEvent event) { if(event.getKeyCode()==KeyEvent.VK_W & Paddle1.getyLoc1()>0) { Paddle1.moveUp(); repaint(); }
if(event.getKeyCode()==KeyEvent.VK_S & Paddle1.getyLoc1()<280) { Paddle1.moveDown(); repaint(); }
if(event.getKeyCode()==KeyEvent.VK_P & Paddle2.getyLoc1()>0) { Paddle2.moveUp(); repaint(); }
if(event.getKeyCode()==KeyEvent.VK_L & Paddle2.getyLoc1()<280) { Paddle2.moveDown(); repaint(); } } public void keyTyped(KeyEvent event){} public void keyReleased(KeyEvent event){} }
public void drawPaddle(Graphics g) { g.setColor(Color.RED); Paddle1.drawPaddle(g);
g.setColor(Color.BLUE); Paddle2.drawPaddle(g); }
public void actionPerformed(ActionEvent e) { if(e.getSource()==btnStart) move(); }
public void move() { Ballx=Ballx+Xinc; Bally=Bally+Yinc; Ball1.setLocation(Ballx+1,Bally+1);
if(Ball1.getBall_xLoc()==0) { Xinc=1; repaint(); }
if(Ball1.getBall_xLoc()==680) { Xinc=-1; repaint(); }
if(Ball1.getBall_yLoc()==0) { Yinc=1; repaint(); }
if(Ball1.getBall_yLoc()==330) { Yinc=-1; repaint(); } }
public void update(Graphics g) { paint(g); }
public void paint(Graphics g) { buffer.setColor(Color.BLACK); buffer.fillRect(0,0,700,350);
drawPaddle(buffer); Ball1.drawBall(buffer); g.drawImage(offscreen,0,0,this); Ballx=Ballx+Xinc; Bally=Bally+Yinc; Ball1.setLocation(Ballx+1,Bally+1); move(); repaint(); } } |
Posted the whole code. If anyone can help, please do, this is my final >.<