[ Foro de Java ]

¿Cómo aumento la velocidad respecto a los puntos obtenidos?

21-Nov-2023 14:09
Invitado (Yeli)
0 Respuestas

Hola, pues estoy creando un juego el cual consiste en una pelota que rebota de pared en pared, para ir consiguiendo puntos hay que presionar espacio justo en medio de un rectángulo rojo, bueno, la idea es que para que no sea tan aburrido el juego, añadirle dificultad, haciendo que cada 5 puntos la velocidad de la pelota aumente en 1 o 2, la situación es que no conozco mucho de programación, por lo que no se como hacer lo que quiero.
He aquí el código para que lo puedan observar:
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class Paddle extends JPanel implements KeyListener {
   public static final int ANCHO_VENTANA = 500;
   public static final int ALTO_VENTANA = 500;
   private Ball ball;
   private int puntos;
   private boolean gameRunning = true;
   private Rectangle hitbox;

   public Paddle() {
       super();
       setPreferredSize(new Dimension(ANCHO_VENTANA, ALTO_VENTANA));
       setFocusable(true);
       addKeyListener(this);
       ball = new Ball(ANCHO_VENTANA / 2, ALTO_VENTANA / 2);
       puntos = 0;
       hitbox = new Rectangle(ANCHO_VENTANA / 2 - 10, ALTO_VENTANA / 2 - 50, 20, 100);
       start();
   }

   @Override
   protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       g.setColor(Color.BLACK);
       g.fillRect(0, 0, ANCHO_VENTANA, ALTO_VENTANA);
       g.setColor(Color.RED);
       int rectX = ANCHO_VENTANA / 2 - 10;
       int rectY = ALTO_VENTANA / 2 - 50;
       g.fillRect(rectX, rectY, 20, 100);
       ball.draw(g);
       g.setColor(Color.WHITE);
       g.drawString("Puntos: " + puntos, 10, 20);
   }

   @Override
   public void keyPressed(KeyEvent e) {
       if (e.getKeyCode() == KeyEvent.VK_SPACE) {
           if (!gameRunning) {
               reset();
               start();
           } else if (hitbox.contains(ball.getX(), ball.getY())) {
               puntos++;
           } else {
               gameRunning = false;
               System.out.println("¡Juego terminado!");
               JOptionPane.showMessageDialog(this, "Game Over. Puntos obtenidos: " + puntos, "Juego terminado", JOptionPane.INFORMATION_MESSAGE);
               stop();
           }
       }
   }

   @Override
   public void keyReleased(KeyEvent e) {
   }

   @Override
   public void keyTyped(KeyEvent e) {
   }

   private void start() {
       new Thread(() -> {
           while (gameRunning) {
               ball.move();
               repaint();
               try {
                   Thread.sleep(10);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       }).start();
   }

   private void stop() {
       gameRunning = false;
       Thread.currentThread().interrupt();
   }

   private void reset() {
       ball = new Ball(ANCHO_VENTANA / 2, ALTO_VENTANA / 2);
       puntos = 0;
       gameRunning = true;
   }

   public static void main(String[] args) {
       SwingUtilities.invokeLater(() -> {
           JFrame frame = new JFrame("Juego de la pelota");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setSize(ANCHO_VENTANA, ALTO_VENTANA);
           frame.setLocationRelativeTo(null);
           Paddle juego = new Paddle();
           frame.getContentPane().add(juego);
           frame.setVisible(true);
       });
   }
}

class Ball {
   private int x;
   private int y;
   private int velocidadX;
   private int velocidadY;

   public Ball(int x, int y) {
       this.x = x;
       this.y = y;
       this.velocidadX = 5;
       this.velocidadY = 5;
   }

   public void move() {
       x += velocidadX;
       y += velocidadY;
       if (x < 0 || x >= Paddle.ANCHO_VENTANA) {
           velocidadX = -velocidadX;
       }
       if (y < 0 || y >= Paddle.ALTO_VENTANA) {
           velocidadY = -velocidadY;
       }
   }

   public void draw(Graphics g) {
       g.setColor(Color.WHITE);
       g.fillOval(x, y, 20, 20);
   }

   public int getX() {
       return x;
   }

   public int getY() {
       return y;
   }
}




(No se puede continuar esta discusión porque tiene más de dos meses de antigüedad. Si tienes dudas parecidas, abre un nuevo hilo.)