import java.awt.geom.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.util.Random; public class Pong extends JPanel implements KeyEventDispatcher, Runnable { // pour le jeu int gameRate=20; int menLeft=4; // pour la barre int barCoord=100; int barX; int barY; int barDir; int barWidth; int barHeight; Thread animator; boolean keepRunning; //pour la balle Random r; int ballX; int ballY; int ballSize; int gameSpeed; double ballDir=0; Pong () { r = new Random(); //la balle ballX=100; ballY=100; ballDir=2*Math.PI + .1 * r.nextDouble()*Math.PI; gameSpeed=4; //la barre barDir=0; barX=100; KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this); animator = new Thread(this); keepRunning=true; animator.start(); } public boolean dispatchKeyEvent(KeyEvent e) { // Nous traitons seulement VK_A ("a") and VK_D ("d") // Voir documentation pour KeyEventDispatcher et KeyEvent (par exemple "google KeyEventDispatcher") // pour voir tous les "keycodes" (comme KEY_PRESSED et VK_V) if (e.getKeyCode()==KeyEvent.VK_A) { if (e.getID() == KeyEvent.KEY_PRESSED) { barDir = -1; } else { barDir = 0; } } if (e.getKeyCode()==KeyEvent.VK_D) { if (e.getID() == KeyEvent.KEY_PRESSED) { barDir = 1; } else { barDir = 0; } } return false; } public void run() { while (keepRunning) { try { Thread.sleep(gameRate); repaint(); } catch (InterruptedException e) { System.out.println(e); } } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; barWidth=(int)(getWidth()*.2); barHeight=(int)(getHeight()*.05); barY=(int)(getHeight()*.95); int prevBarCoord=barX; barX+=barDir*gameSpeed; if (barX>getWidth()-barWidth || barX<0) { barX=prevBarCoord; } g2.setColor(Color.BLUE); g2.fill(new Rectangle(barX,barY,barWidth,barHeight)); ballSize=(int)(getWidth()*.05); ballX=(int)(ballX+Math.sin(ballDir)*gameSpeed); ballY=(int)(ballY+Math.cos(ballDir)*gameSpeed); if (ballX<0 || ballY<0 || ballX>(getWidth()-ballSize)) { ballDir=ballDir+Math.PI + Math.PI*(r.nextDouble()-.5); ballX=(int)(ballX+Math.sin(ballDir)*gameSpeed); ballY=(int)(ballY+Math.cos(ballDir)*gameSpeed); } g2.setColor(Color.RED); g2.fill(new Rectangle(ballX,ballY,ballSize,ballSize)); //return hit if ((ballY+ballSize)>(getHeight()-barHeight) && (ballX <= (barX+barWidth) && ballX>=barX)) { ballDir=ballDir+Math.PI + .02* Math.PI*(r.nextDouble()-.5); gameSpeed = (int)(gameSpeed*1.5); } //lost a point. Go 1.1 to impose a slight delay if (ballY>(getHeight()*1.1)) { menLeft--; ballY=100; ballDir=2*Math.PI + .1 * r.nextDouble()*Math.PI; gameSpeed=4+(4-menLeft); } //men left g2.setColor(Color.BLACK); for (int i=1;i<=menLeft;i++) { g2.fill(new Rectangle((int)(getWidth()-ballSize*1.1*i),ballSize,ballSize,ballSize)); } if (menLeft==0) { System.out.println("Game Over!"); keepRunning=false; } } public static void main(String[] args) { JFrame frame = new JFrame(); Pong pong = new Pong(); frame.add(pong); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Pong Game"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }