Advertisement
Guest User

help please

a guest
Oct 22nd, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. var bgImg;
  2. var myCar;
  3. var bugs = [];
  4. var bugImg;
  5. var music;
  6. var crashSound
  7. var carXPos = 100;
  8. var gameStarted = false;
  9.  
  10. function preload() {
  11. bgImg = loadImage("game-bg.jpg");
  12. bugImg = loadImage("bug.svg");
  13. soundFormats('mp3', 'ogg');
  14. music = loadSound('victory.mp3');
  15. crashSound = loadSound('explosion.wav')
  16.  
  17. }
  18.  
  19. function setup() {
  20. // put setup code here
  21. createCanvas(windowWidth - 10, windowHeight - 10);
  22. //create new car (ypos, speed)
  23. myCar = new Car(350, 5);
  24.  
  25. // Create new bugs!
  26. for(var i = 0; i < 20; i++) {
  27. bugs[i] = new Bug();
  28. }
  29. //start button
  30. startButton = createButton("Let's Play");
  31. startButton.position(200, 200);
  32. startButton.mousePressed(startGame);
  33. }
  34.  
  35. function startGame(){
  36.  
  37. gameStarted = true;
  38.  
  39. music.play();
  40. }
  41.  
  42. function draw() {
  43. // put drawing code here
  44. background(0);
  45.  
  46. if (gameStarted == true){
  47. startButton.hide();
  48. }
  49.  
  50. myCar.drive();
  51.  
  52. // Draw track
  53. image(bgImg, -myCar.xpos, 0, (height / bgImg.height) * bgImg.width, height);
  54.  
  55. // Draw all the bugs
  56. for(var i = 0; i < bugs.length; i++) {
  57. bugs[i].display();
  58. }
  59.  
  60. // Draw the car
  61. myCar.display();
  62. }
  63.  
  64. //constructor
  65. function Car(ypos, speed){
  66. this.xpos = 100;
  67. this.ypos = ypos;
  68. this.speed = speed;
  69. this.c = color(random(255), random(255), random(255));
  70. }
  71. //methods
  72. Car.prototype.drive = function(){
  73. //if(keyCode == RIGHT_ARROW)
  74. // {
  75. // this.xpos = this.xpos + 5;
  76. //console.log('this works');
  77. //}
  78.  
  79. if(keyIsPressed && keyCode == UP_ARROW && this.ypos > 0)
  80. {
  81. this.ypos = this.ypos - 15;
  82. }
  83.  
  84. if(keyIsPressed && keyCode == DOWN_ARROW && this.ypos < height - 50)
  85. {
  86. this.ypos = this.ypos + 15;
  87. }
  88.  
  89. /*if(this.xpos > width)
  90. {
  91. this.xpos = 100; // start off screen
  92. }*/
  93.  
  94. this.xpos = this.xpos + this.speed;
  95.  
  96. // Collision detection!
  97. for(var i = 0; i < bugs.length; i++) {
  98. // Bug variables
  99. var bx = bugs[i].xpos - myCar.xpos;
  100. var by = bugs[i].ypos;
  101. var bw = bugImg.width * bugs[i].scale;
  102. var bh = bugImg.height * bugs[i].scale;
  103.  
  104. // Car variables
  105. var cx = carXPos;
  106. var cy = myCar.ypos;
  107. var cw = 100;
  108. var ch = 50;
  109.  
  110. // Check for intersection of the rectangles
  111. if(!(bx > cx + cw || bx + bw < cx || by > cy + ch || by + bh < cy)) {
  112. // Game over!
  113. crashSound.play();
  114. this.xpos = 0;
  115. }
  116. }
  117.  
  118. //console.log('howdy');
  119. }
  120. Car.prototype.display = function() {
  121. // Draw car
  122. fill(this.c);
  123. rect(carXPos, this.ypos, 100, 50);
  124.  
  125. //wheels
  126. fill(0);
  127. ellipse(carXPos + 20, this.ypos + 45, 30, 30);
  128. ellipse(carXPos + 80, this.ypos + 45, 30, 30);
  129. }
  130.  
  131.  
  132. // constructor
  133. function Bug() {
  134. this.xpos = random((height / bgImg.height) * bgImg.width);
  135. this.ypos = random(height);
  136. this.scale = 0.5 + Math.random();
  137. }
  138. Bug.prototype.display = function() {
  139. // Draw bug
  140. image(bugImg, this.xpos - myCar.xpos, this.ypos, bugImg.width * this.scale, bugImg.height * this.scale);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement