martes, 17 de enero de 2012

Juego: Piedra, Papel o Tijeras

Parte 2: Programando las acciones (Sonidos, Puntuación, JRadioButton, JLabel )


import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

public class Juego extends JFrame{
static URL ruta;
static AudioClip musica;
    
int posx;
Hilo m;
JLabel lblTitulo=new JLabel("Piedra, Papel o Tijeras");
ImageIcon img[]={new ImageIcon("Piedra.jpg"),new ImageIcon("Papel.jpg"),new ImageIcon("Tijeras.jpg")};
ImageIcon im=new ImageIcon("piedra.jpg");
ImageIcon im2=new ImageIcon("papel.jpg");
ImageIcon im3=new ImageIcon("tijeras.jpg");
JLabel lblJugador=new JLabel("Jugador");
JLabel lblMaquina=new JLabel("Maquina");
JLabel lblOpcion1=new JLabel();
JLabel lblOpcion2=new JLabel();
ButtonGroup grupo= new ButtonGroup();
JRadioButton rbPiedra=new JRadioButton("Piedra");
JRadioButton rbPapel=new JRadioButton("Papel");
JRadioButton rbTijeras=new JRadioButton("Tijeras");
JLabel lblGanados=new JLabel("Ganados");
JLabel lblPerdidos=new JLabel("Perdidos");
JLabel lblEmpate=new JLabel("Empate");
JLabel lblGan=new JLabel("0");
JLabel lblPer=new JLabel("0");
JLabel lblEmp=new JLabel("0");
int imagenesSeleccionadas[]=new int[2];
int contador=0,parejas=0;
boolean ban=false;
JButton btnActivado=new JButton();
Juego(){
Panel p=new Panel();
setTitle("Piedra, Papel o Tijeras");
setSize(500,500);
m=new Hilo();
m.start();
p.setLayout(null);
lblTitulo.setFont(new Font("Arial", Font.BOLD, 20));
p.add(lblTitulo);
p.add(lblJugador);
lblJugador.setBounds(70,70,100,30);
p.add(lblMaquina);
lblMaquina.setBounds(230,70,100,30);
p.add(lblOpcion1);
lblOpcion1.setBounds(50,100,100,100);
p.add(lblOpcion2);
lblOpcion2.setBounds(200,100,100,100);
p.add(rbPiedra);
grupo.add(rbPiedra);
rbPiedra.setBounds(50,200,100,30);
p.add(rbPapel);
grupo.add(rbPapel);
rbPapel.setBounds(50,230,100,30);
p.add(rbTijeras);
grupo.add(rbTijeras);
rbTijeras.setBounds(50,260,100,30);
p.add(lblGanados);
lblGanados.setBounds(50,300,100,30);
p.add(lblPerdidos);
lblPerdidos.setBounds(50,340,100,30);
p.add(lblEmpate);
lblEmpate.setBounds(50,380,100,30);
p.add(lblGan);
lblGan.setBounds(130,300,100,30);
p.add(lblPer);
lblPer.setBounds(130,340,100,30);
p.add(lblEmp);
lblEmp.setBounds(130,380,100,30);
Container c=getContentPane();
c.add(p);
rbPiedra.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent t){
int piedra = 0;
if(rbPiedra.isSelected()==true){
lblOpcion1.setIcon(img[0]);
int n2=(int)(Math.random()*3);
switch(piedra){
case 0:
lblOpcion1.setIcon(img[0]);
break;
case 1:
lblOpcion1.setIcon(img[1]);
break;
case 2:
lblOpcion1.setIcon(img[2]);
break;
}
switch(n2){
case 0:
lblOpcion2.setIcon(img[0]);
break;
case 1:
lblOpcion2.setIcon(img[1]);
break;
case 2:
lblOpcion2.setIcon(img[2]);
break;
}
if(piedra==n2){
lblEmp.setText(String.valueOf(Integer.parseInt(lblEmp.getText())+1));
try{
ruta=new URL("file:Empate.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(piedra==0&&n2==1){
lblPer.setText(String.valueOf(Integer.parseInt(lblPer.getText())+1));
try{
ruta=new URL("file:Lengua.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(piedra==0&&n2==2){
lblGan.setText(String.valueOf(Integer.parseInt(lblGan.getText())+1));
                     try{
ruta=new URL("file:Destello.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
}
}
});
rbPapel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent t){
int papel = 1;
if(rbPapel.isSelected()==true){
lblOpcion1.setIcon(img[1]);
int n2=(int)(Math.random()*3);
switch(papel){
case 0:
lblOpcion1.setIcon(img[0]);
break;
case 1:
lblOpcion1.setIcon(img[1]);
break;
case 2:
lblOpcion1.setIcon(img[2]);
break;
}
switch(n2){
case 0:
lblOpcion2.setIcon(img[0]);
break;
case 1:
lblOpcion2.setIcon(img[1]);
break;
case 2:
lblOpcion2.setIcon(img[2]);
break;
}
if(papel==n2){
lblEmp.setText(String.valueOf(Integer.parseInt(lblEmp.getText())+1));
                     try{
ruta=new URL("file:Empate.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(papel==1&&n2==2){
lblPer.setText(String.valueOf(Integer.parseInt(lblPer.getText())+1));
try{
ruta=new URL("file:Lengua.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(papel==1&&n2==0){
lblGan.setText(String.valueOf(Integer.parseInt(lblGan.getText())+1));
                     try{
ruta=new URL("file:Destello.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
}
}
});
rbTijeras.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent t){
int tijeras = 2;
if(rbTijeras.isSelected()==true){
lblOpcion1.setIcon(img[2]);
int n2=(int)(Math.random()*3);
switch(tijeras){
case 0:
lblOpcion1.setIcon(img[0]);
break;
case 1:
lblOpcion1.setIcon(img[1]);
break;
case 2:
lblOpcion1.setIcon(img[2]);
break;
}
switch(n2){
case 0:
lblOpcion2.setIcon(img[0]);
break;
case 1:
lblOpcion2.setIcon(img[1]);
break;
case 2:
lblOpcion2.setIcon(img[2]);
break;
}
if(tijeras==n2){
lblEmp.setText(String.valueOf(Integer.parseInt(lblEmp.getText())+1));
                       try{
ruta=new URL("file:Empate.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(tijeras==2&&n2==0){
lblPer.setText(String.valueOf(Integer.parseInt(lblPer.getText())+1));
try{
ruta=new URL("file:Lengua.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(tijeras==2&&n2==1){
lblGan.setText(String.valueOf(Integer.parseInt(lblGan.getText())+1));
                     try{
ruta=new URL("file:Destello.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
}
}
});
}
class Hilo extends Thread{
public void run(){
try{
while(true){
lblTitulo.setVisible(true);
Thread.sleep(500);
lblTitulo.setVisible(false);
Thread.sleep(500);
posx=posx-20;
lblTitulo.setBounds(posx,10,250,30);
if(posx<0){posx=400;}
}
}catch(Exception er){}
}
}
public int Aleatorio(){
return((int)(Math.random()*11));
}

}



No hay comentarios:

Publicar un comentario