Advertisement
backlight0815

Untitled

Jun 14th, 2022
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. class Spaceship {
  2. let name: String
  3. var health: Int
  4. var position: Int
  5.  
  6. init(name: String, health : Int, position: Int) {
  7. self.name = name
  8. self.health = health
  9. self.position = position
  10. }
  11.  
  12. func moveLeft() {
  13. position -= 1
  14. }
  15.  
  16. func moveRight() {
  17. position += 1
  18. }
  19.  
  20.  
  21. func wasHit() {
  22. health -= 5
  23. if health <= 0 {
  24. print("Sorry, your ship was hit one too many times. Do you want to play again?")
  25.  
  26. }
  27. }
  28.  
  29. }
  30.  
  31. class Fighter: Spaceship{
  32. let weapon: String
  33. var remainingFirePower: Int
  34.  
  35. init(weapon: String, remainingFirePower: Int,name: String, health: Int, position: Int) {
  36.  
  37. self.weapon = weapon
  38. self.remainingFirePower = remainingFirePower
  39. super.init(name: name, health: health, position: position)
  40.  
  41. }
  42.  
  43. func fire() {
  44. if remainingFirePower > 0 {
  45. remainingFirePower -= 1
  46. } else {
  47. print("You have no more power.")
  48. }
  49. }
  50. }
  51. xx
  52. //let destroyer = Fighter()
  53. //destroyer.weapon = "Laser"
  54. //destroyer.remainingFirePower = 10
  55. //print(destroyer.position)
  56. //destroyer.moveRight()
  57. //print(destroyer.position)
  58.  
  59. //let falcon = Spaceship()
  60. //falcon.weapon = "Twin Gun"
  61. //print(falcon.weapon)
  62.  
  63. //destroyer.fire()
  64. //print(destroyer.remainingFirePower)
  65. //destroyer.fire()
  66. //print(destroyer.remainingFirePower)
  67. //destroyer.fire()
  68. //print(destroyer.remainingFirePower)
  69.  
  70. class ShieldedShip: Fighter {
  71. var shieldStrength: Int
  72.  
  73. init(shieldStrength: Int, weapon: String, remainingFirePower: Int, name: String,health:Int,position:Int)
  74.  
  75. {
  76. self.shieldStrength = shieldStrength
  77. super.init(weapon: weapon, remainingFirePower: remainingFirePower, name: name, health: health, position: position)
  78. }
  79.  
  80. override func wasHit() {
  81. if shieldStrength > 0 {
  82. shieldStrength -= 5
  83. } else {
  84. super.wasHit()
  85. }
  86. }
  87. }
  88.  
  89. //let defender = ShieldedShip()
  90. //defender.name = "Defender"
  91. //defender.weapon = "Cannon"
  92. //defender.moveRight()
  93. //print(defender.position)
  94. //defender.fire()
  95. //print(defender.remainingFirePower)
  96.  
  97. //defender.wasHit()
  98. //print(defender.shieldStrength)
  99. //print(defender.health)
  100.  
  101. let falcon = Spaceship(name: "Falcon", health:100, position:0)
  102.  
  103. let destroyer = Fighter(weapon: "Laser", remainingFirePower:10, name: "Destroyer", health:60, position:0)
  104.  
  105. let defender = ShieldedShip(shieldStrength: 20, weapon: "Cannon", remainingFirePower:8, name: "Defender",health:80, position:0)
  106.  
  107. let sameShip = falcon
  108. print(falcon.position)
  109. print(sameShip.position)
  110. sameShip.moveLeft()
  111. print(falcon.position)
  112. print(sameShip.position)
  113.  
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement