import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class Main
extends JFrame {
// Ancho y alto de la ventana
public static final int ANCHO = 600;
public static final int ALTO = 350;
// Constantes
public final int PLAYER_VS_PLAYER = 0;
public final int PLAYER_VS_COMP = 1;
// Controlador de pantallas
private Pantallas pantallas;
// Las paletas
private Paleta paleta1;
private Paleta paleta2;
// La pelota
private Pelota pelota;
// El marcador
private Marcador marcador;
/***************************************************************************
*************************** INICIALIZAR ***************************
***************************************************************************/
public static void main
(String[] args
) { new Main
(); }
public Main()
{
// Comprobamos que las imágenes estén en su sitio
Archivos.comprobarRutaImagenes();
// Propiedades de la ventana
this.setTitle("Pong!");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(ANCHO, ALTO+20); // +20 por el borde de la ventana
this.setLocation(150,150);
this.setResizable(false);
// Listeners del teclado
formKeyPressed(evt);
}
formKeyReleased(evt);
}
});
// Creamos el controlador de pantallas
pantallas = new Pantallas();
this.setVisible(true);
}
private void iniGame(int tipoJuego)
{
pantallas.setFalse();
pantallas.pantallaJuego = true;
if (tipoJuego == PLAYER_VS_PLAYER)
{
paleta1 = new Paleta(Paleta.MANEJA_PLAYER, Paleta.PLAYER1);
paleta2 = new Paleta(Paleta.MANEJA_PLAYER, Paleta.PLAYER2);
} else {
paleta1 = new Paleta(Paleta.MANEJA_PLAYER, Paleta.PLAYER1);
paleta2 = new Paleta(Paleta.MANEJA_MAQUINA, Paleta.PLAYER2);
}
pelota = new Pelota(ANCHO/2-Pelota.anchoImagen/2, ALTO/2-Pelota.altoImagen/2);
marcador = new Marcador();
}
/***************************************************************************
*************************** LEER TECLADO ***************************
***************************************************************************/
public void formKeyPressed
(KeyEvent evt
) {
// Selección Player vs Player o Player vs Computer
if (evt.
getKeyCode() == KeyEvent.
VK_1 && pantallas.
pantallaPrincipal == true) this.iniGame(PLAYER_VS_PLAYER);
if (evt.
getKeyCode() == KeyEvent.
VK_2 && pantallas.
pantallaPrincipal == true) this.iniGame(PLAYER_VS_COMP);
// Mover paletas
if (pantallas.pantallaJuego == true)
{
paleta1.keyPressed(evt);
paleta2.keyPressed(evt);
}
}
public void formKeyReleased
(KeyEvent evt
) {
// Mover paletas
if (pantallas.pantallaJuego == true)
{
paleta1.keyReleased(evt);
paleta2.keyReleased(evt);
}
}
/***************************************************************************
*************************** DIBUJAR ***************************
***************************************************************************/
{
/***********************
****** CHOCAR ******
***********************/
if (pantallas.pantallaJuego == true)
{
// Pelota <=> Paleta 1
if (paleta1.getRectangulo().intersects(pelota.getRectangulo())==true)
{
if (paleta1.abajo==true)
pelota.setDireccion(Pelota.PALETA1, Pelota.DIR_PALETA_ABAJO);
if (paleta1.arriba==true)
pelota.setDireccion(Pelota.PALETA1, Pelota.DIR_PALETA_ARRIBA);
if (paleta1.abajo==false && paleta1.arriba==false)
pelota.setDireccion();
}
// Pelota <=> Paleta 2
if (paleta2.getRectangulo().intersects(pelota.getRectangulo())==true)
{
if (paleta2.abajo==true)
pelota.setDireccion(Pelota.PALETA2, Pelota.DIR_PALETA_ABAJO);
else if (paleta2.arriba==true)
pelota.setDireccion(Pelota.PALETA2, Pelota.DIR_PALETA_ARRIBA);
else if (paleta2.abajo==false && paleta2.arriba==false)
pelota.setDireccion();
}
}
/***********************
****** MOVER ******
***********************/
if (pantallas.pantallaJuego == true)
{
paleta1.mover(pelota.posY);
paleta2.mover(pelota.posY);
// Comporbamos los goles
int n = pelota.mover();
if (n!=Pelota.NO_GOL)
{
marcador.addPunto(n);
pelota.setCentrada();
}
}
/***********************
****** DIBUJAR ******
***********************/
pantallas.dibujar(g2);
if (pantallas.pantallaJuego == true)
{
paleta1.dibujar(g2);
paleta2.dibujar(g2);
pelota.dibujar(g2);
marcador.dibujar(g2);
}
repaint();
}
{
Image mImage
= createImage
(ANCHO, ALTO
);
g2.drawImage(mImage, 0, 20, this);
}
}