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 = 100; private final int HEART_SIZE = 80; private final int BIG_HEART_SIZE = 190; 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:/chuan.jpg'));
    boy.setBounds(SCREEN_WIDTH / 2 - BOY_SIZE / 2, SCREEN_HEIGHT / 2 - BOY_SIZE / 2, BOY_SIZE, BOY_SIZE);
    boy.setContentAreaFilled(false);  // 设置按钮背景透明
    boy.setBorderPainted(false);  // 设置按钮边框不可见
    container.add(boy);

    heart = new JButton(new ImageIcon('D:/ai.jpg'));
    heart.setBounds(getRandomX(), getRandomY(), HEART_SIZE, HEART_SIZE);
    heart.setContentAreaFilled(false);  // 设置按钮背景透明
    heart.setBorderPainted(false);  // 设置按钮边框不可见
    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);
    bigHeart.setContentAreaFilled(false);  // 设置按钮背景透明
    bigHeart.setBorderPainted(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);
}

private void showDialog() {
    JOptionPane.showMessageDialog(this, '爱艺');
}

@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 step = 10;

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

    if (boy.getBounds().intersects(heart.getBounds())) {
        collectedHearts++;

        if (collectedHearts == TOTAL_HEARTS) {
            generateNewHeart();
            bigHeart.setIcon(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(true);
            showDialog();
        } else {
            generateNewHeart();
        }
    }
}

@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);
    });
}

}

Love Collector: A Fun and Interactive Game to Collect Hearts

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

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