• 周一. 12月 11th, 2023

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

snake game in java – Java From 5gw.org

[db:作者]

4月 9, 2022

xmlns=”http://www.w3.org/1999/xhtml” dir=”ltr” lang=”en”>
snake game in java – Java
/**
* Bytes CSS
* Style: ‘bytes.answers’; Style ID: 18
*/
@import url(“/clientscript/vbulletin_css/style-12635a82-00018.css”);

 
 
470,370 Members | 1,562 Online

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. //paint method
  7. public void paint(Graphics g){ 
  8.     levelObject.paint(g, playerObject);
  9. … 
  10.  

May 27 ’13
#1

5 2319


(adsbygoogle = window.adsbygoogle || []).push({});

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. }

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. //main game loop
  7.  public void actionPerformed(ActionEvent e){
  8.         playerObject.testMethod();
  9.  
  10.         repaint();
  11.     }
  12.  
  13. //paint method
  14. public void paint(Graphics g){ 
  15.     levelObject.paint(g, playerObject);
  16. … 

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


Message
 
Cancel Changes

Post your reply

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

Similar topics


23 posts

views

Thread by BlackHawke |
last post: by


14 posts

views

Thread by Pete Wood |
last post: by


15 posts

views

Thread by Michael Rybak |
last post: by


2 posts

views

Thread by Hans Kamp |
last post: by


1 post

views

Thread by Nathaniel |
last post: by


19 posts

views

Thread by foolsmart2005 |
last post: by


reply

views

Thread by perto1 |
last post: by

By using this site, you agree to our Privacy Policy and Terms of Use.


(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);

ga(‘create’, ‘UA-90111-7’, ‘bytes.com’);
ga(‘send’, ‘pageview’);

loadBytesTracking()

//0){location.replace(‘https://bytes.com/showthread.php?p=’+cpostno);};} }

if(typeof window.orig_onload == “function”) window.orig_onload();
}

//]]>

《snake game in java – Java From 5gw.org》有38个想法

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注