Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Move() method of Snake Game

807591Apr 14 2008 — edited Apr 14 2008
For this assignment you are supposed to write the move() method of class Snake. A snake is represented as a collection of boxes on the screen. The dimensions of the screen are 20 by 20 such boxes. Therefore, the upper left corner has coordinates (0,0), the lower right corner has coordinates (19,19). The snake object is an array of such coordinates. In the beginning (as defined in the constructor of class Snake) the snake stretches from position (11,10) to (19,10); in other words, it is positioned in the middle-right of the window.

The direction is which a snake is going is defined by two variables: dirX and dirY. The way the direction changes depends on keys pressed (check the keyTyped() method of class DrawGame). The original direction is to the right: dirX = 1; dirY = 0; The change of direction is implemented for you, just assume that dirX and dirY values will be changing during the run of the program.

THIS IS WHAT THEY GAVE:
 import java.awt.*;
import javax.swing.*;
import java.awt.event.*; // needed for event handling

public class Snake {
  
  final int SCREEN_SIZE_X = 20;
  final int SCREEN_SIZE_Y = 20;
  
  final int SNAKE_LENGTH = 10;
  SnakeSection [] snakeSecs = new SnakeSection[SNAKE_LENGTH];
  
  int headIndex = 0;
 
  int dirX = 1;
  int dirY = 0;
  
  public Snake() {
    for (int i=0; i<SNAKE_LENGTH; i++) {
      snakeSecs=new SnakeSection(10+i,10);
}
}

// The idea behind moving the snake is as follows.
// First, the "front" of the snake is the head position.
//
// The only really new part of the snake will be the
// SnakeSection in the head position.
// To obtain the new location of the head, just take the
// previous SnakeSection from the head position and add the
// current direction variables to its coordinates.
//
// Update the rest of the positions if needed.
//
public void move() {

}

public void paint(Graphics g) {
for (int i=0; i<SNAKE_LENGTH; i++) {
g.drawRect(snakeSecs[i].x*20,snakeSecs[i].y*20,20,20);
}
}
} public class SnakeSection {
public int x;
public int y;

public SnakeSection(int x, int y) {
this.x = x;
this.y = y;
}

} {code) import java.awt.*;
import javax.swing.*;
import java.awt.event.*; // needed for event handling
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
import java.awt.BorderLayout;

public class DrawGame extends JPanel implements KeyListener, ActionListener {
// This attribute keeps track of the number of "time steps" the
// program has taken so far. At each time step, the snakes should
// move, we should check for intersections, and check for keyboard events,
// etc. This attribute is really just for convenience in debugging,
// and is not needed for the functioning of the game.
int totalTimeSteps=0;

Toolkit toolkit;
Timer timer;

Snake snake = new Snake();

JButton quit = new JButton("Quit");
JTextField typingArea;

boolean oval = true;

public DrawGame() {
// First thing to do: Start up the periodic task:
System.out.println("About to start the snake.");
startSnake(200); // Argument is number of milliseconds per snake move.
System.out.println("Snake started.");

setBackground(Color.yellow);
this.add(quit); // place button in panel
quit.addActionListener(this); // panel is listener for button

typingArea = new JTextField(20);
typingArea.addKeyListener(this);
add(typingArea, BorderLayout.PAGE_START);

}

public void startSnake(int milliseconds) {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
Date firstTime = new Date(); // Start task now.
timer.schedule(new AdvanceTheSnakeTask(), firstTime, milliseconds);
}

class AdvanceTheSnakeTask extends TimerTask {
public void run() {

// Put stuff here that should happen during every advance.
//toolkit.beep();

snake.move();

repaint();
}
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

snake.paint(g);
}

public void keyTyped(KeyEvent e) {
if (e.getKeyChar()=='a') {
snake.dirX=-1;
snake.dirY=0;
}
if (e.getKeyChar()=='s') {
snake.dirX=1;
snake.dirY=0;
}
if (e.getKeyChar()=='w') {
snake.dirX=0;
snake.dirY=-1;
}
if (e.getKeyChar()=='z') {
snake.dirX=0;
snake.dirY=1;
}
}

public void keyPressed(KeyEvent e) {
}

/** Handle the key released event from the text field. */
public void keyReleased(KeyEvent e) {
}

public void actionPerformed(ActionEvent e){
if (e.getSource() == quit)
System.exit(0);
}
}
this is my answer: I do not know if this is right:
public void move() {
private int numberOfParts;
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;
}
}
is this right?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 12 2008
Added on Apr 14 2008
1 comment
563 views