Java - Código de movimiento por teclado

 
Vista:
sin imagen de perfil

Código de movimiento por teclado

Publicado por Santiago (1 intervención) el 28/04/2017 00:10:06
Buenas, quisiera saber si hay alguien tan amable de comentar un código que he encontrado en internet sobre un simple movimiento de un punto en un Panel por teclado; se me hace algo confuso, agradecería mucho si alguien me lo explicara. Gracias

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class PingBall extends JComponent {
 
    private final static int ANCHO = 768;
 
    private final static int ALTO = 384;
 
    private final static int DIAMETRO = 10;
 
    private float x, y;
 
    private float vx, vy;
 
    private boolean arriba, abajo, izquierda, derecha;
 
    public PingBall() {
        setPreferredSize(new Dimension(ANCHO, ALTO));
        x = 10;
        y = 20;
        addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e) {
                    actualiza(e.getKeyCode(), true);
                }
 
                public void keyReleased(KeyEvent e) {
                    actualiza(e.getKeyCode(), false);
                }
 
                private void actualiza(int Codigotecla, boolean pressed) {
                    switch (Codigotecla) {
                        case KeyEvent.VK_UP:
                            arriba = pressed;
                            break;
 
                        case KeyEvent.VK_DOWN:
                            abajo = pressed;
                            break;
 
                        case KeyEvent.VK_LEFT:
                            izquierda = pressed;
                            break;
 
                        case KeyEvent.VK_RIGHT:
                            derecha = pressed;
                            break;
 
 
 
                    }
                }
            });
        setFocusable(true);
    }
    private float clamp(float valor, float min, float max) {
        if (valor > max)
            return max;
        if (valor < min)
            return min;
        return valor;
    }
 
    private void fisica(float dt) {
        vx = 0;
        vy = 0;
        if (arriba)
            vy = -300;
        if (abajo)
            vy = 300;
        if (izquierda)
            vx = -300;
        if (derecha)
            vx = 300;
        x = clamp(x + vx * dt, 0, ANCHO - DIAMETRO);
        y = clamp(y + vy * dt, 0, ALTO - DIAMETRO);
    }
 
    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, ANCHO, ALTO);
        g.setColor(Color.RED);
        g.fillOval(Math.round(x), Math.round(y), DIAMETRO, DIAMETRO);
    }
 
    private void dibuja() throws Exception {
        SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    paintImmediately(0, 0, ANCHO, ALTO);
                }
            });
    }
 
    public void cicloPrincipalJuego() throws Exception {
        long tiempoViejo = System.nanoTime();
        while (true) {
            long tiempoNuevo = System.nanoTime();
            float dt = (tiempoNuevo - tiempoViejo) / 1000000000f;
            tiempoViejo = tiempoNuevo;
            fisica(dt);
            dibuja();
        }
    }
 
    public static void main(String[] args) throws Exception {
        JFrame jf = new JFrame("PingBall");
        jf.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        jf.setResizable(false);
        PingBall ball = new PingBall();
        jf.getContentPane().add(ball);
        jf.pack();
        jf.setVisible(true);
        ball.cicloPrincipalJuego();
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder