gpt4 book ai didi

Java - 如何使用 SwingWorker 创建多线程游戏

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:21:07 25 4
gpt4 key购买 nike

我想用线程创建一个 [1 人 vs PC] 游戏。

我们的棋盘上有 10*10 个两种颜色的形状,如下所示:

enter image description here

when the Player clicks on BLUE Circles , Their color turns into Gray.

at the other side PC should turn all RED Rectangles into Gray.

the WINNER is who Clears all his/her own Shapes Earlier.


播放器的代码工作正常,但是,我的问题在于实现游戏的 PC 端,正如我在此 article 中所读到的我应该使用 SwingWorker 在 GUI 中实现线程。这是我第一次使用 SwingWorkers,我不知道应该如何才能正常工作。

这是我的代码:

主类

public class BubblePopGame {

public static final Color DEFAULT_COLOR1 = Color.BLUE;
public static final Color DEFAULT_COLOR2 = Color.RED;

public BubblePopGame() {
List<ShapeItem> shapes = new ArrayList<ShapeItem>();

int Total = 10;
for (int i = 1; i <= Total; i++) {
for (int j = 1; j <= Total; j++) {
if ((i + j) % 2 == 0) {

shapes.add(new ShapeItem(new Ellipse2D.Double(i * 25, j * 25, 20, 20),
DEFAULT_COLOR1));
} else {
shapes.add(new ShapeItem(new Rectangle2D.Double(i * 25, j * 25, 20, 20),
DEFAULT_COLOR2));
}
}
}

JFrame frame = new JFrame("Bubble Pop Quest!!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ShapesPanel panel = new ShapesPanel(shapes);
frame.add(panel);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new BubblePopGame();
}
});
}

形状项目类

public class ShapeItem {

private Shape shape;
private Color color;

public ShapeItem(Shape shape, Color color) {
super();
this.shape = shape;
this.color = color;
}

public Shape getShape() {
return shape;
}

public void setShape(Shape shape) {
this.shape = shape;
}

public Color getColor() {
return color;
}

public void setColor(Color color) {
this.color = color;
}

ShapesPanel 类

public class ShapesPanel extends JPanel {

private List<ShapeItem> shapes;
private Random rand = new Random();
private SwingWorker<Boolean, Integer> worker;

public ShapesPanel(List<ShapeItem> shapesList) {
this.shapes = shapesList;
worker = new SwingWorker<Boolean, Integer>() {

@Override
protected Boolean doInBackground() throws Exception {
while (true) {
Thread.sleep(200);
int dim = rand.nextInt(300);
publish(dim);
return true;
}
}

@Override
protected void done() {
Boolean Status;
try {
Status = get();
System.out.println(Status);
super.done(); //To change body of generated methods, choose Tools | Templates.
} catch (InterruptedException ex) {
Logger.getLogger(ShapesPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(ShapesPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}

@Override
protected void process(List<Integer> chunks) {
int mostRecentValue = chunks.get(chunks.size()-1);
System.out.println(mostRecentValue);
Color color2 = Color.LIGHT_GRAY;
ShapeItem tmpShape = shapes.get(mostRecentValue);
if(tmpShape.getColor()==Color.RED){
tmpShape.setColor(color2);
}
repaint();
}

};
worker.execute ();

addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Color color1 = Color.LIGHT_GRAY;
for (ShapeItem item : shapes) {
if (item.getColor() == Color.BLUE) {
if (item.getShape().contains(e.getPoint())) {
item.setColor(color1);
}
}
}
repaint();
}
});
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g.create();

for (ShapeItem item : shapes) {
g2.setColor(item.getColor());
g2.fill(item.getShape());
}

g2.dispose();
}

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

private Color getRandomColor() {
return new Color(rand.nextFloat(), rand.nextFloat(),
rand.nextFloat());
}

最佳答案

如果我对你的代码的理解是正确的,你正在制作一个游戏,其中人类玩家必须尽可能快地点击他的所有形状,而 PC 也随机点击形状。第一个清除他所有形状的人获胜。

如果这是正确的,您可能需要将 SwingWorker 调整为

  • 循环直到游戏结束。目前,由于 return 语句
  • ,您的循环在第一次到达循环末尾时退出
  • 由于您没有对 SwingWorker 的 boolean 返回值做任何事情,您不妨让它返回 void
  • 无需在done 方法中调用get。调用该方法时,SwingWorker 已完成。您似乎只对中间结果感兴趣
  • process 方法中,您可能希望遍历所有值。请注意,process 方法不会在您发布 内容时每次 调用。当 EDT(事件调度线程)可用时,您发布的值被分组并批量传递给 process 方法

关于Java - 如何使用 SwingWorker 创建多线程游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26679641/

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