This site requires JavaScript, please enable it in your browser!
Greenfoot back
wahaj
wahaj wrote ...

2012/4/12

trying to make a snake game

wahaj wahaj

2012/4/12

#
im trying to make a snake game part of my class project. but mi completely new to programming so im already stuck at the beginning. the following code inst compiling. it says at line 31 cannot find symbol - variable length ive used .length before when i was going through the book and it worked fine but its not working here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.awt.Color;
 
/**
 * Write a description of class SnakeWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class SnakeWorld extends World
{
 
    /**
     * Constructor for objects of class SnakeWorld.
     *
     */
    public SnakeWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        createAnts();
    }
 
    /** checks to see if there are any ants in the world
     * if there arent then it adds one ant
     */ 
    public void createAnts()
    {
 
        if (getObjects(Ant.class).length == 0)
        {
            addObject(new Ant(), Greenfoot.getRandomNumber(550), Greenfoot.getRandomNumber(350) );
        }
    }
 
}
danpost danpost

2012/4/13

#
The method 'getObjects(Class cls)' return a List object which has a 'size' (not a 'length'). Though you could ask: if (getObjects(Ant.class).size() == 0) better would be to use the List method 'isEmpty' if (getObjects(Ant.class).isEmpty())
wahaj wahaj

2012/4/13

#
i did not know about these methods. i must have missed them when i was looking through the documentation. thanks for the help. edit: i have another problem ive come across. my snake is rectangular and the food for him is circular. if i use the getOneIntersectingObject( ) method then the snake will eat the food before it even touches it because the circular food is enclosed in a box. also if the snake is a line below it will still end up eating it because of the box. to solve this problem i was thinking of comparing the two object's coordinates. unfortunately i dont know how. i can find the individual coordinates but they remain in the specific actor's code as variable which to my knowledge are private. so how can i compare them? i was thinking of doing this in the world class because thats where the removing and adding of "Food" is taking place
Denver005 Denver005

2016/8/12

#
can i have your codes for my project please reply
You need to login to post a reply.