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

Comments for Lab 6 – Snake

Return to Lab 6 – Snake

A new version of this scenario was uploaded on Mon Nov 19 23:24:58 UTC 2012
A new version of this scenario was uploaded on Tue Nov 20 00:46:19 UTC 2012
A new version of this scenario was uploaded on Tue Nov 20 00:46:45 UTC 2012
A new version of this scenario was uploaded on Tue Nov 20 00:49:21 UTC 2012
A new version of this scenario was uploaded on Tue Nov 20 16:04:18 UTC 2012
A new version of this scenario was uploaded on Tue Nov 20 16:04:31 UTC 2012
A new version of this scenario was uploaded on Tue Nov 20 16:08:08 UTC 2012
A new version of this scenario was uploaded on Wed Nov 21 09:45:38 UTC 2012
A new version of this scenario was uploaded on Wed Nov 21 09:45:45 UTC 2012
A new version of this scenario was uploaded on Wed Nov 21 20:55:15 UTC 2012
A new version of this scenario was uploaded on Wed Nov 21 20:56:03 UTC 2012
A new version of this scenario was uploaded on Wed Nov 21 21:01:10 UTC 2012
hkrhässleholmhkrhässleholm

2012/11/22

Hi Malmotri! I don't understand almost anything your "Snake" class! Could anyone please explain the codes? Specially the part I am mentioning down. public void act() { if (segment == "head") { Board board = (Board)getWorld(); for (int i = length - 1; i > 0; i--) board.snake[i].setLocation(board.snake[i - 1].getX(), board.snake[i - 1].getY()); *Why do we need to write the codes in act method? *What do "length-1" and "i--" do? *"board.snake[i].setLocation(board.snake[i - 1].getX(), board.snake[i - 1].getY());"- what does it do? *Explain the "move" method". Here : if (getX() + speedX < 0 || getX() + speedX >= 584 || getY() + speedY < 0 || getY() + speedY >= 585 -what do they do? * Actor body = getOneObjectAtOffset(speedX * 2, speedY * 2, Snake.class); -what is happening there?
danpostdanpost

2012/11/22

@hkrhässleholm, it is not horrible programming to have code in the act method, it just makes it less readable when there is alot of code there, which would becomes poor programming. * see http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html on how to construct a 'for' loop; and 'i++' is the same as 'i=i+1' *"board.snake[i].setLocation(board.snake[i-1].getX(),board.snake[i-1].getY());" move the snake (segment) to the position of the one in front of it, repeating this starting from the back of the snake will move the whole snake body forward one step *the checks in the 'if statement make sure the snake is in world limits after a tentative move, checking each edge *the 'Actor body =..." gets the snake object that is located two places forward from the head of the snake, if one exists there. What the code is doing is looking ahead 2 places to see if the head of the snake will run into its body; in context, by using this, the code is saying "if the head moves toward the body and will hit the body on the next move if allowed to continue in the same direction, then end the game right then." The game ends on a tentative collision, not on a collision (programmer preference?). I hope that answers your questions.
hkrhässleholmhkrhässleholm

2012/11/22

Hi thanks for your explanation! But something is confusing to me still now! for example, here: getX() + speedX >= 584 the value 584 has nothing to do with the speed where the value of speed is already assigned before. So why do we need to check the size of the world if its bigger than 584 or equal in term of "speedX"?
danpostdanpost

2012/11/22

@hkrhässleholm, if 'getX()' returns the x-coordinate at which 'this' object is at, then 'getX() + speedX' would be the location at which the object will be after its next move. Comparing that value with the maximum limit in the x direction that the object can move determines whether it will move out of bounds or not. Sidenote: I just looked at the scenario's code and the world is created with 'super(500, 500, 1);' and the world edge checks for right and down are now compared to 500. A better coding would be getX() + speedX >= getWorld().getWidth()
jabirfatah91jabirfatah91

2012/11/22

OHH! Those were helpful for me too. But more questions has been discovered. Form the "Board" class. 1. What does "addArray" method do? 2. What does this line "System.arraycopy(snake, 0, newSnake, 0, snake.length);" mean? 3. What the sign "<>" is used and what the line "List<Item> item = getObjects(Item.class);" does actually? From the "Item" class: 1. by referring this line "Actor item = getOneIntersectingObject(Wall.class);"- why "Wall.class" is used to get an intersecting object? Souldn't it be "Snake.class"?
danpostdanpost

2012/11/22

@jabirfatah91 1. the "addArray" method basically increases the size of the 'snake' array 2. refer to the System class API for the low-down on 'arraycopy' http://docs.oracle.com/javase/7/docs/api/java/lang/System.html 3. the "<className>" indicates what type of objects will be stored in the list created; "List<Item>item=getObjects(Item.class);" creates a list object called 'item' to hold Item class objects and populates the list with the objects returned from the 'getObjects(Item.class)' call Item class: 1. why is"Wall.class" is used? Well, actually, it probably should have both checks in there; but, the Snake cannot go into the Wall object to 'eat' the Item class object; so, the 'act' method of the Item class relocates it until it is not located within a wall.
hkrhässleholmhkrhässleholm

2012/11/24

Hi danpost again! In the food class: What does these codes do actually? [Actor food=getOneIntersectingObject(SnakeHunter.class); if(food!=null){ a=Greenfoot.getRandomNumber(25); b=Greenfoot.getRandomNumber(25); setLocation(a*20+10, b*20+10);] I tried by removing line "b=Greenfoot.getRandomNumber(25);" and "setLocation(a*20+10, b*20+10);] "- but they work fine!
hkrhässleholmhkrhässleholm

2012/11/24

OOps!! In this game its in the Item class.
MalmotriMalmotri

2012/11/24

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); }
hkrhässleholmhkrhässleholm

2012/11/24

Hi malmotri! Thanks it's clear to me now. Okay, one more thing I want to ask. In your "Board" class, inside the constructor Board, you have written a line- [ snake[0] = new Snake("head",color1);] Well, I clearly undersatand that the term "head" is snake's head and the "color1" is the corresponded color that you have chosen for the head. But my question is that (1)HOW THE GREENFOOT PROGRAM KNOWS(OR IDENTIFY THE TERM) THAT IT'S THE HEAD OF THE SNAKE? (2) IS IT ACTUALLY IMPORTED FROM GREENFOOT?
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