A computer game Character moves around a game area which has dimensions of width:500px, height:300px.
A Character can move left, right, up and down. It can also eat an apple or hit a cactus plant.
When a Character is initialised its x-coordinate is always 0 and its y-coordinate is always 150 - ie it is placed on the left of the game area, in the middle. Its score is also initialised to 0.
This table describes Character class methods:
| method | description |
|---|---|
| constructor | initialises x-coordinate to 0, y-coordinate to 150 and score to 0 |
| up | decrements the y-coordinate by one. If the new value is less than 0, the y-coordinate is set to 300 |
| down | increments the y-coordinate by one. If the new value is greater than 300, the y-coordinate is set to 0 |
| left | decrements the x-coordinate by one. If the new value is less than 0, the x-coordinate is set to 500 |
| right | increments the x-coordinate by one. If the new value is greater than 500, the x-coordinate is set to 0 |
| eatApple | increments the score by one. Calls method eatAppleSound() |
| bumpCactus | decrements the score by one. Calls method bumpCactusSound() |
| eatAppleSound | plays an apple eating sound |
| bumpCactusSound | plays a bump sound |
| getScore | returns the object's current score |
| getX | returns the x-coordinate |
| getY | returns the y-coordinate |
| getCoordinates | returns a string displaying the current position of the character eg "x:0 ; y:5" |
Design the Character class
Design test stubs for the eatAppleSound and bumpCactusSound methods. For example, they can just include print statements.
In a computer program we should be able to construct a new Character as follows:
c = Character()
Test your design with this program:
c = Character()
while c.getX()<500:
c.right()
print(c.getCoordinates())
while c.getY()<300:
c.down()
print(c.getCoordinates())
Save your solution as character-1.py
Design a new Character class method, checkCenter.
This method increments the score by 2 every time the Character crosses the centre of the game area, x-coordinate 250, y-coordinate 150.
Write some code to test that your solution works. For example, everytime a method is called that updates the position of the Character, checkCenter should be called next.
Save your solution as character-2.py
Every time the Character moves across the centre of the stage, the co-ordinates and the current score are added to a file, centreLog.txt
The coordinates will ofcourse always be (250,150) but the score will be different in each log entry.
Here is a list of possible log entries in centerLog.txt:
| "x:250 ; y:150 ; score:3" |
| "x:250 ; y:150 ; score:1" |
| "x:250 ; y:150 ; score:7" |
| "x:250 ; y:150 ; score:3" |
Design a solution to this problem. Be creative. Use your skills and knowledge.
Save your solution as character-3.py
Write pseudocode for the entire module which contains your solution to Task 3.
Make sure you are thoughtful when testing your solution.
If your code has a validation check, test your code with acceptable/valid data and unacceptable/invalid data.