It looks like this in my Item class.
What this does is if the item is placed on the wall it gets new x and y value.
Actor item = getOneIntersectingObject(Wall.class);
if(item !=null){
The code is written like this because i want the snake to always hit the item directly on it and not a side hit.
So to explain how it's build up. the size of my world is 500x500. r1 and r2 get random number from 0 to 24.
these are multiplied with 20 then i add 10. Max x and y value can therefore be 490x and 490y.
so it can be placed 10, 30 , 50 , 70 90 and so on in both x and y.
My snake segment is 20 pix. The same size as my item object. If the item is placed at 10, 10 is going to be added in the top left corner. This is because object is placed in the center of it's x and y value. 20/2 = 10.
I use the same idea for both snake and item. therefore the snake hit the item exactly in the correct position.
One more thing. The speed is 20 the same as the size of the snake. So the snake are moving perfectly at the world edge. if the snake head start att x=10 it will go 24 step to reach the opposite side. 20*24=480. 480 and 10 from staring point and you have x= 490. witch is the heads x-value.
Hope this make things more clear.
r1 = Greenfoot.getRandomNumber(24);
r2 = Greenfoot.getRandomNumber(24);
setLocation(r1*20+10, r2*20+10);
}
2012/11/24
Lab 6 – Snake
2012/11/10
Lab 5 – Shot from a cannon
2012/10/31
scrolling
2012/10/4
Lab2 – Bouncing Ball