From d419c4d33ee61ec98247fd3aba57ba5001077fc8 Mon Sep 17 00:00:00 2001 From: Victor Brun Date: Wed, 3 May 2023 12:27:51 +0200 Subject: [PATCH] work in progress avancement du perso --- source/Jeu.ts | 10 ++++------ source/Pacman.ts | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/source/Jeu.ts b/source/Jeu.ts index d421f14..158b14d 100644 --- a/source/Jeu.ts +++ b/source/Jeu.ts @@ -9,8 +9,6 @@ class Jeu extends Scene { public carte_ : Array>; public wall_ : Sprite; public pas_ : number; - public px : number; - public py : number; public pacman_ : Pacman; @@ -19,9 +17,7 @@ class Jeu extends Scene { public constructor(element : HTMLElement) { super(element,false); /* Ecrire ici le code qui initialise la scene. */ - this.pas_ = 32; - this.px = 4; - this.py = 7; + this.pas_ = 32; } @@ -50,7 +46,7 @@ private dessinerLabyrinthe(){ this.appendChild(this.wall_); } if (this.carte_[i][j] == 8){ - this.pacman_ = new Pacman(this,document.createElement("img")); + this.pacman_ = new Pacman(this,document.createElement("img"),4,7); this.pacman_.setImage("Squirrel.png",32,32); this.pacman_.setXY(this.pas_*j,this.pas_*i); this.appendChild(this.pacman_); @@ -64,11 +60,13 @@ private dessinerLabyrinthe(){ console.log(this.carte_); this.initialiserCarte(); this.dessinerLabyrinthe(); + this.pacman_.animer(); } //--------------------------------------------------------------------------------------------pause public override pause() { /* Ecrire ici le code qui met la scene en pause. */ + this.pacman_.figer(); } //------------------------------------------------------------------------------------------unpause diff --git a/source/Pacman.ts b/source/Pacman.ts index 356ff03..cc91f65 100644 --- a/source/Pacman.ts +++ b/source/Pacman.ts @@ -3,39 +3,49 @@ class Pacman extends Sprite { //Attributs public scene_ : Jeu; private ecouteurDeplacer : any; + public px_ : number; + public py_ : number; //Constructeur - public constructor(scene : Jeu,element : HTMLElement){ + public constructor(scene : Jeu,element : HTMLElement, py_ : number, px_ : number){ super(element); this.scene_ = scene; - this.ecouteurDeplacer + this.px_ = 4; + this.py_ = 7; + this.ecouteurDeplacer = (event : KeyboardEvent) => {this.deplacer(event)};; } public haut(){ - if(this.scene_.carte_[this.scene_.py-1][this.scene_.px] == 1){ + this.py_ = this.py_-1; + this.px_ = this.px_-1; + if(this.scene_.carte_[this.py_-1][this.px_] == 1){ }else{ - this.setXY(this.getX()-this.scene_.pas_,this.getY()); + this.py_ = this.py_-1; + this.setXY(this.getX(),this.getY()-this.scene_.pas_); } } public bas(){ - if(this.scene_.carte_[this.scene_.py+1][this.scene_.px] == 1){ + if(this.scene_.carte_[this.py_+1][this.px_] == 1){ }else{ - this.setXY(this.getX()+this.scene_.pas_,this.getY()); + this.py_ = this.py_+1; + this.setXY(this.getX(),this.getY()+this.scene_.pas_); } } public gauche(){ - if(this.scene_.carte_[this.scene_.py][this.scene_.px-1] == 1){ + if(this.scene_.carte_[this.py_][this.px_-1] == 1){ }else{ - this.setXY(this.getX(),this.getY()-this.scene_.pas_); + this.px_ = this.px_-1; + this.setXY(this.getX()-this.scene_.pas_,this.getY()); } } public droite(){ - if(this.scene_.carte_[this.scene_.py+1][this.scene_.px+1] == 1){ + if(this.scene_.carte_[this.py_][this.px_+1] == 1){ }else{ - this.setXY(this.getX(),this.getY()+this.scene_.pas_); + this.px_ = this.px_+1; + this.setXY(this.getX()+this.scene_.pas_,this.getY()); } } private deplacer(event : KeyboardEvent){ @@ -46,13 +56,18 @@ class Pacman extends Sprite { this.droite(); } if(event.key == "ArrowDown"){ - this.haut(); - } - if(event.key == "ArrowLeft"){ this.bas(); } + if(event.key == "ArrowUp"){ + this.haut(); + } } + public animer(){ + window.addEventListener("keydown", this.ecouteurDeplacer); + } + public figer(){ + window.removeEventListener("keydown", this.ecouteurDeplacer); } } \ No newline at end of file