xxxxxxxxxx
348
//Made by YM_Zhou with p5.js and p5.play
//Unnamed space shooter game
//sprite
let heroPlane;
let heroBullet;
// let testTarget; //test only
let commonEnemy;
//constants
const maxHP = 5; //maximum hp of hero
const maxMssl = 2; //maximum number of missiles
const maxLas = 1; //maximum number of laser
const lasC = 1000; //charge needed to get a laser
const sp = 6; //speed of the plane
const shootCD = 30; //normal shoot cooldown
const powerUp1CD = 5; //shoot cooldown of more bullet powerup
const up1Time = 180;
const bulletSp = 10; //player bullet speed
const afterHit = 10; //player does not take damage 10 frames after hit
const bgSp = 3;
//variables
let heroHP = 5; //current hp
let mssl = 2; //current missile
let las = 0; //current laser
let shootCounter = 0;
let lasCharge = 0; //current laser charge
let powerUp = 0; //0:normal;1:veryLowShootCD
let playerHurt = true;
let hitCounter = 0;
let bgY1; //background1
let bgY2; //background2
let generalCount1 = 0;
let point = 0;
//image
let star; //background star
//sound
let pickup;
let failure;
let shoot;
let bgm;
function preload() {
star = loadImage("bg/star.png");
shoot = loadSound("sfx/shoot.wav")
pickup = loadSound("sfx/pickup.wav")
failure = loadSound("sfx/fail.wav")
bgm = loadSound("bgm.mp3")
/*
shoot sound effect: https://freesound.org/people/MATRIXXX_/sounds/413057/
pickup sound effect:
https://freesound.org/people/suntemple/sounds/253172/
failure sound effect:
https://freesound.org/people/suntemple/sounds/253174/
bgm:
https://freesound.org/people/zagi2/sounds/248117/
*/
}
function setup() {
new Canvas(640, 1280);
noSmooth();
bgY1 = height / 2;
bgY2 = -height / 2;
//loadAni
heroBullet = new Group();
heroBullet.addAni("fly", "sprite/heroBullet/bullet.png");
heroBullet.vel.y = -bulletSp;
heroBullet.vel.x = 0;
heroBullet.rotationLock = true;
heroBullet.scale = 10;
heroBullet.rotationLock = true;
// console.log(heroBullet.collider);
heroPlane = new Sprite(width / 2, height / 2 + 40, 8, 8, "kinematic");
heroPlane.addAni("left", "sprite/hero/heroPlaneL.png");
heroPlane.addAni("right", "sprite/hero/heroPlaneR.png");
heroPlane.addAni("fly", "sprite/hero/heroPlane.png");
heroPlane.scale = 10;
heroPlane.rotationLock = true;
// testTarget = new Sprite(width / 2, 10);
//common enemy1
commonEnemy = new Group();
commonEnemy.addAni("sprite/enemy/common1.png");
commonEnemy.vel.y = 5;
commonEnemy.scale = 10;
commonEnemy.rotationLock = true;
//common enemy2
fastEnemy = new Group();
fastEnemy.addAni("sprite/enemy/common2.png");
fastEnemy.vel.y = 8;
fastEnemy.scale = 10;
fastEnemy.rotationLock = true;
miniBoss = new Group();
miniBoss.addAni("sprite/enemy/common4.png");
miniBoss.scale = 12;
miniBoss.rotationLock = true;
//enemy overlap bullet
// testTarget.overlaps(heroBullet,destroyE);
commonEnemy.overlaps(heroBullet, destroyE);
fastEnemy.overlaps(heroBullet, destroyE);
heroPlane.overlaps(commonEnemy, hitPlayer);
heroPlane.overlaps(fastEnemy, hitPlayer);
fastEnemy.overlaps(commonEnemy);
miniBoss.overlaps(commonEnemy);
miniBoss.overlaps(fastEnemy);
miniBoss.overlaps(heroBullet);
heroBullet.overlaps(heroBullet);
bgm.loop();
}
function draw() {
background(0);
imageMode(CENTER);
scrollingBg();
heroController();
cheatMaster();
//spawner list
enemySpawner(60, commonEnemy, 2, 7, 5);
enemySpawner(130, commonEnemy, 2, 7, 5);
enemySpawner(280,commonEnemy,3,7,5);
enemySpawner(360,commonEnemy,3,7,5);
enemySpawner(450, fastEnemy, 1, 7, 5);
enemySpawner(650,commonEnemy,4,7,5);
enemySpawner(700, fastEnemy, 2, 7, 5);
enemySpawner(1100, fastEnemy, 1, 7, 5);
enemySpawner(1150, fastEnemy, 2, 7, 5);
enemySpawner(1200,commonEnemy,5,7,5);
enemySpawner(1600, fastEnemy, 1, 7, 5);
enemySpawner(1650, fastEnemy, 1, 7, 5);
enemySpawner(1700, fastEnemy, 1, 7, 5);
// bossSpawner(1800,miniBoss,1,7,5)
// miniBossController();
}
function heroController() {
movementController(sp, 40);
powerController();
cheatMaster();
if (playerHurt == false) {
if (hitCounter >= afterHit) {
playerHurt = true;
} else {
hitCounter++;
}
}
}
function scrollingBg() {
/*
DONE:
make the background looping
*/
if(bgY1 >= 3* height / 2)
{
bgY1 = -height/2
}
else
{
bgY1 += bgSp;
}
if(bgY2 >= 3* height / 2)
{
bgY2 = -height/2
}
else
{
bgY2 += bgSp;
}
image(star, width / 2, bgY1,star.width * 5, star.height * 5);
image(star, width / 2, bgY2,star.width * 5, star.height * 5);
}
function destroyE(target, bullet) {
//when the bullet hit enemy, destroy it
target.remove();
//remove the bullet as well
bullet.remove();
/*
need to be done:
explosion when the target is destroyed
*/
}
function shootController(CD) {
if (kb.pressing(" ") && shootCounter <= 0) {
//if the counter is 0, spawn a bullet
new heroBullet.Sprite(heroPlane.x, heroPlane.y, 3, 6, "kinematic");
// if(powerUp == 0)
// {
shoot.play(0,1,0.3);
// }
//when the bullet is shot, set the counter to CD
shootCounter = CD;
// console.log("shoot")
}
if (shootCounter >= 0) {
//if the counter is not 0, reduce to 0 gradually
shootCounter--;
}
}
function enemySpawner(time, type, number, collX, collY) {
/*
adjust the generator so that the sprites would not overlap
update 11.22: this function is finished
*/
let gap = collX;
let block = width / number;
if (frameCount == time) {
for (i = 0; i <= number - 1; i++) {
new type.Sprite(
random(i * block + gap, (i + 1) * block - gap),
0,
collX,
collY
);
}
}
}
function bossSpawner(time, type, number, collX, collY) {
let gap = collX;
let block = width / number;
if (frameCount == time) {
new type.Sprite(
width / 2,
0,
collX,
collY
);
}
}
// function miniBossController()
// {
// if (miniBoss.y >= 100)
// {
// miniBoss.vel.y = 0;
// miniBoss.vel.x = 5;
// }
// else
// {
// miniBoss.vel.y = 1;
// }
// }
function movementController(sp, size) {
//movement controller
if (kb.pressing("ArrowLeft") && !kb.pressing("ArrowRight")) {
heroPlane.ani = "left";
if (heroPlane.x >= size) {
heroPlane.vel.x = -sp;
}
} else if (kb.pressing("ArrowRight") && !kb.pressing("ArrowLeft")) {
heroPlane.ani = "right";
if (heroPlane.x <= width - size) {
heroPlane.vel.x = sp;
}
} else {
heroPlane.ani = "fly";
heroPlane.vel.x = 0;
}
//stuff that deal with strange bug starts HERE, DON'T DELETE!!!!
if (heroPlane.x >= width - size && !kb.pressing("ArrowLeft")) {
heroPlane.ani = "fly";
heroPlane.vel.x = 0;
}
if (heroPlane.x <= size && !kb.pressing("ArrowRight")) {
heroPlane.ani = "fly";
heroPlane.vel.x = 0;
}
//bug dealer ends HERE
if (kb.pressing("ArrowUp") && heroPlane.y >= size) {
heroPlane.vel.y = -sp;
} else if (kb.pressing("ArrowDown") && heroPlane.y <= height - size) {
heroPlane.vel.y = sp;
} else {
heroPlane.vel.y = 0;
}
}
function cheatMaster() {
//cheat stuff
if (kb.presses("1")) {
//to more bullet
powerUp = 1;
}
if (kb.presses("0")) {
//to normal
powerUp = 0;
}
}
function powerController() {
if (powerUp == 0) {
shootController(shootCD);
}
if (powerUp == 1) {
shootController(powerUp1CD);
generalCount1 ++;
if(generalCount1 >= up1Time)
{
powerUp = 0;
generalCount1 = 0;
}
}
}
function hitPlayer(hero, enemy) {
enemy.remove();
/*
need to do:
player plane turn red if hit
*/
if (playerHurt == true) {
heroHP--;
playerHurt = false;
console.log("you r hurt");
} else {
console.log("lucky you");
}
}