/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://cisc1600.sketchpad.cc/sp/pad/view/ro.sF7EMxIrllt/rev.5504
*
* authors:
* xingyiyu
* jarvis gu
* Emily Greenwald
* Aoxin
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
// Project 2: CAR ROAD!
/* @pjs preload="/static/uploaded_resources/p.20282/start.jpg"; */
PImage startScreen;
int gameStage;
int i = 0;
int colorR;
int colorG;
int colorB;
int ballSize = 30;
int ballX;
int ballY;
int carX;
int carY;
int road1;
void setup() { // this is run once.
size(600, 600); // canvas size (Integers only, please.)
startScreen = loadImage("/static/uploaded_resources/p.20282/start.jpg"); //image of car
image (startScreen,0,0,600,600); //image location
gameStage = 0; //game starts off st titlescreen
colorR = random(255); //random red color
colorG = random(255); //random green color
colorB = random(255); //random blue color
carX = 2*width/30; //initial X position for car
carY = 19*height/30; //initial Y position for car
road1 = 2*width/30; //start of road 1
resetBall(); //reset the ball over once the car clicks on the ball
}
void draw() { // this is run repeatedly.
if(gameStage == 0){ //start screen
drawTitleScreen(); //drawing the title screen
}
if(gameStage == 1){ //game screen
background(102,255,255); //sky blue background
drawRoad(); //drawing the road
drawBall(); //drawing the ball
drawCar(); //drawing the car
drawLaser(); //drawing the laser
}
if(gameStage == 2){ //end screen
drawEndScreen(); //drawing the end screen
}
}
void drawTitleScreen(){ //drawing the title screen
textSize(24); //text size
fill(colorR,colorG,colorB); //random colors
textAlign(CENTER); //align from center
text("Welcome to CAR ROAD!",300,50); //title
text("Control the car with arrow key and click the ball",300,100); //directions
text("Try not to let the ball get pass the road!",300,150); //advice
fill(0,255,0); //green color
rect(235,525,130,50); //start rectangle
fill(255,0,0); //red color
text("Start Game",300,555); //start text
if(mousePressed && (mouseX >=235 && mouseX <=365 && mouseY >= 525 && mouseY <= 575)){ //start button
gameStage = 1; //goes to play screen
}
}
void drawEndScreen(){ //drawing the title screen
textSize(24); //text size
fill(colorR,colorG,colorB); //random colors
textAlign(CENTER); //align from center
text("Game Over!",300,75); //end game
text("Click to try again",300,125); //try again
fill(0,255,0); //green color
rect(235,525,130,50); //start button
fill(255,0,0); //red color
text("Retry",300,555); //retry text
if(mousePressed && (mouseX >=235 && mouseX <=365 && mouseY >= 525 && mouseY <= 575)){ //retry button
gameStage = 1; //goes to play screen
resetBall(); //resets the ball hits the car
}
}
void drawRoad(){ //drawing the road
stroke(255,255,0); //yellow color for lines on the road
strokeWeight(10); //stroke of the lines of the road
fill(0); //black color for road
rect(0,height/3,width,height); //rectangle for black road
drawLines(); //draws the middle lines of the road
line(0,height-5,width,height-5); //bottom edge of road
}
void drawLines(){
line(road1,2*height/3,road1+5*width/30,2*height/3); //first middle line on road
line(road1+7*width/30,2*height/3,road1+12*width/30,2*height/3); //second middle line on road
line(road1+14*width/30,2*height/3,road1+19*width/30,2*height/3); //third middle line on road
line(road1+21*width/30,2*height/3,road1+26*width/30,2*height/3); //forth middle line on road
line(road1+28*width/30,2*height/3,road1+33*width/30,2*height/3); //fifth middle line on road
road1 = road1 -2; //makes the road 1 move so it looks like the whole road is moving
if(road1 < -5*width/30){ //return the road back to the start so it looks like the car is keep on drawing foward
road1 = road1+7*width/30;
}
}
void drawBall(){ //drawing the ball
noStroke(); //no stroke for the ball
fill(colorR,colorG,colorB); //the random colors of the ball
ellipse(ballX,ballY,ballSize,ballSize); //random location of the ball and the size of the ball
ballX = ballX -2; //ballX minus 2 so it will keep moving in the x direction
if(ballX <= 0){ //when ball hits end of the left screen game over
gameStage = 2; //game over screen
}
}
void drawCar(){ //drawing the car
strokeWeight(3); //stroke of the lines of the car
stroke(153,0,0); //drak red color for the car outline
fill(255,0,0); //red color for car
rect(carX,carY,3.5*width/30,2*height/30); //starting location of the car and the size of the car
}
void resetBall(){
ballX = width-15; //ballX coordinates starts from the full width minus the radius
ballY = random(1*height/3,height-15); //ballY coordinates starts from 1/3 of the page to the full
}
void drawLaser(){
stroke(colorR,colorG,colorB); //random color
if(mousePressed){ //when mouse is pressed
rect(carX,carY,3.5*width/30,2*height/30); //car
if(ballX >= carX && ballX <= carX+(3.5*width/30) && ballY >= carY && ballY <= carY+(2*height/30)){
resetBall(); //reset ball
}
}
}
void keyPressed(){ //key press functions for keys
if(key == CODED && keyCode == UP && carY > height/3){ //car moves up when key is pressed
carY = carY - 15;
}
else if(key == CODED && keyCode == DOWN && carY+(2*height/30) < height){ //car moves down when key is pressed
carY = carY + 15;
}
else if(key == CODED && keyCode == LEFT && carX > 0){ //car moves left when key is pressed
carX = carX - 15;
}
else if(key == CODED && keyCode == RIGHT && carX+(3.5*width/30) < width){ //car moves right when key is pressed
carX = carX + 15;
}
}