| | |
array problem in java "Snake game"
![]() |
•
•
Join Date: Oct 2006
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
Java Syntax (Toggle Plain Text)
** * @author insert your name here * @version S2, 2006 */ import java.awt.*; public class Snake { private static final int MAX_BODY_PARTS = 1000; private int direction; private int left, top; private Rectangle[] rects ; private int numberOfParts; public Snake () { left = SnakeConstants.SNAKE_START_X; top = SnakeConstants.SNAKE_START_Y; direction = SnakeConstants.SNAKE_RIGHT; rects = new Rectangle[1000]; rects[0] = new Rectangle( SnakeConstants.SNAKE_START_RECT ); numberOfParts = 1; } public void add(){ numberOfParts++; } public void drawSnake( Graphics g ) { g.setColor( Color.RED ); g.fillRect( left, top, SnakeConstants.SIZE, SnakeConstants.SIZE ); Rectangle r; for (int i=0; i<numberOfParts; i++ ) { r = rects[i]; g.setColor( Color. GREEN); g.fillRect( r.x, r.y, r.width, r.height ); } } public void move() { int previousX = left; int previousY = top; int leftSnake = rects[numberOfParts-1].x; int topSnake = rects[numberOfParts-1].y; if ( direction == SnakeConstants.SNAKE_UP ) { top = top - SnakeConstants.SIZE; } else if ( direction == SnakeConstants.SNAKE_DOWN ) { top = top + SnakeConstants.SIZE; } else if ( direction == SnakeConstants.SNAKE_RIGHT ) { left = left + SnakeConstants.SIZE; } else if ( direction == SnakeConstants.SNAKE_LEFT ) { left = left - SnakeConstants.SIZE; } rects[numberOfParts] = new Rectangle( leftSnake, topSnake, SnakeConstants.SIZE, SnakeConstants.SIZE ); if ( intersectsBoundary() ) { left = previousX; top = previousY; } } public void setDirection( int dir ) { direction = dir; } public boolean intersectsBoundary() { Rectangle snakeR = new Rectangle( left, top, SnakeConstants.SIZE, SnakeConstants.SIZE ); return snakeR.intersects( SnakeConstants.NORTH_BOUNDARY )|| snakeR.intersects( SnakeConstants.SOUTH_BOUNDARY )|| snakeR.intersects( SnakeConstants.WEST_BOUNDARY )|| snakeR.intersects( SnakeConstants.EAST_BOUNDARY ) ; } }
Java Syntax (Toggle Plain Text)
/** * * @author Adriana Ferraro * @version S2, 2006 */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SnakeJPanel extends JPanel implements ActionListener, MouseListener, KeyListener { private Timer t; private Snake snake; private YumYum yumyum; public SnakeJPanel() { t = new Timer( 200, this ); snake = new Snake(); yumyum = new YumYum(); addKeyListener( this ); addMouseListener( this ); } //------------------------------------------------------------------- //-------- Move the Snake object and, after the move, --------------- //-------- check that it has not collided with the ----------------- //-------- NoNos. Also check if the snake -------------------------- //-------- has eaten the YumYum object ------------------------------ //------------------------------------------------------------------- public void actionPerformed( ActionEvent e ) { snake.move(); repaint(); } //------------------------------------------------------------------- //-------- Draw all the Snake game objects. ------------------------- //------------------------------------------------------------------- public void paintComponent( Graphics g ) { super.paintComponent( g ); Rectangle gameArea = SnakeConstants.GAME_AREA; g.setColor(Color.WHITE); g.fillRect( gameArea.x, gameArea.y, gameArea.width, gameArea.height ); drawGrid(g); snake.drawSnake(g); yumyum.drawYum(g); } //------------------------------------------------------------------- //-------- Draw a grid which helps to check that -------------------- //-------- the snake game objects are lined up. -------------------- //------------------------------------------------------------------- private void drawGrid(Graphics g ) { int jump = SnakeConstants.SIZE; int x = SnakeConstants.TOP_LEFT.x; int y = SnakeConstants.TOP_LEFT.y; int endX = SnakeConstants.TOP_RIGHT.x; int endY = SnakeConstants.TOP_RIGHT.y + SnakeConstants.HEIGHT; for( ; x<=endX; ) { g.setColor(Color.LIGHT_GRAY); g.drawLine( x, y, x, endY ); if ( x == 0 || x==endX ) { g.setColor(Color.BLACK); g.drawLine( x, y, x, endY ); } x = x + jump; } x = SnakeConstants.TOP_LEFT.x; for( ; y<=endY; ) { g.setColor(Color.LIGHT_GRAY); g.drawLine( x, y, endX, y ); if ( y == 0 || y==endY ) { g.setColor(Color.BLACK); g.drawLine( x, y, endX, y ); } y = y + jump; } } //------------------------------------------------------------------- //-------- When a key is pressed the snake starts. ------------------ //-------- The arrow keys change the direction of the --------------- //-------- Snake object. -------------------------------------------- //------------------------------------------------------------------- public void keyPressed( KeyEvent e ) { t.start(); int code = e.getKeyCode(); if ( code == KeyEvent.VK_UP ) { snake.setDirection( SnakeConstants.SNAKE_UP ); } else if ( code == KeyEvent.VK_DOWN ) { snake.setDirection( SnakeConstants.SNAKE_DOWN ); } else if ( code == KeyEvent.VK_LEFT) { snake.setDirection( SnakeConstants.SNAKE_LEFT ); } else if ( code == KeyEvent.VK_RIGHT ) { snake.setDirection( SnakeConstants.SNAKE_RIGHT ); } } public void keyReleased( KeyEvent newEvent ) { } public void keyTyped( KeyEvent newEvent ) { } public void mousePressed( MouseEvent newEvent ) { requestFocusInWindow(); } public void mouseReleased( MouseEvent newEvent ) { } public void mouseClicked( MouseEvent newEvent ) { } public void mouseEntered( MouseEvent newEvent ) { } public void mouseExited( MouseEvent newEvent ) { } } /* */
•
•
Join Date: Oct 2006
Posts: 101
Reputation:
Solved Threads: 4
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
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
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
http://javaboutique.internet.com/Snake/index.html
*Voted best profile in the world*
![]() |
Similar Threads
- Please help me with a java terminal snake game (Java)
- Snake Game (Pascal and Delphi)
- class array problem! (C++)
- array required, but java.lang.String found (Java)
- [MERGED]can anybody help me with this problem; snake game (Java)
Other Threads in the Java Forum
- Previous Thread: Mortgage Program
- Next Thread: basic file i/o
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linked linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting rotatetext scanner se server service set sms software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






