Monday, September 3, 2012

Even more codes on the crab scenario

Today I worked on controlling the crabs with keys, ending the crab game, adding sounds and writing code to place the crab in the world so that I don't have to do it manually each time I open the scenario.Alright so in order to control the crab with keys you had to delete all the code about random movement and turning at the edge and replace it with the key code. I replace left and right with "A" and "D" keys because I'm a bit more comfortable with them. Ending the game and adding the sound was pretty simple. For ending the game you just had to go to the lobster class and add this Greenfoot.stop(); under the eat(Crab.class);. This means whenever a lobster eats a crab it will stop running. For the sounds I wrote Greenfoot.playSound("slurp.wav"); this under this eat(Wormy.class);. This meant whenever the crab ate a worm it would make a slurp sound.  Now adding random objects was actually a bit tricky. For adding the crab I had to write this addObject( new Crab(), 150, 100);. The book I was using gave me the coordinates for the crab but then it asked me to add 3 lobsters and 10 worms. I used similar coding but replaced it with either lobster or worm. The problem was that they didn't give me any coordinates so I was just kinda writing random numbers so by the time a compiled it I just had a clump of crabs,worms and lobsters in one corner of the screen. So for next time I'm going to work on fixing that.

No comments:

Post a Comment