Snake game using arrays

very simple snake game

Page 1 of 1

4 Replies - 8461 Views - Last Post: 01 April 2009 - 04:48 PM Rate Topic: -----

#1 vtc08  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 31-March 09

Snake game using arrays

Post icon  Posted 31 March 2009 - 04:53 PM

Hi i am having trouble figuring out where to start for my snake game i have to complete for an intro to java class, i am familiar with terms so plz help me, all other parts of this program are complete, except for in the Snake class.
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[i]=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() {
 /******  YOUR CODE HERE!!! ******/
  }
  
  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);
	}
  }
}


Please help me complete the move method

This post has been edited by pbl: 31 March 2009 - 05:01 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Snake game using arrays

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Snake game using arrays

Posted 31 March 2009 - 04:59 PM

I wrote a Snake like that.... 10 years ago :-)

You do what you want but this the way I did it:

Create a JPanel with a GridLayout of 100X100 or whatever you want the size oy your area to be
Fill all the slots with JLabel (do not forget to make them opaque label[i].setOpaque(true);)

Then you do not need any paint() method Swing will do it for you... just change the background color of the labels where the snake is
For the food... just set the JLabel to another color

Happy coding

P.S.
this post will be considered "Beginner"

This post has been edited by pbl: 31 March 2009 - 05:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 vtc08  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 31-March 09

Re: Snake game using arrays

Posted 31 March 2009 - 05:10 PM

i already have all the other parts i have ...displaywindow.java, GameDriver.java and DrawGame.java and SnakeSection.java, i just need to be able to make the move section
Was This Post Helpful? 0
  • +
  • -

#4 vtc08  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 31-March 09

Re: Snake game using arrays

Posted 31 March 2009 - 05:32 PM

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

DrawGame.java
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);
  }
}

Displaywindow.java
import java.awt.*;
import javax.swing.*;

public  class DisplayWindow extends JFrame{

  private Container c;

  public DisplayWindow(){
	super("Display");
	c = this.getContentPane();
  }

  public void addPanel(JPanel p){
	p.setPreferredSize(new Dimension(400,400));
	c.add(p);
  }

  public void showFrame(){
	this.pack();
	this.setVisible(true);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

gamedriver.java
public class GameDriver{
 
  public static void main(String[] args){
	DisplayWindow d = new DisplayWindow();
	DrawGame p = new DrawGame();
	d.addPanel(p);
	d.showFrame();
  }
}

Was This Post Helpful? 0
  • +
  • -

#5 vtc08  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 31-March 09

Re: Snake game using arrays

Posted 01 April 2009 - 04:48 PM

bump
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1