EvgeniyPugach

DZ3.2

Mar 21st, 2020
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.47 KB | None | 0 0
  1. import kotlin.math.pow
  2. import kotlin.math.sqrt
  3.  
  4.  
  5. interface Movable {
  6.     fun move()
  7. }
  8.  
  9. class Player(name: String, strength: Int, x: Double, y: Double, width: Int, hight: Int) : GameObject(x, y, width, hight),
  10.         Movable {
  11.    
  12.     override fun move() {
  13.         println("Car rides")
  14.     }
  15. }
  16.  
  17. class Enemy(name: String, strength: Int, x: Double, y: Double, width: Int, hight: Int) : GameObject(x, y, width, hight),
  18.         Movable {
  19.  
  20.     override fun move() {
  21.         println("Сar rides slow")
  22.     }
  23. }
  24.  
  25. class Block(nature: String, strength: Int, x: Double, y: Double, width: Int, hight: Int) : GameObject(x, y, width, hight) {
  26.     var name: String = ""
  27. }
  28.  
  29. abstract class GameObject(x: Double, y: Double, width: Int, hight: Int) {
  30.  
  31.     var x: Int = 0
  32.     var y: Int = 0
  33.     var width = 0
  34.     var hight = 0
  35.     fun isIntersects(anotherObject: GameObject): Boolean {
  36.         var result : Boolean = false
  37.         if (sqrt((this.x - anotherObject.x).toDouble().pow(2) +
  38.                         (this.y - anotherObject.y).toDouble().pow(2)) == 0.0) result = true
  39.  
  40.         return result
  41.     }
  42. }
  43.  
  44. fun main() {
  45.     var one: Player = Player("BMW", 10, 4.0, 1.0, 25, 50)
  46.     var two: Enemy = Enemy("Honda", 30, 3.0, 5.0, 60, 55)
  47.     var wall: Block = Block("wall", 0, 4.0, 3.0, 5, 80)
  48.  
  49.     println(one.isIntersects(two))
  50.     println(one.isIntersects(wall))
  51.  
  52.     PrintMove(one)
  53.     PrintMove(two)
  54. }
  55.  
  56. fun PrintMove (printMove: Movable) {
  57.     printMove.move()
  58. }
Advertisement
Add Comment
Please, Sign In to add comment