Results 1 to 2 of 2
  1. #1
    Ruuhkis is offline Member
    Join Date
    Feb 2011
    Rep Power

    Default basic snake game

    I feel so stupid about this, i cant figure out how can i make this game so it wont think you die when it eats... I mean how i can make it so snake grows but they dont go on eachother? I've tried it in many ways but never succeed! please point me out in this thing
    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    public class testingSO extends JPanel implements ActionListener, KeyListener {
    	Timer t = new Timer(200, this);
    	Random r = new Random();
    	List<Snake> snakes = new ArrayList<Snake>();
    	Food f;
    	boolean isFood = false;
    	public static void main(String args[]){
    		new testingSO();
    	public testingSO(){
    		JFrame frame = new JFrame("snake");
    		setPreferredSize(new Dimension(400, 400));
    		snakes.add(new Snake(0, 0, 1, 0));
    	public void paintComponent(Graphics g){
    		for(Snake x: snakes){
    			g.drawRect(x.x, x.y, 25, 25);
    		if(f != null && isFood){
    			g.drawRect(f.x, f.y, 25, 25);
    	public void actionPerformed(ActionEvent ae) {
    			int tx = r.nextInt(16);
    			int ty = r.nextInt(16);
    			f = new Food(tx*25, ty*25, 0, 0);
    			isFood = true;
    		if(snakes.size() > 0){
    			for(int x = snakes.size()-1; x > 0; x--){
    				for(int y = snakes.size()-1; y > 0; y--){
    							System.out.println("game over!");
    				snakes.get(x).x = snakes.get(x-1).x;
    				snakes.get(x).y = snakes.get(x-1).y;
    				isFood = false;	
    				snakes.add(new Snake(snakes.get(snakes.size()-1).x, snakes.get(snakes.size()-1).y, 0, 0));
    		snakes.get(0).x += snakes.get(0).vx*25;
    		snakes.get(0).y += snakes.get(0).vy*25;
    	public void keyPressed(KeyEvent ke) {
    		snakes.get(0).vx = 0;
    		snakes.get(0).vy = 0;
    			case KeyEvent.VK_UP:
    				snakes.get(0).vy = -1;
    			case KeyEvent.VK_DOWN:
    				snakes.get(0).vy = 1;
    			case KeyEvent.VK_RIGHT:
    				snakes.get(0).vx = 1;
    			case KeyEvent.VK_LEFT:
    				snakes.get(0).vx = -1;
    	public void keyReleased(KeyEvent ke) {
    	public void keyTyped(KeyEvent kt) {
    The game is just like other snake games. I want it to NOT TO think the snake have hitted himself when it eats food. When it eats food it spawns new piece of snake on last snake piece but i got no idea how to make it so it will move 1 step inb4 it will make the new piece appear or something. I didnt find any useful tutorials from google so dont post any of them. Just help me out with your geniousness. Please i beg you!
    Last edited by Ruuhkis; 03-05-2011 at 05:51 PM. Reason: quote tags changed to code tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    Moderator Edit: quote tags changed to code tags so that the pasted code above is readable.

    OP: consider adding more description of your code and it's problem. I think you are overestimating our ability to guess what your program is doing, is supposed to be doing, etc. Describe the problem as if we have no prior knowledge of the situation (which is true!).

Similar Threads

  1. Snake Game in Java
    By Shyamz1 in forum New To Java
    Replies: 4
    Last Post: 02-10-2011, 02:49 PM
  2. Snake Game Applet
    By Growler in forum Java Applets
    Replies: 6
    Last Post: 07-11-2010, 02:47 PM
  3. Snake game in java
    By freaky in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 06:34 PM
  4. Snake Game
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 10:35 PM
  5. Snake game movement
    By BeerMonkey in forum New To Java
    Replies: 9
    Last Post: 11-27-2008, 12:48 PM

Posting Permissions

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