By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,804 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,804 IT Pros & Developers. It's quick & easy.

snake game in java

P: 59
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
Expand|Select|Wrap|Line Numbers
  1. //player class is just a green fill rect.
  2. public void paint(Graphics g)
  3. {
  4.          g.setColor(Color.green);
  5.        g.fillRect(x, y, width, height);
  6. }



Level.java
Expand|Select|Wrap|Line Numbers
  1. 2d array:
  2.  
  3. 000000
  4. 000000
  5. 010000
  6.  
  7.  
  8. public void paint(Graphics g, Player p){
  9.      for(int y = 0; y < map01.length; y++){
  10.           for(int x = 0; x < map01[y].length; x++){
  11.               if(map01[y][x] == 0)          //BACKGROUND
  12.                     {
  13.                         g.setColor(Color.black); 
  14.                         g.fillRect(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
  15.                       }
  16.                  if(map01[y][x] == 1)          //PLAYER
  17.                    {
  18.                       p = new Player(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
  19.                      }
  20.             }
  21.     }
  22. }

Main.java
Expand|Select|Wrap|Line Numbers
  1. ....
  2. //start level
  3. public void start(){ 
  4.     levelObject = new Level();
  5. ...
  6.  
  7. //paint method
  8. public void paint(Graphics g){ 
  9.     levelObject.paint(g, playerObject);
  10. ... 
  11.  
May 27 '13 #1
Share this Question
Share on Google+
5 Replies


Nepomuk
Expert 2.5K+
P: 3,112
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.
May 27 '13 #2

P: 59
ops yeah forgot about that. just change it to:
Expand|Select|Wrap|Line Numbers
  1.  p = new Player(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
  2. 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
Expand|Select|Wrap|Line Numbers
  1. ....
  2. public void testMethod(){
  3.  ...
  4.  //I want to run this method in main game loop.
  5. }
  6.  
  7. public void paint(...){
  8. ...
  9. }
level.java
//stay same


Main.java
Expand|Select|Wrap|Line Numbers
  1. ....
  2. //start level
  3. public void start(){ 
  4.     levelObject = new Level();
  5. ...
  6.  
  7. //main game loop
  8.  public void actionPerformed(ActionEvent e){
  9.         playerObject.testMethod();
  10.  
  11.         repaint();
  12.     }
  13.  
  14. //paint method
  15. public void paint(Graphics g){ 
  16.     levelObject.paint(g, playerObject);
  17. ... 
here it will give error on on playerObject.testMethod() in Main.java. the error is bc playerObject in Main.java is null.
May 27 '13 #3

Nepomuk
Expert 2.5K+
P: 3,112
In your main class you can call levelObject.p.testMethod(), 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:
Expand|Select|Wrap|Line Numbers
  1. public void testMethod() {
  2.    p.testMethod();
  3. }
Then you can call the Levels testMethod() which in turn will call the Players testMethod().
May 27 '13 #4

P: 59
I end of calling Player in Main.java. and rest player and level class is same as before.

Main.java
...
Expand|Select|Wrap|Line Numbers
  1. public Main()
  2.     {
  3. .....
  4.          levelObject = new Level();
  5.  
  6.             /**************************************/
  7.          /*** load player where its 1 in map ***/
  8.          /**************************************/
  9.          int map01[][] = levelObject.getmap01();
  10.          int tileWidth = levelObject.getTileWidth();
  11.          int tileHeight = levelObject.getTileHeight();
  12.  
  13.          for (int y = 0; y < map01.length; y++){        //Row
  14.              for (int x = 0; x < map01[y].length; x++){ //Col
  15.                  if(map01[y][x] == 1){  //Block
  16.                      playerObject = new Player(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
  17.                  }
  18.              }
  19.          }
  20. }
  21.  
  22. //game loop
  23. public void actionPerformed(ActionEvent e)
  24.     {
  25.         playerObject.testMethod();
  26.  
  27.     }
  28.  
  29. public void paintComponent(Graphics g)
  30.         {    
  31.             super.paintComponent(g); 
  32.  
  33.             levelObject.paint(g);
  34.             playerObject.paint(g);
  35.             repaint();
  36.         }
  37.  
Move player like this
Expand|Select|Wrap|Line Numbers
  1. if(right)
  2.             //x += dx;
  3.          if(left)
  4.             x-=dx;
  5.         if(up)
  6.             y-=dx;
  7.          if(down)
  8.             y+=dx;
  9.  
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.
May 27 '13 #5

Nepomuk
Expert 2.5K+
P: 3,112
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:
Expand|Select|Wrap|Line Numbers
  1. public void print() {
  2.    System.out.println("The player is at " + x + ", " + y);
  3. }
then running the code
Expand|Select|Wrap|Line Numbers
  1. playerObject = new Player(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
  2. playerObject.print();
  3. x++;
  4. playerObject.print();
in the Main class would result in the output
Expand|Select|Wrap|Line Numbers
  1. The player is at 2, 1
  2. 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:
Expand|Select|Wrap|Line Numbers
  1. public void updatePos(int x, int y) {
  2.    this.x = x;
  3.    this.y = y;
  4. }
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
Expand|Select|Wrap|Line Numbers
  1. public void incrementX() {...}
  2. public void decrementX() {...}
  3. public void incrementY() {...}
  4. 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:
Expand|Select|Wrap|Line Numbers
  1. map01[x][y] = 0;
  2. // change x and/or y here
  3. map01[x][y] = 1;
May 28 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.