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

2013/4/16

Changing snake demo 2

1
2
JetLennit JetLennit

2013/4/16

#
1st How do i change snake demo 2 to where it doesnt get a game over whenever it runs into a wall or itself and when it runs into a wall it tuns around 2nd How do i change snake demo 2 to where it moves randomly
For your first question, I would rewrite the code where you can only change direction if you press the button, AND the direction the key wants to go is not behind itself. What you could do is have some booleans like canMoveLeft, canMoveRight, etc, where specific ones become false when you are going the opposite way. Then you can change the if statements to
if (GreenfootIsDown(/* key */) && canMove/*Direction*/
And the second question would be removing those Greenfoot.isKeyDown Statements and add a random number up to 4 digits (so int random = Greenfoot.getRandomNumber(3), 0, 1, 2, 3) then have the if statements say this:
if (random = 0 && canMove/* Direction */
...
or you could do a switch case:
switch(random)
case 0: move/*Direction*/()
...
with the move direction methods having an if statement to see if they can move that way. if you want the snake to move a different direction every step, keep this, otherwise, have an int that counts up to a specific amount of steps, then follow through with the direction if statements.
JetLennit JetLennit

2013/4/16

#
How do i combine the two? (I dont want the snake to be player controlled)
It would look something like this:
//Instance Variables
private boolean canMoveRight = true; //since he starts moving right
private boolean canMoveLeft = false;
private boolean canMoveUp = true;
private boolean canMoveDown = true;

//...

private void setDirection()
    {
        int random = Greenfoot.getRandomNumber(3);
        int dx = 0, dy = 0;
        if (random == 0 && canMoveRight)
        {
            canMoveLeft = false;
            canMoveUp = true;
            canMoveDown = true;
            dx++;
        }
        if (random ==  1 && canMoveLeft)
        {
            canMoveRight = false;
            canMoveUp = true;
            canMoveDown = true;
            dx--;
        }
        if (random = 2 && canMoveDown)
        {
            canMoveRight = true;
            canMoveLeft = true;
            canMoveUp = false;
            dy++;
        }
        if (random == 3 && canMoveUp)
        {
            canMoveRight = true;
            canMoveLeft = true;
            canMoveDown = false;
            dy--;
        }
        if (dx * dx == dy * dy) return;
        direction = getDirection(dx, dy);
        setRotation(direction * 90);
    }
JetLennit JetLennit

2013/4/16

#
Thank you for this. I will work on it soon! (for now i am using temporary code to work on link, i mean the player =))
No problem. I appreciate the chance to help the fellow Greenfooter. :)
danpost danpost

2013/4/16

#
Because you would only want the snake to turn left or right at any given time (or continue in the current direction), you should use something like the following:
turn(90 * (Greenfoot.getRandomNumber(3) - 1));
Then you can remove all those nasty variables.
Ah, didn't think of that. That's much better than my solution.
JetLennit JetLennit

2013/4/16

#
where do i put that?
Basically where you would have all the direction changes.
JetLennit JetLennit

2013/4/16

#
now it goes forward and spins around without changing direction
danpost danpost

2013/4/16

#
Starting with the original code of my Snake Demo 2 scenario, just change the 'setDirection' method in the Snake class to the following:
private void setDirection()
{
    int holdDir = direction;
     direction = (Greenfoot.getRandomNumber(3) + 3 + holdDir) % 4;
    if (!canMove()) direction = holdDir;
}
The only other change I made was the 'act' method in the Snake class, which I adjusted to the following:
public void act()
{
    if (!this.equals(head)) return;
    setDirection(); 
    if (canMove()) moveAndGrow();
}
JetLennit JetLennit

2013/4/16

#
Thank you! and am i allowed to post this scenario if i like the program since i used your program?
danpost danpost

2013/4/16

#
The behaviour of the snake with this is very similar to the snakes in my Total Infestation scenario.
There are more replies on the next page.
1
2