Permalink
Browse files

pacman can now eat ghosts in bonus mode

  • Loading branch information...
1 parent 2c74fb8 commit daedd5d22a0f5bb31c98f17bbe76b67c0ad9f58b @HenJi committed Nov 6, 2011
Showing with 71 additions and 38 deletions.
  1. +7 −7 src/default.opa
  2. +9 −1 src/food.opa
  3. +10 −8 src/ghost.opa
  4. +1 −1 src/info.opa
  5. +37 −15 src/opacman.opa
  6. +4 −4 src/pacman.opa
  7. +3 −2 src/types.opa
View
@@ -94,20 +94,20 @@ Default = {{
mouth_steps = 10
} : Pacman.t
- @private make_ghost(ai, prison, dir, color, eye_color) =
+ @private make_ghost(ai, prison, color, eye_color) =
~{x y} = Set.random_get(ghost_prison) |> Option.get
{ ~ai ~color ~eye_color
- base = Base.make(x, y, dir, 11)
+ base = Base.make(x, y, {up}, 11)
prison = some(prison)
eye_step = 0
eye_steps = 32
} : Ghost.t
ghosts = [
- make_ghost({dumb}, 60, {up}, Color.orange, Color.crimson),
- make_ghost({guard}, 200, {up}, Color.darkred, Color.gold),
- make_ghost({dumb}, 400, {up}, Color.purple, Color.silver),
- make_ghost({guard}, 600, {up}, Color.green, Color.navy),
- ] : list(Ghost.t)
+ ("g1", make_ghost({dumb}, 60, Color.orange, Color.crimson)),
+ ("g2", make_ghost({guard}, 200, Color.darkred, Color.gold)),
+ ("g3", make_ghost({dumb}, 400, Color.purple, Color.silver)),
+ ("g4", make_ghost({guard}, 600, Color.green, Color.navy)),
+ ] : list((string,Ghost.t))
}}
View
@@ -9,7 +9,15 @@
else (food, 0)
match f with
| {normal} -> (food, food_points+d, cur_steroids)
- | {steroids} -> (food, steroid_points+d, some(steroid_len))
+ | {steroids} ->
+ new_steroids = match cur_steroids with
+ | {none} ->
+ {cycles = steroid_len
+ combo = 0}
+ | {some=s} ->
+ {cycles = s.cycles+steroid_len
+ combo = s.combo}
+ (food, steroid_points+d, some(new_steroids))
draw(g, ctx:Canvas.context) =
food = g.food
View
@@ -110,22 +110,24 @@
prison = none}
else {ghost with prison = some(t-1)}
- move(g) =
+ move(g:Game.status) =
ghosts = List.map(
- ghost ->
- (match ghost.ai with
- | {dumb} -> move_one_dumb(ghost)
- | {guard} -> move_one_guard(ghost, g.pacman.base)
- ) |> move_prison,
+ (gid, ghost) ->
+ ghost =
+ (match ghost.ai with
+ | {dumb} -> move_one_dumb(ghost)
+ | {guard} -> move_one_guard(ghost, g.pacman.base)
+ ) |> move_prison
+ (gid, ghost),
g.ghosts)
{g with ~ghosts}
draw(g, ctx:Canvas.context) =
ghosts = match g.on_steroids with
- | {none} -> g.ghosts
+ | {none} -> List.map(_.f2, g.ghosts)
| {some=_} ->
List.map(
- g -> {g with color=scared_color eye_color=scared_eye_color},
+ g -> {g.f2 with color=scared_color eye_color=scared_eye_color},
g.ghosts)
List.iter(draw_one(ctx, _), ghosts)
View
@@ -33,7 +33,7 @@
do match g.on_steroids with
| {none} -> void
- | {some=d} -> cft("Bonus: {1+d/fps}s", 30)
+ | {some=s} -> cft("Bonus: {1+s.cycles/fps}s", 30)
do Canvas.restore(ctx)
View
@@ -54,28 +54,50 @@ default_game = {
do Canvas.restore(ctx)
void
-check_collision(g) =
+check_collision(g:Game.status):Game.status =
pc = Base.center(g.pacman.base)
has_collision = List.fold(
- ghost, res ->
- if res then res
+ (gid, ghost), res ->
+ if Option.is_some(res) then res
else
gc = Base.center(ghost.base)
x = gc.f1 - pc.f1
y = gc.f2 - pc.f2
d = Math.sqrt_i(x*x+y*y)
- d < base_size,
- g.ghosts, false)
- 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
+ if d < base_size then some(gid)
+ else res,
+ g.ghosts, none)
+ if Option.is_none(has_collision) then g
+ else
+ match g.on_steroids with
+ | {some=s} ->
+ cid = Option.get(has_collision)
+ on_steroids = some({s with combo=s.combo+1})
+ ghosts = List.map(
+ (gid, ghost) ->
+ if gid != cid then (gid, ghost)
+ else
+ ~{x y} =
+ Set.random_get(Default.ghost_prison)
+ |> Option.get
+ base = Base.make(x, y, {up}, 11)
+ prison = some(300)
+ (gid, {ghost with ~base ~prison}),
+ g.ghosts)
+ score = g.score + (match s.combo with
+ | 0 -> 100 | 1 -> 200 | 3 -> 500 | _ -> 1000)
+ lives = g.lives + (score/life_points - g.score/life_points)
+ {g with ~ghosts ~score ~lives ~on_steroids}
+ | {none} ->
+ if g.lives == 1 then
+ {g with
+ state = {game_over}
+ lives = 0}
+ else
+ {default_game with
+ food = g.food
+ score = g.score
+ lives = g.lives-1}
@client clean_frame(ctx:Canvas.context) =
Canvas.clear_rect(
View
@@ -44,7 +44,7 @@
draw_clones({~x y=_} -> {~x y=-1}, p, ctx)
else draw_one(p, ctx)
- move(g) =
+ move(g:Game.status) =
p = g.pacman
ignore_incr = p.base.cur_step < 0
cur_step = p.base.cur_step + 1
@@ -78,9 +78,9 @@
lives = g.lives + (score/life_points - g.score/life_points)
on_steroids = match steroids with
| {none} -> none
- | {some=t} ->
- if t < 1 then none
- else some(t-1)
+ | {some=s} ->
+ if s.cycles < 1 then none
+ else some({s with cycles=s.cycles-1})
mouth = p.mouth_step + p.mouth_incr
dmouth =
if (mouth == p.mouth_steps-1 || mouth == 0) then -p.mouth_incr
View
@@ -56,9 +56,10 @@ type Game.state =
type Game.status = {
state : Game.state
pacman : Pacman.t
- ghosts : list(Ghost.t)
+ ghosts : list((string, Ghost.t))
food : map(Base.pos, Food.t)
score : int
lives : int
- on_steroids : option(int) /* Number of cycles on steroids */
+ on_steroids : option({cycles:int combo:int})
+ /* Number of cycles on steroids and ghost combo */
}

0 comments on commit daedd5d

Please sign in to comment.