Results 1 to 3 of 3
  1. #1
    Nickv17 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default Creating Snake Game: Few problems.

    Hey, new guy here. I'm struggling in my Comp Sci class on my snake project. I'm trying to set up a timer to make my snake move automatically, to make my snake grow, (My teacher did this in about four lines of code, so I think it should be easy, and a game over method, which I have no clue what to do. Code would be appreciated with a little explanation of why it works. Heres my Code. I have other classes (I think they're called) but I think all we need is this one. Thanks.

    Java Code:
    import info.gridworld.world.*;
    import info.gridworld.grid.*;
    import info.gridworld.grid.Location;
    import java.util.*;
    
    
    public class SnakeWorld extends World{
    
    	static Random randGen = new Random();
        int r = randGen.nextInt(100);
     	Location snakeLoc = new Location(10,10);
    	Location PikachuLoc = new Location(8,6);
    	Location CharmanderLoc = new Location (11,24);
        int score = 0;
    	public SnakeWorld(){
    		super(new BoundedGrid(30,30));
    		t.schedule(new SnakeMoveTask(this), 100, 100);
    		add(snakeLoc, new SnakePiece());
    	
    		remove(CharmanderLoc);
    		add(PikachuLoc, new Pikachu());
    		
    		remove(PikachuLoc);
    		add(CharmanderLoc, new Charmander());}
    		
    		
    		
    	
    		
    		
    		
    		
    	class SnakeMoveTask extends TimerTask{
    		World w;
    		public SnakeMoveTask(World w){
    			this.w = w;
    		}
    		
    		public void run(){
    			// get rid of snake pieces
    			
    			// tell
    			++times;
    //			setMessage("Moving... " + times+ " " + snake.getLocation());
    			snake.move();
    			checkStuff();
    //			snake.show(w);
    			// add the snake pieces back into the world
    		}
    	}
    	int times = 0;
    	Timer t = new Timer();
    	
    	Snake snake = new Snake();
    	
    	public void checkStuff(){
    		// have I collided with food?  Has snake gone off the screen
    		// has snake collided with self
    		
    		checkFood();
    		checkInBounds();
    		checkCrash();
    			
    	}
    	public void checkFood(){
    		
    	}
    	public void checkInBounds(){
    		
    	}
    	public void checkCrash(){
    		
    	}
    
    	
     	public boolean	keyPressed(String s, Location loc){
    		System.out.println(s);
    		remove(snakeLoc);
    		if(s.equals("RIGHT"))
    			moveRight();
    		else if(s.equals("LEFT"))
    			moveLeft();
    		else if(s.equals("UP"))
    			moveUp();
    		else if(s.equals("DOWN"))
    			moveDown();
    		add(snakeLoc, new SnakePiece());
    		r = randGen.nextInt(100);
    		if(r<30){
    			if(snakeLoc.equals(PikachuLoc)){
    		
    	        
    			PikachuLoc = getRandomEmptyLocation();
    			add(PikachuLoc, new Pikachu());
    		}
    		    else if(snakeLoc.equals(CharmanderLoc)){
    			 
    			CharmanderLoc = getRandomEmptyLocation();
    			add(CharmanderLoc, new Charmander());
    		}	
    		}
    		else if(r<100){
    			
    			if(snakeLoc.equals(PikachuLoc)){
    		
    	        
    			PikachuLoc = getRandomEmptyLocation();
    			add(PikachuLoc, new Pikachu());}
    		
    		else if(snakeLoc.equals(CharmanderLoc)){
    			 
    			CharmanderLoc = getRandomEmptyLocation();
    			add(CharmanderLoc, new Charmander());}
    		}
    		
    		
    		System.out.println(""+score);
    		setMessage(""+score);
    		return true;
    		
    	}
    	public void moveRight(){
    		//remove(snakeLoc);
    		snakeLoc = snakeLoc.getAdjacentLocation(Location.RIGHT);
    	}
    	public void moveDown(){
    		//remove(snakeLoc);
    		snakeLoc = snakeLoc.getAdjacentLocation(Location.SOUTH);
    		
    	}
    	public void moveLeft(){
    		//remove(snakeLoc);
    		snakeLoc = snakeLoc.getAdjacentLocation(Location.LEFT);
    		
    	}
    	public void moveUp(){
    		//remove(snakeLoc);
    		snakeLoc = snakeLoc.getAdjacentLocation(Location.NORTH);
    		
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Nickv17 View Post
    Hey, new guy here.
    Welcome!


    ... I'm trying to set up a timer to make my snake move automatically, to make my snake grow, (My teacher did this in about four lines of code, so I think it should be easy, and a game over method, which I have no clue what to do. ...
    A bit more detail would be helpful here. What problems are there in your code? Does it compile? Does it throw exceptions? What does it do that it's not supposed to do and what does it not do that it's supposed to do.


    Code would be appreciated with a little explanation of why it works. Heres my Code. I have other classes (I think they're called) but I think all we need is this one. Thanks.
    That's not how it works here and repeated requests for code-solutions for homework can be a basis for banning. Rather please ask your very specific questions about your code and we'll be more than happy to help.

    Again, welcome.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    @Fubarable: I don't think the OP was looking for a full solution. I think he was asking for smaller examples that he could put together and modify slightly to fit his problem. I could be wrong though, in which case I am completely with you.

    @OP: As Fubarable said, we really need more info. Having recently written a snake game myself due to boredom, I can say that its not that complex, however, we don't know how far you've gotten, so we need more info (Can the snake move at all? What are you not understanding about Timers? etc.)
    Last edited by Singing Boyo; 01-21-2011 at 05:02 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Snake Game Applet
    By Growler in forum Java Applets
    Replies: 6
    Last Post: 07-11-2010, 02:47 PM
  2. Snake game in java
    By freaky in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 06:34 PM
  3. Java Applet Help - Making "Snake" Game
    By Alphimeda in forum Java Applets
    Replies: 15
    Last Post: 04-04-2010, 05:39 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
  •