import java.awt.* ;
import java.awt.event.* ;
import java.awt.geom.* ;
import javax.swing.* ;
import javax.swing.event.* ;
/**
* This Class holds the code for the game.
* @author William Peckham
* @version 1.0A
*/
public class PlayArea extends JComponent {
/**Variable for the Hieght of the Play Area*/
public int height = 300 ;
/**Variable for the Width of the Play Area*/
public int width = 600 ;
/**Because this is Pong we need a Ball*/
public Ball ball ;
/**This will be player one's paddle*/
public Paddle player1 ;
/**This will be player two's paddle*/
public Paddle player2 ;
/**This variable will hold player one's score*/
public int score1 = 0 ;
/**This variable will hold player two's score*/
public int score2 = 0 ;
/**This variable will keep track of whether the game is paused
* it's default is false, so the game will not be paused at start*/
public boolean paused = false ;
/**variable will keep track of player one's up ('w') key*/
public volatile boolean wpressed = false ;
/**variable will keep track of player one's down ('s') key*/
public volatile boolean spressed = false ;
/**variable will keep track of player two's up (up arrow) key*/
public volatile boolean uppressed = false ;
/**variable will keep track of player two's down (down arrow) key*/
public volatile boolean downpressed = false ;
/**make a new PlayArea*/
public PlayArea ( ) {
System.out.println ( "djkl;" ) ;
this.setSize ( width , height ) ;
this.setPreferredSize ( new Dimension ( width , height ) ) ;
this.addKeyListener ( new keylistener ( ) ) ;
setFocusable ( true ) ;
requestFocusInWindow ( ) ;
ball = new Ball ( this ) ;
player1 = new Paddle ( this , 0 ) ;
player2 = new Paddle ( this , 0 ) ;
player2.x = width - player2.width ;
}
public void run ( ) {
if ( ! paused ) {
if ( wpressed ) {
player1.step ( true , true ) ;
} else if ( spressed ) {
player1.step ( true , false ) ;
} else {
player1.step ( false , false ) ;
}
if ( uppressed ) {
player2.step ( true , true ) ;
} else if ( downpressed ) {
player2.step ( true , false ) ;
} else {
player2.step ( false , false ) ;
}
ball.step ( ) ;
if ( ! ball.checkAndCollideWithHorizWall ( ) ) {
if ( ball.x > width / 2 ) {
ball.checkAndCollideWithPaddle ( player2 ) ;
} else {
ball.checkAndCollideWithPaddle ( player1 ) ;
}
} else {
ball.initialize ( ) ;
}
}
repaint ( ) ;
}
public void paint ( Graphics g ) {
Graphics2D g2 = ( Graphics2D ) g ;
g2.fill ( player1 ) ;
g2.fill ( player2 ) ;
g2.fill ( ball ) ;
}
class keylistener implements KeyListener {
public void keyReleased ( KeyEvent e ) {
if ( e.getKeyCode ( ) == KeyEvent.VK_W ) {
PlayArea.this.wpressed = false ;
} else if ( e.getKeyCode ( ) == KeyEvent.VK_S ) {
PlayArea.this.spressed = false ;
}
if ( e.getKeyCode ( ) == KeyEvent.VK_UP ) {
PlayArea.this.uppressed = false ;
} else {
if ( e.getKeyCode ( ) == KeyEvent.VK_DOWN ) {
PlayArea.this.downpressed = false ;
}
}
System.out.println ( e.getKeyText ( e.getKeyCode ( ) ) + " released" ) ;
}
public void keyPressed ( KeyEvent e ) {
System.out.println ( "dsssd" ) ;
if ( e.getKeyCode ( ) == KeyEvent.VK_W ) {
PlayArea.this.wpressed = true ;
}
if ( e.getKeyCode ( ) == KeyEvent.VK_S ) {
PlayArea.this.spressed = true ;
}
if ( e.getKeyCode ( ) == KeyEvent.VK_UP ) {
PlayArea.this.uppressed = true ;
}
if ( e.getKeyCode ( ) == KeyEvent.VK_DOWN ) {
PlayArea.this.downpressed = true ;
}
System.out.println ( e.getKeyText ( e.getKeyCode ( ) ) + " pressed" ) ;
}
public void keyTyped ( KeyEvent e ) {
System.out.println ( "DDDD" ) ;
}
}
}