Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //global variable initialisation
- int score;
- int xFrog;
- int yFrog;
- int trainx = 0;
- int trainy = 130;
- //array for number of cars
- Car[] cars = new Car[3];
- //setup function
- void setup() {
- size(800,750);
- score = 0;
- //position of frog
- xFrog = width/2;
- yFrog = height-20;
- //car creation
- smooth();
- for (int i = 0; i < cars.length; i ++ ) {
- cars[i] = new Car(color(193,41,41),0,i,random(-1,5));
- }
- }
- //draw function that displays cars and calls functions
- void draw () {
- drawBackground();
- wave();
- log();
- train();
- for (int i = 0; i < cars.length; i ++ ) {
- cars[i].move();
- cars[i].display(); // display for the cars
- }
- frog();
- }
- //function for the creation of the frog
- void frog () {
- fill(26, 95, 26);
- ellipse(xFrog, yFrog, 20, 20);
- ellipse(xFrog-5, yFrog-9, 10, 10);
- ellipse(xFrog+5, yFrog-9, 10, 10);
- fill(0);
- ellipse(xFrog-5, yFrog-9, 3, 3);
- ellipse(xFrog+5, yFrog-9, 3, 3);
- }
- //this function holds all of the background initialisation
- //a limitation of this method is the toll it takes on the browser over time
- void drawBackground () {
- stroke(0);
- background(90, 255, 90);
- //grass
- grassx = 0;
- grassy = 0;
- //road
- strokeWeight(1);
- int roadx = width/28;
- int roady = height * 0.62;
- int liney = (roady) + ((roadx + height/3.7) / 2.5);
- fill(120);
- rect(grassx,roady,width+1,height/3.7);
- fill(255);
- for (i = 0; i < 8; i++){
- rect(roadx,liney,50,15);
- roadx += (width/8);
- }
- //river
- int rivery = roady-(height/5);
- int riverwidth = height/8;
- fill(0,128,255);
- rect(-1,rivery*0.9,801,riverwidth);
- //train
- boardx = width/400;
- boardy = width/7;
- strokeWeight(5);
- line(-1,125,801,125);
- line(-1,185,801,185);
- for (j = 0; j < 60; j++){
- strokeWeight(1);
- fill(204,102,0);
- rect(boardx,boardy,10,80);
- boardx += 16;
- }
- }
- //an array that stores the positions of the waves
- int [] wavesArray = [50,-130,-310,-490,-670,-850];
- int wavespeed = 3;
- int wavesize = 10;
- //function that spawns the waves and moves them
- void wave () {
- stroke(0,200,255);
- strokeWeight(10);
- fill(0, 128, 255);
- for (i = 0; i < wavesArray.length; i++){
- if (wavesArray[i] > width) {
- wavesArray[i] = -5;
- }
- wavesArray[i] += wavespeed;
- }
- wavesize += 5;
- if (wavesize > 90) {
- noFill();
- wavesize = 10;
- }
- for (j=0; j < wavesArray.length; j++) {
- arc(wavesArray[j], 300, wavesize, 100, PI/14, PI/1.5);
- }
- }
- //intialising the variables for the logs
- int logx = 300;
- int logy = height*2.95;
- //function to create the log (Would have rather made it with a class or loop)
- void log() {
- noStroke();
- strokeWeight(1);
- fill(150,100,0);
- logx += 2;
- rect(logx,logy,200,30);
- ellipse(logx,logy+15,30,30);
- ellipse(logx+200,logy+15,30,30);
- fill(150,100,0);
- ellipse(logx,logy+15,30,30);
- fill(220);
- ellipse(logx,logy+15,20,20);
- fill(0);
- stroke(1);
- line(logx+10,logy+5,logx+210,logy+5);
- line(logx+15,logy+15,logx+214,logy+15);
- line(logx+10,logy+24,logx+210,logy+24);
- if (logx-100 > width){
- logx = -200;
- }
- }
- //class for the movement of the car
- class Car {
- color c; // sets up variables for the cars, positions and speeds, as well as color so that they can be changed in the main area
- float xpos;
- float ypos;
- float xspeed;
- Car(color c_, float xpos_, float ypos_, float xspeed_) {
- c = c_;
- xpos = xpos_;
- ypos = height-(ypos_*140);
- xspeed = xspeed_;
- }
- void display() {
- noStroke();
- //fill(c);
- //rect(xpos,ypos,60,40);
- fill(111, 169, 219);
- rect(xpos, ypos, 33, 16);
- fill(255, 0, 0);
- rect(xpos-16, ypos+16, 66, 26);
- fill(0);
- ellipse(xpos, ypos+43, 16, 16);
- ellipse(xpos+33, ypos+43, 16, 16);
- }
- void move() {
- xpos = xpos + xspeed;
- if (xpos > width +10) {
- xpos = -20;
- }
- if (xpos < -25){
- xpos = width +10;
- }
- }
- }
- //function for creation and movement of the dank engine
- void train() {
- //train creation
- fill(255,60,50);
- triangle(trainx+70,trainy-20,trainx+80,trainy+55,trainx+100,trainy+55);
- fill(120,120,240);
- triangle(trainx+60,trainy-30,trainx+70,trainy+20,trainx+90,trainy-30);
- ellipse(trainx+15,trainy+40,30,30);
- ellipse(trainx+65,trainy+40,30,30);
- fill(120,120,240);
- rect(trainx,trainy,80,40);
- fill(255,60,50);
- rect(trainx,trainy-30,35,30);
- fill(255);
- rect(trainx+5,trainy-25,25,20);
- text('Dank Engine',trainx+5,trainy+25)
- //train move
- trainx += 4;
- if (trainx > width) {
- trainx = -50;
- }
- //smoke
- }
- //controlkeys
- void keyPressed(){
- if (key == 'w' || key == 'W'){
- yFrog -= 5;
- score += 1;
- }
- else if (key == 's' || key == 'S'){
- yFrog += 5;
- score += 1;
- }
- else if (key == 'a' || key == 'A'){
- xFrog -= 5;
- }
- else if (key == 'd' || key == 'D'){
- xFrog += 5;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement