Bueno amigos de T! hoy les comparto en este post un trabajo pequeño que he hecho en compañía de unos amigos, no es la gran cosa pero tal vez a alguien de la comunidad le sirva, el juego consiste en adivinar el operador que satistface el resultado de la operación entre ambos números, es muy sencillo y tiene diferentes niveles, entre mas difícil el nivel, menos tiempo te da para dar una respuesta, el juego termina cuando el jugador pierde sus tres vidas.....
----------------------------------------------------- Archivo 1 -----------------------------------------------------------
package Juego.prueba1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game extends JFrame implements ActionListener{
JTextField n1, n2, res;
JButton ba, bs, br, bp, sig;
JLabel l1, l2, score, lifes, timer;
Timer tm;
int op1, op2, sg, sl, sc, lf;
char[] operando = {'+' , '-' , '*' , '/'};
public Game(){
setLayout(null);
//Inicializar score y lifes
sc = 0; lf=3;
//Operandos, operador y solucion
op1=random();op2=random();sg=random(4);sl=operacion();
//Campos de texto
n1 = new JTextField();n1.setBounds(35, 30, 50, 40);n1.setText(String.valueOf(op1));n1.disable();add(n1);n1.setHorizontalAlignment(n1.CENTER);
n1.setFont(new Font("Arial",Font.BOLD,16));
//op = new JTextField();op.setBounds(92, 30, 40, 40);op.setText(String.valueOf(operando[sg]));op.disable();add(op);op.setHorizontalAlignment(op.CENTER);
//op.setFont(new Font("Arial",Font.BOLD,18));
n2 = new JTextField();n2.setBounds(125, 30, 50, 40);n2.setText(String.valueOf(op2));n2.disable();add(n2);n2.setHorizontalAlignment(n2.CENTER);
n2.setFont(new Font("Arial",Font.BOLD,16));
res = new JTextField();res.setBounds(217, 30, 50, 40);res.setText(String.valueOf(sl));res.disable();add(res);res.setHorizontalAlignment(res.CENTER);
res.setFont(new Font("Arial",Font.BOLD,16));
//Botones
ba = new JButton(new ImageIcon("src/ad.png","Suma" ));ba.setBounds(30, 90, 50, 40);add(ba);ba.addActionListener(this);
bs = new JButton(new ImageIcon("src/sust.png","Resta" ));bs.setBounds(95, 90, 50, 40);add(bs);bs.addActionListener(this);
bp = new JButton(new ImageIcon("src/prod.png","Multiplicación" ));bp.setBounds(160, 90, 50, 40);add(bp);bp.addActionListener(this);
br = new JButton(new ImageIcon("src/raz.png","División" ));br.setBounds(225, 90, 50, 40);add(br);br.addActionListener(this);
//Etiquetas
l1 = new JLabel(new ImageIcon("src/int.png","¿?" ));l1.setBounds(85, 30, 40, 40);add(l1);l1.setHorizontalTextPosition(l1.CENTER);
l2 = new JLabel(new ImageIcon("src/eq.png","=" ));l2.setBounds(175, 30, 40, 40);add(l2);l2.setHorizontalTextPosition(l2.CENTER);
lifes = new JLabel(new ImageIcon("src/ht3.png","Vidas restantes" ));lifes.setBounds(200,150,90,30);add(lifes);lifes.setHorizontalAlignment(lifes.RIGHT);
score = new JLabel("Puntuación: " );score.setBounds(10, 150, 150, 30);score.setFont(new Font("Arial",Font.BOLD,15));add(score);
//timer = new JLabel();timer.setBounds(200, 150, 150, 30);timer.setFont(new Font("Arial",Font.BOLD,14));add(timer);
//Inicializar timer
tm = new Timer(10000,new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"Tiempo agotado!","Siga intentando",JOptionPane.INFORMATION_MESSAGE);
tm.stop();
restLife();
init();
}
});
tm.start();
}
int random(){
return (int)(Math.random()*100);
}
int random(int a){
int aux = (int)(Math.random()*5);
if (aux < a)
return aux;
else
return random(a);
}
int operacion(){
if(sg == 0)
return op1 + op2;
else if(sg == 1)
return op1 - op2;
else if(sg == 2)
return op1 * op2;
else
return op1 / op2;
}
void restLife(){
lf--;
if(lf == 2)
lifes.setIcon(new ImageIcon("src/ht2.png","Vidas restantes" ));
else if(lf == 1)
lifes.setIcon(new ImageIcon("src/ht.png","Vidas restantes" ));
else if(lf == 0){
JOptionPane.showMessageDialog(null,"¡Game Over!nnPuntaje final: "+sc,"Game over!",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
void init(){
op1 = random();op2 = random();sg = random(4); sl = operacion();
n1.setText(String.valueOf(op1));/*op.setText(String.valueOf(operando[sg]));*/n2.setText(String.valueOf(op2));res.setText(String.valueOf(sl));
tm.restart();
}
public void actionPerformed(ActionEvent e){
tm.stop();
//Botón Adición
if(e.getSource() == ba){
if(sg == 0){
JOptionPane.showMessageDialog(null,"Correcto!","+10 pts",JOptionPane.INFORMATION_MESSAGE);
sc+=10;
}else{
JOptionPane.showMessageDialog(null,"Incorrecto!","0 pts",JOptionPane.ERROR_MESSAGE);
restLife();
}
}
//Botón Sustracción
if(e.getSource() == bs){
if(sg == 1){
JOptionPane.showMessageDialog(null,"Correcto!","+10 pts",JOptionPane.INFORMATION_MESSAGE);
sc+=10;
}else{
JOptionPane.showMessageDialog(null,"Incorrecto!","0 pts",JOptionPane.ERROR_MESSAGE);
restLife();
}
}
//Botón Producto
if(e.getSource() == bp){
if(sg == 2){
JOptionPane.showMessageDialog(null,"Correcto!","+10 pts",JOptionPane.INFORMATION_MESSAGE);
sc+=10;
}else{
JOptionPane.showMessageDialog(null,"Incorrecto!","0 pts",JOptionPane.ERROR_MESSAGE);
restLife();
}
}
//Botón Razón
if(e.getSource() == br){
if(sg == 3){
JOptionPane.showMessageDialog(null,"Correcto!","+10 pts",JOptionPane.INFORMATION_MESSAGE);
sc+=10;
}else{
JOptionPane.showMessageDialog(null,"Incorrecto!","0 pts",JOptionPane.ERROR_MESSAGE);
restLife();
}
}
score.setText("Puntuación: "+sc);
init();
}
}
----------------------------------------------------- Archivo 2 -----------------------------------------------------------
package Juego.prueba1;
public class Main{
public static void main(String args[]){
Game a=new Game();
a.setVisible(true);
a.setTitle("Adivina el numero!" );
a.setBounds(200,100,320,220);
a.setResizable(false);
a.setDefaultCloseOperation(a.EXIT_ON_CLOSE);
}
}
El juego ejecutado, es igual a este, las imagenes y los audios se las pueden cambiar a su estilo
Se aceptan puntos :3
----------------------------------------------------- Archivo 1 -----------------------------------------------------------
package Juego.prueba1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game extends JFrame implements ActionListener{
JTextField n1, n2, res;
JButton ba, bs, br, bp, sig;
JLabel l1, l2, score, lifes, timer;
Timer tm;
int op1, op2, sg, sl, sc, lf;
char[] operando = {'+' , '-' , '*' , '/'};
public Game(){
setLayout(null);
//Inicializar score y lifes
sc = 0; lf=3;
//Operandos, operador y solucion
op1=random();op2=random();sg=random(4);sl=operacion();
//Campos de texto
n1 = new JTextField();n1.setBounds(35, 30, 50, 40);n1.setText(String.valueOf(op1));n1.disable();add(n1);n1.setHorizontalAlignment(n1.CENTER);
n1.setFont(new Font("Arial",Font.BOLD,16));
//op = new JTextField();op.setBounds(92, 30, 40, 40);op.setText(String.valueOf(operando[sg]));op.disable();add(op);op.setHorizontalAlignment(op.CENTER);
//op.setFont(new Font("Arial",Font.BOLD,18));
n2 = new JTextField();n2.setBounds(125, 30, 50, 40);n2.setText(String.valueOf(op2));n2.disable();add(n2);n2.setHorizontalAlignment(n2.CENTER);
n2.setFont(new Font("Arial",Font.BOLD,16));
res = new JTextField();res.setBounds(217, 30, 50, 40);res.setText(String.valueOf(sl));res.disable();add(res);res.setHorizontalAlignment(res.CENTER);
res.setFont(new Font("Arial",Font.BOLD,16));
//Botones
ba = new JButton(new ImageIcon("src/ad.png","Suma" ));ba.setBounds(30, 90, 50, 40);add(ba);ba.addActionListener(this);
bs = new JButton(new ImageIcon("src/sust.png","Resta" ));bs.setBounds(95, 90, 50, 40);add(bs);bs.addActionListener(this);
bp = new JButton(new ImageIcon("src/prod.png","Multiplicación" ));bp.setBounds(160, 90, 50, 40);add(bp);bp.addActionListener(this);
br = new JButton(new ImageIcon("src/raz.png","División" ));br.setBounds(225, 90, 50, 40);add(br);br.addActionListener(this);
//Etiquetas
l1 = new JLabel(new ImageIcon("src/int.png","¿?" ));l1.setBounds(85, 30, 40, 40);add(l1);l1.setHorizontalTextPosition(l1.CENTER);
l2 = new JLabel(new ImageIcon("src/eq.png","=" ));l2.setBounds(175, 30, 40, 40);add(l2);l2.setHorizontalTextPosition(l2.CENTER);
lifes = new JLabel(new ImageIcon("src/ht3.png","Vidas restantes" ));lifes.setBounds(200,150,90,30);add(lifes);lifes.setHorizontalAlignment(lifes.RIGHT);
score = new JLabel("Puntuación: " );score.setBounds(10, 150, 150, 30);score.setFont(new Font("Arial",Font.BOLD,15));add(score);
//timer = new JLabel();timer.setBounds(200, 150, 150, 30);timer.setFont(new Font("Arial",Font.BOLD,14));add(timer);
//Inicializar timer
tm = new Timer(10000,new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"Tiempo agotado!","Siga intentando",JOptionPane.INFORMATION_MESSAGE);
tm.stop();
restLife();
init();
}
});
tm.start();
}
int random(){
return (int)(Math.random()*100);
}
int random(int a){
int aux = (int)(Math.random()*5);
if (aux < a)
return aux;
else
return random(a);
}
int operacion(){
if(sg == 0)
return op1 + op2;
else if(sg == 1)
return op1 - op2;
else if(sg == 2)
return op1 * op2;
else
return op1 / op2;
}
void restLife(){
lf--;
if(lf == 2)
lifes.setIcon(new ImageIcon("src/ht2.png","Vidas restantes" ));
else if(lf == 1)
lifes.setIcon(new ImageIcon("src/ht.png","Vidas restantes" ));
else if(lf == 0){
JOptionPane.showMessageDialog(null,"¡Game Over!nnPuntaje final: "+sc,"Game over!",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
void init(){
op1 = random();op2 = random();sg = random(4); sl = operacion();
n1.setText(String.valueOf(op1));/*op.setText(String.valueOf(operando[sg]));*/n2.setText(String.valueOf(op2));res.setText(String.valueOf(sl));
tm.restart();
}
public void actionPerformed(ActionEvent e){
tm.stop();
//Botón Adición
if(e.getSource() == ba){
if(sg == 0){
JOptionPane.showMessageDialog(null,"Correcto!","+10 pts",JOptionPane.INFORMATION_MESSAGE);
sc+=10;
}else{
JOptionPane.showMessageDialog(null,"Incorrecto!","0 pts",JOptionPane.ERROR_MESSAGE);
restLife();
}
}
//Botón Sustracción
if(e.getSource() == bs){
if(sg == 1){
JOptionPane.showMessageDialog(null,"Correcto!","+10 pts",JOptionPane.INFORMATION_MESSAGE);
sc+=10;
}else{
JOptionPane.showMessageDialog(null,"Incorrecto!","0 pts",JOptionPane.ERROR_MESSAGE);
restLife();
}
}
//Botón Producto
if(e.getSource() == bp){
if(sg == 2){
JOptionPane.showMessageDialog(null,"Correcto!","+10 pts",JOptionPane.INFORMATION_MESSAGE);
sc+=10;
}else{
JOptionPane.showMessageDialog(null,"Incorrecto!","0 pts",JOptionPane.ERROR_MESSAGE);
restLife();
}
}
//Botón Razón
if(e.getSource() == br){
if(sg == 3){
JOptionPane.showMessageDialog(null,"Correcto!","+10 pts",JOptionPane.INFORMATION_MESSAGE);
sc+=10;
}else{
JOptionPane.showMessageDialog(null,"Incorrecto!","0 pts",JOptionPane.ERROR_MESSAGE);
restLife();
}
}
score.setText("Puntuación: "+sc);
init();
}
}
----------------------------------------------------- Archivo 2 -----------------------------------------------------------
package Juego.prueba1;
public class Main{
public static void main(String args[]){
Game a=new Game();
a.setVisible(true);
a.setTitle("Adivina el numero!" );
a.setBounds(200,100,320,220);
a.setResizable(false);
a.setDefaultCloseOperation(a.EXIT_ON_CLOSE);
}
}
El juego ejecutado, es igual a este, las imagenes y los audios se las pueden cambiar a su estilo

Se aceptan puntos :3

