Foros del Web » Programación para mayores de 30 ;) » Java »

Leer teclado

Estas en el tema de Leer teclado en el foro de Java en Foros del Web. Hola, soy nuevo en el foro y en el ámbito de java, espero me puedan entender y ayudar. El problema es el siguiente, yo estuve ...
  #1 (permalink)  
Antiguo 04/10/2014, 17:33
 
Fecha de Ingreso: octubre-2014
Mensajes: 2
Antigüedad: 9 años, 7 meses
Puntos: 0
Leer teclado

Hola, soy nuevo en el foro y en el ámbito de java, espero me puedan entender y ayudar.

El problema es el siguiente, yo estuve haciendo un programa en java que se intente parecer a la aplicación 2048(juego para smartphone) donde aparece un "tablero" y algunas "fichas", la meta es ir sumando los números que sean iguales hasta llegar a 2048.
El error es que al apretar para algún lado ('a' 's' 'd' o 'w') se mueve en mal, no asía la dirección que debería.
Les dejo el código:

Código PHP:
import java.awt.*;
import javax.swing.*;

import java.util.Random;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/*    Positions[][];
    ______    ______    ______    _______
     |      | |     | |     | |      |
     | 0|0 | | 0|1 | | 0|2 | | 0|3  |
     |_____|    |_____| |_____| |______|
     ______    ______    ______    _______
     |      | |     | |     | |      |
     | 1|0 | | 1|1 | | 1|2 | | 1|3  |
     |_____|    |_____| |_____| |______|
     ______    ______    ______    _______    
     |      | |     | |     | |      |
     | 2|0 | | 2|1 | | 2|2 | | 2|3  |
     |_____|    |_____| |_____| |______|
     ______    ______    ______    _______
     |      | |     | |     | |      |
     | 3|0 | | 3|1 | | 3|2 | | 3|3  |
     |_____|    |_____| |_____| |______|
*/



public class Panel extends JFrame {
    
    private 
int[][] Positions;
    
    private 
int PosX 50;
    private 
int PosY 80;
    
    public 
void paint(Graphics g)    {
        
super.paint(g);
        
        
int RandOK 0;
        
        do
        {
            
Random randomGenerator = new Random();
            
int rand1 randomGenerator.nextInt(3);
            
int rand2 randomGenerator.nextInt(3);
            if ( 
Positions[rand1][rand1] == )
            {
                
Positions[rand1][rand2] = 2;
                
RandOK 1;
            }
        } while (
RandOK == 0);
        
        for (
int i 0;4;i++)
        {
            for (
int x 0;4;x++)
            {
                
Toolkit t Toolkit.getDefaultToolkit ();
                
Image imagen t.getImage ("C:/Users/Facu/Desktop/carpeta especial para las pelotudeces que me pasa karma/2048/"+Positions[i][x]+".jpg");
                
g.drawImage (imagenPosXPosYthis);
                
PosX += 65;
            }
            
PosX 50;
            
PosY += 65;
        }
        
PosX 50;
        
PosY 80;
    }
    
    public 
Panel()
    {
            
KeyListener listener = new MyKeyListener();
            
addKeyListener(listener);
            
setFocusable(true);
    }
    
    public class 
MyKeyListener implements KeyListener {
        
        public 
void keyTyped(KeyEvent e) {
        }
        public 
void keyPressed(KeyEvent e) {
            switch(
KeyEvent.getKeyText(e.getKeyCode()))
            {
                case 
"A":    moveKeys(1);
                case 
"S":    moveKeys(2);
                case 
"D":    moveKeys(3);
                case 
"W":    moveKeys(4);
            }
        }
        public 
void keyReleased(KeyEvent e) {
        }
    }
    
    public 
void moveKeys(int arg)
    {
        switch(
arg)
        {
            case 
1:    {    //Izquierda
                
for (int i 0;4;i++)
                {
                    for (
int x 0;4;x++)
                    {
                        if (
Positions[i][x] == || == 0)
                            continue;
                            
                        else if ( 
Positions[i][x] == Positions[i][x-1] )
                        {
                            
Positions[i][x-1] *= 2;
                            
Positions[i][x] = 0;
                        }
                        else if ( 
Positions[i][x-1] == )
                        {
                            
Positions[i][x-1] = Positions[i][x];
                            
Positions[i][x] = 0;
                        }
                    }
                }
            }
            case 
2:    {    //Abajo
                
for (int i 0;4;i++)
                {
                    for (
int x 0;4;x++)
                    {
                        if (
Positions[i][x] == || == 3)
                            continue;
                            
                        else if ( 
Positions[i][x] == Positions[i+1][x] )
                        {
                            
Positions[i+1][x] *= 2;
                            
Positions[i][x] = 0;
                        }
                        else if ( 
Positions[i+1][x] == )
                        {
                            
Positions[i+1][x] = Positions[i][x];
                            
Positions[i][x] = 0;
                        }
                    }
                }
            }
            case 
3:    {    //Derecha
                
for (int i 0;4;i++)
                {
                    for (
int x 0;4;x++)
                    {
                        if (
Positions[i][x] == || == 3)
                            continue;
                            
                        else if ( 
Positions[i][x] == Positions[i][x+1] )
                        {
                            
Positions[i][x+1] *= 2;
                            
Positions[i][x] = 0;
                        }
                        else if ( 
Positions[i][x+1] == )
                        {
                            
Positions[i][x+1] = Positions[i][x];
                            
Positions[i][x] = 0;
                        }
                    }
                }
            }
            case 
4:    {    //Arriba
                
for (int i 0;4;i++)
                {
                    for (
int x 0;4;x++)
                    {
                        if (
Positions[i][x] == || == 0)
                            continue;
                            
                        else if ( 
Positions[i][x] == Positions[i-1][x] )
                        {
                            
Positions[i-1][x] *= 2;
                            
Positions[i][x] = 0;
                        }
                        else if ( 
Positions[i-1][x] == )
                        {
                            
Positions[i-1][x] = Positions[i][x];
                            
Positions[i][x] = 0;
                        }
                    }
                }
            }
        }
        
repaint();
    }
    public 
void CargarPos()
    {
        
Positions = new int[4][4];
    }
    public static 
void main(String[] arg)
    {    
        
Panel Tablero = new Panel();
        
Tablero.CargarPos();
        
Tablero.setBounds(4040400400);
        
Tablero.setTitle("2048 - Prototype");
        
Tablero.setVisible(true);
        
    }


Última edición por AM_Anti; 04/10/2014 a las 23:33

Etiquetas: movimiento, programa, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:28.