我目前正在制作游戏Snake。我有创建一个基本的二维arraylist的想法,所以我可以创建单元格,但我无法找到任何形式的帮助创建该2d arraylist来存储每个单元格的x和y值。我需要有关如何创建2d arraylist以及如何使用它的帮助。
Snake.java
package snake;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Snake extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
static final int BOX_WIDTH = 600;
static final int BOX_HEIGHT = BOX_WIDTH;
int UPDATE_RATE = 300;
ArrayList<Cell> CellList = new ArrayList<Cell>();
//ode below????
//2d arrayList
ArrayList[][] Cell = new ArrayList[10][10];
public Snake() {
setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));
/*for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
CellList.add(new Cell(i,j));
}
} */
Thread gameThread = new Thread() {
public void run() {
while(true){
repaint();
try {Thread.sleep(1000/UPDATE_RATE);}
catch (InterruptedException ex) {}
}
}
};
gameThread.start();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("SNEK");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Snake snake = new Snake();
frame.setContentPane(snake);
frame.setSize(BOX_WIDTH, BOX_HEIGHT);
frame.pack();
frame.addKeyListener(snake);
frame.setVisible(true);
}
});
}
public void paintComponent(Graphics g) {
g.setColor(Color.pink);
g.fillRect(0, 0, BOX_WIDTH, BOX_HEIGHT);
/*for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
CellList.get(i,j).draw(g);
}
} */
}
@Override
public void keyPressed(KeyEvent arg0) {
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {}
}
Cell.java
package snake;
import java.awt.Color;
import java.awt.Graphics;
public class Cell extends Snake{
private static final long serialVersionUID = 1L;
final int CELL_HEIGHT = 10;
final int CELL_WIDTH = 10;
int status = 0;
int xPos;
int yPos;
public Cell(int x, int y) {
xPos = x * 120;
yPos = y * 120;
}
public void draw(Graphics g) {
g.setColor(Color.magenta);
g.fillRect(xPos, yPos, CELL_WIDTH, CELL_HEIGHT);
}
}
答案 0 :(得分:1)
如果你想用java制作游戏,我建议你使用游戏开发应用程序框架-libGDX
下载:https://libgdx.badlogicgames.com/download.html
它易于使用,而且你可以同时为桌面,机器人,ios和html构建游戏。在YouTube上有很多关于libGDX的教程。
以下是其中一些:
https://www.youtube.com/watch?v=QKK4kDogg-8&list=PLaNw_AbDFccHbzuObI4xHHp6WtiN2cRQv
https://www.youtube.com/watch?v=EJwXzmUQChg&list=PLXY8okVWvwZ0JOwHiH1TntAdq-UDPnC2L
https://www.youtube.com/watch?v=a8MPxzkwBwo&list=PLZm85UZQLd2SXQzsF-a0-pPF6IWDDdrXt
答案 1 :(得分:0)
你肯定不需要2d数组列表。 相反,你需要一个2d的Cell数组:
而不是:
ArrayList[][] Cell = new ArrayList[10][10];
使用此:
Cell[][] cells = new Cell[10][10];
然后在构造函数中:
public Snake() {
setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
cells[i][j] = new Cell(/*do what ever you ned here*/);
}
}
...