Hi, I'm currently trying to recreate the "battleship" game in Greenfoot.
Right now you're able to drag a ship to the field (the tiles where you play on). The problem is, that you're also able to put a ship between multiple tiles (e.g. putting a 2-tile ship in the middle of 4 tiles, which makes it 4 tiles large). A solution to this would be to make ships automatically snap to the tiles where their (mostly) located at.
I wanted to know if thats even possible and if so, a short explanation would be really nice.
Here's the code of the ship (the drag-function):
The field consists of 10x10 tiles, 100 tiles in total. Each tile is an object (using a loop to create multiple tiles of the same object).
I think the information given is enough to understand everything (otherwise I'll happily add the missing information) and hopefully being able to help me.
Many thanks in advance!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class BB extends Schiffe { /** * Act */ public void act() { dragShip(); } public void dragShip() { if (Greenfoot.mouseDragged( this )) { MouseInfo mouse = Greenfoot.getMouseInfo(); setLocation(mouse.getX(),mouse.getY()); } } } |