Scratch Sprites - What should init do?
The Obvious
In the previous section I decided that I felt that the following were the "obvious" data that we stored about each sprite.
- Name - as a string
- xLocation - as an integer (assumed to be -240 to +240)
- yLocation - as an integer (assumed to be -180 to +180)
- size - as an integer (assumed to be 0 to 100)
- Direction - as an integer (assumed to be -180 to +180)
- Show - as a Boolean
So what is the default value of each of these?
Five of these six values have easy defaults. Each of the number has a CLEAR default when a new version is made:
- The sprite starts as (0,0). This means x and y each default to zero.
- The sprite is full size. This means size defaults to 100
- The sprite is always pointing to the right. This means directions defaults to 90.
- The sprite is always visible. This menas show defaults to True
The only one that we can't easily default is the name.
Scratch itself give it a name and will automatically add a number to this if you have more than one of the same "kind" [eg. Cloud, Cloud2, Cloud3, etc] This is done through the use of a special kind of "class variable" which is different from the "instance variables" we are discussing. We won't actually look at how to solve this in Python. Instead, we will just make the decision that the name must be provided by the creator. This is similar to what we talked about with the CreditCard class. We said that the balance can start at zero by default but the owner MUST be provided when a new instance is created.
Class Diagrams
When instances of a class get their values from the init function (aka the constructor) we normally include that information in the class diagram. Thus, the new and improved class diagram might look like this: