Week 2 - You Try It, Part 2 [Optional]
Python Object to model a Scratch Sprite
Code Activity
Earlier I showed you working code for two classes:
- CreditCard.py (and it you like, here is the Wallet code to go with it.)
- Fraction.py
While we will not make a habit of having you write classes in this course - mostly just using them - I think it is worth TRYING to write a class from scratch. In fact, let's have you write a Python version of the Scratch Sprite that we designed together last week.
Using the CreditCard and Fraction examples from above, create a Python Class called Sprite which you save in a file named Sprite.py. This class should provide all of the data and functionality that was outlined in the class diagram shown below:
Most of these are fairly straight forward because they are simple operations. But a few of them have some error checking and one even has (heaven forbid) trigonometry. And non-standard trigonometry at that. Since my MAIN learning outcome with this is really just to get you to explore with the structure of Python class code, I will give you the option to create one of two versions.
A class called SimpleSprite in a file called SimpleSprite.py
- This version does not do any error checking about the validity of parameters.
- If the user says mySprite.setX(5000) then it is allowed.
- If the current direction is 90 and the user says mySprite.turnClockwise(100) then it goes to 190 rather than -170 (which is what Scratch would do).
- This version only allows move to happen if the current direction value is one of the four cardinal directions - 0, 90, 180, and -90 for the direction. In other words, a move() will change x or y but not both.
Once you write your Sprite you can download this tester program and run it to see if your outcomes agree with what I was expecting.
If you REALLY want to test yourself:
A class called Sprite in a file called Sprite.py
- This version does everything that the SimpleSprite did but adds some additional functionality that makes it a little closer to the true Scratch Sprite:
- If a user moves or sets an X/Y value that put it beyond the border of the stage (-240 to +240 for x and -180 to +180 for y). It just sets the appropriate value to that limit.
- mySprite.setX(5000) would end up with x=240
- If a change to the angle [either through pointInDirection(), turnClockwise() or turnCounterClockwise() ] were to cause an angle that is not between -180 to +180 inclusively than the value should "wrap" appropriately.
- mySprite.pointInDirection(200) should end up with direction=-160
- mySprite.pointInDirection(160) with mySprite.turnClockwise(400) should end up with direction=-160
- If a sprite is pointing in any valid direction (-180 to +180) and a move() is executed it should calculate the accurate trigonometry of the move.
- mysprite.pointInDirection(30) followed by mySprite.move(100) should create x=50 and y=86.6.
- Note, python has math.sin() and math.cos() in the math libarary. HOWEVER, they expect radians. Fortunately, there is a math.radians() which converts degrees to radians
- import math
- rad = math.radians(45)
- print(math.cos(rad))
- print(math.sin(rad))
- produces:
- 0.7071... as expected
- If a user moves or sets an X/Y value that put it beyond the border of the stage (-240 to +240 for x and -180 to +180 for y). It just sets the appropriate value to that limit.
Once you write your Sprite you can download this tester program and run it to see if your outcomes agree with what I was expecting.