Esse jogo entitulado "Pong" foi feito totalmente em linguagem Java utilizando de bibliotecas próprias do Java e utilizando somente quatro classes que se encontram abaixo.

Classe Game (Principal)

package pong;


import java.awt.Canvas;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.image.BufferStrategy;

import java.awt.image.BufferedImage;


import javax.swing.JFrame;


public class Game extends Canvas implements Runnable, KeyListener{


/**

*/

private static final long serialVersionUID = 6513238800018010786L;

public static int WIDTH = 180;

public static int HEIGHT = 120;

public static int SCALE = 3;

public BufferedImage layer = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);

public static Player player;

public static Enemy enemy;

public static Ball ball;

public Game() {

this.setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));

this.addKeyListener(this);

player = new Player(100, HEIGHT-6);

enemy = new Enemy(100,1);

ball = new Ball (WIDTH/2 - 2, HEIGHT/2 - 2);

}

public static void main(String[] args) {

Game game = new Game();

JFrame frame = new JFrame("Pong");

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(game);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

new Thread(game).start();

}

public void tick() {

player.tick();

enemy.tick();

ball.tick();

}

public void render() {

BufferStrategy bs = this.getBufferStrategy();

if (bs == null) {

this.createBufferStrategy(3);

return;

}

Graphics g = layer.getGraphics();

g.setColor(Color.WHITE);

g.fillRect(0, 0, WIDTH, HEIGHT);

g.setColor(Color.BLACK);

g.drawString("Boa Sorte", WIDTH/2 - 25, HEIGHT/2);

player.render(g);

enemy.render(g);

ball.render(g);

ball.fim(g);

g = bs.getDrawGraphics();

g.drawImage(layer, 0, 0, WIDTH*SCALE, HEIGHT*SCALE,null);

bs.show();

}

@Override

public void run() {

while(true) {

tick();

render();

try {

Thread.sleep(1000/60);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


@Override

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_RIGHT){

player.right = true;

}

else if (e.getKeyCode() == KeyEvent.VK_LEFT){

player.left = true;

}

}


@Override

public void keyReleased(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_RIGHT){

player.right = false;

}

else if (e.getKeyCode() == KeyEvent.VK_LEFT){

player.left = false;

}

}


@Override

public void keyTyped(KeyEvent arg0) {

// TODO Auto-generated method stub

}


}

Classe Player

package pong;


import java.awt.Color;

import java.awt.Graphics;


public class Player {

public boolean right, left;

public int x, y;

public int width, height;

public static int pont = 0;

public Player(int x, int y) {

this.x = x;

this.y = y;

this.width = 40;

this.height = 5;


}


public void tick() {

if (right) {

x++;

}

else if (left) {

x--;

}

if(x+width > Game.WIDTH) {

x = Game.WIDTH - width;

}

else if (x < 0) {

x = 0;

}

}

public void render(Graphics g) {

g.setColor(Color.blue);

g.fillRect(x, y, width, height);

if (pont == 0) {

g.setColor(Color.blue);

g.drawString("0", Game.WIDTH/4, Game.HEIGHT/2);

} else if (pont == 1) {

g.setColor(Color.blue);

g.drawString("1", Game.WIDTH/4, Game.HEIGHT/2);

} else if (pont == 2) {

g.setColor(Color.blue);

g.drawString("2", Game.WIDTH/4, Game.HEIGHT/2);

} else if (pont == 3) {

g.setColor(Color.blue);

g.drawString("3", Game.WIDTH/4, Game.HEIGHT/2);

} else if (pont == 4) {

g.setColor(Color.blue);

g.drawString("4", Game.WIDTH/4, Game.HEIGHT/2);

}else if (pont == 5) {

g.setColor(Color.blue);

g.drawString("5", Game.WIDTH/4, Game.HEIGHT/2);

} else if (pont == 6) {

g.setColor(Color.blue);

g.drawString("6", Game.WIDTH/4, Game.HEIGHT/2);

} else if (pont == 7) {

pont = 0;

}

if (Game.enemy.pontEnemy == 7) {

pont = 0;

}

}

}


