Skip to content

Added Car Instances example |Prof Harris |Open@RIT #973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/data/examples/en/15_Instance_Mode/03_Car_Instances.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* @name Car Instances
* @frame 400,400
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
<b>Prof WM Harris,</b></a> <b>How </b>to create three instances of Car Class and
invoke class methods.<br/>
A function is created for the canvas setup, and
3 car instances are initialized with different colors and canvas
positions. The speed of each car is set by passing value to the
instance’s start method. A second function calls class methods to
display and move the cars.
*/
class Car {
/* Constructor expects parameters for
fill color, x and y coordinates that
will be used to initialize class properties.
*/
constructor(cColor, x, y) {
this.color = cColor;
this.doors = 4;
this.isConvertible = false;
this.x = x;
this.y = y;
this.speed = 0;
}

start(speed) { // method expects parameter!
this.speed = speed;
}

display() { // method!
fill(this.color);
rect(this.x, this.y, 20, 10);
}

move() { // method!
this.x += this.speed;
// Wrap x around boundaries
if (this.x < -20) {
this.x = width;
} else if (this.x > width) {
this.x = -20;
}
}
} //end class Car

let rav4;
let charger;
let nova;

function setup() {
createCanvas(200, 400);
/* Construct the 3 Cars */
//constructor expects cColor, x, y
rav4 = new Car("silver", 100, 300);
charger = new Car("gold", 0, 200);
nova = new Car("blue", 200, 100);
nova.doors = 2; //update nova's doors property

console.log("rav4", rav4);
console.log("charger", charger);
console.log("nova", nova);

//call start methods of Car instances
//the start method expects a number for speed
rav4.start(2.3);
charger.start(-4);
nova.start(random(-1, 1));
}

function draw() {
background("beige");

//display and move all 3 Cars
rav4.display();
charger.display();
nova.display();

rav4.move();
charger.move();
nova.move();
}
82 changes: 82 additions & 0 deletions src/data/examples/es/15_Instance_Mode/03_Car_Instances.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* @name Car Instances
* @frame 400,400
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
<b>Prof WM Harris,</b></a> <b>How </b>to create three instances of Car Class and
invoke class methods.<br/>
A function is created for the canvas setup, and
3 car instances are initialized with different colors and canvas
positions. The speed of each car is set by passing value to the
instance’s start method. A second function calls class methods to
display and move the cars.
*/
class Car {
/* Constructor expects parameters for
fill color, x and y coordinates that
will be used to initialize class properties.
*/
constructor(cColor, x, y) {
this.color = cColor;
this.doors = 4;
this.isConvertible = false;
this.x = x;
this.y = y;
this.speed = 0;
}

start(speed) { // method expects parameter!
this.speed = speed;
}

display() { // method!
fill(this.color);
rect(this.x, this.y, 20, 10);
}

move() { // method!
this.x += this.speed;
// Wrap x around boundaries
if (this.x < -20) {
this.x = width;
} else if (this.x > width) {
this.x = -20;
}
}
} //end class Car

let rav4;
let charger;
let nova;

function setup() {
createCanvas(200, 400);
/* Construct the 3 Cars */
//constructor expects cColor, x, y
rav4 = new Car("silver", 100, 300);
charger = new Car("gold", 0, 200);
nova = new Car("blue", 200, 100);
nova.doors = 2; //update nova's doors property

console.log("rav4", rav4);
console.log("charger", charger);
console.log("nova", nova);

//call start methods of Car instances
//the start method expects a number for speed
rav4.start(2.3);
charger.start(-4);
nova.start(random(-1, 1));
}

function draw() {
background("beige");

//display and move all 3 Cars
rav4.display();
charger.display();
nova.display();

rav4.move();
charger.move();
nova.move();
}
82 changes: 82 additions & 0 deletions src/data/examples/ko/15_Instance_Mode/03_Car_Instances.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* @name Car Instances
* @frame 400,400
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
<b>Prof WM Harris,</b></a> <b>How </b>to create three instances of Car Class and
invoke class methods.<br/>
A function is created for the canvas setup, and
3 car instances are initialized with different colors and canvas
positions. The speed of each car is set by passing value to the
instance’s start method. A second function calls class methods to
display and move the cars.
*/
class Car {
/* Constructor expects parameters for
fill color, x and y coordinates that
will be used to initialize class properties.
*/
constructor(cColor, x, y) {
this.color = cColor;
this.doors = 4;
this.isConvertible = false;
this.x = x;
this.y = y;
this.speed = 0;
}

start(speed) { // method expects parameter!
this.speed = speed;
}

display() { // method!
fill(this.color);
rect(this.x, this.y, 20, 10);
}

move() { // method!
this.x += this.speed;
// Wrap x around boundaries
if (this.x < -20) {
this.x = width;
} else if (this.x > width) {
this.x = -20;
}
}
} //end class Car

let rav4;
let charger;
let nova;

function setup() {
createCanvas(200, 400);
/* Construct the 3 Cars */
//constructor expects cColor, x, y
rav4 = new Car("silver", 100, 300);
charger = new Car("gold", 0, 200);
nova = new Car("blue", 200, 100);
nova.doors = 2; //update nova's doors property

console.log("rav4", rav4);
console.log("charger", charger);
console.log("nova", nova);

//call start methods of Car instances
//the start method expects a number for speed
rav4.start(2.3);
charger.start(-4);
nova.start(random(-1, 1));
}

function draw() {
background("beige");

//display and move all 3 Cars
rav4.display();
charger.display();
nova.display();

rav4.move();
charger.move();
nova.move();
}
82 changes: 82 additions & 0 deletions src/data/examples/zh-Hans/15_Instance_Mode/03_Car_Instances.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* @name Car Instances
* @frame 400,400
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
<b>Prof WM Harris,</b></a> <b>How </b>to create three instances of Car Class and
invoke class methods.<br/>
A function is created for the canvas setup, and
3 car instances are initialized with different colors and canvas
positions. The speed of each car is set by passing value to the
instance’s start method. A second function calls class methods to
display and move the cars.
*/
class Car {
/* Constructor expects parameters for
fill color, x and y coordinates that
will be used to initialize class properties.
*/
constructor(cColor, x, y) {
this.color = cColor;
this.doors = 4;
this.isConvertible = false;
this.x = x;
this.y = y;
this.speed = 0;
}

start(speed) { // method expects parameter!
this.speed = speed;
}

display() { // method!
fill(this.color);
rect(this.x, this.y, 20, 10);
}

move() { // method!
this.x += this.speed;
// Wrap x around boundaries
if (this.x < -20) {
this.x = width;
} else if (this.x > width) {
this.x = -20;
}
}
} //end class Car

let rav4;
let charger;
let nova;

function setup() {
createCanvas(200, 400);
/* Construct the 3 Cars */
//constructor expects cColor, x, y
rav4 = new Car("silver", 100, 300);
charger = new Car("gold", 0, 200);
nova = new Car("blue", 200, 100);
nova.doors = 2; //update nova's doors property

console.log("rav4", rav4);
console.log("charger", charger);
console.log("nova", nova);

//call start methods of Car instances
//the start method expects a number for speed
rav4.start(2.3);
charger.start(-4);
nova.start(random(-1, 1));
}

function draw() {
background("beige");

//display and move all 3 Cars
rav4.display();
charger.display();
nova.display();

rav4.move();
charger.move();
nova.move();
}