class CAR { color c; float x; int y; float v; CAR (color colour, float xPos, int yPos, float velocity) { c = colour; x = xPos; y = yPos; v = velocity; } void move(){ x=x+v; if (x>width){ x = 0; } } void display(){ fill(c); rect(x,y,50,20); } } class BAR{ float maxValue; float value; float x = 0; float y; String units; int barHeight; String meter; int fontsize; String measurement; float barLength; BAR (float maxValueTEMP, float valueTEMP, float yTEMP, String unitsTEMP, int fontsizeTEMP, int barHeightTEMP, String measurementTEMP) { maxValue = maxValueTEMP; value = valueTEMP; y = yTEMP; units = unitsTEMP; fontsize = fontsizeTEMP; barHeight = barHeightTEMP; measurement = measurementTEMP; } void display() { barLength = (width*value)/maxValue; fill (100); rect (x, y, barLength, barHeight); fill(255); meter = measurement+" = "+str(value)+" "+units; text(meter,x,y+fontsize); } } CAR [] carsArray; //where [0] is the player's car BAR [] barsArray; // where [0] is acceleration, [1] is deacceleration, [2] is max speed and [3] is velocity boolean accelerating = false; float acceleration = 0.21875; //inital acceleration float deacceleration = acceleration; //inital deacceleration = acceleration float maxSpeed = 30; float maxMaxSpeed = 100; int velocityRoundedUp; float u = 0; //u for inital velocity int barHeight = 50; PFont font; int fontsize = 30; float minRate = 0.03125; //smallest increatment of acceleration and it has to be an excacte binary value void setup(){ size(1000,400); carsArray = new CAR [2]; carsArray[0]= new CAR(color(255,180,0),0,120,u); carsArray[1]= new CAR(color(0,0,255),50,50,15); barsArray = new BAR [4]; barsArray[0]= new BAR(1, acceleration, height-4*barHeight, "velocity / frame", fontsize, barHeight,"acceleration"); barsArray[1]= new BAR(1, deacceleration, height-3*barHeight, "velocity / frame", fontsize, barHeight,"deacceleration"); barsArray[2]= new BAR(maxMaxSpeed, maxSpeed, height-2*barHeight, "pixles / frame", fontsize, barHeight,"max speed"); barsArray[3]= new BAR(maxSpeed, u, height-barHeight, "pixles / frame", fontsize, barHeight,"velocity"); noStroke(); font = loadFont("CourierNewPSMT-30.vlw"); textFont(font,fontsize); rectMode(CORNER); } void draw(){ background(0); for (int x=0; x<2; x++) { carsArray[x].move(); carsArray[x].display(); } velocityRoundedUp = int (carsArray[0].v); if (accelerating==true) { carsArray[0].v += acceleration; carsArray[0].c=color(0,255,0); } if ((accelerating==true)&&(velocityRoundedUp>=maxSpeed)){ carsArray[0].v = maxSpeed; carsArray[0].c=color(255,255,0); } if ((accelerating==false)&&(velocityRoundedUp!=0)){ carsArray[0].v -= deacceleration; carsArray[0].c=color(255,0,0); } if ((accelerating==false)&&(velocityRoundedUp>maxSpeed)){ carsArray[0].v=maxSpeed; } if ((accelerating==false)&&(velocityRoundedUp==0)){ carsArray[0].c=color(255,180,0); carsArray[0].v=0; } barsArray[0].value=acceleration; barsArray[1].value=deacceleration; barsArray[2].value=maxSpeed; barsArray[3].value=carsArray[0].v; barsArray[3].maxValue=maxSpeed; for (int x=0; x<4; x++) { barsArray[x].display(); } } void mousePressed() { accelerating=!accelerating; } void keyPressed() { if (key == CODED) { if ((keyCode == UP)&&(acceleration!=1)) { acceleration += minRate; } if ((keyCode == DOWN)&&(acceleration!=0)) { acceleration -= minRate; } if ((keyCode == RIGHT)&&(deacceleration!=1)){ deacceleration += minRate; } if ((keyCode == LEFT)&&(deacceleration!=0)){ deacceleration -= minRate; } } if ((key=='z')&&(maxSpeed!=0)){ maxSpeed -= 1; } if ((key=='x')&&(maxSpeed!=maxMaxSpeed)){ maxSpeed += 1; } }