xxxxxxxxxx
85
let gunSprite;
let backgroundimg;
let shootsound;
let Gun = [];
let GunObj;
let mousespeed = 2.5;
let step = 0;
function preload() {
backgroundimg = loadImage("background.jpg");
gunSprite = loadImage("gunsprite.png");
shootsound = loadSound("shootsound.mp3");
}
function setup() {
createCanvas(720, 540);
imageMode(CENTER);
// get the width and hieght of each sprite
let w = gunSprite.width / 5
for (let i = 0; i < 5; i++) {
Gun[i] = gunSprite.get(i * w, 0, w, gunSprite.height);
}
gunObj = new GunClass(Gun, shootsound);
}
// if mouse is pressed, add another bullet
function mousePressed(){
if (mouseX >=680 && mouseY <= 40){
// pause menu
}
else {
gunObj.gunMove = true;
}
}
function draw() {
let moveX = (width/2-mouseX)*mousespeed;
let moveY = (height/2-mouseY)*mousespeed/2.5;
image(backgroundimg, width/2, height/2, width, height, backgroundimg.width/2 - moveX - 400, backgroundimg.height/2 - moveY -100, 800, 600);
// cross
stroke(0);
line(width/2-20, height/2, width/2+20, height/2);
line(width/2, height/2-20, width/2, height/2+20);
push();
translate(moveX, moveY);
// display all zombies
// display all human mobs
pop();
gunObj.display();
gunObj.move();
}
class GunClass {
constructor(images, shootsound){
this.gunMove = false;
this.sprite = images;
this.frame = 0;
this.shootsound = shootsound;
}
display() {
image(this.sprite[this.frame], width/2+150, height-110, gunSprite.width*1.3/4, gunSprite.height*1.3);
}
move() {
if (this.gunMove == true && frameCount%3 == 0){
if (this.frame==0) {this.shootsound.play();}
this.frame++;
if (this.frame == 4){
this.frame = 0;
this.gunMove = false;
}
}
}
}