Advertisement
Guest User

Untitled

a guest
May 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. //global variable initialisation
  2. int score;
  3. int xFrog;
  4. int yFrog;
  5. int trainx = 0;
  6. int trainy = 130;
  7.  
  8. //array for number of cars
  9. Car[] cars = new Car[3];
  10.  
  11. //setup function
  12. void setup() {
  13. size(800,750);
  14. score = 0;
  15. //position of frog
  16. xFrog = width/2;
  17. yFrog = height-20;
  18. //car creation
  19. smooth();
  20. for (int i = 0; i < cars.length; i ++ ) {
  21. cars[i] = new Car(color(193,41,41),0,i,random(-1,5));
  22. }
  23. }
  24. //draw function that displays cars and calls functions
  25. void draw () {
  26. drawBackground();
  27. wave();
  28. log();
  29. train();
  30. for (int i = 0; i < cars.length; i ++ ) {
  31. cars[i].move();
  32. cars[i].display(); // display for the cars
  33. }
  34. frog();
  35. }
  36. //function for the creation of the frog
  37. void frog () {
  38. fill(26, 95, 26);
  39. ellipse(xFrog, yFrog, 20, 20);
  40. ellipse(xFrog-5, yFrog-9, 10, 10);
  41. ellipse(xFrog+5, yFrog-9, 10, 10);
  42. fill(0);
  43. ellipse(xFrog-5, yFrog-9, 3, 3);
  44. ellipse(xFrog+5, yFrog-9, 3, 3);
  45. }
  46. //this function holds all of the background initialisation
  47. //a limitation of this method is the toll it takes on the browser over time
  48. void drawBackground () {
  49. stroke(0);
  50. background(90, 255, 90);
  51. //grass
  52. grassx = 0;
  53. grassy = 0;
  54. //road
  55. strokeWeight(1);
  56. int roadx = width/28;
  57. int roady = height * 0.62;
  58. int liney = (roady) + ((roadx + height/3.7) / 2.5);
  59. fill(120);
  60. rect(grassx,roady,width+1,height/3.7);
  61. fill(255);
  62. for (i = 0; i < 8; i++){
  63. rect(roadx,liney,50,15);
  64. roadx += (width/8);
  65. }
  66.  
  67. //river
  68. int rivery = roady-(height/5);
  69. int riverwidth = height/8;
  70. fill(0,128,255);
  71. rect(-1,rivery*0.9,801,riverwidth);
  72. //train
  73. boardx = width/400;
  74. boardy = width/7;
  75. strokeWeight(5);
  76. line(-1,125,801,125);
  77. line(-1,185,801,185);
  78. for (j = 0; j < 60; j++){
  79. strokeWeight(1);
  80. fill(204,102,0);
  81. rect(boardx,boardy,10,80);
  82. boardx += 16;
  83. }
  84.  
  85. }
  86. //an array that stores the positions of the waves
  87. int [] wavesArray = [50,-130,-310,-490,-670,-850];
  88. int wavespeed = 3;
  89. int wavesize = 10;
  90.  
  91. //function that spawns the waves and moves them
  92. void wave () {
  93. stroke(0,200,255);
  94. strokeWeight(10);
  95. fill(0, 128, 255);
  96. for (i = 0; i < wavesArray.length; i++){
  97.  
  98. if (wavesArray[i] > width) {
  99. wavesArray[i] = -5;
  100. }
  101. wavesArray[i] += wavespeed;
  102. }
  103. wavesize += 5;
  104.  
  105. if (wavesize > 90) {
  106. noFill();
  107. wavesize = 10;
  108. }
  109.  
  110. for (j=0; j < wavesArray.length; j++) {
  111. arc(wavesArray[j], 300, wavesize, 100, PI/14, PI/1.5);
  112. }
  113. }
  114.  
  115. //intialising the variables for the logs
  116. int logx = 300;
  117. int logy = height*2.95;
  118.  
  119. //function to create the log (Would have rather made it with a class or loop)
  120. void log() {
  121. noStroke();
  122. strokeWeight(1);
  123. fill(150,100,0);
  124. logx += 2;
  125. rect(logx,logy,200,30);
  126. ellipse(logx,logy+15,30,30);
  127. ellipse(logx+200,logy+15,30,30);
  128. fill(150,100,0);
  129. ellipse(logx,logy+15,30,30);
  130. fill(220);
  131. ellipse(logx,logy+15,20,20);
  132. fill(0);
  133. stroke(1);
  134. line(logx+10,logy+5,logx+210,logy+5);
  135. line(logx+15,logy+15,logx+214,logy+15);
  136. line(logx+10,logy+24,logx+210,logy+24);
  137. if (logx-100 > width){
  138. logx = -200;
  139. }
  140. }
  141. //class for the movement of the car
  142. class Car {
  143. 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
  144. float xpos;
  145. float ypos;
  146. float xspeed;
  147.  
  148. Car(color c_, float xpos_, float ypos_, float xspeed_) {
  149. c = c_;
  150. xpos = xpos_;
  151. ypos = height-(ypos_*140);
  152. xspeed = xspeed_;
  153. }
  154.  
  155. void display() {
  156. noStroke();
  157. //fill(c);
  158. //rect(xpos,ypos,60,40);
  159. fill(111, 169, 219);
  160. rect(xpos, ypos, 33, 16);
  161. fill(255, 0, 0);
  162. rect(xpos-16, ypos+16, 66, 26);
  163. fill(0);
  164. ellipse(xpos, ypos+43, 16, 16);
  165. ellipse(xpos+33, ypos+43, 16, 16);
  166. }
  167.  
  168. void move() {
  169. xpos = xpos + xspeed;
  170. if (xpos > width +10) {
  171. xpos = -20;
  172. }
  173. if (xpos < -25){
  174. xpos = width +10;
  175. }
  176. }
  177. }
  178. //function for creation and movement of the dank engine
  179. void train() {
  180. //train creation
  181. fill(255,60,50);
  182. triangle(trainx+70,trainy-20,trainx+80,trainy+55,trainx+100,trainy+55);
  183. fill(120,120,240);
  184. triangle(trainx+60,trainy-30,trainx+70,trainy+20,trainx+90,trainy-30);
  185. ellipse(trainx+15,trainy+40,30,30);
  186. ellipse(trainx+65,trainy+40,30,30);
  187. fill(120,120,240);
  188. rect(trainx,trainy,80,40);
  189. fill(255,60,50);
  190. rect(trainx,trainy-30,35,30);
  191. fill(255);
  192. rect(trainx+5,trainy-25,25,20);
  193. text('Dank Engine',trainx+5,trainy+25)
  194. //train move
  195. trainx += 4;
  196. if (trainx > width) {
  197. trainx = -50;
  198. }
  199. //smoke
  200.  
  201. }
  202.  
  203. //controlkeys
  204. void keyPressed(){
  205. if (key == 'w' || key == 'W'){
  206. yFrog -= 5;
  207. score += 1;
  208. }
  209. else if (key == 's' || key == 'S'){
  210. yFrog += 5;
  211. score += 1;
  212. }
  213. else if (key == 'a' || key == 'A'){
  214. xFrog -= 5;
  215. }
  216. else if (key == 'd' || key == 'D'){
  217. xFrog += 5;
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement