* Adapt status and fix key handling in IE.

This commit is contained in:
michael 2019-07-24 07:25:33 +00:00
parent 20d3f2f5de
commit f8e50a2d39

View File

@ -79,6 +79,7 @@ type
lbBonusCnt: TJSHTMLElement; lbBonusCnt: TJSHTMLElement;
lbLives: TJSHTMLElement; lbLives: TJSHTMLElement;
lbScore: TJSHTMLElement; lbScore: TJSHTMLElement;
lbStatus: TJSHTMLElement;
lbHiscore: TJSHTMLElement; lbHiscore: TJSHTMLElement;
lbGhostCnt: TJSHTMLElement; lbGhostCnt: TJSHTMLElement;
FCanvasEl:TJSHTMLCanvasElement; FCanvasEl:TJSHTMLCanvasElement;
@ -125,6 +126,7 @@ type
procedure StartTimer; procedure StartTimer;
procedure ShowText(aText: string; OnDone : TProcedure); procedure ShowText(aText: string; OnDone : TProcedure);
procedure UpdateScore(); procedure UpdateScore();
procedure UpdateStatus(aText : String);
// Initializing code // Initializing code
procedure InitSprite(var aSprite: TSprite; aImg: TJSHTMLImageElement; aSpd: Double); procedure InitSprite(var aSprite: TSprite; aImg: TJSHTMLImageElement; aSpd: Double);
procedure InitSprites(); procedure InitSprites();
@ -551,6 +553,11 @@ begin
lbGhostCnt.InnerText:= inttostr(GhostCnt); lbGhostCnt.InnerText:= inttostr(GhostCnt);
end; end;
procedure TPacman.UpdateStatus(aText: String);
begin
lbStatus.InnerText:=aText;
end;
//============================================================================== //==============================================================================
// Initialization code // Initialization code
//============================================================================== //==============================================================================
@ -696,16 +703,22 @@ end;
function TPacman.HandleKeyPress(k: TJSKeyBoardEvent): Boolean; function TPacman.HandleKeyPress(k: TJSKeyBoardEvent): Boolean;
Var
aCode : String;
begin begin
Result:=True; Result:=True;
if FDying then exit; if FDying then exit;
case k.Code of aCode:=k.Key;
if aCode='' then
aCode:=K.Code;
case aCode of
// For some reason, it is opposite of what you'd expect... // For some reason, it is opposite of what you'd expect...
TJSKeyNames.ArrowRight : PacManDir:='W'; 'Right', TJSKeyNames.ArrowRight : PacManDir:='W';
TJSKeyNames.ArrowUp : PacManDir:='N'; 'Up', TJSKeyNames.ArrowUp : PacManDir:='N';
TJSKeyNames.ArrowLeft : PacManDir:='E'; 'Left', TJSKeyNames.ArrowLeft : PacManDir:='E';
TJSKeyNames.ArrowDown : PacManDir:='S'; 'Down', TJSKeyNames.ArrowDown : PacManDir:='S';
'KeyP' : Pause:=not Pause; 'P', 'KeyP' : Pause:=not Pause;
end; end;
k.preventDefault; k.preventDefault;
end; end;
@ -1073,6 +1086,12 @@ end;
procedure TPacman.SetupPacman; procedure TPacman.SetupPacman;
Function GetElement(aName : String) : TJSHTMLElement;
begin
Result:=TJSHTMLElement(Document.getElementById(aName));
end;
Var Var
I : integer; I : integer;
El : TJSElement; El : TJSElement;
@ -1085,31 +1104,32 @@ begin
FCanvasEl:=TJSHTMLCanvasElement(Document.getElementById(FCanvasID)); FCanvasEl:=TJSHTMLCanvasElement(Document.getElementById(FCanvasID));
FCanvas:=TJSCanvasRenderingContext2D(FCanvasEl.getContext('2d')); FCanvas:=TJSCanvasRenderingContext2D(FCanvasEl.getContext('2d'));
FBtnReset:=TJSHTMLButtonElement(Document.getElementById(FResetID)); FBtnReset:=TJSHTMLButtonElement(Document.getElementById(FResetID));
FCBXSound:=TJSHTMLInputElement(Document.getElementById('cbx-sound')); FCBXSound:=TJSHTMLInputElement(GetElement('cbx-sound'));
FCBXSound.onchange:=@CheckSound; FCBXSound.onchange:=@CheckSound;
if Assigned(FBtnReset) then if Assigned(FBtnReset) then
FBtnReset.OnClick:=@DoResetClick; FBtnReset.OnClick:=@DoResetClick;
FCanvasEl.width := Round(FCanvasEl.OffsetWidth); FCanvasEl.width := Round(FCanvasEl.OffsetWidth);
FCanvasEl.height := Round(FCanvasEl.OffsetHeight); FCanvasEl.height := Round(FCanvasEl.OffsetHeight);
for I:=1 to 4 do for I:=1 to 4 do
ImgGhost[i]:=TJSHTMLImageElement(Document.getElementById('ghost'+IntToStr(i))) ; ImgGhost[i]:=TJSHTMLImageElement(GetElement('ghost'+IntToStr(i))) ;
ImgGhost[5]:=TJSHTMLImageElement(Document.getElementById('ghost-scared')); ImgGhost[5]:=TJSHTMLImageElement(GetElement('ghost-scared'));
ImgBonus:=TJSHTMLImageElement(Document.getElementById('cherry')); ImgBonus:=TJSHTMLImageElement(GetElement('cherry'));
pnBonusBarOuter:=TJSHTMLElement(Document.getElementById('bonus-outer'));
pnBonusBarInner:= TJSHTMLElement(Document.getElementById('bonus-inner'));
pnScareBarOuter:=TJSHTMLElement(Document.getElementById('scare-outer'));
pnScareBarInner:=TJSHTMLElement(Document.getElementById('scare-inner'));
for I:=1 to ControlCount do for I:=1 to ControlCount do
begin begin
El:=Document.GetElementById('control-'+ControlNames[i]); El:=GetElement('control-'+ControlNames[i]);
if Assigned(El) then if Assigned(El) then
TJSHTMLElement(El).onClick:=@DoMouseClick; TJSHTMLElement(El).onClick:=@DoMouseClick;
end; end;
lbScore:=TJSHTMLCanvasElement(Document.getElementById('score')); pnBonusBarOuter:=GetElement('bonus-outer');
lbHiscore:=TJSHTMLCanvasElement(Document.getElementById('highscore')); pnBonusBarInner:= GetElement('bonus-inner');
lbLives:=TJSHTMLCanvasElement(Document.getElementById('lives')); pnScareBarOuter:=GetElement('scare-outer');
lbBonusCnt:=TJSHTMLCanvasElement(Document.getElementById('bonus')); pnScareBarInner:=GetElement('scare-inner');
lbGhostCnt:=TJSHTMLCanvasElement(Document.getElementById('ghosts')); lbScore:=GetElement('score');
lbStatus:=GetElement('status');
lbHiscore:=GetElement('highscore');
lbLives:=GetElement('lives');
lbBonusCnt:=GetElement('bonus');
lbGhostCnt:=GetElement('ghosts');
// Sprites need the images, so this can only be done in this part // Sprites need the images, so this can only be done in this part
InitSprites(); InitSprites();
document.onkeydown:=@HandleKeyPress; document.onkeydown:=@HandleKeyPress;
@ -1170,6 +1190,7 @@ begin
InitVars(Level1Field); InitVars(Level1Field);
InitCells(Level1Field); InitCells(Level1Field);
RestartLevel(); RestartLevel();
UpdateStatus('Playing');
end; end;
procedure TPacman.RestartLevel(); procedure TPacman.RestartLevel();
@ -1214,14 +1235,16 @@ procedure TPacman.NextLevel();
begin begin
StopTimer; StopTimer;
ShowText('YOU WIN !!!',@RestartGame); ShowText('YOU WIN !!!',@RestartGame);
UpdateStatus('You win');
end; end;
procedure TPacman.GameOver(); procedure TPacman.GameOver();
begin begin
ShowText('YOU LOOSE !!!',@RestartGame); ShowText('YOU LOST !!!',@RestartGame);
UpdateStatus('You lost');
end; end;
procedure TPacman.playsound(aAudio: TAudio); procedure TPacman.PlaySound(aAudio: TAudio);
begin begin
if (not AudioDisabled) and (FAudio.Loaded) then if (not AudioDisabled) and (FAudio.Loaded) then
FAudio.play(aAudio); FAudio.play(aAudio);
@ -1410,7 +1433,7 @@ var
begin begin
for a in TAudio do for a in TAudio do
begin begin
F:=TJSHTMLAudioElement(document.getElementbyID('audio-'+audionames[a])); F:=TJSHTMLAudioElement(Document.GetElementByID('audio-'+audionames[a]));
Files[a]:=F; Files[a]:=F;
FilesOK[a]:=F.readyState>=3; FilesOK[a]:=F.readyState>=3;
if not FilesOK[a] then if not FilesOK[a] then