This site requires JavaScript, please enable it in your browser!
Greenfoot back

Report as inappropriate.

Malmotri
Malmotri presents ...

2012/11/19

Lab 6 – Snake

No description.

2918 views / 6 in the last 7 days

Tags: with-source

open in greenfoot
Your browser does not support the canvas tag.
danpostdanpost

2012/11/24

@hkrhässleholm, other than "head" being a character array of length 4 that are ordered 'h', 'e', 'a', 'd', all greenfoot can do is whatever processing is programmed for it. In this case, it will be set as the first parameter in the matching constructor in the Snake class (a variable called 'segment', in the constructor). From there, it is set to the Snake class instance variable with the same name ('this.segment=segment;' accomplishes that). Finally, in the 'act' method the value of the string is checked before executing the code there ('if(segment="head")'). Hope this clears things up.
hkrhässleholmhkrhässleholm

2012/11/24

Thanks danpost. I got that. More question: 1. In this game we can see that the snake always move forward (even when no key is pressed). Which method is used for that and where are the codes for those exactly?
danpostdanpost

2012/11/24

The method you are looking for is the 'act' method within the Snake class. All objects (world and actors) will each have a turn at their 'act' methods being executed. It is called one cycle (or frame) when all have executed once. There will be many cycles repeated every second, which is what puts 'life' into your world. The first statement (the 'if') in the 'act' method of the Snake class puts a condition on executing the code located there. It basically says, "Do not execute my code unless you are acting on the 'head' segment of the snake." (it actually says the same thing in the opposite sense - "Only if acting on the 'head' of the snake should you execute my code.") Inside the conditional block of code, starting from the back end of the snake, each segment is moved to the location of the segment in front of it; then, the 'head' of the snake (which is user-controlled) will move. If there are no changes (no keys pressed to change the direction of the 'head'), the 'head' will continue in the direction last instructed.
A new version of this scenario was uploaded on Sun Nov 25 22:53:50 UTC 2012
A new version of this scenario was uploaded on Sun Nov 25 22:55:29 UTC 2012
A new version of this scenario was uploaded on Sun Nov 25 22:56:21 UTC 2012
A new version of this scenario was uploaded on Sun Nov 25 22:59:04 UTC 2012
A new version of this scenario was uploaded on Sun Nov 25 23:13:09 UTC 2012
A new version of this scenario was uploaded on Sun Nov 25 23:26:46 UTC 2012
A new version of this scenario was uploaded on Mon Nov 26 15:30:48 UTC 2012

See all comments

Want to leave a comment? You must first log in.

Who likes this?

No votes yet.