How to get objects to move in an opposite direction.
in
Programming Questions
•
4 months ago
- Car myCar1;
- Car myCar2;
- void setup ( ) {
- size ( 500, 550 );
- myCar1 = new Car ( color ( 192, 122, 0 ), 132, 60, 3 );
- myCar2 = new Car ( color ( 0, 120, 192 ), 298, 200, 2 );
- }
- void draw ( ) {
- background ( 100 );
- stroke ( 34 );
- strokeWeight ( 4 );
- fill ( 34 );
- rect ( 80, 0, 300, 550 );
- stroke ( 255 );
- strokeWeight ( 12 );
- line ( 230, 10, 230, 45 );
- line ( 230, 80, 230, 115 );
- line ( 230, 150, 230, 185 );
- line ( 230, 220, 230, 255 );
- line ( 230, 290, 230, 325 );
- line ( 230, 360, 230, 395 );
- line ( 230, 430, 230, 465 );
- line ( 230, 500, 230, 535 );
- myCar1.drive ( );
- myCar1.display ( );
- myCar2.drive ( );
- myCar2.display ( );
- }
- class Car {
- color d;
- float xpos;
- float ypos;
- float yspeed;
- Car ( color tempD, float tempXpos, float tempYpos, float tempYspeed ) {
- d = tempD;
- xpos = tempXpos;
- ypos = tempYpos;
- yspeed = tempYspeed;
- }
- void display ( ) {
- strokeWeight ( 3 );
- stroke ( 255 );
- fill ( d );
- rectMode ( CORNER );
- rect ( xpos, ypos, 30, 50 );
- }
- void drive ( ) {
- ypos = ypos + yspeed;
- if ( ypos > width ) {
- ypos = 0;
- }
- }
- }
1