Skip to content

Commit 008a65d

Browse files
chore: Tidy ups 03
1 parent 08938ce commit 008a65d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/main/scala-2.11/nl/amsscala/simplegame/Page.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ protected trait Page {
2222
* @return None if not ready else the same GameState if drawn
2323
*/
2424
protected[simplegame] def render(gs: GameState) = {
25+
def gameOverTxt = "Game Over?"
26+
def explainTxt = "Use the arrow keys to\nattack the hidden monster."
27+
2528
if (bgImage.isReady && heroImage.isReady && monsterImage.isReady) {
2629
ctx.drawImage(bgImage.element, 0, 0, canvas.width, canvas.height)
2730
ctx.drawImage(heroImage.element, gs.hero.pos.x, gs.hero.pos.y)
@@ -33,6 +36,20 @@ protected trait Page {
3336
ctx.textAlign = "left"
3437
ctx.textBaseline = "top"
3538
ctx.fillText(f"Goblins caught: ${gs.monstersCaught}%03d", 32, 32)
39+
40+
if (gs.newGame) {
41+
ctx.textAlign = "center"
42+
ctx.font = "48px Helvetica"
43+
44+
ctx.fillText(
45+
if (gs.isGameOver) gameOverTxt else {
46+
val txt = explainTxt.split('\n')
47+
ctx.fillText(txt(1), canvas.width / 2, canvas.height / 2 + 32)
48+
txt(0)
49+
}, canvas.width / 2, canvas.height / 2 - 48
50+
)
51+
}
52+
3653
Some(gs)
3754
} else None
3855
}

src/main/scala-2.11/nl/amsscala/simplegame/game.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,13 @@ private case class GameState(
102102
if (newHero.pos.isValidPosition(Position(canvas.width, canvas.height), Hero.size)) // Are they touching?
103103
if (newHero.pos.areTouching(monster.pos, Hero.size)) // Reset the game when the player catches a monster
104104
new GameState(canvas, monstersCaught, true)
105-
else copy(hero = newHero)
105+
else copy(hero = newHero, newGame = false)
106106
else this
107107
}
108108
}
109109

110+
def isGameOver = newGame && monstersCaught != 0
111+
110112
/**
111113
* Auxiliary GameState constructor
112114
*

0 commit comments

Comments
 (0)