Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Spaceship {
- let name: String
- var health: Int
- var position: Int
- init(name: String, health : Int, position: Int) {
- self.name = name
- self.health = health
- self.position = position
- }
- func moveLeft() {
- position -= 1
- }
- func moveRight() {
- position += 1
- }
- func wasHit() {
- health -= 5
- if health <= 0 {
- print("Sorry, your ship was hit one too many times. Do you want to play again?")
- }
- }
- }
- class Fighter: Spaceship{
- let weapon: String
- var remainingFirePower: Int
- init(weapon: String, remainingFirePower: Int,name: String, health: Int, position: Int) {
- self.weapon = weapon
- self.remainingFirePower = remainingFirePower
- super.init(name: name, health: health, position: position)
- }
- func fire() {
- if remainingFirePower > 0 {
- remainingFirePower -= 1
- } else {
- print("You have no more power.")
- }
- }
- }
- xx
- //let destroyer = Fighter()
- //destroyer.weapon = "Laser"
- //destroyer.remainingFirePower = 10
- //print(destroyer.position)
- //destroyer.moveRight()
- //print(destroyer.position)
- //let falcon = Spaceship()
- //falcon.weapon = "Twin Gun"
- //print(falcon.weapon)
- //destroyer.fire()
- //print(destroyer.remainingFirePower)
- //destroyer.fire()
- //print(destroyer.remainingFirePower)
- //destroyer.fire()
- //print(destroyer.remainingFirePower)
- class ShieldedShip: Fighter {
- var shieldStrength: Int
- init(shieldStrength: Int, weapon: String, remainingFirePower: Int, name: String,health:Int,position:Int)
- {
- self.shieldStrength = shieldStrength
- super.init(weapon: weapon, remainingFirePower: remainingFirePower, name: name, health: health, position: position)
- }
- override func wasHit() {
- if shieldStrength > 0 {
- shieldStrength -= 5
- } else {
- super.wasHit()
- }
- }
- }
- //let defender = ShieldedShip()
- //defender.name = "Defender"
- //defender.weapon = "Cannon"
- //defender.moveRight()
- //print(defender.position)
- //defender.fire()
- //print(defender.remainingFirePower)
- //defender.wasHit()
- //print(defender.shieldStrength)
- //print(defender.health)
- let falcon = Spaceship(name: "Falcon", health:100, position:0)
- let destroyer = Fighter(weapon: "Laser", remainingFirePower:10, name: "Destroyer", health:60, position:0)
- let defender = ShieldedShip(shieldStrength: 20, weapon: "Cannon", remainingFirePower:8, name: "Defender",health:80, position:0)
- let sameShip = falcon
- print(falcon.position)
- print(sameShip.position)
- sameShip.moveLeft()
- print(falcon.position)
- print(sameShip.position)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement