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 #9

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
8 changes: 8 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Greta Jochem
Game Design
Simple Shooter Lab
September 30th

I refractered the enemy code into its own file, enemy.js, which has instructions for how to create an
enemy and add it to the game. Similarily, I refractered the ship (or player as the lab called it) code into its own file. The ship's code is more complicated than the refractered enemy code. It contains instructions to make a ship, but also has an update function allowing the user to move the ship. It also has a collider function that takes handles situations in which the ship collides with either a rock or a enemy. In game.js, I have standard functions, preload, create, update, that control the game. In the update function, overlaps between different sprites are detected, and then sent to collision functions within that specific sprite that decide what to do. I also implemented a rock group with a collision function that causes it to
die.
15 changes: 15 additions & 0 deletions enemy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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.scale.set(.20, .20);
this.anchor.setTo(0.5, 0.5);
game.physics.enable(this, Phaser.Physics.ARCADE);
this.body.allowRotation = false;
game.add.existing(this);
}
73 changes: 43 additions & 30 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,58 @@ 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 wasd;
var enemy;

function Rock(group, viewGroup, x, y) {
var rock = group.create(0, 0, 'rock');
rock.position.x = x;
rock.position.y = y;

rock.scale.setTo(0.10, 0.10); //resize png
rock.anchor.setTo(0.5, 0.5);

game.physics.enable(rock, Phaser.Physics.ARCADE);


//tell rocks how to handle colissions
rock.collide = function ( ) {
//rock is destoyed when collides with something
this.destroy( );
};
}

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');

ship.anchor.setTo(0.5, 0.5);
this.cursors = game.input.keyboard.createCursorKeys();

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),
};
//enable arcade physics
game.physics.startSystem(Phaser.Physics.ARCADE);

//adding instance of ship in at center
ship = new Ship (game, game.world.centerX, game.world.centerY);
enemy = new Enemy (game, 50, 50);

rocks = game.add.group ();
viewGroup = game.add.group();
for ( var i = 0; i < 10; i++) {
var rock = Rock(rocks, viewGroup, 80*i, 400);
}

}

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;

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;
}

//rocks hit ship
game.physics.arcade.overlap(ship, rocks, collisionHandler1, null, this);
//enemy hit ships
game.physics.arcade.overlap(enemy, ship, ship.collisionHandler2, null, this);
}

//how rock handles colissions
function collisionHandler1 (ship, collider) {
collider.collide();
}

2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<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="game.js"></script>
</head>
<body>
Expand Down
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.
56 changes: 56 additions & 0 deletions ship.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Ship.prototype = Object.create(Phaser.Sprite.prototype);

Ship.prototype.constructor = Ship;

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


Ship.prototype.collide = function ( ) {
//ship is destoyed when collides with something
this.destroy( );
}

Ship.prototype.collisionHandler2 = function(enemy, collider){
collider.collide();
}

function Ship (game, x, y) {

Phaser.Sprite.call(this, game, x, y, 'ship');
//scale sprite
this.scale.set(0.50, 0.50);
this.anchor.setTo(0.5, 0.5);
game.physics.enable(this, Phaser.Physics.ARCADE);
this.body.allowRotation = false;
game.add.existing(this);

this.cursors = game.input.keyboard.createCursorKeys();
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 mouse*/
this.angle = Math.atan2(this.position.x - mX, this.position.y - mY) * -57.2957795;

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;
}

}