Hello all,
I'm only starting out, and I've read somewhere on this forum to start small - so, Pong!
Here is a game I've wrote rather quickly (due to having a good beginner's grasp over programming languages such as Java and C++).
First and foremost, the game is based on this tutorial: link
It is a Java applet, the screen is drawn every 15ms via a Timer. I assume there is no need to explain pong to anyone
I would appreciate some advice on how to improve the AI further (I don't want the AI to be invincible of course).
Also, I would LOVE to know how to make the drawing of the computer paddle (right) less jittery and jumpy (make it smooth).
And, as should be common, any and all advice and criticism on all other aspects are welcome.
OH! And there is a null pointer error I can't find, though the game runs as predicted :S
Hopefully I wont bore you. Here's my code:
Player paddle class :
public class PlayerPaddle {
private int yPos = 0;
final int XPOS = 30;
public PlayerPaddle() {
setPos(120);
}
public void setPos(int pos) {
yPos = pos;
if(yPos > 230)
setPos(230);
else if (yPos<0)
setPos(0);
}
public int getPos() {
return yPos;
}
}
Computer paddle class:
public class ComputerPaddle {
private int yPos = 0;
private int score; // keeps track of score, should be tracked in PongMain though
private int paddleSpeed;
final int XPOS = 460;
public ComputerPaddle(int ballPos, int speed) {
setPos(ballPos);
setScore(0);
paddleSpeed = speed;
}
public void setPos(int pos) {
yPos = pos;
if(yPos > 230)
setPos(230);
else if(yPos < 0)
setPos(0);
}
public int getPos() {
return yPos;
}
public void setScore(int score) {
this.score = score;
}
public int getScore() {
return score;
}
public int getSpeed() {
return paddleSpeed;
}
}
Ball class:
public class Ball {
private int xPos, yPos;
public int dx, dy;
public Ball() {
reset();
}
public void setPos(int x, int y) {
xPos = x;
yPos = y;
}
public int getX() {
return xPos;
}
public int getY() {
return yPos;
}
public void move() {
setPos(xPos+dx,yPos+dy);
}
public void reset() {
setPos(250,140);
// a 50/50 chance of direction in which the ball is headed
double ran = Math.random()*10;
if(ran<=5) {
dy = -5;
dx = 5;
} else {
dy = 5;
dx = -5;
}
}
}
Computer player simulator class (idk how else to call it):
/* an AI class,
* has a thread that checks the position of the ball, and changes the current position of the paddle accordingly
* a paddle (myPaddle) may read the current position at leisure,
* the paddle has no 'intelligence', it is being played by an Computer type
*/
public class Computer implements Runnable {
int currentPos, difficulty;
// currentPos - position at witch Computer wishes to place the paddle
// difficulty - time (in ms) during which the Computer doesn't react
ComputerPaddle myPaddle;
Ball ball;
Thread thread = new Thread(this);
Computer(ComputerPaddle p, Ball b, int dif) {
myPaddle = p;
difficulty = dif;
ball = b;
currentPos = myPaddle.getPos();
thread.start();
}
public void run() {
while(!Thread.interrupted()) {
try {
if(currentPos > ball.getY() - 35)
currentPos = currentPos - myPaddle.getSpeed();
else if(currentPos < ball.getY() - 35)
currentPos = currentPos + myPaddle.getSpeed();
Thread.sleep(difficulty);
} catch(InterruptedException g) {}
}
}
public int getPos() {
return currentPos;
}
}
and finally, the Apllet (PongMain) class:
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.Timer;
public class PongMain extends Applet implements MouseMotionListener, ActionListener {
Ball ball;
PlayerPaddle pLeft;
ComputerPaddle pRight;
Computer AI;
Timer time = new Timer(15,this);
Font newFont = new Font("sansserif", Font.ITALIC, 15);
Graphics bufferGraphics;
Image offscreen;
long startingTime;
final int WIDTH = 500, HEIGHT = 300;
// =================
// initializes fields and applet
public void init() {
setSize(WIDTH,HEIGHT);
setVisible(true);
ball = new Ball();
pLeft = new PlayerPaddle();
pRight = new ComputerPaddle((ball.getY() - 35), 15);
AI = new Computer(pRight, ball, 40);
addMouseMotionListener(this);
setBackground(Color.green);
offscreen = createImage(WIDTH,HEIGHT);
bufferGraphics = offscreen.getGraphics();
}
// starts the Timer time and runs it until a player (or a computer) reaches score of 3
public void start() {
startingTime = System.currentTimeMillis()/1000;
time.start();
while(Math.abs(pRight.getScore())<3);
time.stop();
repaint();
}
public void stop() {
}
// every 15ms, this is performed
public void actionPerformed(ActionEvent arg0) {
ball.move();
pRight.setPos(AI.getPos());
checkCollision();
repaint();
}
public void checkCollision() {
// bounce off of horizontal bounds
if(ball.getY() <=0 || ball.getY() >= 290)
ball.dy = ball.dy*(-1);
// if the ball reaches the player side (left) and hits the paddle
// increase the speed (unless its maxed)
if(ball.getX() == 40 && hitPaddle() ) {
ball.dx = ball.dx*(-1);
if(ball.dx >0 && ball.dx < 7)
ball.dx++;
if(ball.dx < 0 && ball.dx > -7)
ball.dx--;
}
// if the ball reaches the computer side (right) and hits the paddle
// notice the speed does not increase
if(ball.getX() == 460 && hitComPaddle())
ball.dx = ball.dx*(-1);
// if the computer paddle fails, append score and reset
if(ball.getX() >= WIDTH ) {
pRight.setScore(pRight.getScore() -1 );
ball.reset();
}
// if the player fails, append score and reset
if(ball.getX() <= 0) {
pRight.setScore(pRight.getScore() + 1);
ball.reset();
}
}
public boolean hitPaddle() {
boolean didHit = false;
if((pLeft.getPos() - 10) <= ball.getY() && (pLeft.getPos() + 70) > ball.getY()) {
didHit = true;
// if the paddle is hit close the the edge, the vertical speed is increased
if(( ball.getY() >= (pLeft.getPos() - 10) && ball.getY() <= (pLeft.getPos() + 10)) || ( ball.getY() >= (pLeft.getPos() + 50) && ball.getY() <= (pLeft.getPos() + 70)))
if( ball.dy > 0 && ball.dy < 8) ball.dy++;
else if( ball.dy > -8) ball.dy--;
}
return didHit;
}
public boolean hitComPaddle() {
boolean didHit = false;
if( (pRight.getPos() - 10 ) <= ball.getY() && ( pRight.getPos() + 70) > ball.getY())
didHit = true;
return didHit;
}
// as explained in tutorial, the screen is first drawn onto and Image via bufferGraphics
// once the image is painted completely, it is painted on screen
// thus: [INPUT] ---(serial)--> [BUFFER(IMAGE)] ---(block)--> [SCREEN]
public void paint(Graphics g) {
bufferGraphics.clearRect(0, 0, WIDTH, HEIGHT);
bufferGraphics.setColor(Color.BLUE);
bufferGraphics.fillRect(pLeft.XPOS, pLeft.getPos(), 10, 70);
bufferGraphics.fillRect(pRight.XPOS, pRight.getPos(), 10, 70);
bufferGraphics.setColor(Color.WHITE);
bufferGraphics.setFont(newFont);
bufferGraphics.drawString("Score", 140, 15);
bufferGraphics.drawString("" + pRight.getScore(), 300, 15);
bufferGraphics.drawString("Speed", 140, 30);
bufferGraphics.drawString("x: " + Math.abs(4 - Math.abs(ball.dx)) + " y: " + Math.abs(4 - Math.abs(ball.dy)), 300, 30);
bufferGraphics.fillRect(255,0,2,300);
if(Math.abs(ball.dx) == 5)
bufferGraphics.setColor(Color.RED);
else if(Math.abs(ball.dx) == 6)
bufferGraphics.setColor(Color.YELLOW);
else if(Math.abs(ball.dx) == 7)
bufferGraphics.setColor(Color.WHITE);
bufferGraphics.fillOval(ball.getX(), ball.getY(), 10, 10);
bufferGraphics.setColor(Color.WHITE);
bufferGraphics.drawString("Game length", 140, 45);
bufferGraphics.drawString("" + (System.currentTimeMillis()/1000 - startingTime) + "s", 300, 45);
g.drawImage(offscreen, 0, 0, this);
try{
Toolkit.getDefaultToolkit().sync();
} catch(AWTError error) {}
}
public void update(Graphics g){
paint(g);
}
public void mouseMoved(MouseEvent arg0) {
pLeft.setPos(arg0.getY() - 35);
}
public void mouseDragged(MouseEvent arg0) {
}
}
I can't attach the files (am not permitted, something about .java and .class extensions, sorry)