Greenfoot is gratis beschikbaar voor Microsoft Windows, Mac OS X, and Linux.ģ De Java taal « Object georienteerd » taalĮén van de meest gebruikte programmeertalen te wereld Ontwikkelen van applicaties voor verschillende systemen Windows, Mac, Linux, Android, … Is gemaakt om de Java taal te leren kennen. If there are no changes (no keys pressed to change the direction of the 'head'), the 'head' will continue in the direction last instructed.2 Greenfoot Ontwikkelomgeving gebaseerd op de Java programmeertaal. 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. The first statement (the 'if') in the 'act' method of the Snake class puts a condition on executing the code located there. There will be many cycles repeated every second, which is what puts 'life' into your world. It is called one cycle (or frame) when all have executed once. All objects (world and actors) will each have a turn at their 'act' methods being executed. The method you are looking for is the 'act' method within the Snake class. 480 and 10 from staring point and you have x= 490. if the snake head start att x=10 it will go 24 step to reach the opposite side. So the snake are moving perfectly at the world edge. The speed is 20 the same as the size of the snake. therefore the snake hit the item exactly in the correct position. I use the same idea for both snake and item. This is because object is placed in the center of it's x and y value. If the item is placed at 10, 10 is going to be added in the top left corner. So it can be placed 10, 30, 50, 70 90 and so on in both x and y. Max x and y value can therefore be 490x and 490y. These are multiplied with 20 then i add 10. r1 and r2 get random number from 0 to 24. The code is written like this because i want the snake to always hit the item directly on it and not a side hit. What this does is if the item is placed on the wall it gets new x and y value.Īctor item = getOneIntersectingObject(Wall.class) 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?). *the 'Actor body =." gets the snake object that is located two places forward from the head of the snake, if one exists there. *the checks in the 'if statement make sure the snake is in world limits after a tentative move, checking each edge *"((),()) " 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 * see on how to construct a 'for' loop and 'i++' is the same as 'i=i+1' 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |