array problem in java "Snake game"

Reply

Join Date: Oct 2006
Posts: 7
Reputation: MNR is an unknown quantity at this point 
Solved Threads: 0
MNR MNR is offline Offline
Newbie Poster

array problem in java "Snake game"

 
0
  #1
Oct 16th, 2006
Hi,
I have to make a snake game. i need an array to store allposition of the snakes body. i will also need a variable to store the number of parts in the snakes body. I know that i have to use the number of parts ++ but i dont know where i have to put this, nor where to declare it.
Im pretty sure it should be in SnakeClass but not sure how to pass a parameter from SnakeClass to SnakeJPanel Class.
I first triedto write a method in SnakeClass but i could not pass the parameter to Jpanel class , (Somehow i was getting a compile error).
Below is my work i have done so far, so please help.



  1. **
  2. * @author insert your name here
  3. * @version S2, 2006
  4. */
  5. import java.awt.*;
  6. public class Snake {
  7. private static final int MAX_BODY_PARTS = 1000;
  8. private int direction;
  9. private int left, top;
  10. private Rectangle[] rects ;
  11. private int numberOfParts;
  12. public Snake () {
  13. left = SnakeConstants.SNAKE_START_X;
  14. top = SnakeConstants.SNAKE_START_Y;
  15. direction = SnakeConstants.SNAKE_RIGHT;
  16. rects = new Rectangle[1000];
  17. rects[0] = new Rectangle( SnakeConstants.SNAKE_START_RECT );
  18. numberOfParts = 1;
  19. }
  20. public void add(){
  21. numberOfParts++;
  22. }
  23. public void drawSnake( Graphics g ) {
  24. g.setColor( Color.RED );
  25. g.fillRect( left, top, SnakeConstants.SIZE, SnakeConstants.SIZE );
  26. Rectangle r;
  27. for (int i=0; i<numberOfParts; i++ ) {
  28. r = rects[i];
  29. g.setColor( Color. GREEN);
  30. g.fillRect( r.x, r.y, r.width, r.height );
  31. }
  32. }
  33. public void move() {
  34. int previousX = left;
  35. int previousY = top;
  36. int leftSnake = rects[numberOfParts-1].x;
  37. int topSnake = rects[numberOfParts-1].y;
  38. if ( direction == SnakeConstants.SNAKE_UP ) {
  39. top = top - SnakeConstants.SIZE;
  40. } else if ( direction == SnakeConstants.SNAKE_DOWN ) {
  41. top = top + SnakeConstants.SIZE;
  42. } else if ( direction == SnakeConstants.SNAKE_RIGHT ) {
  43. left = left + SnakeConstants.SIZE;
  44. } else if ( direction == SnakeConstants.SNAKE_LEFT ) {
  45. left = left - SnakeConstants.SIZE;
  46. }
  47. rects[numberOfParts] = new Rectangle( leftSnake, topSnake, SnakeConstants.SIZE, SnakeConstants.SIZE );
  48. if ( intersectsBoundary() ) {
  49. left = previousX;
  50. top = previousY;
  51. }
  52. }
  53. public void setDirection( int dir ) {
  54. direction = dir;
  55. }
  56. public boolean intersectsBoundary() {
  57. Rectangle snakeR = new Rectangle( left, top, SnakeConstants.SIZE, SnakeConstants.SIZE );
  58. return snakeR.intersects( SnakeConstants.NORTH_BOUNDARY )|| snakeR.intersects( SnakeConstants.SOUTH_BOUNDARY )|| snakeR.intersects( SnakeConstants.WEST_BOUNDARY )|| snakeR.intersects( SnakeConstants.EAST_BOUNDARY ) ;
  59. }
  60. }

  1. /**
  2.   *
  3.   * @author Adriana Ferraro
  4.   * @version S2, 2006
  5. */
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. public class SnakeJPanel extends JPanel implements ActionListener, MouseListener, KeyListener {
  10. private Timer t;
  11. private Snake snake;
  12. private YumYum yumyum;
  13. public SnakeJPanel() {
  14. t = new Timer( 200, this );
  15. snake = new Snake();
  16. yumyum = new YumYum();
  17. addKeyListener( this );
  18. addMouseListener( this );
  19. }
  20. //-------------------------------------------------------------------
  21. //-------- Move the Snake object and, after the move, ---------------
  22. //-------- check that it has not collided with the -----------------
  23. //-------- NoNos. Also check if the snake --------------------------
  24. //-------- has eaten the YumYum object ------------------------------
  25. //-------------------------------------------------------------------
  26. public void actionPerformed( ActionEvent e ) {
  27. snake.move();
  28. repaint();
  29. }
  30. //-------------------------------------------------------------------
  31. //-------- Draw all the Snake game objects. -------------------------
  32. //-------------------------------------------------------------------
  33. public void paintComponent( Graphics g ) {
  34. super.paintComponent( g );
  35. Rectangle gameArea = SnakeConstants.GAME_AREA;
  36.  
  37.  
  38.  
  39. g.setColor(Color.WHITE);
  40. g.fillRect( gameArea.x, gameArea.y, gameArea.width, gameArea.height );
  41. drawGrid(g);
  42. snake.drawSnake(g);
  43. yumyum.drawYum(g);
  44. }
  45. //-------------------------------------------------------------------
  46. //-------- Draw a grid which helps to check that --------------------
  47. //-------- the snake game objects are lined up. --------------------
  48. //-------------------------------------------------------------------
  49. private void drawGrid(Graphics g ) {
  50. int jump = SnakeConstants.SIZE;
  51. int x = SnakeConstants.TOP_LEFT.x;
  52. int y = SnakeConstants.TOP_LEFT.y;
  53. int endX = SnakeConstants.TOP_RIGHT.x;
  54. int endY = SnakeConstants.TOP_RIGHT.y + SnakeConstants.HEIGHT;
  55. for( ; x<=endX; ) {
  56. g.setColor(Color.LIGHT_GRAY);
  57. g.drawLine( x, y, x, endY );
  58. if ( x == 0 || x==endX ) {
  59. g.setColor(Color.BLACK);
  60. g.drawLine( x, y, x, endY );
  61. }
  62. x = x + jump;
  63. }
  64. x = SnakeConstants.TOP_LEFT.x;
  65. for( ; y<=endY; ) {
  66. g.setColor(Color.LIGHT_GRAY);
  67. g.drawLine( x, y, endX, y );
  68. if ( y == 0 || y==endY ) {
  69. g.setColor(Color.BLACK);
  70. g.drawLine( x, y, endX, y );
  71. }
  72. y = y + jump;
  73. }
  74. }
  75. //-------------------------------------------------------------------
  76. //-------- When a key is pressed the snake starts. ------------------
  77. //-------- The arrow keys change the direction of the ---------------
  78. //-------- Snake object. --------------------------------------------
  79. //-------------------------------------------------------------------
  80. public void keyPressed( KeyEvent e ) {
  81. t.start();
  82. int code = e.getKeyCode();
  83. if ( code == KeyEvent.VK_UP ) {
  84. snake.setDirection( SnakeConstants.SNAKE_UP );
  85. } else if ( code == KeyEvent.VK_DOWN ) {
  86. snake.setDirection( SnakeConstants.SNAKE_DOWN );
  87. } else if ( code == KeyEvent.VK_LEFT) {
  88. snake.setDirection( SnakeConstants.SNAKE_LEFT );
  89. } else if ( code == KeyEvent.VK_RIGHT ) {
  90. snake.setDirection( SnakeConstants.SNAKE_RIGHT );
  91. }
  92. }
  93. public void keyReleased( KeyEvent newEvent ) { }
  94. public void keyTyped( KeyEvent newEvent ) { }
  95. public void mousePressed( MouseEvent newEvent ) {
  96. requestFocusInWindow();
  97. }
  98. public void mouseReleased( MouseEvent newEvent ) { }
  99. public void mouseClicked( MouseEvent newEvent ) { }
  100. public void mouseEntered( MouseEvent newEvent ) { }
  101. public void mouseExited( MouseEvent newEvent ) { }
  102. }
  103. /*
  104. */
Attached Files
File Type: zip Snakegame.zip (3.2 KB, 94 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 101
Reputation: cms271828 is an unknown quantity at this point 
Solved Threads: 4
cms271828 cms271828 is offline Offline
Junior Poster

Re: array problem in java "Snake game"

 
0
  #2
Oct 16th, 2006
I put the code in Netbeans, but you've still got a few classes to write.
Theres loads of ways of representing the snake.
You could use a double array of ints, then if a square is 0, its empty, if its 1, its part of the snake, 2 could be food.
If your variables are static, which might help for some of them, then you just call them/update them using the class name and '.'.
Not much I can do with your program at the mo, because too much stuff missing and I don't really understand how you're trying to do it exactly.
I recommend keeping variables collected into their own groups, of course sometimes you will need a variable from another class, but if your unnecesarily throwing variables from one class to another things can get messy and its harder to debug that way.
Let me know how you get on
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: array problem in java "Snake game"

 
0
  #3
Oct 16th, 2006
I'm not sure how object orientated this is but it may give you a few ideas perhaps...

http://javaboutique.internet.com/Snake/index.html
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC