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
if (GreenfootIsDown(/* key */) && canMove/*Direction*/
if (random = 0 && canMove/* Direction */ ...
switch(random) case 0: move/*Direction*/() ...
//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);
}turn(90 * (Greenfoot.getRandomNumber(3) - 1));
private void setDirection()
{
int holdDir = direction;
direction = (Greenfoot.getRandomNumber(3) + 3 + holdDir) % 4;
if (!canMove()) direction = holdDir;
}public void act()
{
if (!this.equals(head)) return;
setDirection();
if (canMove()) moveAndGrow();
}