gpt4 book ai didi

java - 将 jLabel move 到 jPanel 中的不同位置(类似 Pacman 的游戏)

转载 作者:行者123 更新时间:2023-11-30 11:23:44 24 4
gpt4 key购买 nike

我正在制作一个像吃 bean 人这样的游戏,到目前为止我只是从网格开始。我启动了网格,但我需要弄清楚如何将某些内容 move 到网格中的不同位置,以便当用户单击或我的幽灵 move 时,它会显示在屏幕上。我如何让它 move ?我尝试了很多不同的方法,但没有一个对我有用。

import java.awt.*;

import javax.swing.*;
import javax.swing.border.BevelBorder;


public class GUI {

public static void main(String[] args) {
final JFrame f = new JFrame("Frame Test");
GridLayout Layout = new GridLayout(50,50);

JPanel panel = new JPanel(new GridLayout(50, 50, 1, 1));

//Not sure if I need this or not?
//panel.setLayout(new GridBagLayout());

//first set of black
for (int i = 0; i < 1000; i++) {
JLabel a = new JLabel(new ImageIcon("black-square.jpg"), JLabel.CENTER);
panel.add(a);

}

//adds pacman
JLabel b = new JLabel(new ImageIcon("pacman.png"), JLabel.CENTER);
panel.add(b);

//next set of black
for (int i = 0; i < 1000; i++) {
JLabel c = new JLabel(new ImageIcon("black-square.jpg"), JLabel.CENTER);
panel.add(c);
}


//do the thing
f.setContentPane(panel);
f.setSize(1000, 1000);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);


}

最佳答案

先看看 Concurrency in SwingHow to Use Swing Timers

下一个问题是容器在 LayoutManager 的控制下。虽然使用它可以实现 move ,但它会是 block 状的,因为每个组件都会跳过单元格。

如果你想要流畅的 move ,你将不得不设计自己的布局逻辑,这可能会非常复杂。

尽管如此,您的目标应该是保持游戏的“虚拟” View 。这使您无需与 UI 进行大量比较即可知道迷宫的形状和角色的位置。然后你应该简单地渲染这个“虚拟” View 或模型的状态

更新了非常基本的示例

这是一个基本示例,它使用 GridLayoutsetComponentZOrder move 到关于面板的组件...没有碰撞检测,“AI”是可怜的……

enter image description here

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class PacMan101 {

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

public PacMan101() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MazePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class MazePane extends JPanel {

private JLabel pacMan;
private JLabel ghost;

public MazePane() {
pacMan = new JLabel(new ImageIcon(getClass().getResource("/PacMan.png")));
ghost = new JLabel(new ImageIcon(getClass().getResource("/Ghost.png")));

setLayout(new GridLayout(8, 8));
add(pacMan);

for (int index = 1; index < (8 * 8) - 1; index++) {
add(new JPanel() {

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

});
}

add(ghost);

Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
move(pacMan);
move(ghost);
revalidate();
repaint();
}

protected void move(Component obj) {
int order = getComponentZOrder(obj);
int row = order / 8;
int col = order - (row * 8);

boolean moved = false;
while (!moved) {
int direction = (int) (Math.round(Math.random() * 3));
int nextRow = row;
int nextCol = col;
switch (direction) {
case 0:
nextRow--;
break;
case 1:
nextCol++;
break;
case 2:
nextRow++;
break;
case 3:
nextCol--;
break;
}

if (nextRow >= 0 && nextRow < 8 && nextCol >= 0 && nextCol < 8) {
row = nextRow;
col = nextCol;
moved = true;
}
}

order = (row * 8) + col;
setComponentZOrder(obj, order);

}
});
timer.start();
}

}

}

关于java - 将 jLabel move 到 jPanel 中的不同位置(类似 Pacman 的游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21032140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com