This site requires JavaScript, please enable it in your browser!
Greenfoot_back
Back to discussions
No_avatar_thumb
Swedishsam wrote ...

2012/5/5

Help with some coding please.

No_avatar Swedishsam

2012/5/5

OK, so I've finished making the basics of my snake game, i'm not going to make my snake grown and instead just keep it as a simple grey blob. I want to create a leveling system of sorts so when the snake eats a certain amount of berries the speed of the game gets faster. I also want to stop the snake from being able to go straight down when its going upwards. You can see how the game works and the code here. Snake Game
Fractal_r3800c0s800f10000t0z80x0y0 danpost

2012/5/5

To stop the snake from reversing upon itself, make a check on the rotation before setting each new one. For example:
You will need to do similar to that for all four directions. I added the following method to the Snake class (you can add it, if you want).
Fractal_r3800c0s800f10000t0z80x0y0 danpost

2012/5/5

For the speeding up thing, you have a counter, but need an integer variable 'runSpeed'. You can initially set the the value of 'runSpeed' (and initially Greenfoot.setSpeed(runSpeed)), and use it in future setSpeed commands after increasing its value when the counter reaches one of its limits. Note: if the limits are evenly spaced (meaning like after every 10 berries are eaten, the level increases) you can use the modulus (remainder) to determine whether it is time to increase level or not
in the eatAberry() method
No_avatar Swedishsam

2012/5/5

I'm not quite sure how to apply the first bit of code, i applied it in several different ways but that made it so when i pressed up it went left or otherwise it wouldn't move at all. If you could explain how i would make this similar for all the directions that would be very much appreciated.
Fractal_r3800c0s800f10000t0z80x0y0 danpost

2012/5/5

This is the new 'moveAndTurn()' method (for now)
I have noticed that this code will always favour the 'up' key (meaning if the up key is pressed with any combination of other arrow keys, the resultant direction will be up).
No_avatar Swedishsam

2012/5/5

Ok, seems im not really cut out for doing this type of stuff, i added the speed thing to the counter and now im getting a cannot find symbol method - getValue
Fractal_r3800c0s800f10000t0z80x0y0 danpost

2012/5/5

I assumed, since you had a bumpCount(int) method in the counter class, that you would also have a method in it to return the value of the counter (normally, it would be called getValue()). Check the counter class code and see if there is a (public) method that returns the value of the counter, and if you find it, use that method name instead of 'getValue()'. If you do not find anything, post the counter class code for help.
No_avatar Swedishsam

2012/5/5

I cant seem to able to tell what method name im supposed to use, Here is all the code that is used for the counter. In the Snake Class
In SnakeWorld
In the counter class
Fractal_r3800c0s800f10000t0z80x0y0 danpost

2012/5/6

It appears you do not have a way to get the value of the counter out of its class, SO... INSERT, BETWEEN lines 26 and 27 of the Counter class, the following method:
Then change line 7 of the eatAberry() method of the Snake class to
No_avatar_thumb Swedishsam

2012/5/6

//
You need to login to post a reply