Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var bgImg;
- var myCar;
- var bugs = [];
- var bugImg;
- var music;
- var crashSound
- var carXPos = 100;
- var gameStarted = false;
- function preload() {
- bgImg = loadImage("game-bg.jpg");
- bugImg = loadImage("bug.svg");
- soundFormats('mp3', 'ogg');
- music = loadSound('victory.mp3');
- crashSound = loadSound('explosion.wav')
- }
- function setup() {
- // put setup code here
- createCanvas(windowWidth - 10, windowHeight - 10);
- //create new car (ypos, speed)
- myCar = new Car(350, 5);
- // Create new bugs!
- for(var i = 0; i < 20; i++) {
- bugs[i] = new Bug();
- }
- //start button
- startButton = createButton("Let's Play");
- startButton.position(200, 200);
- startButton.mousePressed(startGame);
- }
- function startGame(){
- gameStarted = true;
- music.play();
- }
- function draw() {
- // put drawing code here
- background(0);
- if (gameStarted == true){
- startButton.hide();
- }
- myCar.drive();
- // Draw track
- image(bgImg, -myCar.xpos, 0, (height / bgImg.height) * bgImg.width, height);
- // Draw all the bugs
- for(var i = 0; i < bugs.length; i++) {
- bugs[i].display();
- }
- // Draw the car
- myCar.display();
- }
- //constructor
- function Car(ypos, speed){
- this.xpos = 100;
- this.ypos = ypos;
- this.speed = speed;
- this.c = color(random(255), random(255), random(255));
- }
- //methods
- Car.prototype.drive = function(){
- //if(keyCode == RIGHT_ARROW)
- // {
- // this.xpos = this.xpos + 5;
- //console.log('this works');
- //}
- if(keyIsPressed && keyCode == UP_ARROW && this.ypos > 0)
- {
- this.ypos = this.ypos - 15;
- }
- if(keyIsPressed && keyCode == DOWN_ARROW && this.ypos < height - 50)
- {
- this.ypos = this.ypos + 15;
- }
- /*if(this.xpos > width)
- {
- this.xpos = 100; // start off screen
- }*/
- this.xpos = this.xpos + this.speed;
- // Collision detection!
- for(var i = 0; i < bugs.length; i++) {
- // Bug variables
- var bx = bugs[i].xpos - myCar.xpos;
- var by = bugs[i].ypos;
- var bw = bugImg.width * bugs[i].scale;
- var bh = bugImg.height * bugs[i].scale;
- // Car variables
- var cx = carXPos;
- var cy = myCar.ypos;
- var cw = 100;
- var ch = 50;
- // Check for intersection of the rectangles
- if(!(bx > cx + cw || bx + bw < cx || by > cy + ch || by + bh < cy)) {
- // Game over!
- crashSound.play();
- this.xpos = 0;
- }
- }
- //console.log('howdy');
- }
- Car.prototype.display = function() {
- // Draw car
- fill(this.c);
- rect(carXPos, this.ypos, 100, 50);
- //wheels
- fill(0);
- ellipse(carXPos + 20, this.ypos + 45, 30, 30);
- ellipse(carXPos + 80, this.ypos + 45, 30, 30);
- }
- // constructor
- function Bug() {
- this.xpos = random((height / bgImg.height) * bgImg.width);
- this.ypos = random(height);
- this.scale = 0.5 + Math.random();
- }
- Bug.prototype.display = function() {
- // Draw bug
- image(bugImg, this.xpos - myCar.xpos, this.ypos, bugImg.width * this.scale, bugImg.height * this.scale);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement