Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab 2 #20

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Maya Saxena
Comp 23
Lab 2

The first thing I did was move the ship code to the ship.js file. I was unsure
whether I should move the ship movement code to the ship file, but then I
decided that it was part of the ship and not relevant to the rest of the game.

I created the enemy.js file next and moved the enemy creation code to it as
well. The enemy doesn't really do anything, so it doesn't need an update
function.

I then attempted to write the collision code and was initially unsuccessful
because I tried to use collide rather than overlap. After switching to
overlap, my collision handler worked and killed the ship on collision.

Then I created the rock.js file, which is nearly identical to the enemy file
other than differing sprites and also a collision method that destroys the
rock on collide. This collide method couldn't be used on the ship because it
doesn't always get destroyed on collide.

I added a rock to the game.js file and determined that collision was working,
so I decided to up the ante and add more rocks and enemies in random places. I
changed the collision code to overlap the enemy and rock groups. I wrote the
random location functions to vary the location of the rocks and enemies.
14 changes: 14 additions & 0 deletions enemy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Enemy.prototype = Object.create(Phaser.Sprite.prototype);

Enemy.prototype.constructor = Enemy;

Enemy.prototype.force = {x:0.0, y:0.0};

function Enemy(game, x, y) {
Phaser.Sprite.call(this, game, x, y, 'enemy');
this.anchor.setTo(0.5, 0.5);
this.scale.setTo(0.1, 0.1);
game.physics.enable(this, Phaser.Physics.ARCADE);
this.body.allowRotation = false;
game.add.existing(this);
}
62 changes: 35 additions & 27 deletions game.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,55 @@

var boundsX = 800, boundsY = 600;
var game = new Phaser.Game(boundsX, boundsY, Phaser.AUTO, "game", {preload:preload, update:update, create:create});

var ship;
var wasd;
var enemies;
var numEnemies = 3;
var enemyMargin = 100;

var rocks;
var numRocks = 10;
var rockMargin = 50;

function preload () {
game.load.image('ship', 'ship.png');
game.load.image('enemy', 'evil.png');
game.load.image('rock', 'rock.png');
}

function create() {
ship = game.add.sprite(50, 50, 'ship');
game.physics.startSystem(Phaser.Physics.ARCADE);
ship = new Ship(game, 50, 50);

ship.anchor.setTo(0.5, 0.5);
this.cursors = game.input.keyboard.createCursorKeys();
enemies = game.add.group();
// Adds numEnemies to random locations within margin in world
for (var i = 0; i < numEnemies; i++) {
enemies.add(new Enemy(game, getRandomX(enemyMargin), getRandomY(enemyMargin)));
}

wasd = {
up: game.input.keyboard.addKey(Phaser.Keyboard.W),
down: game.input.keyboard.addKey(Phaser.Keyboard.S),
left: game.input.keyboard.addKey(Phaser.Keyboard.A),
right: game.input.keyboard.addKey(Phaser.Keyboard.D),
};
rocks = game.add.group();
// Adds numRocks to random locations within margin in world
for (var i = 0; i < numRocks; i++) {
rocks.add(new Rock(game, getRandomX(rockMargin), getRandomY(rockMargin)));
}
}

function update() {
var mX = game.input.mousePointer.x;
var mY = game.input.mousePointer.y;
/* look at the mouse */
ship.angle = Math.atan2(ship.position.x - mX, ship.position.y - mY) * -57.2957795;
game.physics.arcade.overlap(ship, enemies, collideShipWithEnemy);
game.physics.arcade.overlap(ship, rocks, collideShipWithRock);
}

if (wasd.up.isDown) {
ship.y -= 3;
}
if (wasd.down.isDown) {
ship.y += 3;
}
if (wasd.left.isDown) {
ship.x -= 3;
}
if (wasd.right.isDown) {
ship.x += 3;
}
function collideShipWithEnemy() {
ship.kill();
}

function collideShipWithRock(ship, rocks) {
rocks.collide();
}

function getRandomX(margin) {
return Math.random() * (boundsX - margin * 2) + margin;
}

function getRandomY(margin) {
return Math.random() * (boundsY - margin * 2) + margin;
}
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.4.3/custom/phaser-arcade-physics.js"></script>
<script src="ship.js"></script>
<script src="enemy.js"></script>
<script src="rock.js"></script>
<script src="game.js"></script>
</head>
<body>
<div id="game"></div>
</body>
</html>

17 changes: 17 additions & 0 deletions rock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Rock.prototype = Object.create(Phaser.Sprite.prototype);

Rock.prototype.constructor = Rock;

Rock.prototype.force = {x:0.0, y:0.0};

function Rock(game, x, y) {
Phaser.Sprite.call(this, game, x, y, 'rock');
this.anchor.setTo(0.5, 0.5);
game.physics.enable(this, Phaser.Physics.ARCADE);
this.body.allowRotation = false;
game.add.existing(this);

this.collide = function () {
this.destroy();
}
}
Binary file added rock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions ship.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Ship.prototype = Object.create(Phaser.Sprite.prototype);

Ship.prototype.constructor = Ship;

Ship.prototype.force = {x:0.0, y:0.0};

var wasd;
function Ship(game, x, y) {
Phaser.Sprite.call(this, game, x, y, 'ship');
this.anchor.setTo(0.5, 0.5);
game.physics.enable(this, Phaser.Physics.ARCADE);
this.body.allowRotation = false;
game.add.existing(this);

wasd = {
up: game.input.keyboard.addKey(Phaser.Keyboard.W),
down: game.input.keyboard.addKey(Phaser.Keyboard.S),
left: game.input.keyboard.addKey(Phaser.Keyboard.A),
right: game.input.keyboard.addKey(Phaser.Keyboard.D),
};
}

Ship.prototype.update = function() {
var mX = game.input.mousePointer.x;
var mY = game.input.mousePointer.y;
/* look at the mouse */
this.angle = Math.atan2(this.position.x - mX, this.position.y - mY) * -57.2957795;

if (game.input.activePointer.isDown) {
/* move towards the mouse */
game.physics.arcade.moveToPointer(this, 60, game.input.activePointer, 500);
}

if (wasd.up.isDown) {
ship.y -= 3;
}
if (wasd.down.isDown) {
ship.y += 3;
}
if (wasd.left.isDown) {
ship.x -= 3;
}
if (wasd.right.isDown) {
ship.x += 3;
}
}