俄罗斯方块游戏-快速独立于计时器的左右移动

时间:2019-06-18 23:38:49

标签: java swing

我想用Java制作俄罗斯方块游戏。

我正在使用摆动计时器来放下瓷砖。 但是问题是,当我尝试左右移动瓷砖时, 运动速度受计时器延迟值的限制。瓷砖每秒下降一次,因此,我只能在计时器重置和重新绘制时更改它们的位置。在这种情况下,如何执行独立的第二操作或任何其他可以帮助我的方法,我们将不胜感激。

package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

enum Shapes {
    // T, S1, S2, K,L1, L2,
    //above members will be left commented until other tiles are prepared
    I;

    public static Shapes getRandomShapes() {

        Random random = new Random();
        return values()[random.nextInt(values().length)];
    }
}

public class Engine extends JPanel implements KeyListener, ActionListener {


    private static final long serialVersionUID = 1L;
    private int[] xPos = new int[800];
    private int[] yPos = new int[800];
    int moves = 0;
    Timer timer;
    int delay;

    Engine() {
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
        delay = 1000;
        timer = new Timer(delay, this);
        timer.start();
    }

    private boolean left;
    private boolean right;

    private void fall() {
        yPos[0] += 30;
        yPos[1] += 30;
        yPos[2] += 30;
        yPos[3] += 30;

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.drawRect(5, 5, 482, 842);
        g.setColor(Color.BLACK);
        g.fillRect(7, 7, 478, 838);

        if (Shapes.getRandomShapes() == Shapes.I) {
            if (moves == 0) {
                xPos[0] = 60;
                xPos[1] = 90;
                xPos[2] = 120;
                xPos[3] = 150;
                yPos[0] = 6;
                yPos[1] = 6;
                yPos[2] = 6;
                yPos[3] = 6;
                moves++;
            }
            for (int x = 0; x < 4; x++) {

                ImageIcon Iimage = new ImageIcon(Engine.class.getClassLoader().getResource("images/I.png"));
                Iimage.paintIcon(this, g, xPos[x], yPos[x]);

            }
        }
        g.dispose();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        timer.start();

        fall();
        if (left) {
            xPos[0] -= 30;
            xPos[1] -= 30;
            xPos[2] -= 30;
            xPos[3] -= 30;
            left = false;
        }
        if (right) {
            xPos[0] += 30;
            xPos[1] += 30;
            xPos[2] += 30;
            xPos[3] += 30;
            right = false;
        }

        repaint();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub

        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            right = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            left = true;

        }
    }


}

1 个答案:

答案 0 :(得分:1)

将运动逻辑(向下)与计时器解耦,因此计时器可以以较高的速度跳动。

我将捕获“最后一滴”的Instant并计算每次滴答经过的时间,当等于或大于1秒时,将其滴下

以下示例演示了使用Instant计算经过的Duration时间,然后根据需要执行某些操作的基本思想

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.Instant;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Drop {

    public static void main(String[] args) {
        new Drop();
    }

    public Drop() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {
        private Instant lastDropTime;
        private JLabel label;
        public TestPane() {
            setLayout(new BorderLayout());
            label = new JLabel("Drop in ...");
            add(label);

            Timer timer = new Timer(5, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    if (lastDropTime == null) {
                        lastDropTime = Instant.now();
                    }
                    // I'm using toMillis so I can display the time
                    // but you could just use toSeconds
                    Duration duration = Duration.between(lastDropTime, Instant.now());
                    long millis = duration.toMillis();
                    long seconds = duration.toSeconds();

                    if (seconds >= 1) {
                        // Drop down here...
                        lastDropTime = Instant.now();
                        millis = 1000;
                    }

                    label.setText("Drop in " + (1000 - millis));
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}