Professional Documents
Culture Documents
package com.mycompany.pingpong;
import javax.swing.*;
import java.awt.event.ActionEvent;
//the size of the game will be 640x480, the size of the JFrame needs to be slightly larger
f.setSize(900,500);
}
}
PADDLE:
package com.mycompany.pingpong;
/**
* A paddle is a rectangle/block that can move up and down
* @param x the x position to start drawing the paddle
* @param y the y position to start drawing the paddle
* @param height the paddle height
* @param speed the amount the paddle may move per frame
* @param color the paddle color
*/
public Paddle(int x, int y, int height, int speed, Color color) {
this.x = x;
this.y = y;
this.height = height;
this.speed = speed;
this.color = color;
}
/**
* Paints a rectangle on the screen
* @param g graphics object passed from calling method
*/
public void paint(Graphics g){
/**
* Move the paddle towards this y position until it's centered on it
* @param moveToY - position the paddle is centered on
*/
public void moveTowards(int moveToY) {
//make sure the paddle is far enough away from the target moveToY position. If it's closer
than the speed to where it should be, don't bother moving the paddle.
if(Math.abs(centerY - moveToY) > speed){
//if the center of the paddle is too far down
if(centerY > moveToY){
//move the paddle up by the speed
y -= speed;
}
//if the center of the paddle is too far up
if(centerY < moveToY){
//move the paddle down by speed
y += speed;
}
}
}
/**
* Checks if this paddle is colliding with a ball
* @param b the ball we're checking for a collision with
* @return true if collision is detected
*/
public boolean checkCollision(Ball b){
//check if the Ball is between the x values starting from one ball width to the left of the
paddle to the right side of the paddle
if(b.getX() > (x - b.getSize()) && b.getX() < rightX){
//check if Ball is between the top and bottom y values of the paddle
if(b.getY() > y && b.getY() < bottomY){
//if we get here, we know the ball and the paddle have collided
return true;
}
}
//if we get here, one of the checks failed, and the ball has not collided
return false;
}
}
BALL:
package com.mycompany.pingpong;
import java.awt.*;
import static java.awt.Color.black;
//paint the ball at x, y with a width and height of the ball size
g.fillOval(x, y, size, size);
/**
* Detect collision with screen borders and reverse direction
* @param top - the y value of the top of the screen
* @param bottom - the y value of the bottom of the screen
*/
public void bounceOffEdges(int top, int bottom){
/**
* Reverse's the ball's change in x value
*/
public void reverseX(){
cx *= -1;
}
/**
* Reverse's the ball's change in y value
*/
public void reverseY(){
cy *= -1;
}
/**
* Increases the speed of the ball (and cy/cx) by 1 up to the possible MAX_SPEED constant
*/
public void increaseSpeed(){
//make sure current speed is less than max speed before incrementing
if(speed < MAX_SPEED){
//increase the speed by one
speed ++;
//figure out if cx or cy is positive or negative (+1 or -1) and multiply this by the speed, for
the new speed
//e.g. -3 becomes -4, 3 becomes 4, and so on.
cx = (cx / Math.abs(cx)*speed);
cy = (cy / Math.abs(cy)*speed);
}
}
PONGGAME:
package com.mycompany.pingpong;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
//This class must extend JPanel so we can use paintComponent and implement
MouseMotionListener to track mouse
public class PongGame extends JPanel implements MouseMotionListener {
//Constants for window width and height, in case we want to change the width/height later
static final int WINDOW_WIDTH = 900, WINDOW_HEIGHT = 480;
private final Ball gameBall;
private final Paddle userPaddle;
private final Paddle pcPaddle;
private int userScore, pcScore;
/**
* Standard constructor for a PongGame
*/
public PongGame() {
}
public void gameOver(Graphics g)
{
String msg = "Game Over";
Font small = new Font("MV Boli", Font.BOLD, 35);
FontMetrics metr = getFontMetrics(small);
int width=this.getWidth();
int height=this.getHeight();
g.setColor(Color.white);
g.setFont(small);
if (inGame)
{
int checkWins = 1;
move();
}
repaint();
/**
* resets the game to start a new round
*/
public void reset(){
gameBall.setX(300);
gameBall.setY(200);
gameBall.setCx(3);
gameBall.setCy(3);
gameBall.setSpeed(7);
bounceCount = 0;
/**
* Updates and draws all the graphics on the screen
*/
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(857, 20, 20, 20);
g.drawLine(857, 450, 857, 21);
g.drawLine(857, 450, 20, 450);
g.drawLine(20, 450, 20, 21);
g.drawLine(857, 245, 20, 245);
g.drawRect(425, 450, 50, 10);
g.drawRect(425, 20, 50, 10);
g.setColor(Color.white);
g.drawLine(450, 21, 450, 580);
g.drawLine(448, 21, 448, 580);
g.drawLine(446, 21, 446, 580);
g.drawLine(452, 21, 452, 580);
g.drawLine(444, 21, 444, 580);
g.drawLine(454, 21, 454, 580);
g.drawLine(456, 21, 456, 580);
g.drawLine(442, 21, 442, 580);
g.drawLine(458, 21, 458, 580);
//update score
g.setColor(Color.black);
g.setFont(new Font("Times New Roman", Font.BOLD, 15));
//the drawString method needs a String to print, and a location to print it at.
g.drawString("Player " + userScore + " Computer " + pcScore + " ", 180, 16 );
}
/**
* Called once each frame to handle essential game operations
*/
public void gameLogic() {
//edge check/bounce
gameBall.bounceOffEdges(0, WINDOW_HEIGHT);
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
//Update saved mouse position on mouse move
userMouseY = e.getY();
}
}
OUTPUT: