xxxxxxxxxx
121
//This is the final project for HCDE 524. The inspiration and help came from pretty much some of the assignments before and p5.play library examples and medium article on creating an asteroid game.
//Commands for play. Arrow keys to move + space to shoot
//Variables
var bg;
var bullets;
var ghost;
var pacman;
var pacImage, bulletImage, ballImage;
var MARGIN = 30;
function setup() {
createCanvas(500, 500); //// Create a container to display the results
//loading the images
bg = loadImage('assets/bg.png');
bulletImage = loadImage('assets/pac_bullet.png');
pacImage = loadImage('assets/pac_pacman.png');
ballImage = loadImage('assets/pac_ball.png');
//creating sprite for pacman
pacman = createSprite(width / 2, height / 2);
pacman.maxSpeed = 6;
pacman.friction = 0.98;
pacman.setCollider('circle', 0, 0, 20);
pacman.addImage('normal', pacImage);
//animating when the wrong key is pressed
pacman.addAnimation('moving', 'assets/pac_pacman01.png', 'assets/pac_pacman01.png');
ghost = new Group();
bullets = new Group();
for (var i = 0; i < 8; i++) {
var ang = random(360);
var px = width / 1 + 1000 * cos(radians(ang));
var py = height / 1 + 1000 * sin(radians(ang));
createGhost(2, px, py);
}
}
function draw() {
background(bg);
//instructions
fill(255);
textAlign(CENTER);
text('Controls: Arrow Keys + Space', width / 2, 20);
//postion the sprites in the margin
for (var i = 0; i < allSprites.length; i++) {
var s = allSprites[i];
if (s.position.x < -MARGIN) s.position.x = width + MARGIN;
if (s.position.x > width + MARGIN) s.position.x = -MARGIN;
if (s.position.y < -MARGIN) s.position.y = height + MARGIN;
if (s.position.y > height + MARGIN) s.position.y = -MARGIN;
}
ghost.overlap(bullets, ghostHit);
pacman.bounce(ghost);
pacman.bounce(ghost);
// when the key pressed up, down, right, left
if (keyDown(LEFT_ARROW))
pacman.rotation -= 5; //rotation frequency
if (keyDown(RIGHT_ARROW))
pacman.rotation += 5;//rotation frequency
if (keyDown(UP_ARROW)) {
pacman.addSpeed(0.2, pacman.rotation);
pacman.changeAnimation('moving'); //animation of moving
} else
pacman.changeAnimation('normal');
//when space is pressed the bullet is fired
if (keyWentDown('space')) {
var bullet = createSprite(pacman.position.x, pacman.position.y);
bullet.addImage(bulletImage);
bullet.setSpeed(10 + pacman.getSpeed(), pacman.rotation);
bullet.life = 30;
bullets.add(bullet);
}
drawSprites();
}
//creating the ghost function
function createGhost(type, x, y) {
var a = createSprite(x, y); //create a sprite
var img = loadImage('assets/asteroid' + floor(random(0, 3)) + '.png');
a.addImage(img);
a.setSpeed(2.5 - (type / 3), random(360)); //setting the speed when they are moving in the canvas
a.rotationSpeed = 0.8; //setting the rotation speed
// a.debug = true;
a.type = type;
if (type == 2)
a.scale = 0.6;
if (type == 1)
a.scale = 0.3;
a.mass = 2 + a.scale;
a.setCollider('circle', 0, 0, 60); // collide
ghost.add(a);
return a;
}
//ghostHit function to split and reduce the size
function ghostHit(ghost, bullet) {
var newType = ghost.type - 1;
if (newType > 0) {
createGhost(newType, ghost.position.x, ghost.position.y);
createGhost(newType, ghost.position.x, ghost.position.y);
}
// setting up bullet position
for (var i = 0; i < 10; i++) {
var p = createSprite(bullet.position.x, bullet.position.y);
p.addImage(ballImage);
p.setSpeed(random(3, 5), random(360));
p.friction = 0.95; //creating fiction between the bullet and ghost
p.life = 10;
}
//removes bullet and ghost from the canvas
bullet.remove();
ghost.remove();
}