Hampton P
Hampton P

Reputation: 11

Creating a grid with a 2d array

I am currently working on creating the game Snake. I had the idea of creating a basic 2-dimensional arraylist so I can create cells but I can't find any form of help on creating that 2d arraylist to store the x and y values of each of the cells. I need help on how to create that 2d arraylist and how to use it.

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);
}

}

Upvotes: 1

Views: 1896

Answers (2)

Pocho
Pocho

Reputation: 19

If you want to make games using java, I suggest you to use game-development application framework-libGDX

Download: https://libgdx.badlogicgames.com/download.html

It's easy to use, plus you build games for desktop,android,ios and html at the same time.There are lots of tutorial on YouTube about libGDX.

Here are some of them:

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

Upvotes: 1

ItamarG3
ItamarG3

Reputation: 4122

You most certainly don't need a 2d array list. Instead, you need a 2d array of Cells:

Instead of this:

ArrayList[][] Cell = new ArrayList[10][10];

Use this:

Cell[][] cells = new Cell[10][10];

Then in the constructor:

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*/);
        }
    }
    ...

Upvotes: 0

Related Questions