Skip to content

Commit

Permalink
Blinking effect added on some texts + r to restart on game over
Browse files Browse the repository at this point in the history
  • Loading branch information
HenJi committed Nov 6, 2011
1 parent 31374de commit 185cdca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
25 changes: 17 additions & 8 deletions src/info.opa
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,30 @@

do Canvas.set_text_align(ctx, {align_center})
do Canvas.set_font(ctx, "bold 40px Arial")
cft(t,x,y) = Canvas.fill_text(ctx, t, x, y)
do cft("OPAcman", info_width/2, 50)
do Canvas.fill_text(ctx, "OPAcman", info_width/2, 50)

do Canvas.set_text_align(ctx, {align_start})
do Canvas.set_font(ctx, "bold 20px Arial")
do Canvas.translate(ctx, 10, 70)

do cft("Lives: {g.lives}", 10, 100)
do cft("Score: {g.score}", 10, 130)
do cft("Food left: {Map.size(g.food)}", 10, 160)
cft(t,dy) =
do Canvas.translate(ctx, 0, dy)
Canvas.fill_text(ctx, t, 0, 0)

do cft("Lives: {g.lives}", 30)
do cft("Score: {g.score}", 30)
do cft("Food left: {Map.size(g.food)}", 30)

do if g.state == {game_over} then
cft("'r': restart", 30)

do match g.on_steroids with
| {none} -> void
| {some=d} -> cft("Bonus: {1+d/fps}s", 10, 190)
| {some=d} -> cft("Bonus: {1+d/fps}s", 30)

p = g.pacman.base.pos
do cft("Player at ({p.x},{p.y})", 10, info_height-10)
do Canvas.fill_text(ctx,
"Player at ({p.x},{p.y})", 10, info_height-10)

do Canvas.restore(ctx)
void
Expand All @@ -51,6 +59,7 @@
else "Starts in {1+t/fps}s"
draw_in_center(ctx, text)

draw_game_over(ctx) = draw_in_center(ctx, "GAME OVER")
draw_game_over(ctx) =
draw_in_center(ctx, "GAME OVER")

}}
25 changes: 18 additions & 7 deletions src/opacman.opa
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ check_collision(g) =
2+base_size*grid_width,
2+base_size*grid_heigth)

@client blink(f) =
t = Date.now() |> Date.in_milliseconds
t = t / 100
t = t - (t/10)*10
if t > 5 then f()

@client next_frame(ctx:Canvas.context)() =
draw_board(g) =
do clean_frame(ctx)
Expand All @@ -91,12 +97,12 @@ check_collision(g) =
do draw_board(g)
g = match g.state with
| {game_over} ->
do Info.draw_game_over(ctx)
do blink(->Info.draw_game_over(ctx))
g
| {initiating=t} ->
if t < -fps/2 then {g with state={running}}
else
do Info.draw_init(t, ctx)
do blink(->Info.draw_init(t, ctx))
{g with state={initiating=(t-1)}}
| {running} ->
Pacman.move(g)
Expand All @@ -107,16 +113,17 @@ check_collision(g) =
@client keyfun(e) =
g = game.get()
p = g.pacman
do Dom.transform([#debug <- "{e.key_code}"])
p = match (p.base.dir, e.key_code) with
// z
| ({down}, {some=122}) ->
// z + w
| ({down}, {some=122}) | ({down}, {some=119}) ->
{p with next_dir={up}
base={p.base with dir={up}
cur_step=-p.base.cur_step}}
| (_, {some=122}) -> {p with next_dir={up}}

// q
| ({right}, {some=113}) ->
// q + a
| ({right}, {some=113}) | ({right}, {some=97}) ->
{p with next_dir={left}
base={p.base with dir={left}
cur_step=-p.base.cur_step}}
Expand All @@ -139,7 +146,11 @@ check_collision(g) =
// space (pause)
| (_, {some=32}) -> {p with next_dir=Base.Dir.get_still(p.base.dir)}
| _ -> p
game.set({g with pacman=p})
g = match (g.state, e.key_code) with
// r (reset if game over)
| ({game_over}, {some=114}) -> default_game
| _ -> {g with pacman=p}
game.set(g)

@client init() =
match Canvas.get(#game_holder) with
Expand Down

0 comments on commit 185cdca

Please sign in to comment.