Skip to content

Commit

Permalink
Some infos fixed + true pause
Browse files Browse the repository at this point in the history
  • Loading branch information
HenJi committed Nov 6, 2011
1 parent 185cdca commit 8a2a2a0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
32 changes: 28 additions & 4 deletions src/info.opa
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,55 @@
| {none} -> void
| {some=d} -> cft("Bonus: {1+d/fps}s", 30)

do Canvas.restore(ctx)

do Canvas.save(ctx)
do Canvas.set_font(ctx, "bold 20px Arial")
do Canvas.translate(ctx, 2+base_size*grid_width, 0)
p = g.pacman.base.pos
do Canvas.fill_text(ctx,
"Move with", 10, info_height-100)
do Canvas.fill_text(ctx,
"'wasd' or 'zqsd'", 10, info_height-70)
do Canvas.fill_text(ctx,
"'space': pause", 10, info_height-40)
do Canvas.fill_text(ctx,
"Player at ({p.x},{p.y})", 10, info_height-10)

do Canvas.restore(ctx)

void

draw_in_center(ctx, text) =
draw_in_center(ctx, text, caption) =
f = 70
w=2+base_size*grid_width
h=2+base_size*grid_heigth
do Canvas.save(ctx)

do Canvas.set_text_align(ctx, {align_center})
do Canvas.set_font(ctx, "bold {f}px Arial")
do Canvas.fill_text(ctx, text, w/2, (h-f)/2)

do match caption with
| {none} -> void
| {some=t} ->
do Canvas.set_font(ctx, "bold {f/2}px Arial")
do Canvas.fill_text(ctx, t, w/2, h/2)
void

do Canvas.restore(ctx)
void

draw_init(t, ctx) =
text =
if t < 0 then "Go!"
else "Starts in {1+t/fps}s"
draw_in_center(ctx, text)
draw_in_center(ctx, text, none)

draw_game_over(ctx) =
draw_in_center(ctx, "GAME OVER")
draw_in_center(ctx, "GAME OVER", some("'r' to restart"))

draw_pause(ctx) =
draw_in_center(ctx, "PAUSE", some("'space' to resume"))


}}
17 changes: 14 additions & 3 deletions src/opacman.opa
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ check_collision(g) =
d = Math.sqrt_i(x*x+y*y)
d < base_size,
g.ghosts, false)
if has_collision then
if has_collision && g.lives == 1 then
{g with
state = {game_over}
lives = 0}
else if has_collision then
{default_game with
food = g.food
score = g.score
lives = g.lives-1}
else g

@client clean_frame(ctx:Canvas.context) =
Expand Down Expand Up @@ -99,6 +104,9 @@ check_collision(g) =
| {game_over} ->
do blink(->Info.draw_game_over(ctx))
g
| {pause} ->
do blink(->Info.draw_pause(ctx))
g
| {initiating=t} ->
if t < -fps/2 then {g with state={running}}
else
Expand Down Expand Up @@ -143,12 +151,15 @@ check_collision(g) =
cur_step=-p.base.cur_step}}
| (_, {some=100}) -> {p with next_dir={right}}

// space (pause)
| (_, {some=32}) -> {p with next_dir=Base.Dir.get_still(p.base.dir)}
| _ -> p
g = match (g.state, e.key_code) with
// r (reset if game over)
| ({game_over}, {some=114}) -> default_game

// space (pause toggle)
| ({running}, {some=32}) -> {g with state={pause}}
| ({pause}, {some=32}) -> {g with state={running}}

| _ -> {g with pacman=p}
game.set(g)

Expand Down
1 change: 1 addition & 0 deletions src/types.opa
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Food.t =
type Game.state =
{initiating:int}
/ {running}
/ {pause}
/ {game_over}

type Game.status = {
Expand Down

0 comments on commit 8a2a2a0

Please sign in to comment.