Vous êtes sur la page 1sur 21

Package PongV2;

Import java.applet.Applet;
Import java.awt.Graphics;

Public class Tennis extends Applet implements Runnable, KeyListener {


final int WIDTH = 700, HEIGHT = 500;
Thread thread;
HumanPaddle p1;
AIPaddle p2;
Ball b1;
boolean gameStarted;
Graphics gfx;
Image img;

public void init(){


this.resize(WIDTH, HEIGHT);
gameStarted = false;
this.addKeyListener(this);
p1 = new HumanPaddle (1);
b1 = new Ball();
p2 = new AIPaddle(2, b1);
img = createImage (WIDTH, HEIGHT);
gfx = img.getGraphics();
thread = new Thread(this);
thread.start();
}

public void paint(Graphics g){


gfx.setColor(Color.black);
gfx.fillRect (0, 0, WIDTH, HEIGHT);
if (b1.getX() < -10 || b1.getX() > 710){
gfx.setColor(Color.red);
gfx.drawString(“Game Over”, 350, 250);
}
else {
p1.draw (gfx);
b1.draw(gfx);
p2.draw(gfx);
}

if(!gameStarted) {
gfx.setColor(Color.white);
gfx.drawString(“Tennis”, 340, 100);
gfx.drawString(“Press Enter to Begin..”, 310, 130);
}
g.drawImage (img, 0, 0, this);
}

public void update(Graphics g){


paint(g);
}

public void run() {


for(;;) {
if(gameStarted) {
p1.move();
p2.move();
b1.move();
b1.checkPaddleCollision (p1, p2);
repaint ();
}

try {
Thread.sleep(10);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void keyPressed(KeyEvent e) {


if(e.getKeycode() == KeyEvent.VK_W) {
p1.setUpAccel(true);
}
else if(e.getKeyCode() == KeyEvent.VK_S) {
p1.setDownAccel(true);
}
else if(e.getKeyCode() == KeyEvent.VK_ENTER){
gameStarted = true;
}
}

public void keyReleased(KeyEvent e) {


if(e.getKeycode() == KeyEvent.VK_W) {
p1.setUpAccel(false);

}
else if(e.getKeyCode() == KeyEven.VK_S) {
p1.setDownAccel(false);
}

}
public void keyTyped(KeyEvent arg0) {

}
*Create an Interface Paddle

package PongV2;

import java.awt.Graphics;

public interface Paddle {


public void draw(Graphics g);
public void move();
public int getY();

}
*Create New Class name HumanPaddle

package PongV2;

import java.awt.Color;
import java.awt.Graphics;

public class HumanPaddle implements Paddle {


double y, yVel;
boolean upAccel, downAccel;
final double GRAVITY = 0.94;
int player, x;

public HumanPaddle (int player) {


upAccel = false; downAccel = false;
y = 210; yVel = 0;

if(player ==1)
x = 20;
else
x = 660;
}
public void draw(Graphics g) {
g.setColor(Color.white);
g.fillRect (x, (int)y, 20, 80);
}

public void move() {


if(upAccel){
yVel -= 2;
}
else if(downAccel) {
yVel += 2;
}
else if (!upAccel && !downAccel) {
yVel *= GRAVITY;
}
if (yVel >= 5)
yVel = 5;
else if(yVel <= -5)
yVel = -5;

y += yVel;

if (y < 0)
y = 0;
if (y > 420)
y = 420;
}
public void setUpAccel (boolean input) {
upAccel = input;
}

public void setDownAccel (boolean input) {


downAccel = input;
}

public void getY() {


return (int)y;

}
*Create a new Class name Ball

package PongV2;

import java.awt.Color;
import java.awt.Graphics;

public class Ball {


double xVel, yVel, x, y;

public Ball() {
x = 350;
y = 250;
xVel = getRandomSpeed() * getRandomDirection();
yVel = getRandomSpeed() * getRandomDirection();
}

public double getRandomSpeed() {


return (Math.random() *3 + 2);
}

public int getRandomDirection() {


int rand = (int) (Math.random() * 2);
if(rand == 1)
return 1;
else
return -1;
}

public void draw (Graphics g) {


g.setColor(Color.white);
g.fillOval ((int)x-10, (int)y-10, 20, 20);
}

public void checkPaddleCollision(Paddle p1, Paddle p2){


if(x <= 50) {
if(y >= p1.getY() && y <= p1.getY() + 80)
xVel = -xVel;
}
else if (x >= 650){
if(y >= p2.gety() && y <= p2.getY() + 80)
xVel = -xVel;

}
}

public void move() {


x += xVel;
y += yVel;
if (y < 10)
yVel = -yVel;
if (y > 490)
yVel = -yVel;
}

public int getX() {


return (int)x;
}

public int getY() {


return (int)y;
}
}
*Create a new Class name AIPaddle and copy HumanPaddle

package PongV2;

import java.awt.Color;
import java.awt.Graphics;

public class AIPaddle implements Paddle {


double y, yVel;
boolean upAccel, downAccel;
final double GRAVITY = 0.94;
int player, x;
Ball b1;

public AIPaddle (int player, Ball b) {


upAccel = false; downAccel = false;
b1 = b;
y = 210; yVel = 0;

if(player ==1)
x = 20;
else
x = 660;
}
public void draw(Graphics g) {
g.setColor(Color.white);
g.fillRect (x, (int)y, 20, 80);
}

public void move(Graphics g) {


y = b1.getY() – 40;

if (y < 0)
y = 0;
if (y > 420)
y = 420;
}

public void getYGraphics g) {


return (int)y;

}
NEW FORMAT!!!
New class “Game”

Import java.awt.Canvas;
Import java.awt.JFrame;
Import java.awt.Dimension;

public class Game extends Canvas implements Runnable {


private static final long serialVersionUID = 1L

public static PlayerPaddle player;


public static AIPaddle ai;
public static Ball ball;
InputHandler IH;

JFrame frame;
public final int WIDTH = 400;
public final int HEIGHT = WIDTH / 16 * 9;
public final Dimension gameSize = new Dimension(WIDTH, HEIGHT);
public final String TITLE = “Pong InDev”;

BufferedImage image = new BufferedImage (WIDTH, HEIGHT,


BufferedImage.TYPE_INT_RGB);

static boolean gameRunning = false;

int p1Score, p2Score;

boolean gameStarted;

public void run() {

while (gameRunning) {
tick();
render();

try{
Thread.sleep(7);
}catch(Exception e) {
e.printStackTrace();
}

public synchronized void start() {


gameRunning = true;
thread = new Thread(this).start();

public static synchronized void stop() {


gameRunning = false;
System.exit(0);
}

public Game() {
frame = new JFrame();

setMinimumSize(gameSize);
setPreferredSize(gameSize);
setMaximum(gameSize);

frame.add(this, BorderLayout.CENTER);
frame.pack();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setTitle(TITLE);
frame.setLocationRelativeTo(null);

IH = new InputHandler();

player = new PlayerPaddle(10, 60);


ai = new AIPaddle(getWidth() – 20, 60);
ball = new Ball(getWidth() / 2, getHeight() / 2);

gameRunning = true; //asdasdasd


gameStarted = false;
}

public void tick() {


player.tick(this);
ai.tick(this);
ball.tick(this);
}

public void render() {


BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();

g.drawImage (image, 0, 0, getWidth(), getHeight(),


null);
g.setColor(Color.RED);
g.fillRect(0, 0, getWidth(), getHeight());

g.setColor(Color.WHITE);

g.drawString(“Player 1: “ + p1Score, 5, 10);


g.drawString(“Player 2: “ + p2Score, getWidth() -60, 10);

if(p1Score >= 10 || p2score >= 10){


g.setColor(Color.RED);
g.drawString(“Game Over”, 200, 56.25);
}

player.render(g);
ai.render(g);
ball.render(g);

g.dispose();
bs.show();
}

}
CREATE NEW CLASS “PlayerPaddle”

Import java.awt.Color;
Import java.awt.Graphics;

public class PlayerPaddle {

int x;
int y;
int width = 15;
int height = 40;
int speed = 1;

Rectangle boundingBox;

boolean goingUp = false;


boolean goindDown = false;

public PlayerPaddle(int x, int y) {


this.x = x;
this.y = y;

boundingBox = new Rectangle(x, y, width, height);


boundingBox.setBounds(x, y, width, height);
}

public void tick(Game game) {


boundingBox.setBounds(x, y, width, height);

if (goingUp && y > 0 ) {


y -= speed;

}
if (goingDown && y < game.getHeight() - height) {
y += speed;

System.out.println

public void render(Graphics g) {


g.setColor(Color.BLUE);
g.fillRect(x, y, width, height);

}
CREATE NEW CLASS “InputHandler”

public class InputHandler implements KeyListener {

public InputHandler (Game game) {


game.addKeyListener(this);
}

public void keyPressed(KeyEvent e) {


int keyCode = e.getKeyCode();

if(keyCode == KeyEvent.VK_W){
Game.player.goingUp = true;
}

if(keyCode == KeyEvent.VK_S){
Game.player.goingDown = true;
}

if(keyCode == KeyEvent.VK_UP) {
Game.ai.goingUp = true;

if(keyCode == KeyEvent.VK_DOWN) {
Game.ai.goingDown = true;

if(keyCode == KeyEvent.VK_ESCAPE) {
System.exit(0);
}

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


gameStarted = true;
}

public void keyReleased(KeyEvent e) {


int keyCode = e.getKeyCode();

if(keyCode == KeyEvent.VK_W){
ß Game.player.goingUp = false;
}

if(keyCode == KeyEvent.VK_S){
Game.player.goingDown = false;
}

if(keyCode == KeyEvent.VK_UP) {
Game.ai.goingUp = false;

if(keyCode == KeyEvent.VK_DOWN) {
Game.ai.goingDown = false;

public void keyTyped(KeyEvent e) {

}
}
CREATE A NEW CLASS “AIPaddle”

public class AIPaddle {

int x;
int y;
int width = 15;
int height = 40;
int speed = 1;

Boolean isTwoPlayer = false;

Rectangle boundingBox;

boolean goingUp = false;


boolean goindDown = false;

public AIPaddle(int x, int y) {


this.x = x;
this.y = y;

boundingBox = new Rectangle(x, y, width, height);


boundingBox.setBounds(x, y, width, height);

public void tick(Game game) {


boundingBox.setBounds(x, y, width, height);

if(!istwoPlayer){
if(game.ball.y < y + height && y >= 0){
y -= speed;
}else if(game.ball.y > y && y + height <=
game.getHeight(0)){
y += speed;
}

}else{
if(goingUp){
y -= speed;
}else if(goingDown) {
y += speed;
}

public void render(Graphics g) {


g.setColor(Color.BLUE);
g.fillRect(x, y, width, height);

}
CREATE A NEW CLASS “Ball”

Import java.awt.Graphics;

public class Ball {

int x, y;
int size = 16;
int speed = 2;

Rectangle boundingBox;

int vx, vy;

public Ball(int x, int y) {


this.x = x;
this.y = y;

vx = speed;
vy = speed;

boundingBox = new Rectangle(x, y, size, size);


boundingBox.setBounds(this.x, this.y, this.size, this.size);

public void tick(Game game) {

boundingBox.setBounds(x, y, size, size);

if(x <= 0) {
game.p2Score++;
vx = speed;
}else of (x + size >= game.getWidth()) {
game.p1Score++;
vx = -speed;
}
if(y <= 0) {
vy = speed;
} else of (y + size >= game.getHeight()) {
vy = -speed;
}

x += vx;
y += vy;

paddleCollide(game);
}
private void paddleCollide(Game game) {
if(boundingBox.intersects(game.player.boundingBox)) {
vx = speed;
}else if (boundingBox.intersects(game.ai.boundBox)) {
vx = -speed;
}
}

public void render (Graphics g) {


g.setColor(Color.RED);
g.fillOval (x, y, size, size);
}

}
CREATE NEW CLASS “Launcher”

public class Launcher {

public static void main(String[] args){


new MainMenu();
}
}
Create new class “MainMenu”

Import javax.swing.JButton;
Import javax.swing.JCheckBox;
Import javax.swing.JFrame;

public class Mainmenu extends JFrame {


private static final long serialVersionUID = 1L;

int screenWidth = 250;


int sreenHeight = 150;

int buttonWidth = 100;


int buttonHeight = 40;

JButton Play, Quit;


JCheckBox twoPlayer;

public MainMenu() {
addButtons();
addActions();

getContentPane().setLayout(null);

Play.setBounds((screenWidth – buttonWidth) / 2, 5, buttonWidth,


buttonHeight;
Quit.setBounds((screenWidth – buttonWidth) / 2, 50, buttonWidth,
buttonHeight);
twoPlayer.setBounds((screenWidth - buttonWidth) / 2, 95, buttonWidth
* 2, buttonHeight);

getContentPane().add(Play);
getContentPane().add(Quit);
getContentPane().add(twoPlayer);

pack();
setVisible(true);
setLocationRelativeTo(null);
setSize(screenWidth, screenHeight);
setTitle(“Pong Menu”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);

private void addButtons() {

Play = new JButton(“Play”);


Quit = new JButton(“Quit”)
twoPlayer = new JCheckBox (“Two Players?”);

private void addActions() {

Play.addActionListener (new ActionListener () {


public void actionPerfomed(ActionEvent e) {
dispose();

Game game = new Game();

if(twoPlayer.isSelected()) {
game.ai.isTwoPlayer = true;
}else {
game.ai.isTwoPlayer = false;
}
game start();
}

});

Quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

}
}

Vous aimerez peut-être aussi