snake game in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • game2d
    New Member
    • Apr 2013
    • 59

    snake game in java

    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
    Code:
    //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
    Code:
    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
    Code:
    ....
    //start level
    public void start(){ 
        levelObject = new Level();
    ...
    
    //paint method
    public void paint(Graphics g){ 
        levelObject.paint(g, playerObject);
    ...
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Where and when is Player.paint(.. .) called? In line 18 of the Level.java you create a new Player, but from what I can see this Players paint method may never be called.

    Comment

    • game2d
      New Member
      • Apr 2013
      • 59

      #3
      ops yeah forgot about that. just change it to:
      Code:
       p = new Player(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
      p.paint(g);
      but there is will one problem. what if i want to call method from player.java in main game loop in Main.java. that will give me problem, for ex:


      Player.java
      Code:
      ....
      public void testMethod(){
       ...
       //I want to run this method in main game loop.
      }
      
      public void paint(...){
      ...
      }
      level.java
      //stay same


      Main.java
      Code:
      ....
      //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);
      ...
      here it will give error on on playerObject.te stMethod() in Main.java. the error is bc playerObject in Main.java is null.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        In your main class you can call levelObject.p.t estMethod(), provided the Player object p in your Level class is visible (which it would be if it's public or has no visibility modifier).

        Alternatively, you can have a method like this in your Level class:
        Code:
        public void testMethod() {
           p.testMethod();
        }
        Then you can call the Levels testMethod() which in turn will call the Players testMethod().

        Comment

        • game2d
          New Member
          • Apr 2013
          • 59

          #5
          I end of calling Player in Main.java. and rest player and level class is same as before.

          Main.java
          ...
          Code:
          public Main()
          	{
          .....
                   levelObject = new Level();
          		
                  	/**************************************/
           		/*** load player where its 1 in map ***/
           		/**************************************/
           		int map01[][] = levelObject.getmap01();
           		int tileWidth = levelObject.getTileWidth();
           		int tileHeight = levelObject.getTileHeight();
           		
           		for (int y = 0; y < map01.length; y++){        //Row
           			for (int x = 0; x < map01[y].length; x++){ //Col
           				if(map01[y][x] == 1){  //Block
           					playerObject = new Player(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
           				}
           			}
           		}
          }
          
          //game loop
          public void actionPerformed(ActionEvent e)
          	{
          		playerObject.testMethod();
          		
          	}
          
          public void paintComponent(Graphics g)
          		{	
          			super.paintComponent(g); 
          			
          			levelObject.paint(g);
          			playerObject.paint(g);
          			repaint();
          		}
          Move player like this
          Code:
          if(right)
          			//x += dx;
          		 if(left)
          			x-=dx;
          		if(up)
          			y-=dx;
          		 if(down)
          			y+=dx;
          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:

          map01[x][y] = 0
          map01[x+1][y] = 1
          00000
          00000
          00100

          map01[x][y] = 0
          map01[x][y-1] = 1
          00000
          00100
          00000

          if yes, than i am not sure how to do this task.

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            No, the player wouldn't move just like that; this is because when you create a new Player object in line 16 of the Main.java, you give it a set of coordinates but you never change them. In Java, when you hand parameters to a method (and a constructur is a kind of method) then the values are given to the method but not the handlers. (It's neither pure call-by-value nor pure call-by-reference; this however is a complex topic so I'll not go into it now.) So for example if Player had a print method as such:
            Code:
            public void print() {
               System.out.println("The player is at " + x + ", " + y);
            }
            then running the code
            Code:
            playerObject = new Player(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
            playerObject.print();
            x++;
            playerObject.print();
            in the Main class would result in the output
            Code:
            The player is at 2, 1
            The player is at 2, 1
            because the values are never changed. What you need is a method for updating the position of the player, for example:
            Code:
            public void updatePos(int x, int y) {
               this.x = x;
               this.y = y;
            }
            It might also be a good idea to check for errors, such as values for x or y that are smaller than 0. Alternatively, you could have functions like
            Code:
            public void incrementX() {...}
            public void decrementX() {...}
            public void incrementY() {...}
            public void decrementY() {...}
            which might resemble your controls more closely. Or is that code with dx and dy that kind of function?

            EDIT: Oh, you can update the map as well; this depends on how you want to represent the field and player of course and how you want to do stuff like collision detection. The way to do that is pretty easy:
            Code:
            map01[x][y] = 0;
            // change x and/or y here
            map01[x][y] = 1;
            Last edited by Nepomuk; May 28 '13, 07:49 AM. Reason: Forgot something

            Comment

            Working...