hello, as the title says, I want the movement of the snake to stop when touching blocks. The problem is that i have all my movement code in the world for the snake, so I don't know how to solve it. So I have done a new movement code/method in my snake class. But when it touching the blocks (the classes are Block and Block1) the snake goes around the blocks. I would be able to fix this, but the method getOneIntersectingObject can't be used in World. So I wondering if it is possible to make something as getOneIntersectingObject in the world? Here is my code for the world and snake class(i have mario theme).
AND
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Space here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MarioBros extends World
{
Mario [] Marios;
//Mario h = new Mario(false,false);
Block block = new Block();
Block1 block1 = new Block1();
Coin coin = new Coin();
Ghost ghost = new Ghost();
int x = 1;
int y = 0;
int mariolastX;
int mariolastY;
int luigilastXjects of class Space.
*
*/
public MarioBros()
{
super(20, 15, 40);
addObject(coin, Greenfoot.getRandomNumber(20), Greenfoot.getRandomNumber(15));
addObject(ghost, Greenfoot.getRandomNumber(20), Greenfoot.getRandomNumber(15));
Marios = new Mario[4];
for(int i=0;i<4;i++)
{
Marios[i] = new Mario();
addObject(Marios[i], 7- (i*1), 6);
}
Marios[0].setImage("marios.png");
Marios[1].setImage("luigi.png");
prepare();
}
public void act()
{
checkKeyPress();
follow();
takeCoin();
moveMario();
hitGoomba();
touchBlock();
}
public void addArray()
{
// create a new segment
Mario M = new Mario();
// create temporary segment array of equal length
Mario[] newMario = new Mario[Marios.length];
// copy data from original array to temporary array
for(int i = 0; i < Marios.length; i++)
{
newMario[i] = Marios[i];
}
// re-create original array allowing one more element
Marios = new Mario[Marios.length+1];
// copy data from temporary array to re-created original array
for(int i = 0; i < newMario.length; i++)
{
Marios[i] = newMario[i];
}
// add the new segment to the array
Marios[Marios.length-1] = M;
// add the new segment into the world
addObject(M, mariolastX, mariolastY);
}
public void touchBlock()
{
Block1 block1 = new Block1();
if(Marios[0].block()==true)
{
Marios[0].setLocation(Marios[0].getX() - x, Marios[0].getY() - y );
}
}
public void follow()
{
// save the location of the 'head' segment (segment in front)
// these are the 'goto' values for the next segment
mariolastX = Marios[0].getX();
mariolastY = Marios[0].getY();
for(int i = 1; i < Marios.length; i++ )
{
//store next segment posiotion in a temporary variable
luigilastX = Marios[i].getX();
luigilastY = Marios[i].getY();
//move to the heads position
Marios[i].setLocation(mariolastX, mariolastY);
// set 'goto' values for next segment
mariolastX = luigilastX;
mariolastY = luigilastY;
}
}
public void moveMario()
{
Marios[0].setLocation(Marios[0].getX() + x, Marios[0].getY() + y );
}
public void checkKeyPress()
{
if(Greenfoot.isKeyDown("right") )
{
if(y== 1 || y == -1)
{
x = 1;
y = 0;
}
}
if(Greenfoot.isKeyDown("left") )
{
if (y == 1 || y == -1)
{
x = -1;
y = 0;
}
}
if(Greenfoot.isKeyDown("up"))
{
if( x == 1 || x == -1)
{
x = 0;
y = -1;
}
}
if(Greenfoot.isKeyDown("down"))
{
if (x == 1 || x == -1)
{
x = 0;
y = 1;
}
}
}
public void takeCoin()
{
if(Marios[0].getX() == coin.getX() && Marios[0].getY() == coin.getY())
{
addArray();
Greenfoot.playSound("coin.mp3");
removeObject(coin);
addObject(coin , Greenfoot.getRandomNumber(10), Greenfoot.getRandomNumber(10));
nbrGoomba++;
}
}
public void hitGoomba()
{
for(int i=1; i<nbrGoomba; i++)
{
if(Marios[0].getX()==Marios[i].getX() && Marios[0].getY()==Marios[i].getY())
{
Greenfoot.playSound("end.mp3");
addObject(new GameOver(), 10, 7);
Greenfoot.stop();
}
}
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
Block1 block14 = new Block1();
addObject(block14, 11, 6);
Block block = new Block();
addObject(block, 10, 6);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Head here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Mario extends Actor
{
int x = 1;
int y = 0;
public boolean touchBlock = false;
public void act()
{
flower();
flowerBlock();
//hitGhost();
block();
}
public void move()
{
setLocation(getX()+x, getY()+y);
}
public boolean block()
{
Actor block1 = getOneIntersectingObject(Block1.class);
if(block1 !=null )
{
setLocation(getX()-x, getY()-y);
}
return touchBlock;
}
public void flowerBlock()
{
Actor block = getOneIntersectingObject(Block.class);
if (block !=null)
{
getWorld().addObject(new Flower(), 10,5);
setLocation(getX()-x, getY()-y);
}
}
public void flower()
{
Actor flower = getOneIntersectingObject(Flower.class);
if (flower !=null)
{
getWorld().removeObject(flower);
Greenfoot.playSound("fire.mp3");
}
}
public void hitGhost()
{
Actor Ghost = getOneIntersectingObject(Ghost.class);
if (Ghost !=null)
{
Greenfoot.stop();
getWorld().addObject(new GameOver(), 10,7);
Greenfoot.playSound("end.mp3");
}
}
}
