/////////////////////////////// /* Interative Toy - cowboy By Kaicheng Li (KC) Student number: 99137116 "A" and "D" to control the cowboy */////////////////////////////// // declar Variables for use later float MoveSpeedT = 2; float MoveSpeedR = 2; float CowBoyX=100; float CowBoyY=250; float treeX=0; float rockX=0; float CowSpeed=0; float cowX=0; boolean left=false; boolean right=false; // Set up the size void setup() { size(400, 400); } //draw all functions void draw() { backGround(); fence(); rocks(); trees(); bull(); cowBoy(); if (MoveSpeedT+20>400) { MoveSpeedT=random(-200, -70); treeX= random(-10, 370); if (treeX<310) { treeX=random(-10, 70); } } if (MoveSpeedR+10>400) { MoveSpeedR=random(-300, -70); rockX= random(-50, 310); if (rockX<350) { rockX=random(-50, 10); } } if (CowSpeed>400) { CowSpeed=random(-120, -150); cowX=random(-50, 120); } textSize(30); text("Score", 310, 30); textSize(25); text(frameCount, 310, 60); println(frameCount); } //background //roadside left, right and the road at mid void backGround() { noStroke(); fill(103, 125, 16); rect(0, 0, 400, 400); fill(249, 175, 100); rect(100, 0, 300, 400); } //trees void trees() { MoveSpeedT+=5; noStroke(); fill(73, 85, 16); triangle(treeX+10, MoveSpeedT+20, treeX+30, MoveSpeedT+20, treeX+20, MoveSpeedT+0); triangle(treeX+20, MoveSpeedT+17, treeX+5, MoveSpeedT+40, treeX+35, MoveSpeedT+40); rectMode(CORNERS); fill(132, 80, 62); rect(treeX+15, MoveSpeedT+40, treeX+25, MoveSpeedT+60); } //rocks void rocks() { MoveSpeedR+=5; fill(157, 130, 117); triangle(rockX+55, MoveSpeedR+40, rockX+93, MoveSpeedR+37, rockX+80, MoveSpeedR+17); fill(163, 123, 103); quad(rockX+50, MoveSpeedR+40, rockX+60, MoveSpeedR+50, rockX+70, MoveSpeedR+20, rockX+60, MoveSpeedR+20); quad(rockX+60, MoveSpeedR+30, rockX+80, MoveSpeedR+30, rockX+90, MoveSpeedR+20, rockX+73, MoveSpeedR+9); } //the wild bull on road void bull() { CowSpeed+=3; fill(50, 50, 50); rect(cowX+150, CowSpeed+60, cowX+180, CowSpeed+80); quad(cowX+150, CowSpeed+80, cowX+140, CowSpeed+120, cowX+190, CowSpeed+120, cowX+180, CowSpeed+80); strokeWeight(4); stroke(250, 250, 250); line(cowX+140, CowSpeed+60, cowX+140, CowSpeed+70); line(cowX+140, CowSpeed+70, cowX+148, CowSpeed+70); line(cowX+181, CowSpeed+70, cowX+190, CowSpeed+70); line(cowX+190, CowSpeed+60, cowX+190, CowSpeed+70); } //cowboy void cowBoy() { //cowBoy's bull noStroke(); fill(73, 46, 19); rect(CowBoyX+150, CowBoyY+ 60, CowBoyX+180, CowBoyY+ 80); quad(CowBoyX+150, CowBoyY+ 80, CowBoyX+140, CowBoyY+ 120, CowBoyX+ 190, CowBoyY+ 120, CowBoyX+ 180, CowBoyY+ 80); strokeWeight(4); stroke(250, 250, 250); line(CowBoyX+140, CowBoyY+ 60, CowBoyX+140, CowBoyY+ 70); line(CowBoyX+140, CowBoyY+ 70, CowBoyX+148, CowBoyY+ 70); line(CowBoyX+181, CowBoyY+ 70, CowBoyX+190, CowBoyY+ 70); line(CowBoyX+190, CowBoyY+ 60, CowBoyX+190, CowBoyY+ 70); //his head and hat noStroke(); fill(0, 0, 0); ellipseMode(CORNERS); ellipse(CowBoyX+155, CowBoyY+40, CowBoyX+175, CowBoyY+ 60); fill(140, 50, 25); quad(CowBoyX+155, CowBoyY+38, CowBoyX+ 152, CowBoyY+50, CowBoyX+ 177, CowBoyY+50, CowBoyX+ 174, CowBoyY+38); stroke(140, 50, 25); strokeWeight(3); line(CowBoyX+143, CowBoyY+50, CowBoyX+185, CowBoyY+50); //body and limbs noStroke(); fill(22, 80, 123); rect(CowBoyX+155, CowBoyY+61, CowBoyX+175, CowBoyY+85); rect(CowBoyX+150, CowBoyY+62, CowBoyX+155, CowBoyY+70); quad(CowBoyX+175, CowBoyY+62, CowBoyX+ 175, CowBoyY+67, CowBoyX+ 195, CowBoyY+45, CowBoyX+ 190, CowBoyY+45); quad(CowBoyX+175, CowBoyY+62, CowBoyX+140, CowBoyY+90, CowBoyX+150, CowBoyY+90, CowBoyX+180, CowBoyY+62); quad(CowBoyX+175, CowBoyY+80, CowBoyX+175, CowBoyY+85, CowBoyX+190, CowBoyY+90, CowBoyX+195, CowBoyY+87); //his rope noFill(); stroke(0); curve(CowBoyX+102, CowBoyY+10, CowBoyX+200, CowBoyY+30, CowBoyX+190, CowBoyY+45, CowBoyX+190, CowBoyY+45); ellipseMode(CORNERS); ellipse(CowBoyX+190, CowBoyY+20, CowBoyX+220, CowBoyY+30); } void fence() { fill(255); rectMode(CENTER); //leftside for (int square = 10; square < width; square = square + 20) { rect(100, square, 10, 10); } //rightside for (int square = 10; square < height; square = square + 20) { rect(300, square, 10, 10); } } //Controls "a" and "d" to control the cowboy void keyPressed() { if (key == 'a') { left=true; CowBoyX=CowBoyX-10; if (CowBoyX<-50) { CowBoyX=-50; } } if (key == 'd') { right=true; CowBoyX=CowBoyX+10; if (CowBoyX>120) { CowBoyX=120; } } }