Let us say we want to program our crab so that there is a 10% chance at every step that the crab turns a little bit off course. We will encounter calls to methods that belong to other objects in a later chapter. For example, the signature of Greenfoot s getrandomnumber method is static int getrandomnumber(int limit) This tells us that we must write the name of the class itself (Greenfoot) before the dot in the method call. Methods that belong to the class itself are marked with the keyword static at the beginning of the method signature. The method signature tells us whether a given method belongs to objects of that class, or to the class itself. Both kinds of methods are defined in a class. When a method belongs to an object, we write thod-name ( parameters ) to call it. When methods belong to a class, we write thod-name ( parameters ) to call the method. Note: Static methods Methods may belong to objects or classes. Concept: Methods that belong to classes (as opposed to objects) are marked with the keyword static in their signature. Since the getrandomnumber method is not in the Crab or Animal class, but in a class called Greenfoot, we have to write Greenfoot. When the method is defined in another class, we need to specify the class or object that has the method, followed byĢ 48 Introduction to Programming with Greenfoot a full stop (dot), followed by the method name and parameter. When we called methods that were defined in our own class or inherited, it was enough to write the method name and parameter list. The notation used here is called dot notation. The limit 20 is excluded, so the number is actually in the range 0 to 19. For example, Greenfoot.getRandomNumber(20) will give us a random number between 0 and 20. It will then return a random number between 0 (zero) and the limit. This method, called getrandomnumber, expects a parameter that specifies the limit of the number. We can achieve this in Greenfoot by using random numbers: The Greenfoot environment itself has a method to give us a random number. Concept: When a method we wish to call is not in our own class or inherited, we need to specify the class or object that has the method before the method name, followed by a dot. Crabs don t always go in an exact straight line, so let us add a little random behavior: The crab should go roughly straight, but every now and then it should turn a little off course. But when it walks, it always walks exactly straight. 3.1 Adding random behavior In our current implementation, the crab can walk across the screen, and it can turn at the edge of our world. The first thing we shall look at is adding some random behavior. Adding code will get a little easier from now on, since we have seen many of the fundamental concepts. Now, we will add more interesting behavior. There were many new things that we had to look at. 1 3 Improving the Crab more sophisticated programming topics: concepts: random behavior, keyboard control, sound dot notation, random numbers, defining methods, comments In the previous chapter, we looked at the basics of starting to program our first game.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |