Loading...
Logo
Processing Forum
hello geeks, i'm still very new to computer programming, and I decided to get creative so i came up with the sketch in the screenshot below in Processing, LOL, it might look very basic as its just a static sketch but it took me close to 3hrs to come up with it as i'm still an amateur, so here's my question, how do I make the cars on the highway move back and forth, as in "Perpetual Motion" . I hope to get a reply from you guys as soon as possible. Thanks!

Replies(1)

You need a draw() loop. Then you can use variables instead of static numbers or maybe even methods like sin(), noise() etc. See all the examples that come with Processing, about 99% of these sketches are dynamic / interactive and use the setup() and draw() methods. Also check out the learning and reference sections on the website.

Note that you can post code on the forum as shown below.

Code Example
Copy code
  1. color blue = color(0, 0, 255);
  2. color orange = color(255, 160, 0);
  3.  
  4. void setup() {
  5.   size(400, 700);
  6.   rectMode(CENTER);
  7.   smooth();
  8. }
  9.  
  10. void draw() {
  11.   background(0);
  12.   strokeWeight(12);
  13.   stroke(255);
  14.   float h = height/8;
  15.   for (int i=0; i<8; i++) {
  16.     float y = (i*h*1.5+frameCount*2)%(height+h)-h;
  17.     line(width/2, y, width/2, y+h);
  18.   }
  19.   drawCar(noise(frameCount*0.005)*width/2, sin(frameCount*0.005)*height/2+height/2, orange);
  20.   drawCar(mouseX, mouseY, blue);
  21. }
  22.  
  23. void drawCar(float x, float y, color c) {
  24.   noStroke();
  25.   fill(255);
  26.   ellipse(x-22, y-30, 10, 10);
  27.   ellipse(x+23, y-30, 10, 10);
  28.   ellipse(x-22, y+30, 10, 10);
  29.   ellipse(x+23, y+30, 10, 10);
  30.   strokeWeight(2);
  31.   stroke(255);
  32.   fill(c);
  33.   rect(x, y, 40, 70);
  34.   line(x-20, y-35, x-10, y-50);
  35.   line(x+20, y-35, x+10, y-50);
  36.   line(x-10, y-50, x+10, y-50);
  37.   line(x-20, y+35, x-10, y+50);
  38.   line(x+20, y+35, x+10, y+50);
  39.   line(x-10, y+50, x+10, y+50);
  40. }