Advertisement
CleverCode

Car Animation with Processing

Feb 24th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.83 KB | None | 0 0
  1. import processing.sound.*;
  2.  
  3. SoundFile file;
  4.  
  5. String audioName = "SuonoMotore.mp3";
  6. String path;
  7.  
  8. Car car;
  9. Dash dash1;
  10. Dash dash2;
  11. Dash dash3;
  12. Dash dash4;
  13. Dash dash5;
  14. Tree tree1;
  15. Tree tree2;
  16. Tree tree3;
  17. Tree tree4;
  18. Tree tree5;
  19. Tree tree6;
  20. Tree tree7;
  21. Tree tree8;
  22. Tree tree9;
  23. Tree tree10;
  24. Tree tree11;
  25. Tree tree12;
  26. Cloud cloud1;
  27. Cloud cloud2;
  28. Cloud cloud3;
  29. Cloud cloud4;
  30. Cloud cloud5;
  31. Cloud cloud6;
  32. Cloud cloud7;
  33. Cloud cloud8;
  34. Cloud cloud9;
  35. Cloud cloud10;
  36.  
  37. void setup() {
  38.   size(720, 240);
  39.   smooth();
  40.  
  41.   //create the lines on the road
  42.   dash1 = new Dash(0);
  43.   dash2 = new Dash(width/4);
  44.   dash3 = new Dash(-width/4);
  45.   dash4 = new Dash(-width/2-30);
  46.  
  47.   //create the clouds
  48.   cloud1 = new Cloud();
  49.   cloud2 = new Cloud();
  50.   cloud3 = new Cloud();
  51.   cloud4 = new Cloud();
  52.   cloud5 = new Cloud();
  53.   cloud6 = new Cloud();
  54.   cloud7 = new Cloud();
  55.   cloud8 = new Cloud();
  56.   cloud9 = new Cloud();
  57.   cloud10 = new Cloud();
  58.  
  59.   //create the trees
  60.   tree1 = new Tree();
  61.   tree2 = new Tree();
  62.   tree3 = new Tree();
  63.   tree4 = new Tree();
  64.   tree5 = new Tree();
  65.   tree6 = new Tree();
  66.   tree7 = new Tree();
  67.   tree8 = new Tree();
  68.   tree9 = new Tree();
  69.   tree10 = new Tree();
  70.   tree11 = new Tree();
  71.   tree12 = new Tree();
  72.  
  73.   //create the car
  74.   car = new Car("car.png", 0, 25, 400, 132);
  75. }
  76.  
  77. float j = random(4);
  78. float z = random(5);
  79.  
  80. int value = #0087DA;
  81.  
  82. void mousePressed() {
  83.   if (value == #0087DA) {
  84.     value = #0019FF;
  85.   } else {
  86.     value = #0087DA;
  87.   }
  88. }
  89.  
  90. void draw() {  
  91.   float mid = width/2;
  92.   float cen = height/2;
  93.  
  94.   background(value);
  95. /*
  96.   if (mousePressed == true) {
  97.     background(0, 25, 255);
  98.   } else {
  99.     background(0, 135, 218);
  100.   }
  101. */
  102.   if (key == ' ') {
  103.     PlaySound();
  104.   }
  105.  
  106.   translate(mid, cen);
  107.  
  108.   //draw and move the road
  109.   drawRoad();
  110.   dash1.move();
  111.   dash1.display();
  112.   dash2.move();
  113.   dash2.display();
  114.   dash3.move();
  115.   dash3.display();
  116.   dash4.move();
  117.   dash4.display();
  118.  
  119.   //draw and move the clouds
  120.   cloud1.move();
  121.   cloud1.display();
  122.   cloud2.move();
  123.   cloud2.display();
  124.   cloud3.move();
  125.   cloud3.display();
  126.   cloud4.move();
  127.   cloud4.display();
  128.   cloud5.move();
  129.   cloud5.display();
  130.   cloud6.move();
  131.   cloud6.display();
  132.   if (j>3) {
  133.     cloud7.move();
  134.     cloud7.display();
  135.     cloud8.move();
  136.     cloud8.display();
  137.   }
  138.   if (j>3.5) {
  139.     cloud9.move();
  140.     cloud9.display();
  141.   }
  142.   if (j>3.75) {
  143.     cloud10.move();
  144.     cloud10.display();
  145.   }  
  146.  
  147.   //draw and move the trees
  148.   tree1.move();
  149.   tree1.display();
  150.   tree2.move();
  151.   tree2.display();
  152.   tree3.move();
  153.   tree3.display();
  154.   tree4.move();
  155.   tree4.display();
  156.   tree5.move();
  157.   tree5.display();
  158.   tree6.move();
  159.   tree6.display();
  160.   if (z>3) {
  161.     tree7.move();
  162.     tree7.display();
  163.     tree8.move();
  164.     tree8.display();
  165.   }
  166.   if (z>4) {
  167.     tree9.move();
  168.     tree9.display();
  169.     tree10.move();
  170.     tree10.display();
  171.   }
  172.   if (z>4.5) {
  173.     tree11.move();
  174.     tree11.display();
  175.   }
  176.   if (z>4.75) {
  177.     tree12.move();
  178.     tree12.display();
  179.   }
  180.  
  181.   //draw and move the car
  182.   car.sway();
  183.   car.display();
  184.  
  185.   String text = "Created by ...";
  186.   textSize(20);
  187.   fill(255);                    
  188.   text(text, -350, 100);
  189. }
  190.  
  191. void PlaySound() {
  192.   path = sketchPath(audioName);
  193.   file = new SoundFile(this, path);
  194.   file.play();
  195. }
  196.  
  197. void drawRoad() {  
  198.   rectMode(CENTER);
  199.  
  200.   //shoulder
  201.   noStroke();
  202.   fill(92, 26, 3);
  203.   rect(0, height/4, width, height/2);
  204.  
  205.   //asphalt
  206.   fill(96, 96, 96);
  207.   rect(0, height/4+9, width+20, height/2 - 32);
  208.   strokeWeight(1);
  209.  
  210.   //edges
  211.   fill(245, 246, 250);
  212.   rect(0, 32, width, 4);
  213.   rect(0, height/2-15, width, 4);
  214. }
  215.  
  216. class Car {
  217.   float w;
  218.   float h;
  219.   float xpos;
  220.   float ypos;
  221.   float angle;
  222.   float wheelX1;
  223.   float wheelX2;
  224.   float wheelY1;
  225.   float wheelY2;
  226.   PImage carShape;
  227.   float yoffset = 0.0;
  228.  
  229.   Car(String shapeName, float tempX, float tempY, float sizeX, float sizeY) {
  230.     carShape = loadImage(shapeName);
  231.     w = sizeX;
  232.     h = sizeY;
  233.     xpos = tempX;
  234.     ypos = tempY;
  235.     wheelX1 = tempX + width/6 + 3;
  236.     wheelY1 = tempY + height/7;
  237.     wheelX2 = tempX - width/6+11;
  238.     wheelY2 = tempY + height/7 +3;
  239.     angle = random(0, TWO_PI);
  240.   }
  241.  
  242.   void sway() {
  243.     angle += 0.05;
  244.     yoffset = sin(angle)*3;
  245.   }
  246.  
  247.   void display() {
  248.     imageMode(CENTER);
  249.  
  250.     //brake light
  251.     stroke(0);
  252.     strokeWeight(2);
  253.     fill(219, 7, 7);
  254.     quad(-width/4 - 15, ypos + yoffset, -width/4 +15, ypos +yoffset, -width/4 +15, ypos-30 +yoffset, -width/4-12, ypos-30+yoffset);
  255.     strokeWeight(1);
  256.     fill(171, 7, 7);
  257.     quad(-width/4 - 15, ypos + yoffset, -width/4 +15, ypos +yoffset, -width/4 +15, ypos-12 +yoffset, -width/4-15, ypos-12+yoffset);
  258.  
  259.     //head light
  260.     noStroke();
  261.     fill(255, 204, 64);
  262.     quad(width/5+20, ypos -15 +yoffset, width/5+42, ypos-8+yoffset, width/5+52, ypos+yoffset + 20, width/5+20, ypos+yoffset +20);
  263.     stroke(0);
  264.     fill(221, 112, 11);
  265.     quad(width/5+20, ypos -15 +yoffset, width/5+33, ypos-8+yoffset, width/5+33, ypos+yoffset + 20, width/5+20, ypos+yoffset +20);
  266.  
  267.     //tinted windows
  268.     noStroke();
  269.     fill(120, 200);
  270.     quad(-width/5, ypos+yoffset, width/6, ypos+yoffset, width/25, ypos+yoffset - 60, -width/7, ypos+yoffset-60);
  271.  
  272.     //car body
  273.     image(carShape, xpos, ypos + yoffset, w, h);    
  274.  
  275.     //wheels
  276.     fill(217, 227, 208);
  277.     ellipse(wheelX1, wheelY1 + yoffset, 48, 46);
  278.     ellipse(wheelX2, wheelY2 + yoffset, 48, 46);
  279.  
  280.     //rotate wheel lines
  281.     stroke(0);
  282.     pushMatrix();
  283.     translate(wheelX1, wheelY1+yoffset);
  284.     rotate(angle*4);
  285.     line(0, 23, 0, -23);
  286.     line(-24, 0, 24, 0);
  287.     popMatrix();
  288.     pushMatrix();
  289.     translate(wheelX2, wheelY2+yoffset);
  290.     rotate(angle*4);
  291.     line(0, 23, 0, -23);    
  292.     line(-24, 0, 24, 0);
  293.     popMatrix();
  294.   }
  295. }
  296.  
  297. class Dash {
  298.   float xpos;
  299.   float ypos = height/4+6;
  300.   float w = 25;
  301.   float h = 5;
  302.   float speed = 4.25;
  303.  
  304.   Dash(float tempXpos) {
  305.     xpos = tempXpos;
  306.   }
  307.  
  308.   void display() {
  309.     fill(255, 234, 151);
  310.     rectMode(CENTER);
  311.     rect(xpos, ypos, w, h);
  312.   }
  313.  
  314.   void move() {
  315.     xpos = xpos - speed;
  316.     if (xpos < -width/2-w) {
  317.       xpos = width/2+w;
  318.     }
  319.   }
  320. }
  321.  
  322. class Tree {
  323.   float xpos;
  324.   float ypos;
  325.   float w;
  326.   float h;
  327.   float speed = 4;
  328.   float r;
  329.   float g;
  330.   float b;
  331.   float k = random(5);
  332.  
  333.   Tree() {
  334.     xpos = random(-width/2, width/2);
  335.     ypos = random(8, 23);
  336.     w = random(10, 25);    
  337.     if (w<17) {
  338.       h = 1.618*w*random(1, 3);
  339.     } else {
  340.       h = 1.618*w*random(1.5, 2.5);
  341.     }
  342.   }
  343.  
  344.   void display() {
  345.     noStroke();
  346.  
  347.     //trunk
  348.     fill(143, 114, 87);
  349.     //rect(xpos, ypos-3*h/8, w/2, 7*h/4);
  350.     rect(xpos, ypos-5*h/16, w/2, 5*h/8);
  351.  
  352.     //leaves
  353.     if (k>0) {
  354.       r = 71;
  355.       g = 135;
  356.       b = 25;
  357.     }    
  358.     fill(r, g, b);
  359.     triangle(xpos, ypos-h, xpos-w, ypos-h/6, xpos+w, ypos-h/6);
  360.   }
  361.  
  362.   void move() {
  363.     xpos = xpos - speed;
  364.     if (xpos < -width/2-2*w) {
  365.       xpos = width/2+w;
  366.     }
  367.   }
  368. }
  369.  
  370. class Cloud {
  371.   float xpos;
  372.   float ypos;
  373.   float r;
  374.   float speed;
  375.   float c;
  376.   float c1 = random(125, 150);
  377.   float c2 = random(150, 175);
  378.   float c3 = random(175, 225);
  379.   float k = random(6);
  380.   float v;
  381.  
  382.   Cloud() {
  383.     xpos = random(-width/2, width/2);
  384.     ypos = random(-3*height/8, -height/4);
  385.     r = random(10, 40);
  386.   }
  387.  
  388.   void display() {
  389.     noStroke();
  390.     v = map(r, 10, 40, 1.5, 1);
  391.     if (k>5) {
  392.       c = c1;
  393.       speed = 2*v;
  394.     } else if (k>4) {
  395.       c = c2;
  396.       speed = 2.25*v;
  397.     } else {
  398.       c = c3;
  399.       speed = 2.5*v;
  400.     }
  401.     fill(c);
  402.     ellipse(xpos, ypos, 2*r, 2*r);
  403.     ellipse(xpos+3*r/4, ypos+r/4, 3*r/2, 3*r/2);
  404.     ellipse(xpos-3*r/4, ypos+r/4, 3*r/2, 3*r/2);
  405.   }
  406.  
  407.   void move() {
  408.     xpos = xpos - speed;
  409.     if (xpos < -width/2 - 9*r/2) {
  410.       xpos = width/2 +9*r/2;
  411.     }
  412.   }
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement