Beginning of snake game


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Beginning of snake game

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    TX
    Posts
    23

    Question Beginning of snake game

    I'm trying to make a snake game equivalent to the one found on many cell phones.

    I'm having trouble getting my snake to be drawn correctly according to keyboard events. The keyboard events fire and my program picks up on the events but the snake is not drawn correctly or really at all for that case. The keys to control the snake are the four arrow keys. My problem lies in using the Graphics object-- i'm not exactly sure what to do with it.

    Thanks for the help!!

    HTML Code:
    //SnakePane.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    
    public class SnakePanel extends JPanel implements KeyListener{
    	Snake snake;
    	Graphics g;
    	
    	public SnakePanel(){
    		super();
    		this.setBackground(Color.yellow);
    		snake = new Snake(Color.black);
    		addKeyListener(this);
    		//give SnakePanel class keyboard focus
    		this.setFocusable(true);
    	}
    	
    	//KeyListener interface methods
    	public void keyPressed(KeyEvent e){
    		System.out.println("keyPressed");
    		
    		update(g, e);
    	}
    	
    	public void keyReleased(KeyEvent e){
    		System.out.println("keyReleased");
    	}
    		
    	public void keyTyped(KeyEvent e){
    		System.out.println("keyTyped");
    	}
    	
    	public void paintComponent(Graphics graphics){
    		System.out.println("paint");
    		g = graphics;
    		//display snake for the first time
    		snake.drawSnake(g);
    	}
    	
    	//overload update() method
    	public void update(Graphics g, KeyEvent e){
    		int keyCode = e.getKeyCode();
    		
    		//get arrow key
    		switch(keyCode){
    			case KeyEvent.VK_UP:
    			//System.out.println("moveUp");
    				snake.moveUp(g);
    				break;
    			case KeyEvent.VK_DOWN:
    				snake.moveDown(g);
    				break;
    			case KeyEvent.VK_RIGHT:
    				snake.moveRight(g);
    				break;
    			case KeyEvent.VK_LEFT:
    				snake.moveLeft(g);
    				break;
    		}
    	}
    		
    	public static void main(String[] args){
    		JFrame frame = new JFrame("Snake");
    		frame.getContentPane().add(new SnakePanel());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(200,200);
    		frame.setLocation(100,100);
    		frame.setVisible(true);
    	}
    
    }
    *****************************
    And below is the Snake.java class**
    *****************************

    HTML Code:
    import java.awt.*;
    
    public class Snake{
    	int xPos, yPos;
    	int size = 10;
    	Color color;
    	
    	public Snake(Color col){
    		xPos = 50;
    		yPos = 50;
    		color = col;
    	}
    	
    	public void drawSnake(Graphics g){
    		g.setColor(Color.black);
    		g.fillRect(xPos,yPos,size,size);
    	}
    	
    	public void moveUp(Graphics g){
    		g.setColor(Color.red);
    		//decrement yPos
    		yPos = yPos - size;
    		g.fillRect(xPos, yPos, size, size);
    	}
    	
    	public void moveRight(Graphics g){
    		g.setColor(Color.red);
    		xPos = xPos + size;
    		g.fillRect(xPos, yPos, size, size);
    	}
    	
    	public void moveLeft(Graphics g){
    		g.setColor(Color.red);
    		xPos = xPos - size;
    		g.fillRect(xPos, yPos, size, size);
    	}
    	
    	public void moveDown(Graphics g){
    		g.setColor(Color.red);
    		yPos = yPos + size;
    		g.fillRect(xPos, yPos, size, size);
    	}
    }

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Have you tried adding println() statements to your code to see how the code flows and where it goes?

    Are the various draw methods being called? Is your update() method being called?

  3. #3
    Join Date
    Sep 2005
    Location
    TX
    Posts
    23

    Graphics

    Well I don't understand java.awt.Graphics;

    In my code SnakePanel.java I have an instance variable Graphics g;
    Then in my paintComponent(Graphics graphics) method (SnakePanel extends JPanel) I say: "g = graphics"

    Then, in my snake class I have methods to move the snake:
    i.e.: void moveUp(Graphics g){
    g.setColor(Color.red);
    g.fillRect(newXPos, newYPos, snakeSize, snakeSize);
    }

    So in the update method of my SnakePanel class I get the keyboard events and say if the up arrow key is hit then I call snake.moveUp(g); snake is an instance of the Snake.java class.

    This is the g referred to above.

    I don't know if this is how to do this or if maybe I should create a graphics object in the snake class?
    Also, since this is a simple game I didn't see a need for double-buffering. Do you think that is needed to counter flickering or will there likely be no flickering?

    Thanks a lot!

  4. #4
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    I've never tried saving the Graphics object passed to the paintxxx() method to use in other methods. I've always done all the painting from the paint() method (I code only AWT not Swing).
    A listener detects a movement, sets some global variables and calls repaint() which sometime later will cause a call to paint() where the global variables are inspected and the necessary painting done. It might be slightly different in Swing.

Similar Threads

  1. Replies: 3
    Last Post: 03-09-2005, 12:24 AM
  2. Snake game applet.
    By Alex in forum Java
    Replies: 0
    Last Post: 08-11-2001, 05:53 PM
  3. Snake game On-Line. Free java sources.
    By Alex in forum web.announcements
    Replies: 0
    Last Post: 08-09-2001, 04:01 PM
  4. Snake game On-Line. Free java sources.
    By Alex in forum java.announcements
    Replies: 0
    Last Post: 08-09-2001, 03:55 PM
  5. Framework of a complex game...
    By AGuy in forum Java
    Replies: 0
    Last Post: 07-31-2000, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center