Java战舰游戏起点麻烦

时间:2013-12-07 04:03:42

标签: java

我正在制作一个完全图形化的战舰游戏,最终将允许2个玩家通过网络玩,但我很难弄清楚如何为网格上的每个方块分配坐标,以及如何让玩家选择他们想要的位置放船。香港专业教育学院制作船只.PNG都是相应的像素长度,以匹配100x100的正方形。 (即)承运人需要5个方格。最后,我可以制作一个小弹出窗口,询问每个转弯时想要放置船的位置吗?我现在的代码非常小,它刚刚开始,但我需要一些帮助才能实现它。任何帮助表示赞赏。

package Battleshiponline;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;
import javax.swing.Timer;


public class Board extends JPanel implements ActionListener {

    private Timer timer;

    public Board() {

        //addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.BLUE);
        setDoubleBuffered(true);
        timer = new Timer(5, this);
        timer.start();
        String[] rowA = new String[]             {"A1","A2","A3","A4","A5","A6","A7","A8","A9","A10"};
        String[] rowB = new String[] {"B1","B2","B3","B4","B5","B6","B7","B8","B9","B10"};
        String[] rowC = new String[] {"C1","C2","C3","C4","C5","C6","C7","C8","C9","C10"};
        String[] rowD = new String[] {"D1","D2","D3","D4","D5","D6","D7","D8","D9","D10"};
        String[] rowE = new String[] {"E1","E2","E3","E4","E5","E6","E7","E8","E9","E10"};
        String[] rowF = new String[] {"F1","F2","F3","F4","F5","F6","F7","F8","F9","F10"};
        String[] rowG = new String[] {"G1","G2","G3","G4","G5","G6","G7","G8","G9","G10"};
        String[] rowH = new String[] {"H1","H2","H3","H4","H5","H6","H7","H8","H9","H10"};
        String[] rowI = new String[] {"I1","I2","I3","I4","I5","I6","I7","I8","I9","I10"};
        String[] rowJ = new String[] {"J1","J2","J3","J4","J5","J6","J7","J8","J9","J10"};

    }
       boolean inGame;




public void paint(Graphics g) {

    super.paint(g);
    Graphics2D g2d = (Graphics2D)g;
    //vert lines(1-10)
    g2d.drawLine(100, 1000, 100, 0);
    g2d.drawLine(200, 1000, 200, 0);
    g2d.drawLine(300, 1000, 300, 0);
    g2d.drawLine(400, 1000, 400, 0);
    g2d.drawLine(500, 1000, 500, 0);
    g2d.drawLine(600, 1000, 600, 0);
    g2d.drawLine(700, 1000, 700, 0);
    g2d.drawLine(800, 1000, 800, 0);
    g2d.drawLine(900, 1000, 900, 0);
    g2d.drawLine(1000, 1000, 1000, 0);
    // horizontal lines(A-J)
    g2d.drawLine(0, 100, 1100,100);
    g2d.drawLine(0, 200, 1100, 200);
    g2d.drawLine(0, 300, 1100, 300);
    g2d.drawLine(0, 400, 1100, 400);
    g2d.drawLine(0, 500, 1100, 500);
    g2d.drawLine(0, 600, 1100, 600);
    g2d.drawLine(0, 700, 1100, 700);
    g2d.drawLine(0, 800, 1100, 800);
    g2d.drawLine(0, 900, 1100, 900);
    Toolkit.getDefaultToolkit().sync();
}


    public void actionPerformed(ActionEvent e) {

        repaint();  
    }


   /* private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            .keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            .keyPressed(e);

        }
*/

1 个答案:

答案 0 :(得分:0)

首先,我建议不要让你的网格方块为100x100像素,因为这会导致网格为1000 x 1000,这对于许多屏幕来说太高了。对于您的gid,请考虑使用GridLayout,其中每个单元格为JButton。这是一个演示,看看它的外观:

public class GridTest extends JPanel implements ActionListener {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Grid Test");
        frame.add(new GridTest());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private void init() {
        GridLayout layout = new GridLayout(10, 10);
        setLayout(layout);
        for (char row = 'A'; row <= 'J'; row++) {
            for (int col = 1; col <= 10; col++) {
                JButton button = new JButton("" + row + col);
                button.setMargin(new Insets(0, 0, 0, 0));
                button.setPreferredSize(new Dimension(50, 50));
                button.addActionListener(this);
                add(button);
            }
        }
    }

    public GridTest() {
        init();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(this, String.format("You pressed %s!", e.getActionCommand()),
                "You Pressed a Button", JOptionPane.INFORMATION_MESSAGE);
        ((JButton) e.getSource()).setEnabled(false);
    }
}

我建议查找Swing并查看所有组件以了解每个组件的功能。 Oracle's Tutorial是一个很好的起点