Exercise – Create a Subclass
Note:
The exercises below are based on a game where a spaceship avoidsobstacles in space. The ship is positioned at the bottom of acoordinate system and can only move left and right while obstacles”fall” from top to bottom. Throughout the exercises, you’ll createclasses to represent different types of spaceships that can be usedin the game. The base class Spaceship has been provided for youbelow.
class Spaceship {
var name: String = “”
var health = 100
var position = 0
func moveLeft() {
position -= 1
}
func moveRight() {
position += 1
}
func wasHit() {
health -= 5
}
}
Define a new class Fighter that inherits from Spaceship. Add
PayPal Gateway not configured
PayPal Gateway not configured