Snake Game in Java

Having Trouble with the game (built in Greenfoot)

Page 1 of 1

6 Replies - 7006 Views - Last Post: 17 May 2009 - 07:45 PM Rate Topic: -----

#1 jab0015  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 20-October 08

Snake Game in Java

Posted 14 May 2009 - 08:21 PM

I am building a snake game in java, This may be very simple mistakes but I am stumped, so I am turning to Dreamincode
any help would be great, I will post all my classes.

For my snake world its saying I need a return type, I don't see why.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

public class SnakeWorld extends World
{
	private static final int minSpeed = 20, maxSpeed = 50;

	private int nextFood;
	private Snake foot;

	public Snakeworld()
	{	
		super(15, 10, 40); 
		nextFood = Greenfoot.getRandomNumber(5);
		addRocks(5);
		addFood();
		addFood();
		foot = new Snakefoot();
		addObject(foot, 7, 5);
	}
	
	public void act()
	{
		if(--nextFood <= 0)
		{
			addFood();
			nextFood = Greenfoot.getRandomNumber(8+2*foot.getPoints());
		}
	}
	
	private void addFood()
	{
		addActor(new Food());
	}
	
	private void addRocks(int count)
	{
		for(int i=0; i<count; i++)
		{
			addActor(new Rock());
		}
	}
	
	private boolean addActor(Actor actor)
	{
		boolean done = false;
		int tries = getWidth();
		while(!done && tries > 0)
		{
			int x = Greenfoot.getRandomNumber(getWidth());
			int y = Greenfoot.getRandomNumber(getHeight());
			List objs = getObjectsAt(x,y,Actor.class);
			if(objs == null || objs.size() == 0)
			{
				addObject(actor,x,y);
				done = true;
			}
			tries--;
		}
		return done;
	}
}



To make my rocks solid (also an abstract class is required for the project)

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public abstract class Solid  extends Actor
{

	public void act() 
	{
		
	}
}



Rock class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Rock  extends Solid
{
	public void act() 
	{
 
	}		
}



import greenfoot.*;
import java.util.List;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Snake  extends Actor
{
  private int dx = 1, dy = 0;
  private int points = 0;
  
  
  public void act() 
  {
	String key = Greenfoot.getKey();
	if(key != null)
	{
	  if(key.equals("up") && dy != 1)
	  {
		dx = 0;
		dy = -1;
		setRotation(-90);
	  }
	  else if(key.equals("down") && dy != -1)
	  {
		dx = 0;
		dy = 1;
		setRotation(90);
	  }
	  else if(key.equals("left") && dx != 1)
	  {
		dx = -1;
		dy = 0;
		setRotation(180);
	  }
	  else if(key.equals("right") && dx != -1)
	  {
		dx = 1;
		dy = 0;
		setRotation(0);
	  }
	}
	
	List objs = getObjectsAtOffset(0,0,Solid.class);
	if(objs!= null && objs.size() > 0){
	  boolean stillThere = false;
	  getWorld().removeObject(this);
	  //System.out.println(points+" points");
	  Greenfoot.stop();
	}
	else
	{
	  List foods = getWorld().getObjectsAt(getX(), getY(), Food.class);
	  if(foods != null && foods.size() > 0){
		Food food = (Food)foods.get(0);
		getWorld().removeObject(food);
		addPoints(food.getPoints());
	  }
	}
  }	
  
  public int getPoints()
  {
	return points;
  }
  
  private void addPoints(int add)
  {
	points += add;
  }
}



Finally the food class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Food  extends Actor
{
	public Food()
	{
	}
	public void act() 
	{
		
	}	
	public int getPoints()
	{
		return 1;
	}
}




If you don't know what greenfoot is then http://greenfoot.org/
Again Thanks, a Reply would be great!

Is This A Good Question/Topic? 0
  • +

Replies To: Snake Game in Java

#2 warmwaffles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-May 09

Re: Snake Game in Java

Posted 14 May 2009 - 08:30 PM

Please post the error output
Was This Post Helpful? 0
  • +
  • -

#3 jab0015  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 20-October 08

Re: Snake Game in Java

Posted 14 May 2009 - 08:46 PM

View Postwarmwaffles, on 14 May, 2009 - 07:30 PM, said:

Please post the error output

For snakeWorld,(line 11) invalid method declaration; return type required
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Snake Game in Java

Posted 14 May 2009 - 08:48 PM

Don't start tou just trhowing class like that:

World: OK I can understand that
SnakeWorld extends World ... by the way you haven't showed us why World looks like

when extanding a class the first question to ask is SnakeWorld a Word ? What are the features that my SnakeWorld have that snake does not have ?


Actor: ok what is an Actor ? Something that interfere in your game ? OK

Solid extends Actor: do Sock really interfere in your game... yes may be. But why does it have to be abstract ? Hown many Solid will be derived from you class Solid ? If it is only one or 2 no reason to make Solid abstract

Rock extends Solid. OK Rock is a Solid. What does Rock have that Solid does not and that Actor does not have ?

Snake entends Actor... yes I guess it interfer in the game

Food extends Actor... ?

OK I thing that you have some made the stuff a lot over complicated

You can have a class Coordinate that contains X,Y and if the coordinates can be changed
Then you can have a Snake, a Food and a Rock class that HAVE (not extend) a Coordinate and a a flaf in Coordinate saying if these coordinates can change or not
Was This Post Helpful? 0
  • +
  • -

#5 warmwaffles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-May 09

Re: Snake Game in Java

Posted 14 May 2009 - 09:06 PM

Whats funny is that your constructor is not properly capitalized. Take a look:

public class SnakeWorld extends World
{
   //...

	public Snakeworld()
	{	


Was This Post Helpful? 0
  • +
  • -

#6 jab0015  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 20-October 08

Re: Snake Game in Java

Posted 14 May 2009 - 09:12 PM

View Postwarmwaffles, on 14 May, 2009 - 08:06 PM, said:

Whats funny is that your constructor is not properly capitalized. Take a look:

public class SnakeWorld extends World
{
   //...

	public Snakeworld()
	{	


Ha okay thanks, but when I try to run it, It keeps on adding the food, and my control arn't working, would you guys know anything about this?
Was This Post Helpful? 0
  • +
  • -

#7 jab0015  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 20-October 08

Re: Snake Game in Java

Posted 17 May 2009 - 07:45 PM

bump
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1