473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

snake game in java

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
5 2612
Nepomuk
3,112 Expert 2GB
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
game2d
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
3,112 Expert 2GB
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
game2d
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
3,112 Expert 2GB
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

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

Similar topics

21
by: BlackHawke | last post by:
My name is Nick Soutter, I own a small game development company (www.aepoxgames.net) making our first game (www.andromedaonline.net) in java. I am writing because we are having a very...
23
by: BlackHawke | last post by:
Hello! This is my second post. Ppl really helped me with the first. I hope there are answers for this one as well I own a game company (www.aepoxgames.net) releasing the beta for our first...
14
by: Pete Wood | last post by:
Hi All, Does anyone have any *unbiased* views (ie. hard facts) as to why .NET would be better than Java for the development of Enterprise applications? Best Regards, -- Pete Wood ..NET...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
2
by: Hans Kamp | last post by:
As a programming exercise I try to program a game. In my case it is called BugEater. The purpose is that you are a snake. It moves automatically but you can control it with the keys on your...
1
by: Nathaniel | last post by:
A few stings ago I asked: I play an online game application that displays how many points I have earned. I want the number of points I have earned to appear as a string in a text box in the...
1
by: yourun | last post by:
hi frens i have to submit my project work on this topi please help me
19
by: foolsmart2005 | last post by:
I have written a snake game. There are 2 levels in the game(I finished 1st level). It can run in VC++ without problem but, when I run it on the dev C++ 4.9.9.2, it cannot run. I want to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.