/////////////////////////////////////////////////////////////////////////////////////////////////////
package minitenis;
import Listeners.ListenerBoton7;
import Listeners.ListenerBoton4;
import Listeners.ListenerBoton3;
import Listeners.ListenerBoton5;
import Listeners.ListenerBoton6;
import Listeners.ListenerBoton2;
import Listeners.ListenerBoton1;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
@SuppressWarnings("serial")
public class Game2 extends JPanel {
//TRAE EL LIENZO
////ATRIBUTOS GAME2////
String sonido1 ="C:/Users/Laurencio/Documents/NetBeansProjects/JUEGO/src/sonidos2/sonido1.mp3";
String sonido2 = "C:/Users/Laurencio/Documents/NetBeansProjects/JUEGO/src/sonidos2/gameover.wav";
static int DifiConf;
boolean GameOver;
int i=0;
static int AlturaP=600;
static int AnchoP=600;
Ball ball = new Ball(this, (float) (AnchoP/(Math.random()*100)),0,0,0);
Raqueta raquet = new Raqueta(this,0,0,0,0,DifiConf);
public static void main(String[] args) throws InterruptedException {
////////////////////////////////// FRAME MENU ////////////////////////
JFrame frame2 = new JFrame("Menu");
JButton boton1 = new JButton();
ListenerBoton1 LB1 = new ListenerBoton1(false);
JButton boton2 = new JButton();
ListenerBoton2 LB2 = new ListenerBoton2(false);
JButton boton3 = new JButton();
ListenerBoton3 LB3 = new ListenerBoton3(false);
frame2.setSize(Game2.AlturaP,Game2.AnchoP);
frame2.setVisible(true);
frame2.setLocationRelativeTo(null);
frame2.setResizable(false);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setLayout(null);
////////////////////////////////// MENU ///////////////////////////////
Panel p = new Panel();
frame2.add(p);
boton1.addActionListener(LB1);
boton1.setText("Play");
boton1.setBounds(240, 150, 130, 50);
frame2.add(boton1);
boton2.addActionListener(LB2);
boton2.setText("Settings");
boton2.setBounds(240,250,130,50);
frame2.add(boton2);
boton3.addActionListener(LB3);
boton3.setText("Exit");
boton3.setBounds(240,350,130,50);
frame2.add(boton3);
//////////////////////////////////// ELECCION DE FRAMES ////////////////////////////////////
int opc = 0;
boolean CancelDecision = true;
while (true){
while (CancelDecision){
System.out.println("loading");
if (LB1.PLAYboolean) {
opc = 1;
System.out.println("OPC 1");
CancelDecision = false;
}
if (LB2.CONFboolean) {
opc = 2;
System.out.println("OPC 2");
CancelDecision = false;
}
if (LB3.exit){
System.out.println("OPC 3");
System.exit(ABORT);
}
Thread.sleep(300);
}
LB1.PLAYboolean = false;
LB2.CONFboolean = false;
switch (opc){
case 1:
////////////////////////////////// FRAME JUEGO ///////////////////////
Game2 game = new Game2();
game.RandomP();
JFrame frame = new JFrame("Pelota Rebotin v. 2.0"); //TRAE EL MARCO
////////////////////////////////// CARGA DE JUEGO ///////////////////
frame.add(game);
frame.setSize(AlturaP, AnchoP);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
////////////////////////////////// INICIO DE JUEGO /////////////////
boolean SetJuego = true;
while (SetJuego){
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
game.move();
game.repaint();
game.Dificultad();
});
ExecutorService executor2 = Executors.newSingleThreadExecutor();
executor.submit(() -> {
try {
game.sonido();
} catch (FileNotFoundException ex) {
Logger.getLogger(Game2.class.getName()).log(Level.SEVERE, null, ex);
} catch (JavaLayerException ex) {
Logger.getLogger(Game2.class.getName()).log(Level.SEVERE, null, ex);
}});
ExecutorService executor3 = Executors.newSingleThreadExecutor();
executor.submit(() -> {
try {
game.sonidoEndGame();
} catch (UnsupportedAudioFileException ex) {
Logger.getLogger(Game2.class.getName()).log(Level.SEVERE, null, ex);
} catch (LineUnavailableException ex) {
Logger.getLogger(Game2.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Game2.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(Game2.class.getName()).log(Level.SEVERE, null, ex);
}
});
Thread.sleep(8);
if (game.GameOver){
opc=0;
SetJuego = false;
CancelDecision = true;
frame.setVisible(false);
}
}
break;
case 2:
////////////////////////////////////FRAME CONFIGURACION/////////////////////////////
JFrame frame3 = new JFrame("Settings");
ListenerBoton4 LB4 = new ListenerBoton4(true);
ListenerBoton5 LB5 = new ListenerBoton5(false);
ListenerBoton6 LB6 = new ListenerBoton6(false);
ListenerBoton7 LB7 = new ListenerBoton7(false);
frame3.setSize(Game2.AlturaP,Game2.AnchoP);
frame3.setVisible(true);
frame3.setLocationRelativeTo(null);
frame3.setResizable(false);
frame3.setLayout(null);
////////////////BOTONERA //////////////
JButton boton4 = new JButton();
JButton boton5 = new JButton();
JButton boton6 = new JButton();
JButton botonATRAS = new JButton();
boton4.setText("Facil");
boton4.setBounds(50,50,90,50);
frame3.add(boton4);
boton5.setText("Intermedio");
boton5.setBounds(150, 50, 130, 50);
frame3.add(boton5);
boton6.setText("Dificil");
boton6.setBounds(290,50, 90, 50);
frame3.add(boton6);
botonATRAS.setText("ATRAS");
botonATRAS.setBounds(10 ,500 , 90, 50);
frame3.add(botonATRAS);
boton4.addActionListener(LB4);
boton5.addActionListener(LB5);
boton6.addActionListener(LB6);
botonATRAS.addActionListener(LB7);
/////////////////TEXTO A MOSTRAR ////////////////
JLabel texto = new JLabel();
texto.setText("Dificultad");
texto.setBounds(190, 10, 90, 50);
frame3.add(texto);
JLabel texto2 = new JLabel();
frame3.add(texto2);
texto2.setBounds(440,50,200,50);
while (LB7.BotAtras != true){
if (LB4.Facil){
LB5.intermedio = false;
LB6.dificil = false;
DifiConf = 120;
texto2.setText("DIFICULTAD EN FACIL");
}
if (LB5.intermedio){
LB4.Facil = false;
LB6.dificil = false;
DifiConf = 90;
texto2.setText("DIFICULTAD EN INTERMEDIO");
}
if (LB6.dificil){
LB4.Facil = false;
LB5.intermedio = false;
DifiConf = 75;
texto2.setText("DIFICULTAD EN DIFICIL");
}
}
opc=0;
CancelDecision = true;
frame3.setVisible(false);
break;
}
}}
////////////////////////////////// SENSOR DE TECLAS //////////////////////////////////
public Game2() {
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed="+KeyEvent.getKeyText(e.getKeyCode()));
raquet.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased="+KeyEvent.getKeyText(e.getKeyCode()));
raquet.keyReleased(e);
}
});
setFocusable(true);
}
////////////////////////////////// OBJETOS CARGADOS EN PANTALLA FRAME1 //////////////////////////////////
@Override //PINTADO DE PANTALLA
public void paint(Graphics g) {
super.paint(g); //BORRA LA PANTALLA
Graphics2D g2d = (Graphics2D) g;
Graphics2D g3d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d); // OBJETO A PINTAR
raquet.paint(g2d);
g2d.setColor(Color.GRAY);
g2d.setFont(new Font("Verdana", Font.BOLD, 30));
g2d.drawString(String.valueOf(Contador()), 10, 30);
g3d.setFont(new Font("Verdana", Font.BOLD, 10));
g3d.drawString("Usar las flechas para moverse. ", 400, 30);
g3d.drawString("Mantener apretado para ganar mas velocidad.", 312, 42);
g3d.drawString("Made by: Laurencio P. ", 450, 550);
}
////////////////////////////////// SONIDO DEL JUEGO FRAME 1 //////////////////////////////////
public void sonido() throws FileNotFoundException,JavaLayerException {
if (ball.collision()){
Player apl = new Player(new FileInputStream(sonido1));
apl.play();
}
}
public void sonidoEndGame() throws UnsupportedAudioFileException, LineUnavailableException, IOException, InterruptedException {
if (ball.GameOverB){
try (Clip sonido3 = AudioSystem.getClip()) {
sonido3.open(AudioSystem.getAudioInputStream(new File(sonido2)));
sonido3.start();
Thread.sleep(1000);
}
}}
////////////////////////////////////METODOS /////////////////////////////
private void Dificultad(){
if (ball.collision()){
ball.xa= (float) (0.5+ball.xa);
ball.ya=(float) (ball.ya+0.5);
}
}
private void move() {
ball.move();
raquet.move();
}
private int Contador(){
if (ball.collision()){
i=i+1;
}
return i;
}
public void GameOverMessage(){
JOptionPane.showMessageDialog(this, "your score is: " + Contador(),
"Game Over", JOptionPane.YES_NO_OPTION);
GameOver = true;
// System.exit(ABORT);
}
public void RandomP(){
if (Math.random()*100 < 50){
ball.xa=2;
System.out.println("VelX Randomize Act");
if (Math.random()*100 < 50){
ball.ya = -1;
System.out.println("VelY Randomize Neg");
} else {
ball.ya = 1;
System.out.println("VelY Randomize Pos");
}
} else {
ball.ya=2;
System.out.println("VelY Randomize Act");
if (Math.random()*100 <50 p=""> ball.xa = -1;
System.out.println("VelX Randomize Neg");
} else {
ball.xa = 1;
System.out.println("VelX Randomize Pos");
}
}
}
}
//////////////////////////////////////////////////////objetos de botones/////////////////////////////////////////////////
package Listeners;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ListenerBoton1 implements ActionListener {
public boolean PLAYboolean;
//CONSTRUCTOR ///
public ListenerBoton1(boolean PLAYboolean) {
this.PLAYboolean = PLAYboolean;
}
public void actionPerformed (ActionEvent e){
System.out.println("BOTON 1 FUE PULSADO");
PLAYboolean = true;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
package Listeners;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ListenerBoton2 implements ActionListener {
public boolean CONFboolean;
public ListenerBoton2(boolean CONFboolean) {
this.CONFboolean = CONFboolean;
}
public void actionPerformed (ActionEvent e){
System.out.println("BOTON 2 FUE PULSADO");
CONFboolean = true;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
package Listeners;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ListenerBoton7 implements ActionListener{
public boolean BotAtras;
public ListenerBoton7(boolean BotAtras) {
this.BotAtras = BotAtras;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(" BOTON ATRAS ");
BotAtras = true;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
package Listeners;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ListenerBoton6 implements ActionListener{
public boolean dificil;
public ListenerBoton6(boolean dificil) {
this.dificil = dificil;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("BOTON 6");
dificil = true;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
package Listeners;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ListenerBoton5 implements ActionListener {
public boolean intermedio;
public ListenerBoton5(boolean intermedio) {
this.intermedio = intermedio;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("BOTON 5 ");
intermedio = true;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
package Listeners;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ListenerBoton4 implements ActionListener{
public boolean Facil;
public ListenerBoton4(boolean Facil) {
this.Facil = Facil;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("BOTON 4");
Facil = true;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
package Listeners;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ListenerBoton3 implements ActionListener {
public boolean exit;
public ListenerBoton3(boolean exit) {
this.exit = exit;
}
public void actionPerformed (ActionEvent e){
System.out.println("BOTON 3 FUE PULSADO");
exit=true;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////// OBJETO BALL
package minitenis;
import minitenis.Game2;
import java.awt.Graphics2D;
import java.awt.Rectangle;
public class Ball {
boolean GameOverB= false;
private static final int DIAMETER = 30;
float x ;
float y;
float xa;
float ya;
private Game2 game;
Ball(Game2 game,float x, float y, float xa, float ya) {
this.game= game;
this.x= x;
this.xa=xa;
this.y=y;
this.ya=ya;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public float getXa() {
return xa;
}
public float getYa() {
return ya;
}
public void move() { //METODO DE MOVIMIENTO
int c=1,d=1;
if (Ball.this.getXa() < 0) {
c=-1;
}
if (Ball.this.getYa()<0 div="">
d=-1;
}
if (Ball.this.getX() + Ball.this.getXa() < 0){
Ball.this.xa = c*Ball.this.getXa();
}
if (Ball.this.getX() + Ball.this.xa > game.getWidth() - 30) {
Ball.this.xa = -1*Ball.this.getXa();
}
if (Ball.this.getY() + Ball.this.getYa() < 0) {
Ball.this.ya= d*Ball.this.getYa();
}
if (Ball.this.getY() + Ball.this.getYa() > game.getHeight() - 30) {
Ball.this.x=0;
Ball.this.xa=0;
Ball.this.y=0;
Ball.this.ya=0;
GameOverB = true;
game.GameOverMessage();
}
if (collision()){
Ball.this.ya = -1*Ball.this.getYa();
y = game.raquet.getTopY() - DIAMETER;
}
Ball.this.x = Ball.this.getX() + Ball.this.xa;
Ball.this.y = Ball.this.getY() + Ball.this.ya;
}
public void paint(Graphics2D g) {
g.fillOval(Math.round(Ball.this.x),Math.round(Ball.this.y), DIAMETER, DIAMETER);
}
public boolean collision() {
return game.raquet.getBounds().intersects(getBounds());
}
public Rectangle getBounds() {
return new Rectangle(Math.round(x), Math.round(y), DIAMETER, DIAMETER);
}
}
0>
////////////////////////////////////////////////////////////////////////////////////////////////////// PANEL
package minitenis;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
public class Panel extends javax.swing.JPanel {
ImageIcon imagen;
String nombre="/imagenes2/FONDO.jpg";
public Panel(){
this.setSize(600,600);
}
@Override
public void paint(Graphics g){
Dimension tamanio = getSize();
imagen = new ImageIcon(getClass().getResource(nombre));
g.drawImage(imagen.getImage(),0,0,tamanio.width, tamanio.height, null);
setOpaque(false);
super.paint(g);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////// OBJETO RAQUETA
package minitenis;
import minitenis.Game2;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
public class Raqueta {
private static final int Y = 520;
public int WITH ;
private static final int HEIGHT = 10;
int x;
int xa;
int ContadorLeft=0, ContadorRight=0;
private Game2 game;
public Raqueta(Game2 game,int x, int xa,int ContadorLeft,int ContadorRight,int WITH) {
this.x = x;
this.xa = xa;
this.game = game;
this.ContadorLeft = ContadorLeft;
this.ContadorRight = ContadorRight;
this.WITH = WITH;
}
public int getContadorLeft() {
return ContadorLeft;
}
public int getContadorRight() {
return ContadorRight;
}
public void move() {
if (x + xa > 0 && x + xa < game.getWidth()-60)
x = x + xa;
}
public void paint(Graphics2D g) {
g.fillRect(x, Y, WITH, HEIGHT);
}
public void keyReleased(KeyEvent e) {
xa = 0;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
Raqueta.this.ContadorRight = 0;
Raqueta.this.ContadorLeft = Raqueta.this.ContadorLeft+1;
if (Raqueta.this.ContadorLeft >= 2){
xa = -6;
System.out.println("Velocidad Aumentada");
} else {
xa = -4;
}
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
Raqueta.this.ContadorLeft = 0;
Raqueta.this.ContadorRight = Raqueta.this.ContadorRight+1;
Raqueta.this.ContadorLeft = 0;
if (Raqueta.this.ContadorRight>=2){
xa = 6;
System.out.println("Velocidad Aumentada");
} else {
xa = 4;
}
}
}
public Rectangle getBounds() {
return new Rectangle(x, Y, WITH, HEIGHT);
}
public int getTopY() {
return Y - HEIGHT;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////