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

*Added background image to Game scene and key board controls of the player #1

Open
wants to merge 1 commit into
base: template-v1
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 assets/background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
209 changes: 209 additions & 0 deletions src/GameScene.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,228 @@
import TimesteppedScene from "./base/TimesteppedScene";

var turnedLeft = false;
var turnedRight = true;

export default class GameScene extends TimesteppedScene {
private player: Phaser.Sprite;
private background : Phaser.Sprite;

preload() {
this.game.load.image('player', 'assets/player.png');
this.game.load.image('background','assets/background.jpg');
}

create() {
this.setBackground();
this.player = this.game.add.sprite(this.game.width / 2, this.game.height / 2, 'player');
this.player.smoothed = false;
this.player.anchor.set(0.5, 0.5);
this.player.scale.set(2, 2);
}

fixedUpdate(dt: number) {

if(this.game.input.keyboard.isDown(Phaser.Keyboard.Q)) {
this.turnLeft();
}else if(this.game.input.keyboard.isDown(Phaser.Keyboard.E)){
this.turnRight();
}else if(this.game.input.keyboard.isDown(Phaser.Keyboard.W) && this.game.input.keyboard.isDown(Phaser.Keyboard.D)){
this.goUpRight();
}else if(this.game.input.keyboard.isDown(Phaser.Keyboard.W) && this.game.input.keyboard.isDown(Phaser.Keyboard.A)){
this.goUpLeft();
}else if(this.game.input.keyboard.isDown(Phaser.Keyboard.S) && this.game.input.keyboard.isDown(Phaser.Keyboard.D)){
this.goDownRight();
}else if(this.game.input.keyboard.isDown(Phaser.Keyboard.S) && this.game.input.keyboard.isDown(Phaser.Keyboard.A)){
this.goDownLeft();
}else if(this.game.input.keyboard.isDown(Phaser.Keyboard.A)){
this.goLeft();
}else if(this.game.input.keyboard.isDown(Phaser.Keyboard.D)){
this.goRight();
}else if(this.game.input.keyboard.isDown(Phaser.Keyboard.W)){
this.goUp();
}else if(this.game.input.keyboard.isDown(Phaser.Keyboard.S)){
this.goDown();
}
}

/**
* set background
*/
setBackground(){
this.background = this.game.add.sprite(this.game.width / 2, this.game.height / 2, 'background');
this.background.anchor.set(0.5, 0.5);
}

/**
* turn left
*/
turnLeft(){
if(!turnedLeft){
this.player.scale.set(-2, 2);
this.player.anchor.set(-this.player.anchor.x,this.player.anchor.y);
this.updatePlayerDir();
}
}

/**
* turn right
*/
turnRight(){
if(!turnedRight){
this.player.scale.set(2, 2);
this.player.anchor.set(-this.player.anchor.x,this.player.anchor.y);
this.updatePlayerDir();
}
}

/**
* update player direction
*/
updatePlayerDir(){
if(!turnedLeft){
turnedLeft = true;
turnedRight = false;
}else{
turnedLeft = false;
turnedRight = true;
}
}

/**
* go Up left
*/
goUpLeft(){
if(!this.checkUpBoundary() || !this.checkLeftBoundary())
return;

if(turnedRight)
this.player.anchor.set(this.player.anchor.x + 0.1, this.player.anchor.y + 0.1);
else
this.player.anchor.set(this.player.anchor.x - 0.1, this.player.anchor.y + 0.1);
}

/**
* go Up right
*/
goUpRight(){
if(!this.checkUpBoundary() || !this.checkRightBoundary())
return;

if(turnedRight)
this.player.anchor.set(this.player.anchor.x - 0.1, this.player.anchor.y + 0.1);
else
this.player.anchor.set(this.player.anchor.x + 0.1, this.player.anchor.y + 0.1);
}

/**
* go down left
*/
goDownLeft(){
if(!this.checkDownBoundary() || !this.checkLeftBoundary())
return;

if(turnedRight)
this.player.anchor.set(this.player.anchor.x + 0.1, this.player.anchor.y - 0.1);
else
this.player.anchor.set(this.player.anchor.x - 0.1, this.player.anchor.y - 0.1);
}

/**
* go Down Right
*/
goDownRight(){
if(!this.checkDownBoundary() || !this.checkRightBoundary())
return;

if(turnedRight)
this.player.anchor.set(this.player.anchor.x - 0.1, this.player.anchor.y - 0.1);
else
this.player.anchor.set(this.player.anchor.x + 0.1, this.player.anchor.y - 0.1);
}

/**
* go left
*/
goLeft(){
if(!this.checkLeftBoundary())
return;

if(!turnedLeft)
this.player.anchor.set(this.player.anchor.x +0.1, this.player.anchor.y);
else
this.player.anchor.set(this.player.anchor.x -0.1, this.player.anchor.y);
}

/**
* go right
*/
goRight(){
if(!this.checkRightBoundary())
return;

if(turnedRight)
this.player.anchor.set(this.player.anchor.x -0.1, this.player.anchor.y);
else
this.player.anchor.set(this.player.anchor.x +0.1, this.player.anchor.y);
}

/**
* go UP
*/
goUp(){
if(!this.checkUpBoundary())
return;

this.player.anchor.set(this.player.anchor.x, this.player.anchor.y + 0.1);
}

/**
* go Down
*/
goDown(){
if(!this.checkDownBoundary())
return;

this.player.anchor.set(this.player.anchor.x, this.player.anchor.y - 0.1);
}

/**
* check Up boundary
*/
checkUpBoundary(){
if(this.player.anchor.y < 7.4)
return true;
else
return false;
}

/**
* check Down boundary
*/
checkDownBoundary(){
if(this.player.anchor.y > -6.6)
return true;
else
return false;
}

/**
* check left boundary
*/
checkLeftBoundary(){
if(this.player.anchor.x < 8.5)
return true;
else
return false;
}

/**
* check right boundary
*/
checkRightBoundary(){
if(this.player.anchor.x > -7.5)
return true;
else
return false;
}
}