xxxxxxxxxx
215
let gunSprite;
let backgroundimg;
let shootsound;
let deathsound;
let bgmusic;
let Gun = [];
let GunObj;
let golemSprite;
let GolemObj;
let Golems = [];
let GolemArr = [];
//UI elements
let score = 0;
let pause;
let pauseIcon;
let bulletCount = 30;
let mousespeed = 2.5;
let step = 0;
function preload() {
backgroundimg = loadImage("background.jpg");
gunSprite = loadImage("gunsprite.png");
shootsound = loadSound("shootsound.mp3");
golemSprite = loadImage("golem.png");
pauseIcon = loadImage("pause.png");
deathsound = loadSound("death.mp3");
bgmusic = loadSound("bgmusic.mp3");
}
function setup() {
createCanvas(720, 540);
frameRate(60);
imageMode(CENTER);
bgmusic.play();
pause = false;
//create Gunsprite
let w = gunSprite.width / 5
for (let i = 0; i < 5; i++) {
Gun[i] = gunSprite.get(i * w, 0, w, gunSprite.height);
}
// create golem Sprite
for (let i = 0; i<4; i++) {
Golems[i] = golemSprite.get(-10 + i * (golemSprite.width/9 - 7), golemSprite.height/5, golemSprite.width/9-30, golemSprite.height/5-30);
}
for (let i=4; i<12; i++) {
Golems[i] = golemSprite.get((i-4) * golemSprite.width/8, golemSprite.height/5*4, golemSprite.width/8, golemSprite.height/5)
}
// create golem Pool of 20 golems
for (let i = 0; i < 20; i++) {
GolemObj = new Golem(Golems, random(-600, 1300), random(200, 500), deathsound);
GolemArr.push(GolemObj);
}
//create gun Object
gunObj = new GunClass(Gun, shootsound);
}
// if mouse is pressed, add another bullet
function mousePressed(){
let moveX = (width/2-mouseX)*mousespeed;
let moveY = (height/2-mouseY)*mousespeed/2.5;
if (mouseX >=680 && mouseY <= 40){
pause = !pause;
}
else {
if (pause == false){
gunObj.gunMove = true;
for (let i = 0; i < 20; i++) {
if (width/2 - moveX < GolemArr[i].posX+80 && width/2 - moveX > GolemArr[i].posX - 80){
if (height/2 - moveY < GolemArr[i].posY+80 && height/2 - moveY > GolemArr[i].posY-80){
if (GolemArr[i].dead == false){
GolemArr[i].dead = true;
score++;
}
}
}
}
}}
}
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);
push();
translate(moveX, moveY);
// display golems
for (let i = 0; i < 20; i++) {
GolemArr[i].display();
GolemArr[i].alive();
GolemArr[i].deadMotion();
GolemArr[i].respawn();
}
// display all zombies
// display all human mobs
pop();
// 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);
// display gun
gunObj.display();
gunObj.move();
//pauseUI
if (pause == true){
background(140,140,30);
}
// display score & UI elements
fill("white");
textFont("Courier New", 25);
text("Score: " + score,20, 510);
image(pauseIcon, 700, 20, 40, 40)
}
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;
}
}
}
}
class Golem {
constructor(images, posX, posY, deadsound){
this.posX = posX;
this.posY = posY;
this.Xscale = 1;
this.speed = random (-3,3);
this.sprite = images;
this.move = true;
this.dead = false;
this.frame = 0;
this.deadsound = deadsound;
this.soundplay = true;
}
display() {
image(this.sprite[this.frame], this.posX, this.posY);
}
alive() {
if (this.move == true){
//golem moving mechanism
if (this.posX > 1400){
this.speed = -this.speed;
this.posX = 1399;
}
else if (this.posX < -700){
this.speed = -this.speed;
this.posX = -699;
}
this.posX += this.speed;
if (this.dead == false && frameCount%10 == 0){
this.frame++;
if (this.frame == 4){
this.frame = 0;
}
}}
}
deadMotion() {
if (this.move == true){
if (this.dead == true && frameCount%15 == 0){
this.speed = 0;
if (this.soundplay){this.deadsound.play(); this.soundplay = false;}
if (this.frame < 11){
this.frame++;
}
else {
this.move = false;
this.frame = 0;
this.speed = random (-3,3);
this.posX = random(-600, 1300);
this.posY = random(200,500);
this.soundplay = true;
}
}}
}
respawn() {
if (this.move == false && frameCount%120 == 0) {
this.move = true;
this.dead = false;
}
}
}