Results 1 to 2 of 2
  1. #1
    AndySun94 is offline Member
    Join Date
    Mar 2012
    Rep Power

    Default Help needed with simple Java Snake game

    Hello ladies and gentleman
    This being my first post on these forums I'm looking forward to becoming involved in this community.

    That being said, I have an issue that I cannot solve on my own.
    The following is a part of a university project to program a simple game, I have completed all necessary parts of the program but it refuses to work.
    Java Code:
    package Snake;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.util.*;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    public class Board extends JPanel implements ActionListener{
    	private boolean gameOver = false;
    	private Timer clock;
    	private int delay = 1000;
    	private enum directions {up, down, left, right};
    	private directions dir = directions.right;
    	Snake snake = new Snake();
    	Apple apple = new Apple();
    	private Image appleIcon; //= new ImageIcon("apple.jpg").getImage();
    	private Image snakeBody; //= new ImageIcon("snakeBody.jpg").getImage();
    	private Image snakeHead; //= new ImageIcon("snakeHead.jpg").getImage();
    	private Dimension dim = new Dimension(500, 500);
    	private int pixelSize = 10;
    	public Board(){
    		ImageIcon iid = new ImageIcon("snakeBody.jpg");
            snakeBody = iid.getImage();
            ImageIcon iia = new ImageIcon("appleIcon.jpg");
            appleIcon = iia.getImage();
            ImageIcon iih = new ImageIcon("snakeHead.png");
            snakeHead = iih.getImage();
    		addKeyListener(new keyboard());
    	public void startGame(){
    		clock = new Timer(delay, this);
    	public void actionPerformed(ActionEvent arg0) {
    		if (!gameOver){
    	public void checkApple(){
    		if (snake.x[0]==apple.getX() && snake.y[0]==apple.getY()){
    	public void checkCollision(){
    		for (int i = snake.getLength(); i > 0; i--) {	
    			if ((i > 4) && (snake.x[0] == snake.x[i]) && (snake.y[0] == snake.y[i])) {
    	            gameOver = true;
    		if (snake.y[0]>600 || snake.y[0]<0 || snake.x[0]>600 || snake.x[0]<0){
    			gameOver = true;
    	public void move(){
    		System.out.println(snake.x[0]+" "+snake.y[0]);
    		for (int i= snake.getLength(); i>0; i--){
    			snake.y[i] = snake.y[i-1];
    			snake.x[i] = snake.x[i-1];
    		if (dir == directions.up){
    			snake.y[0] -= pixelSize;
    		if (dir == directions.down){
    			snake.y[0] += pixelSize;
    		if (dir == directions.left){
    			snake.x[0] -= pixelSize;
    		if (dir == directions.right){
    			snake.x[0] += pixelSize;
    	public void levelUp(){
    	public void relocateApple(){
    		Random rdm = new Random();
    		int newX = rdm.nextInt(50)*pixelSize;
    		int newY = rdm.nextInt(50)*pixelSize;
    		apple.setLocation(newX, newY);
    	public void paint(Graphics g){
    		if (!gameOver){
    			g.drawImage(snakeHead, snake.x[0], snake.y[0], this);
    			g.drawImage(appleIcon, apple.getX(), apple.getY(), this);
    			for (int i=1; i<snake.getLength(); i++){
    				g.drawImage(snakeBody, snake.x[i], snake.y[i], this);
    	private class keyboard extends KeyAdapter{
    		public void keyPressed(KeyEvent e){
    			int key = e.getKeyCode();		
    			if (key == KeyEvent.VK_LEFT && dir!=directions.right){
    				dir = directions.left;
    			}else if(key == KeyEvent.VK_RIGHT && dir!=directions.left){
    				dir = directions.right;
    			}else if(key == KeyEvent.VK_UP && dir!=directions.down){
    				dir = directions.up;
    			}else if(key == KeyEvent.VK_DOWN && dir!=directions.down){
    				dir = directions.down;
    The 2 main issues I have with this are 1) Nothing is painted on the JPanel, it just shows the background color and 2) I've added in a line to test the functionality of the keyboard listener by letting the "move()" method print out the coordinates of the snake head every timer tick, however the listener does not seem to be registering my keys as the coordinates move in a single direction regardless of if or which keys I press.

    If anyone can give me any tips it would be greatly appreciated.

    Thanks, Andy

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Help needed with simple Java Snake game

    Posting uncompilable code rarely leads to a conclusive solution. To get better help sooner, post a [_SSCCE_|] that clearly demonstrates your problem.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Snake Game
    By LuluMM in forum New To Java
    Replies: 0
    Last Post: 03-08-2012, 06:48 AM
  2. Replies: 1
    Last Post: 03-05-2012, 10:44 AM
  3. Replies: 4
    Last Post: 01-24-2012, 03:51 PM
  4. Snake Game in Java
    By Shyamz1 in forum New To Java
    Replies: 4
    Last Post: 02-10-2011, 02:49 PM
  5. Snake game in java
    By freaky in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 06:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts