使用方向键控制男孩移动,需要在 LoveCollector 类中添加方向键事件处理逻辑。在 keyPressed() 方法中,根据按下的方向键来更新男孩的位置,并重新设置 boy 按钮的位置。

以下是修改后的代码,其中添加了方向键事件处理逻辑:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class LoveCollector extends JFrame implements ActionListener, KeyListener {
    private final int SCREEN_WIDTH = 500;
    private final int SCREEN_HEIGHT = 500;
    private final int BOY_SIZE = 50;
    private final int HEART_SIZE = 30;
    private final int BIG_HEART_SIZE = 100;
    private final int TOTAL_HEARTS = 13;
    private final int HEARTS_TO_WIN = 12;

    private JButton boy;
    private JButton heart;
    private JButton bigHeart;
    private int collectedHearts;
    private Random random;

    public LoveCollector() {
        random = new Random();

        setTitle('Love Collector');
        setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        Container container = getContentPane();
        container.setLayout(null);

        boy = new JButton(new ImageIcon('D:/ai.jpg'));
        boy.setBounds(0, 0, BOY_SIZE, BOY_SIZE);
        container.add(boy);

        heart = new JButton(new ImageIcon('D:/ai.jpg'));
        heart.setBounds(getRandomX(), getRandomY(), HEART_SIZE, HEART_SIZE);
        heart.addActionListener(this);
        container.add(heart);

        bigHeart = new JButton(new ImageIcon('D:/ai.jpg'));
        bigHeart.setBounds(SCREEN_WIDTH / 2 - BIG_HEART_SIZE / 2, SCREEN_HEIGHT / 2 - BIG_HEART_SIZE / 2,
                BIG_HEART_SIZE, BIG_HEART_SIZE);
        bigHeart.setVisible(false);
        container.add(bigHeart);

        collectedHearts = 0;

        Timer timer = new Timer(1000, this);
        timer.start();

        // 注册键盘事件监听器
        addKeyListener(this);
        setFocusable(true);
    }

    private int getRandomX() {
        return random.nextInt(SCREEN_WIDTH - HEART_SIZE);
    }

    private int getRandomY() {
        return random.nextInt(SCREEN_HEIGHT - HEART_SIZE);
    }

    private void generateNewHeart() {
        heart.setBounds(getRandomX(), getRandomY(), HEART_SIZE, HEART_SIZE);
        heart.setVisible(true);
    }

    private void showBigHeart() {
        bigHeart.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if (source == heart) {
            collectedHearts++;
            if (collectedHearts == HEARTS_TO_WIN) {
                generateNewHeart();
                showBigHeart();
            } else if (collectedHearts > HEARTS_TO_WIN) {
                JOptionPane.showMessageDialog(this, 'Game Over!');
                System.exit(0);
            } else {
                generateNewHeart();
            }
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        int x = boy.getX();
        int y = boy.getY();
        int speed = 10;

        if (keyCode == KeyEvent.VK_UP) {
            boy.setLocation(x, y - speed);
        } else if (keyCode == KeyEvent.VK_DOWN) {
            boy.setLocation(x, y + speed);
        } else if (keyCode == KeyEvent.VK_LEFT) {
            boy.setLocation(x - speed, y);
        } else if (keyCode == KeyEvent.VK_RIGHT) {
            boy.setLocation(x + speed, y);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // 不需要实现
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // 不需要实现
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            LoveCollector loveCollector = new LoveCollector();
            loveCollector.setVisible(true);
        });
    }
}

在代码中,我们修改了 keyPressed() 方法来处理方向键的按下事件。根据按下的方向键,我们更新了男孩 boy 按钮的位置。通过使用 setLocation() 方法,我们可以设置男孩按钮的新位置。

在这个示例中,我们将男孩的移动速度设置为 10 像素。你可以根据需要调整移动速度。

通过添加这些代码,方向键应该能够控制男孩的移动了。重新运行你的程序,然后按下方向键来测试男孩的移动。

使用方向键控制男孩移动的代码示例

原文地址: https://www.cveoy.top/t/topic/1zI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录