9 Replies - 37250 Views - Last Post: 01 February 2010 - 03:44 PM Rate Topic: -----

#1 sly  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 05-December 08

Java Snake Game

Posted 31 January 2010 - 03:39 PM

Hi im making a snake game in java, and I have a question about graphics for the snake and the mice. Should I use JLabels for making the snake and mice? would that work? Because there needs to be a way for the snake to increase in size so would JLabels be capable of doing so? Or is it possible to use graphics for instance the snake could be a line that eats a bunch of circles (mice). Movement would be necessary though the keys which i have already implemented.

Please would someone point me in the right direction?

thanks!

Here is my code:


GameFrame.java

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.border.*;
import javax.swing.*;



	public class GameFrame extends JFrame {
	
	   private Movement movement1 = new Movement();
	   private Mouse mousemovement1 = new Mouse();
	
	public GameFrame() {
		
	super("Snake Game"); //Name of the JFrame

	
	
   //adding the key listener
	 this.addKeyListener(movement1);

			Image img = Toolkit.getDefaultToolkit().getImage("C:/Documents and Settings/Administrator/My Documents/My Pictures/snakelogo.GIF");
			  setIconImage(img);   //Image on top left corner
			  
	  
			  
			  
			  Dimension screenSize =			//Location of Frame on the Screen												
				  Toolkit.getDefaultToolkit().getScreenSize();
			   Dimension frameSize = getSize();
			   int x = (screenSize.width - frameSize.width) / 4;	
			   int y = (screenSize.height - frameSize.height) / 5;
			   setLocation(x, y);

			   JPanel jpanel_0 = new JPanel();
			   jpanel_0.setBackground(Color.cyan);
			   jpanel_0.add(new JLabel("Snake Game"));

			  
			   Container cp = getContentPane();
			   cp.add(jpanel_0,BorderLayout.CENTER);
			 

			   setSize(600, 450);
			   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			 
			   
			   
			 setVisible(true); //visibility of JFrame
			  
	}
	
			  public static void main(String[] args) {
				  new GameFrame();
				  
				  
				
				  Snake snake1 = new Snake();
				  
				  
				  
				  
				  
		 
		   }
			  
		}


Mouse.java
public class Mouse {

	
	
}




