trying to write 2d snake game in java just for fun. i have tile map as 2d array.

the problem is that its is not drawing player in level 2d loop.

let me know if there is better way to do this. only thing i have to use 2d array tile map.

Player.java

//player class is just a green fill rect.
public void paint(Graphics g)
{
         g.setColor(Color.green);
       g.fillRect(x, y, width, height);
}

Level.java

2d array:

000000
000000
010000


public void paint(Graphics g, Player p){
     for(int y = 0; y < map01.length; y++){
          for(int x = 0; x < map01[y].length; x++){
              if(map01[y][x] == 0)          //BACKGROUND
                    {
                        g.setColor(Color.black); 
                        g.fillRect(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
                      }
                 if(map01[y][x] == 1)          //PLAYER
                   {
                      p = new Player(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
                     }
            }
    }

Main.java

....
//start level
public void start(){ 
    levelObject = new Level();
...

//paint method
public void paint(Graphics g){ 
    levelObject.paint(g, playerObject);
... 

It's not painting because you don't call its paint method. (You create a new Player on line 18, but you never do anything with it).

From the code fragment you have posted it's hard to see what value the Player class is bringing to this program. The map has the position of the player, so why not just paint a green rectangle in Level::paint?

As James Cherrill said. you are making the player, but you are not painting it. Also is your map01 a grid for your snake to go on, or are you going to make levels like in my snake game?

This is the snake game I made a year or so ago, and I have recently started to remake it.

My new version has these classes
Main.java - creates the frame, and runs the game loop which repaints the Disp.java and moves the snake (calls a meethod)
Disp.java - the JPanel which holds the graphics that is added to the frame. Gets repainted from Main.java and repaints the snake
Listeners.java - Holds the key listener which has been added to the frame(Main.java). Changes variables to moves the snake
Snake.java - holds the snake (a Vector or points) which then moves according to boolean variables changed by the Listeners
Instances.java - holds variables that need to be accessed from many different classes.
HighScore.java - a system of adding high scores to a database (for future use).

I technically also use a 2d array to do my movement, but I do not have it defined in an array. Instead I just set the content pane of the frame to be 500 by 500, and then I move the snake 10 at a time (so it works out to a 50 by 50 grid) which controls the snake. Then I use rectangles to check if the snake is out of the screen (in which case it dies), or if it collides with itself(it dies), or if it collects the ball(the snake gets longer).

I use a thread for my loop, so that way it doesn't interfere with the key inputs, and I have a delay of about 25ms, but you can change this to make the game run faster or slower.

Hope this gives you some ideas of how to lay out your program. This is just my solution, so don't try to copy it. I'm sure there are better ways of implementing it.

Edited 3 Years Ago by sirlink99

ops yeah forgot to paint it again. i just change it to and it does prints:

g.fillRect(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
 p.paint(g);

I was thinking this struct might give me problem later bc what if i want to call a method from Player.java in game loop in Main.java. ex:

Player.java

...
public void testMethod(){
 //I want to run this method in main game loop.
}

//player class is just a green fill rect.
public void paint(Graphics g)
{
         g.setColor(Color.green);
       g.fillRect(x, y, width, height);
}

now how to call testMethod() in Main.java?

Main.java

....
//start level
public void start(){ 
    levelObject = new Level();
...

//main game loop
 public void actionPerformed(ActionEvent e){
        playerObject.testMethod();

        repaint();
    }

//paint method
public void paint(Graphics g){ 
    levelObject.paint(g, playerObject);
... 

problem is on line:

playerObject.testMethod();

bc playerObject is null. even tho i am creating it in Level.java.

Edited 3 Years Ago by game06

what you could do is restructure your player constructor to take in a Main.java reference

new Player (this, x * tileWidth, y * tileHeight, tileWidth, tileHeight)

and then store the reference to the Main class in the player class, however ideally you want your pprogram to have the layout of a tree

         Main class
   /                  \
High Score           Draw 
                  /        \  
                Player    Level

With level I am referring to my game with obstacles as well.
Structuring your program this way will allow for minimal extra storage( by storing references back to the parent class), and will make your code less complicated to read and debug. Structuring this way also allows for easier feature implementation in the future.

Edited 3 Years Ago by sirlink99

i just download your game sirlink99 and its amazing. I am trying to create same as your game only by 2d array tile maps. i am not sure how to do the controls in tile maps.

Just wanted to make sure. The way i am moving it. the map doesnt change. in 2d array the '1' always stay at bottom row, 2nd col.

00000
00000
01000

should i change it so that '1' change in map? ex:

if(right)
map01[x][y] = map01[x+1][y]
00000
00000
00100

if(up)
map01[x][y] = map01[x][y-1]
00000
00100
00000

Edited 3 Years Ago by game06

Correct me if I am wrong, but you are using the 2D array as your map. For example if you have a 10 by 10 2D array and your frame is 100 by 100, then each element in the array represents 10px by 10px. These pixels can either be a 1 (snake) or a 0 (background) (and possibly a 2 for the collect ball). Then what you want to do is if you have the right arrow pressed down, then you want to move the head of the snake (1) to array[x+1][y] like you showed. But you also want to keep in mind that you need to work from the back of the snake otherwise you lose reference to the rest of the body (because if you turn and the snake just moves right you get a diagonal snake). This method should work fine, although your paintComponent method will have a nested for loop which will slow your game down slightly. Then depending on the value in the array you either draw nothing, or a circle/rectangle for your snake.

As for your other question. No you don't need to change the position each time, although if you want to you can make it generate in a random location with 2 lines of code.

This question has already been answered. Start a new discussion instead.