Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Snake GUI Logic

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Snake GUI Logic

    public class snake extends JPanel implements ActionListener {
     
    	//declare variables used in the beginning. by making them private, they can be seen only by the classes they belong to 
    	//sets the screen width to be 400
    	private int screenWidth = 400;
    	//sets the screen height to be 400
    	private int screenHeight = 400;
    	//sets the size of each dot to be 10
    	private int dotSize = 10;
    	//sets the max number of dots that can be on the screen (400*400/10*10)
    	private int maxDots = 1600;
    	//used to randomize a dot to appear on screen
    	private int randomPosition;
    	//speed of the game
    	private int delay = 200;
     
    	//creates an array of all of the x coordinates of the snake that can be on screen
    	private int x [] = new int [maxDots];
    	//creates an array of all of the y coordinates of the snake that can be on the screen
    	private int y [] = new int [maxDots];
    	//creates variable for the dots on snake
    	private int dots;
    	//the x coordinate for the current edible dot
    	private int edibleDotX;
    	//the y coordinate for the current edible dot
    	private int edibleDotY;
     
    	//boolean for the left direction of the snake
    	private boolean left = false;
    	//boolean for the right direction of the snake
    	private boolean right = false;
    	//boolean for the up direction of the snake set to true as it starts going up
    	private boolean up = true;
    	//boolean for the down direction of the snake
    	private boolean down = false;
     
     
    	//boolean for whether the game should continue running or end
    	private boolean inGame = true;
     
    	//add a timer object
    	private Timer timer;
     
    	//image variable for the dots. accessible only by the class that needs it
    	private Image dotSprite;
    	//image variable for snake head. accessible only by the class that needs it
    	private Image snakeHead;
     
    	private Image snakeBody;
     
    	//initializes the screen. this method is public because everything is displayed here
    	public snake () {
     
    		JFrame screen = new JFrame ("Snake");
    		screen.setVisible (true);
    		screen.setBackground (Color.black);
    		screen.setSize (screenWidth, screenHeight);
    		screen.setResizable (false);
     
    		loadImages ();
    		Initialize ();
     
    	}
     
    	private void loadImages () {
     
     
    		ImageIcon ish = null;
    		try {
    			ish = new ImageIcon (ImageIO.read(snake.class.getResource("/snakehead.png")));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		snakeHead = ish.getImage ();
     
     
    		ImageIcon id = null;
    		try {
    			id = new ImageIcon (ImageIO.read(snake.class.getResource ("/dot.png")));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		dotSprite = id.getImage ();
     
     
    		ImageIcon isb = null;
    		try {
    			isb = new ImageIcon (ImageIO.read(snake.class.getResource ("/snakebody.png")));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		snakeBody = isb.getImage ();
     
     
     
    	}
     
    	private void Initialize () {
     
    		dots = 1;
    		 for (int z = 0; z < dots; z++) {
    	            x[z] = 50 - z * 10;
    	            y[z] = 50;
    	        }
     
    		 randomEdibleDot ();
     
    		 timer = new Timer (delay, this);
    		 timer.start ();
     
    	}
     
    	@Override
    	 public void paintComponent(Graphics g) {
    	        super.paintComponent(g);
     
    	        doDrawing(g);
    	    }
     
    	    private void doDrawing(Graphics g) {
     
    	        if (inGame = true) {
     
    	            g.drawImage(dotSprite, edibleDotX, edibleDotY, this);
     
    	            for (int z = 0; z < dots; z++) {
    	                if (z == 0) {
    	                    g.drawImage(snakeHead, x[z], y[z], this);
    	                } else {
    	                    g.drawImage(snakeBody, x[z], y[z], this);
    	                }
    	            }
     
    	            Toolkit.getDefaultToolkit().sync();
    	            g.dispose();
     
    	        } else {
     
    	            gameOver();
    	        }        
    	    }
     
     
    	    private void gameOver () {
    	    	JOptionPane
    			.showMessageDialog(
    					null,
    					"Game Over");
    	    }
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		snake screenShow = new snake ();
     
    	}
     
     
    	private void randomEdibleDot () {
     
    		//randomly positions a dot in a new place on the board. 
    		  int r = (int) (Math.random() * randomPosition);
    	        edibleDotX = ((r * dotSize));
     
    	        r = (int) (Math.random() * randomPosition);
    	        edibleDotY = ((r * dotSize));
     
    	}
     
    	private void checkDot() {
     
            if ((x[0] == edibleDotX) && (y[0] == edibleDotY)) {
     
                dots++;
                randomEdibleDot();
            }
        }
     
    	private void moving () {
     
    		 for (int z = dots; z > 0; z--) {
    	            x[z] = x[(z - 1)];
    	            y[z] = y[(z - 1)];
    	        }
     
    	        if (left) {
    	            x[0] -= dotSize;
    	        }
     
    	        if (right) {
    	            x[0] += dotSize;
    	        }
     
    	        if (up) {
    	            y[0] -= dotSize;
    	        }
     
    	        if (down) {
    	            y[0] += dotSize;
    	        }
    	    }
     
    	private void checkDotCollision () {
     
     
    		for (int z = dots; z > 0; z--) {
     
                if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
                    inGame = false;
                }
            }
     
            if (y[0] > screenHeight) {
                inGame = false;
            }
     
            if (y[0] < 0) {
                inGame = false;
            }
     
            if (x[0] > screenWidth) {
                inGame = false;
            }
     
            if (x[0] < 0) {
                inGame = false;
            }
    	}
     
     
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
     
    		if (inGame = true) {
     
                checkDot();
                checkDotCollision();
                moving();
            }
     
            repaint();
        }
     
        private class TAdapter extends KeyAdapter {
     
            @Override
            public void keyPressed(KeyEvent e) {
     
                int key = e.getKeyCode();
     
                if ((key == KeyEvent.VK_LEFT) && (!right)) {
                    left = true;
                    up = false;
                    down = false;
                }
     
                if ((key == KeyEvent.VK_RIGHT) && (!left)) {
                    right = true;
                    up = false;
                    down = false;
                }
     
                if ((key == KeyEvent.VK_UP) && (!down)) {
                    up = true;
                    right = false;
                    left = false;
                }
     
                if ((key == KeyEvent.VK_DOWN) && (!up)) {
                    down = true;
                    right = false;
                    left = false;
                }
            }
        }
    }

    So I'm intending to recreate Snake. There aren't any errors so it's just the logic I think. I think it's in how my components and stuff are done. But I have no idea what I'm doing wrong. So I put all of my code here minus the import statements. Can anyone figure out what I'm doing wrong?


  2. #2
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: Snake GUI Logic

    Could you explain what the problem is? What is the expected result and what result are you getting?

Similar Threads

  1. [SOLVED] more help with snake game
    By godlynom in forum What's Wrong With My Code?
    Replies: 21
    Last Post: October 4th, 2012, 03:32 PM
  2. [SOLVED] Help with the Snake in a Snake Game
    By godlynom in forum What's Wrong With My Code?
    Replies: 20
    Last Post: September 27th, 2012, 06:41 PM
  3. [Snake]Help
    By Totel in forum Object Oriented Programming
    Replies: 23
    Last Post: September 26th, 2012, 09:17 AM
  4. [Help] Snake algorithm
    By 123099 in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 25th, 2012, 12:14 PM
  5. Inheriting Applet and paint() GUI Method Logic?
    By Jakesta42 in forum Object Oriented Programming
    Replies: 1
    Last Post: August 8th, 2011, 06:58 AM

Tags for this Thread