首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我如何开始用游戏计数器填充我的棋盘?

我如何开始用游戏计数器填充我的棋盘?
EN

Stack Overflow用户
提问于 2019-04-25 01:00:35
回答 1查看 80关注 0票数 0

spacebattle

到目前为止,我可以使用jlabels制作一个由瓦片组成的板。游戏的下一步是让红色和蓝色的玩家轮流选择他们最初控制的行星(通过点击行星方块)。一旦玩家点击一颗行星,就应该在该瓦片行星的顶部放置一个小指示器。

行星的阵列数据也应该更新,以便知道行星的所有者。现在,我不知道如何将jlabel瓦片与我的变量链接起来。

如果你能给我指明正确的方向,我将不胜感激。谢谢。

代码语言:javascript
运行
复制
package SpaceBattle;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

//import javax.smartcardio.Card;
import javax.swing.*;

public class spacebattle2 extends JFrame {

    JLabel[] tilelabel = new JLabel[31];
    JLabel[] hexlabel = new JLabel[31];



    ImageIcon tileicon, hexicon;

    int rownum, colnum;
    int c, r, xloc, yloc, hexctr, tilectr;
    int energy, ore, capacity;
    String labelname;
    String type;

    int numtiles = 31;
    int deckctr = 26;


    String[] tiledeck = new String[numtiles];
    int[] tilexloc = new int[numtiles];
    int[] tileyloc = new int[numtiles];

    int tileenergy[] = new int[numtiles];
    int tileore[] = new int[numtiles];
    int tilecapacity[] = new int[numtiles];
    String tiletype[] = new String[numtiles]; 
    String tilename[] = new String[numtiles];
    int tilemeeple[] = new int[numtiles];



    String[] hexnum = new String[numtiles];
    int[] hexxloc = new int[numtiles];
    int[] hexyloc = new int[numtiles];
    String[] hexempty = new String[numtiles];

    int hexenergy[] = new int[numtiles];
    int hexore[] = new int[numtiles];
    int hexcapacity[] = new int[numtiles];
    String hextype[] = new String[numtiles]; 
    String hexname[] = new String[numtiles];
    String hexplayer[] = new String[numtiles];

    String[] players = new String[6];
    String playerturn;

    int startingplanets = 2;
    int bluemaxplanet = startingplanets;
    int redmaxplanet = startingplanets;

    int numplanets = 7;
    int numspace = 24;
    int nonplayertiles = numplanets + numspace;

    String planetCode;
    String planetselected = "none";

    //selectPlanet
    String player = "Red";

    int i;




    public spacebattle2() {

    setLayout(null);    

    createDeck();
    shuffleDeck();
    drawBoard();
    //selectPlanet();

    }


    private void selectPlanet() {
        // TODO Auto-generated method stub

        for(i = 0; i < numtiles; i++) {

            tilelabel[i].addMouseListener(new MouseListener() {

                public void mouseClicked(MouseEvent e) {


                    System.out.println(i);


                }


                public void mouseEntered(MouseEvent arg0) {
                    // TODO Auto-generated method stub


                }

                public void mouseExited(MouseEvent arg0) {
                    // TODO Auto-generated method stub

                }

                public void mousePressed(MouseEvent arg0) {
                    // TODO Auto-generated method stub

                }

                public void mouseReleased(MouseEvent arg0) {
                    // TODO Auto-generated method stub

                }
            });
        }
    }









    private void shuffleDeck() {
        // TODO Auto-generated method stub

        System.out.println("shuffling deck...");

        Random r = new Random();
        int swapIndex;

        String temp;

        for(int startIndex = 0; startIndex < numtiles; startIndex++) {

            System.out.println(startIndex + " startIndex " + tiledeck[startIndex]);

            swapIndex = r.nextInt(numtiles);
            System.out.println(swapIndex + " swapIndex " + tiledeck[swapIndex]);

            if(swapIndex != startIndex) {

                temp = tiledeck[swapIndex];

                tiledeck[swapIndex] = tiledeck[startIndex];
                System.out.println("Moving " + startIndex +  " " + tiledeck[startIndex] + " startIndex to " + swapIndex + " swapIndex");

                tiledeck[startIndex] = temp;
                System.out.println("Moving " + temp +  " temp to " + startIndex + " startIndex");
            }           
        }
    }





    private void createDeck() {
        // TODO Auto-generated method stub

        int s, p, xloc = 667, yloc = 10;




        /*******************************
         * 
         *  Create Space tiles
         * 
         ******************************/

        System.out.println("creating space tiles...");

        tilectr = 0;

        for(s= 0; s < numspace; s++) {

            tiledeck[tilectr] = "000";

            System.out.println(tilectr + " " + tiledeck[tilectr]);

            tilectr++;
        } 


        /**************************
         * 
         *  Create Planet tiles
         * 
         *************************/

        System.out.println("creating planet tiles...");

        for(p = 0; p < numplanets; p++) {

            energy = (int) (Math.random()*3 + 1);
            ore = (int) (Math.random()*3 +1);
            capacity = (int) (Math.random()*3 +1);


            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("");
            stringBuffer.append(energy);
            stringBuffer.append(ore);
            stringBuffer.append(capacity);

            String planetCode = stringBuffer.toString();

            tiledeck[tilectr] = planetCode;

            System.out.println(tilectr + " " + tiledeck[tilectr]);
            tilectr++;
        }

    }



    private void drawBoard() {
        // TODO Auto-generated method stub
        int xloc = 10;
        int yloc = 61;
        int hexctr = 0;


        int colnum = 7;
        int rownum = 4;

        xloc = 10;
        yloc = 61;
        hexctr = 0;


        colnum = 7;
        rownum = 4;

        int row = 0, col = 0, numtiles = 31;

        for(int c = 0; c < numtiles; c ++) {
            if(tiledeck[c] == "000") {
                System.out.println(c + " " + tiledeck[c] + " tile is space");
                tileenergy[c] = 0;
                tileore[c] = 0;
                tilecapacity[c] = 0;
                tiletype[c] = "space";
                tilename[c] = "space";

                tileicon = new ImageIcon(getClass().getResource("/000.png"));   
                tilelabel[c] = new JLabel();
                tilelabel[c].setIcon(tileicon);

                tilelabel[c].setBounds(xloc, yloc, 115, 100);

                add(tilelabel[c]);

                yloc = yloc + 102;
                row++;

                if(row == rownum) {
                    xloc = xloc + 88;
                    row = 0;
                    col++;
                    if(col % 2 == 0) {
                        yloc = 61;
                        rownum = rownum - 1; 
                    }
                    else {
                        yloc = 10;
                        rownum = rownum + 1;
                    }
                }   
            }



            else {
                System.out.println(c + " " + tiledeck[c] +  " tile is planet");
                tileenergy[c] = Integer.parseInt(tiledeck[c].substring(0, 1));
                tileore[c] = Integer.parseInt(tiledeck[c].substring(1, 2));
                tilecapacity[c] = Integer.parseInt(tiledeck[c].substring(2, 3));
                tiletype[c] = "planet";
                tilemeeple[c] = 0;

                tileicon = new ImageIcon(getClass().getResource("/" + tiledeck[c] + ".png"));   
                tilelabel[c] = new JLabel();
                tilelabel[c].setIcon(tileicon);
                tilelabel[c].setBounds(xloc, yloc, 115, 100);
                add(tilelabel[c]);

                yloc = yloc + 102;
                row++;

                if(row == rownum) {
                    xloc = xloc + 88;
                    row = 0;
                    col++;
                    if(col % 2 == 0) {
                        yloc = 61;
                        rownum = rownum - 1; 
                    }
                    else {
                        yloc = 10;
                        rownum = rownum + 1;
                    }
                }
            }
        }
    }
    private Object i(int numtiles2, int i) {
        // TODO Auto-generated method stub
        return null;
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        spacebattle2 board = new spacebattle2();
        board.setSize(900, 600);
        board.setResizable(false);
        board.setTitle("Space Battle");
        board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        board.setVisible(true);
    }//End void main

}
EN

回答 1

Stack Overflow用户

发布于 2019-04-25 09:25:12

我建议重组程序,使用一个" tile“类来保存与给定tile相关的所有信息。拥有一个“瓦片管理器”类也会很有帮助,它知道瓦片的集合以及它们之间的关系。

下面是您的程序的tile类可能的样子。我试着不改变任何功能,只是重组东西。你应该能够把所有的东西都和你的原始程序匹配起来。

代码语言:javascript
运行
复制
class TileInfo {
    TileManager manager;

    public JLabel label = new JLabel();
    public JLabel hexlabel = new JLabel();

    int xloc;
    int yloc;
    int energy;
    int ore;
    int capacity;
    String type;
    String name;
    int meeple;

    String hexnum;
    int hexxloc;
    int hexyloc;
    String hexempty;
    // etc...

    public TileInfo(Icon tileicon, int x, int y, TileManager manager) {
        this.manager = manager;
        label.setIcon(tileicon);
        label.setBounds(x, y, 115, 100);
        label.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                manager.clickedTile(TileInfo.this);
            };
        });
    }
};

然后,您可以扩展此基类以用于"space“和"planet”磁贴:

代码语言:javascript
运行
复制
class SpaceTile extends TileInfo {
    public SpaceTile(Icon icon, int x, int y, TileManager manager) {
        super(icon, x, y, manager);
        energy = 0;
        ore = 0;
        capacity = 0;
        type = "space";
        name = "space";
    }
};

class PlanetTile extends TileInfo {
    public PlanetTile(Icon icon, int x, int y,
                      int energy, int ore, int capacity, TileManager manager) {
        super(icon, x, y, manager);
        this.energy = energy;
        this.ore = ore;
        this.capacity = capacity;
        type = "planet";
        meeple = 0;
    }
};

在你的例子中,子类并不是必须的,但每种情况下的瓦片结构都略有不同,这有助于保持它们的整洁。

每个TileInfo创建自己的鼠标侦听器,但将事件向上传递给磁贴管理器。平铺管理器的作用就像一个容器,并保存数组:

代码语言:javascript
运行
复制
class TileManager {
    public int numtiles;
    public TileInfo[] tile;
    public String tiledeck[];

    public TileManager(int numtiles) {
        this.numtiles = numtiles;
        this.tile = new TileInfo[numtiles];
        this.tiledeck = new String[numtiles];
        System.out.printf("TileManager(%d): tile[%d]\n", numtiles, numtiles);
    }

    public void clickedTile(TileInfo tile) {
        if (tile instanceof SpaceTile) {
            System.out.println("clicked space tile " + tile.label.getBounds());
        } else if (tile instanceof PlanetTile) {
            System.out.println("clicked planet tile " + tile.label.getBounds());
        }
    }
};

请注意,现在您只需要一个数组。如果您更喜欢ArrayListTileManager可以覆盖它;我希望尽可能接近您的原始程序。clickedTile()方法处理平铺上的鼠标单击,但是因为它在平铺管理器中,所以它可以看到所有的平铺,而不是只看到一个。例如,如果您想在单击磁贴B时取消选择磁贴A,则需要能够同时看到两个磁贴。

有一个单独的平铺管理器并不是必须的;您可以将此功能保留在spacebattle2类中。我喜欢有一个管理器类来将创建、操作和绘制图片组的功能与gameplay分开。它的行为有点像“磁贴图书馆”。

最后,这一切如何适应您的主类。为了清晰起见,我们省略了很多代码。

代码语言:javascript
运行
复制
public class spacebattle2 extends JFrame {
    TileManager tiles = new TileManager(31);

    // didn't change anything except tiledeck[] now lives inside tiles
    // this method could be moved into the TileManager
    private void shuffleDeck() {
        for(int startIndex = 0; startIndex < tiles.numtiles; startIndex++) {
            swapIndex = r.nextInt(tiles.numtiles);
            if(swapIndex != startIndex) {
                temp = tiles.tiledeck[swapIndex];
                tiles.tiledeck[swapIndex] = tiles.tiledeck[startIndex];
                tiles.tiledeck[startIndex] = temp;
            }           
        }
    }

    void createTile(String deck, int xloc, int yloc) {
        tileicon = new ImageIcon(getClass().getResource("/" + deck + ".png"));   
        if (deck.equals("000")) {
            tiles.tile[c] = new SpaceTile(tileicon, xloc, yloc, tiles);
        } else {
            int energy = Integer.parseInt(deck.substring(0, 1));
            int ore = Integer.parseInt(deck.substring(1, 2));
            int capacity = Integer.parseInt(deck.substring(2, 3));
            tiles.tile[c] = new PlanetTile(tileicon, xloc, yloc, 0, 0, 0, tiles);
        }
        add(tiles.tile[c].label);
    }

    private void drawBoard() {
    /* ... */

        for(int c = 0; c < tiles.numtiles; c ++) {
            createTile(tiles.tiledeck[c], xloc, yloc);

            yloc = yloc + 102;
            row++;
            if(row == rownum) {
                xloc = xloc + 88;
                row = 0;
                col++;
                if(col % 2 == 0) {
                    yloc = 61;
                    rownum = rownum - 1; 
                } else {
                    yloc = 10;
                    rownum = rownum + 1;
                }
            }
        }
    }
}

drawBoard()中的循环被简化了;它有很多重复的代码,所以我将其分解出来,并添加了一个createTile()方法来创建这两种类型的瓦片。

这仍然是可以改进的地方。我不认为带有公共TileInfo变量的公共数组是访问数据的理想方法,但也不希望对其进行过多更改,以至于无法识别。

这并不意味着最终的设计,而是建议一种组织代码的方式,使其更容易使用,并回答您关于如何将数据与平铺相关联的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55835150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档