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

Development #14

Open
wants to merge 3 commits into
base: master
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
Binary file added .game.js.swp
Binary file not shown.
Binary file added .index.html.swp
Binary file not shown.
5 changes: 5 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Changes:
1. Moved the player to an individual file. Player barely interacts with game.js anymore; the only thing the game does is create the player and let it run by itself
2. Created an enemy group and gave it an individual file.
3. Managed collision handling in the update function of the main game
4. Created rocks, which appear randomly on the screen and apparently don't do anything very interesting. Since the player destroys them if they're hit, the player can completely ignore them.
24 changes: 24 additions & 0 deletions enemy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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, 'evil');
enemy.position.x = x;
enemy.position.y = y;
enemy.scale.setTo(0.05, 0.05);
enemy.anchor.setTo(0.5, 0.5);
game.physics.enable(enemy, Phaser.Physics.ARCADE);
}

Enemy.prototype.update = function() {
var mX = game.input.mousePointer.x;
var mY = game.input.mousePointer.y;

this.angle = Math.atan2(this.position.x - mX, this.position.y - mY) * -57.2957795;
enemies.forEach(function(enemy){
enemy.angle++;
});
}
Binary file added evil.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

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

var ship;

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

function create() {
ship = new Ship(game, game.world.centerX, game.world.centerY);
enemies = game.add.group();
rocks = game.add.group();
enemies.enableBody = true;
rocks.enableBody = true;
for (var i = 0;i<4;i++){
var enemy = Enemy(enemies, 200 + i*50, 300);
var rock = rocks.create(100 + 40*Math.Random(), 200 + 50*Math.Random(), 'rock');
}
}

function update() {
game.physics.arcade.collide(ship, enemies);
game.physics.arcade.collide(ship, rocks);
game.physics.arcade.overlap(ship, enemies, shipDeath, null, this);
game.physics.arcade.overlap(ship, rocks, destroyRock, null, this);

}

function shipDeath(ship, enemy) {
ship.kill();
game.shutdown();
}

function destroyRock(ship, rock) {
rock.kill();
}
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.4.3/custom/phaser-arcade-physics.js"></script>
<script src="game.js"></script>
</head>
<body>
<div id="game"></div>
</body>
</html>

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.
35 changes: 35 additions & 0 deletions ship.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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(ship, Phaser.Physics.ARCADE);
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),
};
game.add.existing(this);
}

Ship.prototype.update = function() {
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;
}
}
Binary file added ship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.