xxxxxxxxxx
334
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 start;
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();
start = true;
pause = true;
//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 (start == false){
if (mouseX >= 680 && mouseY <= 40) {
if (bulletCount > 0) {
pause = !pause;
}
}
else {
if (pause == false && bulletCount > 0) {
gunObj.gunMove = true;
bulletCount--;
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++;
}
}
}
}
}
if (pause == true) {
if (mouseX < 455 && mouseX > 275) {
if (mouseY < 290 && mouseY > 250) {
bulletCount = 30;
score = 0;
pause = false;
}
}
if (bulletCount > 0){
if (mouseX < 455 && mouseX > 275) {
if (mouseY < 330 && mouseY > 290) {
pause = false;
}
}
}
}
}}
else {
if (mouseX < 405 && mouseX > 275) {
if (mouseY < 290 && mouseY > 250) {
start = false;
pause = false;
}
}
}
}
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();
if (bulletCount == 0) {
pause = true;
}
//pauseUI
if (pause == true && start == false) {
background(198, 134, 58);
textFont("Times New Roman", 100);
fill("white");
text("Score: "+score, 180, 150);
textFont("Courier New", 40);
fill(198, 134, 58);
rect(275, 250, 180, 40);
fill("white");
text("Restart", 280, 280);
if (bulletCount != 0) {
textFont("Courier New", 40);
fill(198, 134, 58);
rect(275, 290, 180, 40);
fill("white");
text("Resume", 280, 320);
}
}
// display score & UI elements
fill("white");
textFont("Courier New", 25);
text("Score: " + score, 20, 510);
image(pauseIcon, 700, 20, 40, 40);
text("Ammo: " + bulletCount, 20, 470);
if (start == true && pause == true){
background(198, 134, 58);
textFont("Times New Roman", 100);
fill("white");
text("Golem Hunters", 50, 150);
textFont("Courier New", 40);
fill(198, 134, 58);
rect(275, 250, 130, 40);
fill("white");
text("START", 280, 280);
textFont("Courier New", 20);
text("Left Click = Shoot / Move your mouse to aim.", 70, 330);
text("Shoot as Many Golems", 70, 355);
text("as possible using", 70, 380);
text("Limited amounts of", 70, 405);
text("Bullets!!", 70, 430);
}
}
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.flip = 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.flip = true;
this.speed = -this.speed;
this.posX = 1399;
} else if (this.posX < -700) {
this.flip = true;
this.speed = -this.speed;
this.posX = -699;
}
if (this.flip == true){
for (let i=0; i<11; i++){
this.sprite[i]
}
}
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;
}
}
}