Movement.java

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class Movement implements KeyListener{

	@Override
	 public void keyPressed(KeyEvent e) 
	{
	  
		if(e.getKeyChar() == 'a')
		{
	   
			System.out.println("you pressed a");
		
		}
		if(e.getKeyChar() == 's')
		{
	   
			System.out.println("you pressed s");
		   
		}
		if(e.getKeyChar() == 'w')
		{
		  
			System.out.println("you pressed w");
		 
		}
		if(e.getKeyChar() == 'd')
		{
		 
			System.out.println("you pressed d");
		
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}



Snake.java

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.border.*;


public class Snake {

}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Snake Game

#2 Sloth  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 31-January 10

Re: Java Snake Game

Posted 31 January 2010 - 05:16 PM

There are a few ways to do this. By convention, a snake is made up of many segments. For each movement, each segment takes the place of the segment in front of it. When it grows, a segment is added to the end. Create something to be segments, such as polygons, points, or a custom object. keep track of these by storing them in an array or list. Some ways are easier than others, or try a few others if you have trouble.

I would have a single variable that keeps track of the snake's direction.(From your code, it seems he'll be constantly moving, as he should). When one of the four keys is pressed, change the variable. Seperately, whenever the snake should move, update the head's position according to the variable. Remember, you'll also need to move each part to the previous one's old position. (part1 takes the head's old spot, part2 takes part1's old spot, etc).

Use some sort of check after each move to see if the snake's head caught a rat. If it did, add a part to it.

Hope this info was helpful. They're just suggestions, so code it whichever way you feel works best.
Was This Post Helpful? 0
  • +
  • -

#3 sly  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 05-December 08

Re: Java Snake Game

Posted 31 January 2010 - 07:23 PM

Im having trouble currently adding graphics to my JPanel, I have created a new class called Snake to display the snake and that is were all of the graphics information on the snake is held....

I reconnect the Snake Class to the class with the main method so that it would show the graphics, but for some reason it doesn't...

I hope you can help!!! thanks!!!


GameFrame.java
import java.awt.*;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.border.*;
import javax.swing.*;



	public class GameFrame extends JFrame {
	
	   private Movement movement1 = new Movement();
	   private Snake snake1 =  new Snake();
	   private Mouse mousemovement1 = new Mouse();
	
	public GameFrame() {
		
	super("Snake Game"); //Name of the JFrame
	
	
	 this.addKeyListener(movement1);	//adding the key listener

			Image img = Toolkit.getDefaultToolkit().getImage("C:/Documents and Settings/Administrator/My Documents/My Pictures/snakelogo.GIF");
			  setIconImage(img);   //Image on top left corner
			  
	  
			  
			  
			  Dimension screenSize =			//Location of Frame on the Screen												
				  Toolkit.getDefaultToolkit().getScreenSize();
			   Dimension frameSize = getSize();
			   int x = (screenSize.width - frameSize.width) / 4;	
			   int y = (screenSize.height - frameSize.height) / 5;
			   setLocation(x, y);

			   
			   Container cp = getContentPane();			//Background color of the Frame
			   cp.setLayout(new FlowLayout());
			   cp.setBackground(Color.yellow);
			   
			   JPanel jpanel_0 = new JPanel();	  //Text on top of screen "Snake Game V.01" highlighted.
			   jpanel_0.add(new JLabel("Snake Game V.01"));		 
			   jpanel_0.setOpaque(true);
			   jpanel_0.setBackground(Color.green);
			   
			   Container cp1 = getContentPane();	//Centers the title "Snake Game V.01"
			   cp1.add(jpanel_0,BorderLayout.CENTER);
   
   
			   
			   setSize(600, 450); // Size of JFrame
			   
			   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	 //Shuts down program once JFrame is closed   
			   
			   
			 setVisible(true); //visibility of JFrame
			 
			 
			  
	}
	
	
			  public static void main(String[] args) {
				  new GameFrame();
				  
				
			 
		 
		   }
			  
		}



Mouse.java
public class Mouse {

	
	
	
}





Movement.java
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class Movement implements KeyListener{

	@Override
	 public void keyPressed(KeyEvent e) 
	{
	  
		if(e.getKeyChar() == 'a')
		{
	   
			System.out.println("you pressed a");
		
		}
		if(e.getKeyChar() == 's')
		{
	   
			System.out.println("you pressed s");
		   
		}
		if(e.getKeyChar() == 'w')
		{
		  
			System.out.println("you pressed w");
		 
		}
		if(e.getKeyChar() == 'd')
		{
		 
			System.out.println("you pressed d");
		
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}



Snake.java
import java.awt.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.border.*;


public class Snake extends JPanel{

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

		  Graphics2D g2d = (Graphics2D) g;

		  g2d.setColor(Color.blue);

		  for (int i=0; i<=1000; i++) {
			  Dimension size = getSize();
			  Insets insets = getInsets();

			  int w =  size.width - insets.left - insets.right;
			  int h =  size.height - insets.top - insets.bottom;

			  Random r = new Random();
			  int x = Math.abs(r.nextInt()) % w;
			  int y = Math.abs(r.nextInt()) % h;
			  g2d.drawLine(x, y, x, y);
		  }
  }
}



Was This Post Helpful? 0
  • +
  • -

#4 ts230  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 225
  • Joined: 11-July 09

Re: Java Snake Game

Posted 31 January 2010 - 07:27 PM

Try putting the painting code in the paint() method, e.g. just get rid of the Component part.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Java Snake Game

Posted 31 January 2010 - 07:34 PM

You are only adding 1 JPanel to the main JFrame. Though you instantiate Snake, which is a subclass of JPanel, you cannot see it or interact with it until you add it to the JFrame, which you never do. You only add the cp1.add(jpanel_0,BorderLayout.CENTER); line but never do anything with snake1. If you ever need to have more than one different JPanels for 1 JFrame (as you seem to), I would look into State Machines. They allow for multiple states to run on the same JPanel.

Each state controls its own events, drawings, and updating. If this sounds interesting, stay tuned for one of my future blog entries: State Machines (actually, I'm working on the code for it now). But for now, make sure you do something with the snake1 JPanel.
Was This Post Helpful? 0
  • +
  • -

#6 sly  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 05-December 08

Re: Java Snake Game

Posted 31 January 2010 - 08:04 PM

Is there a way to do this without using statemachines?

This post has been edited by sly: 31 January 2010 - 08:04 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Java Snake Game

Posted 31 January 2010 - 08:28 PM

You could add it to the JFrame after the constructor, however adding and removing components from a JFrame outside of the constructor and constructor-help methods is NOT recommended as it causes many unknown effects
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Java Snake Game

Posted 31 January 2010 - 09:10 PM

Hey sly, I remeber seeing something almost exactly like this in the Java forum. Could you please pick one and not double post? It's confusing to see them both. Could a Mod or Admin merge these please?
Was This Post Helpful? 0
  • +
  • -

#9 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2631
  • View blog
  • Posts: 15,651
  • Joined: 24-September 08

Re: Java Snake Game

Posted 01 February 2010 - 09:57 AM

HOLY CRAP THAT DOT WAS SUPPOSED TO BE A MOUSE?!?!?! It's all so clear now! </life altering realization> I never really thought about it before; I just assumed he has a weird dot craving.... XD
Was This Post Helpful? 0
  • +
  • -

#10 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Java Snake Game

Posted 01 February 2010 - 03:44 PM

View PostDogstopper, on 31 Jan, 2010 - 11:10 PM, said:

Hey sly, I remeber seeing something almost exactly like this in the Java forum. Could you please pick one and not double post? It's confusing to see them both. Could a Mod or Admin merge these please?


Done. Thanks for catching this.

@OP:
Yes, please keep from posting multiple topics.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1