Advertisement
Jorell_Ramos_Sinaga

Untitled

Oct 3rd, 2021
1,406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.85 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>More Object with Class</title>
  6. </head>
  7. <body>
  8.  
  9.     <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  10.     <script type="text/javascript">
  11.        
  12.         //Car constructor
  13.         class Car { // 2015 ES6 JS
  14.             constructor (x, y){ // def __init__
  15.                 this.x = x;
  16.                 this.y = y;
  17.                 this.odometer = 0;
  18.  
  19.             }
  20.  
  21.             draw(){
  22.                 let carHtml = '<img src="img/car.png">'
  23.  
  24.                 this.carElement = $(carHtml);
  25.  
  26.                 this.carElement.css({
  27.                     position: "absolute",
  28.                     left: this.x,
  29.                     top: this.y
  30.                 });
  31.  
  32.                 $("body").append(this.carElement);
  33.             }
  34.  
  35.             moveRight(){
  36.                 this.x += 100
  37.  
  38.                 this.carElement.css({
  39.                     left: this.x,
  40.                     top: this.y
  41.                 });
  42.             }
  43.  
  44.             moveLeft(){
  45.                 this.x -= 100
  46.  
  47.                 this.carElement.css({
  48.                     left: this.x,
  49.                     top: this.y
  50.                 });
  51.             }
  52.  
  53.             moveUp(){
  54.                 this.y -= 100
  55.  
  56.                 this.carElement.css({
  57.                     left: this.x,
  58.                     top: this.y
  59.                 });
  60.             }
  61.  
  62.             moveDown(){
  63.                 this.y += 100
  64.  
  65.                 this.carElement.css({
  66.                     left: this.x,
  67.                     top: this.y
  68.                 });
  69.             }
  70.         };
  71.  
  72.         let tesla = new Car(10, 20);
  73.         let toyota = new Car(100, 200);
  74.  
  75.         tesla.draw();
  76.         tesla.moveDown();
  77.         tesla.moveRight();
  78.         toyota.draw();
  79.  
  80.     </script>
  81. </body>
  82. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement