Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

New info area + collision detection (stops game)

  • Loading branch information...
commit edfa87ba910f344a5701e092f99dde6302fd428e 1 parent faaa6b2
@HenJi authored
View
6 src/food.opa
@@ -5,11 +5,11 @@
| (food, {none}) -> (food, 0, cur_steroids)
| (food, {some=f}) ->
(food, d) =
- if food == Map.empty then (Default.food, 1000)
+ if food == Map.empty then (Default.food, clear_bonus)
else (food, 0)
match f with
- | {normal} -> (food, 10+d, cur_steroids)
- | {steroids} -> (food, 100+d, some(300))
+ | {normal} -> (food, food_points+d, cur_steroids)
+ | {steroids} -> (food, steroid_points+d, some(steroid_len))
draw(g, ctx:Canvas.context) =
food = g.food
View
37 src/info.opa
@@ -0,0 +1,37 @@
+@client Info = {{
+
+ draw(g, ctx:Canvas.context) =
+ info_height = 2+base_size*grid_heigth
+ do Canvas.save(ctx)
+
+ do Canvas.translate(ctx, 2+base_size*grid_width, 0)
+
+ do Canvas.clear_rect(ctx, 0, 0, info_width, info_height)
+ do Canvas.set_stroke_style(ctx, {color=Color.black})
+ do Canvas.set_line_width(ctx, 2.)
+ do Canvas.stroke_rect(ctx, 1, -5,
+ info_width+10, info_height+10)
+
+ 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.set_text_align(ctx, {align_start})
+ do Canvas.set_font(ctx, "bold 20px Arial")
+
+ do cft("Lives: {g.lives}", 10, 100)
+ do cft("Score: {g.score}", 10, 130)
+ do cft("Food left: {Map.size(g.food)}", 10, 160)
+
+ do match g.on_steroids with
+ | {none} -> void
+ | {some=d} -> cft("Bonus: {1+d/60}", 10, 190)
+
+ p = g.pacman.base.pos
+ do cft("Player at ({p.x},{p.y})", 10, info_height-10)
+
+ do Canvas.restore(ctx)
+ void
+
+}}
View
62 src/opacman.opa
@@ -6,6 +6,14 @@ fps = 60
base_size = 32
grid_width = List.length(List.head(grid))
grid_heigth = List.length(grid)
+info_width = 200
+
+food_points = 10
+steroid_points = 100
+steroid_len = 300 /* frames */
+clear_bonus = 500
+life_points = 2500
+
/* Defaults */
@@ -14,6 +22,7 @@ default_game = {
ghosts = Default.ghosts
food = Default.food
score = 0
+ lives = 3
on_steroids = none
} : Game.status
@@ -44,33 +53,42 @@ default_game = {
do Canvas.restore(ctx)
void
+check_collision(g) =
+ pc = Base.center(g.pacman.base)
+ has_collision = List.fold(
+ ghost, res ->
+ if 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 then {g with lives=0}
+ else g
+
@client clean_frame(ctx:Canvas.context) =
Canvas.clear_rect(
ctx, 0, 0,
- 2+2*base_size*grid_width,
- 2+2*base_size*grid_heigth)
-
-@client print_infos(g:Game.status) =
- p = g.pacman
- cont =
- <>
- Pacman at ({p.base.pos.x},{p.base.pos.y}), moving {"{p.base.dir}"}
- - {Map.size(g.food)} food left
- - Score: {g.score}
- - Steroids: {g.on_steroids}
- </>
- Dom.transform([#info <- cont])
+ 2+base_size*grid_width,
+ 2+base_size*grid_heigth)
@client next_frame(ctx:Canvas.context)() =
g = game.get()
- |> Pacman.move
- |> Ghost.move
- do clean_frame(ctx)
- do Wall.draw(ctx)
- do Food.draw(g, ctx)
- do Pacman.draw(g, ctx)
- do Ghost.draw(g, ctx)
- game.set(g)
+ if g.lives == 0 then
+ void
+ else
+ g = Pacman.move(g)
+ |> Ghost.move
+ |> check_collision
+ do clean_frame(ctx)
+ do Wall.draw(ctx)
+ do Food.draw(g, ctx)
+ do Pacman.draw(g, ctx)
+ do Ghost.draw(g, ctx)
+ do Info.draw(g, ctx)
+ game.set(g)
@client keyfun(e) =
g = game.get()
@@ -121,7 +139,7 @@ default_game = {
body() =
<>
<canvas id="game_holder"
- width="{2+base_size*grid_width}"
+ width="{2+base_size*grid_width+info_width}"
height="{2+base_size*grid_heigth}">
You can't see canvas, upgrade your browser !
</canvas>
View
4 src/pacman.opa
@@ -57,7 +57,6 @@
(dir, dx, dy) =
if cur_step != 0 then (p.base.dir, 0, 0)
else
- do print_infos(g)
(dx, dy) = Base.Dir.deltas(p.base.dir)
(ddx, ddy) = Base.Dir.deltas(p.next_dir)
dir = test_wall(p.next_dir, Base.Dir.get_still(p.next_dir),
@@ -76,6 +75,7 @@
if cur_step != p.base.max_steps/2 then (g.food, 0, g.on_steroids)
else Food.check(pos, g.food, g.on_steroids)
score = g.score + dscore
+ lives = g.lives + (score/life_points - g.score/life_points)
on_steroids = match steroids with
| {none} -> none
| {some=t} ->
@@ -90,6 +90,6 @@
mouth_step = mouth
mouth_incr = dmouth
base = { p.base with ~pos ~dir ~cur_step } }
- {g with ~pacman ~food ~score ~on_steroids}
+ {g with ~pacman ~food ~score ~lives ~on_steroids}
}}
View
1  src/types.opa
@@ -52,5 +52,6 @@ type Game.status = {
ghosts : list(Ghost.t)
food : map(Base.pos, Food.t)
score : int
+ lives : int
on_steroids : option(int) /* Number of cycles on steroids */
}
Please sign in to comment.
Something went wrong with that request. Please try again.