Classe Enemy

package pong;


import java.awt.Color;

import java.awt.Graphics;


public class Enemy {

public double x, y;

public int  width, height;

public static int pontEnemy = 0;

public Enemy(int x, int y) {

this.x = x;

this.y = y;

this.width = 40;

this.height = 5;

}

public void tick() {

x+= (Game.ball.x - x -6) * 0.07;

}


public void render(Graphics g) {

g.setColor(Color.red);

g.fillRect((int)x,(int)y, width, height);

if (pontEnemy == 0) {

g.setColor(Color.red);

g.drawString("0", 135, Game.HEIGHT/2);

} else if (pontEnemy == 1) {

g.setColor(Color.red);

g.drawString("1", 135, Game.HEIGHT/2);

} else if (pontEnemy == 2) {

g.setColor(Color.red);

g.drawString("2", 135, Game.HEIGHT/2);

} else if (pontEnemy == 3) {

g.setColor(Color.red);

g.drawString("3", 135, Game.HEIGHT/2);

} else if (pontEnemy == 4) {

g.setColor(Color.red);

g.drawString("4", 135, Game.HEIGHT/2);

}else if (pontEnemy == 5) {

g.setColor(Color.red);

g.drawString("5", 135, Game.HEIGHT/2);

} else if (pontEnemy == 6) {

g.setColor(Color.red);

g.drawString("6", 135, Game.HEIGHT/2);

} else if (pontEnemy == 7) {

pontEnemy = 0;

}

if (Game.player.pont == 7) {

pontEnemy = 0;

}

}

}


Classe Ball

package pong;


import java.awt.Color;

import java.awt.Graphics;

import java.awt.Rectangle;

import java.util.Random;


public class Ball {

public double x, y;

public int  width, height;

public double dx, dy;

public double speed = 2;

public int temp = 120;

public Ball(int x, int y) {

this.x = x;

this.y = y;

this.width = 4;

this.height = 4;

int angle = new Random().nextInt(120 - 45) + 45 + 1;

dx = Math.cos(Math.toRadians(angle));

dy = Math.sin(Math.toRadians(angle));

}

public void fim(Graphics g) {

if (y >= Game.HEIGHT) {

g.setColor(Color.black);

g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);

g.setColor(Color.red);

g.drawString("VOCÊ PERDEU", Game.WIDTH/2-45, Game.HEIGHT/2);

while(temp>0) {

temp--;

return;

}

Game.enemy.pontEnemy++;

new Game();

return;

} else if (y < 0) {

g.setColor(Color.black);

g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);

g.setColor(Color.blue);

g.drawString("VOCÊ GANHOU", Game.WIDTH/2-45, Game.HEIGHT/2);

while(temp>0) {

temp--;

return;

}

Game.player.pont++;

new Game();

return;

}

}

public void tick() {

if(x+(dx*speed) + width >= Game.WIDTH) {

dx*=-1;

} else if (x+(dx*speed) < 0) {

dx*=-1;

}

Rectangle bounds = new Rectangle((int)(x+(dx*speed)),(int)(y+(dy*speed)),width, height);

Rectangle boundsPlayer = new Rectangle(Game.player.x,Game.player.y,Game.player.width,Game.player.height);

Rectangle boundsEnemy = new Rectangle((int)Game.enemy.x,(int)Game.enemy.y,Game.enemy.width,Game.enemy.height);

if(bounds.intersects(boundsPlayer)) {

int angle = new Random().nextInt(120 - 45) + 45 + 1;

dx = Math.cos(Math.toRadians(angle));

dy = Math.sin(Math.toRadians(angle));

if (dy > 0)

dy*=-1;

}else if (bounds.intersects(boundsEnemy)) {

int angle = new Random().nextInt(120 - 45) + 45 + 1;

dx = Math.cos(Math.toRadians(angle));

dy = Math.sin(Math.toRadians(angle));

if(dy < 0)

dy*=-1;

}

x+=dx*speed;

y+=dy*speed;

}


public void render(Graphics g) {

g.setColor(Color.green);

g.drawOval((int)x,(int)y, width, height);

}


}