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

2012/6/10

Question 8D If anyone has any suggestions I'd appreciate it.

Jesse_Orange Jesse_Orange

2012/6/10

#
Right as some of you may know I've been working on a Snake game for a while. If i wanted the Snake to say explode when it hits a wall would i just change the image under canMove as can move determines whether of not the Snake has hit an obstacle. Also at the moment my Snake uses up, down, left, right. Could i change this so that if you hold right the Snake turns and rotates clockwise and vice versa? Also do any of you have any good ideas for improvements? You can find the scenario here Thanks everyone :)
danpost danpost

2012/6/10

#
It is better to just add a new actor class 'Explosion' and add one to the world at the collision site, then use 'getWorld().removeObject(this);' to remove the head. The Explosion class can vary the size and transparency of the image and remove itself with time, if desired. You could change what the keys do, however you want. If you are going for universal directioning (instead of 4-way or 8-way motion), that might be a way to go; otherwise, it is best to leave it as is.
Jesse_Orange Jesse_Orange

2012/6/10

#
How would I go about universal directioning? At the moment I just have 4 way control :)
danpost danpost

2012/6/10

#
In a snake game, having all the body segments playing follow the leader with the segment in front, going with universal directioning would be quite a task (almost too much to chew, but possible). I, for one, would not be up for it (not now, at least). On the other hand, 8-way motion would not be difficult, still using the same four arrow keys (key combinations producing diagonal motion).
Jesse_Orange Jesse_Orange

2012/6/10

#
Question - how do you use diagonals? do you have to write the names of 2 keys like if greenfoot is key down (up, left) set offset y=-1 x=-1? The other control type seems very difficult considering each of the segments would need to know the exact movements of the leader :)
Jesse_Orange Jesse_Orange

2012/6/10

#
I know how to set offsets using a key of choice but i dunno whether you have to do something differently in order to use more than one key at a time?
danpost danpost

2012/6/10

#
Check out my Line Following Demo. It has code for both 4-way and 8-way movement.
Jesse_Orange Jesse_Orange

2012/6/10

#
Not that i disagree as your code works but is there a way to set diagonals using this type of code?
     if(Greenfoot.isKeyDown("nameofkey")) //move up
     {
         offsetX=0;
         offsetY=-1;
        }
Can i combine the if statement for each key to create diagonal movement?
danpost danpost

2012/6/10

#
I guess that is not a very good example as it does not accept keystrokes. So, this is something like what you want:
int dx = 0, dy = 0;
if (Greenfoot.isKeyDown("right")) dx += 1;
if (Greenfoot.isKeyDown("down")) dy += 1;
if (Greenfoot.isKeyDown("left")) dx -= 1;
if (Greenfoot.isKeyDown("up")) dy -= 1;
Then you can 'setLocation(getX() + dx, getY() + dy);'. As far as the following part, you may have to add a new Actor class, 'Trail' (a transparent, one-pixel sized object that holds the previously created Trail object). Have the head of the snake create one every time it moves. Have a static Trail variable, called 'latestTrail', in the Trail class and initialize it to null. Also, in the Trail class, add an instance Trail variable, called 'leader'. In the constructor, set 'latestTrail.leader' to 'this', then set 'latestTrail' to 'this'. In the act method of Trail, say 'if (getIntersectingObjects(null).isEmpty()) getWorld().removeObject(this);'. Now, all the segments have to do is follow the trail (use '(Trail) ((Trail) (getWorld().getObjectsAt(getX(), getY(), Trail.class).get(0))).leader' to get the next Trail object to proceed to; then set location to the leader's location).
danpost danpost

2012/6/10

#
I thought I would include the longer version of the '(Trail) ((Trail)(...)' statement:
Trail onTrail = (Trail) getWorld().getObjectsAt(getX(), getY(), Trail.class).get(0);
Trail nextTrail = onTrail.leader;
setLocation(nextTrail.getX(), nextTrail.getY());
Looking at this, the initial casting of (Trail) in my previous post is not needed.
You need to login to post a reply.