adding stop , forward , backward
in
Programming Questions
•
1 year ago
this code read data from a text file and draw a car object that moves according to the positions in the text file . now I create stop , forward and backward button . How could I perform stop , forward backward it ?
- import apwidgets.*;
- boolean ret = false;
- boolean st = true;
- float zoom;
- // A vector to store the offset from the center
- PVector offset;
- // The previous offset
- PVector poffset;
- // A vector for the mouse position
- PVector mouse;
- Car myCar1;
- PImage car;
- String[] lines;
- int index = 0;
- void setup() {
- size(screenWidth, screenHeight, OPENGL);
- zoom = 1.0;
- offset = new PVector(0, 0);
- poffset = new PVector(0, 0);
- myCar1 = new Car(color(108, 87, 229));
- car = loadImage("3.jpg");
- smooth();
- }
- void draw() {
- background(255);
- pushMatrix();
- // draw stop button
- fill(106,90,205);
- stroke(106,90,205);
- smooth();
- ellipse(650,700,50,50);
- fill(255);
- stroke(255);
- rect(650,700,15,15);
- // draw forward button
- fill(106,90,205);
- stroke(106,90,205);
- rect(500,700,90,45);
- fill(255);
- stroke(255);
- smooth();
- triangle(500,700,525,710,525,690);
- triangle(475,700,500,710,500,690);
- fill(106,90,205);
- stroke(106,90,205);
- rect(800,700,90,45);
- fill(255);
- stroke(255);
- triangle(800,690,800,710,825,700);
- triangle(775,690,775,710,800,700);
- // draw backward button
- // Everything must be drawn relative to center
- translate(width/2, height/2);
- frameRate(10);
- // Use scale for 2D "zoom"
- scale(zoom);
- // The offset (note how we scale according to the zoom)
- translate(offset.x/zoom, offset.y/zoom);
- image(car, -3, -120, car.width/4, car.height/4);
- lines = loadStrings("positions.txt");
- myCar1.Move();
- rect(150, 150, 10, 20);
- // An arbitrary design so that we have something to see!
- randomSeed(1);
- stroke(0);
- noFill();
- rectMode(CENTER);
- float h = 100;
- popMatrix();
- }
- class Car {
- color c;
- int z = 10;
- int t = 20;
- Car(color tempC) {
- c = tempC;
- }
- void display() {
- fill(162, 211, 172);
- rect(200, 200, 10, 30);
- }
- void Move() {
- fill(c);
- if (index < lines.length) {
- if( st = true){
- String[] pieces = split(lines[index], '\t');
- if (pieces.length == 2) {
- int x = int(pieces[0]);
- int y = int(pieces[1]);
- rect (x-z/16, y-t/2, z, t);
- ellipse(x, y, 5, 5);
- }
- // Go to the next line for the next run through draw()
- index = index + 1;
- }
- }
- }
- }
- void mousePressed()
- {
- mouse = new PVector(mouseX, mouseY);
- poffset.set(offset);
- if ( mouseY > 680 )
- {
- //Backward
- if ( mouseX < 590 & 480 < mouseX ){
- BACKWATD
- }
- //forward
- else if ( mouseX < 890 & 780 < mouseX){
- FORWARD
- }
- }
- // stop
- else if (mouseX < 750 & mouseX > 620 ){
- STOP
- }
- }
- }
and this is some positions in my text file :
- 44.8 0.4
- 45 0
- 45 0
- 45.9 -1
- 46.9 -0.2
- 47.9 0.5
- 48 0
- 49 -1
1