diff --git a/src/default.opa b/src/default.opa new file mode 100644 index 0000000..65b09c2 --- /dev/null +++ b/src/default.opa @@ -0,0 +1,81 @@ +/** + * Game grid + * + * + * 1: Normal food + * 2: Special food (TODO) + * 5: Teleport - Moving the player at one end of the line will teleport it at + * the other end - Ghosts can't teleport. (TODO) + * 8: Wall + */ + +grid = [ + [1,1,1,1,1,1,1,1,1,1,1,1,8,8,1,1,1,1,1,1,1,1,1,1,1,1], + [2,8,8,8,8,1,8,8,8,8,8,1,8,8,1,8,8,8,8,8,1,8,8,8,8,2], + [1,8,8,8,8,1,8,8,8,8,8,1,8,8,1,8,8,8,8,8,1,8,8,8,8,1], + [1,8,8,8,8,1,8,8,8,8,8,1,8,8,1,8,8,8,8,8,1,8,8,8,8,1], + [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], + [1,8,8,8,8,1,8,8,1,8,8,8,8,8,8,8,8,1,8,8,1,8,8,8,8,1], + [1,8,8,8,8,1,8,8,1,8,8,8,8,8,8,8,8,1,8,8,1,8,8,8,8,1], + [1,1,1,1,1,1,8,8,1,1,1,1,8,8,1,1,1,1,8,8,1,1,1,1,1,1], + [8,8,8,8,8,1,8,8,8,8,8,0,8,8,0,8,8,8,8,8,1,8,8,8,8,8], + [8,8,8,8,8,1,8,8,8,8,8,0,8,8,0,8,8,8,8,8,1,8,8,8,8,8], + [8,8,8,8,8,1,8,8,0,0,0,0,0,0,0,0,0,0,8,8,1,8,8,8,8,8], + [8,8,8,8,8,1,8,8,0,8,8,8,0,0,8,8,8,0,8,8,1,8,8,8,8,8], + [8,8,8,8,8,1,8,8,0,8,0,0,8,8,0,0,8,0,8,8,1,8,8,8,8,8], + [5,0,0,0,0,1,0,0,0,8,0,0,0,0,0,0,8,0,0,0,1,0,0,0,0,5], + [8,8,8,8,8,1,8,8,0,8,0,0,0,0,0,0,8,0,8,8,1,8,8,8,8,8], + [8,8,8,8,8,1,8,8,0,8,8,8,8,8,8,8,8,0,8,8,1,8,8,8,8,8], + [8,8,8,8,8,1,8,8,0,0,0,0,0,0,0,0,0,0,8,8,1,8,8,8,8,8], + [8,8,8,8,8,1,8,8,0,8,8,8,8,8,8,8,8,0,8,8,1,8,8,8,8,8], + [8,8,8,8,8,1,8,8,0,8,8,8,8,8,8,8,8,0,8,8,1,8,8,8,8,8], + [1,1,1,1,1,1,1,1,1,1,1,1,8,8,1,1,1,1,1,1,1,1,1,1,1,1], + [1,8,8,8,8,1,8,8,8,8,8,1,8,8,1,8,8,8,8,8,1,8,8,8,8,1], + [2,8,8,8,8,1,8,8,8,8,8,1,8,8,1,8,8,8,8,8,1,8,8,8,8,2], + [1,1,1,8,8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,8,8,1,1,1], + [8,8,1,8,8,1,8,8,1,8,8,8,8,8,8,8,8,1,8,8,1,8,8,1,8,8], + [8,8,1,8,8,1,8,8,1,8,8,8,8,8,8,8,8,1,8,8,1,8,8,1,8,8], + [1,1,1,1,1,1,8,8,1,1,1,1,8,8,1,1,1,1,8,8,1,1,1,1,1,1], + [1,8,8,8,8,8,8,8,8,8,8,1,8,8,1,8,8,8,8,8,8,8,8,8,8,1], + [1,8,8,8,8,8,8,8,8,8,8,1,8,8,1,8,8,8,8,8,8,8,8,8,8,1], + [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], +] + +Default = {{ + + @private get_grid_nums(n) : set(Base.pos) = + List.foldi( + y, l, acc -> + List.foldi( + x, v, acc -> + if v == n then Set.add(~{x y}, acc) + else acc, + l, acc), + grid, Set.empty:set(Base.pos)) + + food = get_grid_nums(1) + walls = get_grid_nums(8) + + pacman = { + base = Base.make(0, 0, {right}, 10) + next_dir = {right} + mouth_step = 0 + mouth_incr = 1 + mouth_steps = 10 + } : Pacman.t + + @private make_ghost(ai, x, y, dir, color, eye_color) = { + ~ai ~color ~eye_color + base = Base.make(x, y, dir, 10) + eye_step = 0 + eye_steps = 32 + } : Ghost.t + + ghosts = [ + make_ghost({dumb}, 5, 4, {right}, Color.orange, Color.crimson), + make_ghost({guard}, 20, 4, {down}, Color.darkred, Color.gold), + make_ghost({dumb}, 20, 22, {left}, Color.purple, Color.silver), + make_ghost({guard}, 5, 22, {up}, Color.green, Color.navy), + ] : list(Ghost.t) + +}} diff --git a/src/food.opa b/src/food.opa index 745223c..9083791 100644 --- a/src/food.opa +++ b/src/food.opa @@ -1,43 +1,3 @@ -(initial_food, walls) = [ - [1,1,1,1,1,1,1,1,1,1,1,1,8,8,1,1,1,1,1,1,1,1,1,1,1,1], - [2,8,8,8,8,1,8,8,8,8,8,1,8,8,1,8,8,8,8,8,1,8,8,8,8,2], - [1,8,8,8,8,1,8,8,8,8,8,1,8,8,1,8,8,8,8,8,1,8,8,8,8,1], - [1,8,8,8,8,1,8,8,8,8,8,1,8,8,1,8,8,8,8,8,1,8,8,8,8,1], - [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], - [1,8,8,8,8,1,8,8,1,8,8,8,8,8,8,8,8,1,8,8,1,8,8,8,8,1], - [1,8,8,8,8,1,8,8,1,8,8,8,8,8,8,8,8,1,8,8,1,8,8,8,8,1], - [1,1,1,1,1,1,8,8,1,1,1,1,8,8,1,1,1,1,8,8,1,1,1,1,1,1], - [8,8,8,8,8,1,8,8,8,8,8,0,8,8,0,8,8,8,8,8,1,8,8,8,8,8], - [8,8,8,8,8,1,8,8,8,8,8,0,8,8,0,8,8,8,8,8,1,8,8,8,8,8], - [8,8,8,8,8,1,8,8,0,0,0,0,0,0,0,0,0,0,8,8,1,8,8,8,8,8], - [8,8,8,8,8,1,8,8,0,8,8,8,0,0,8,8,8,0,8,8,1,8,8,8,8,8], - [8,8,8,8,8,1,8,8,0,8,0,0,8,8,0,0,8,0,8,8,1,8,8,8,8,8], - [0,0,0,0,0,1,0,0,0,8,0,0,0,0,0,0,8,0,0,0,1,0,0,0,0,0], - [8,8,8,8,8,1,8,8,0,8,0,0,0,0,0,0,8,0,8,8,1,8,8,8,8,8], - [8,8,8,8,8,1,8,8,0,8,8,8,8,8,8,8,8,0,8,8,1,8,8,8,8,8], - [8,8,8,8,8,1,8,8,0,0,0,0,0,0,0,0,0,0,8,8,1,8,8,8,8,8], - [8,8,8,8,8,1,8,8,0,8,8,8,8,8,8,8,8,0,8,8,1,8,8,8,8,8], - [8,8,8,8,8,1,8,8,0,8,8,8,8,8,8,8,8,0,8,8,1,8,8,8,8,8], - [1,1,1,1,1,1,1,1,1,1,1,1,8,8,1,1,1,1,1,1,1,1,1,1,1,1], - [1,8,8,8,8,1,8,8,8,8,8,1,8,8,1,8,8,8,8,8,1,8,8,8,8,1], - [2,8,8,8,8,1,8,8,8,8,8,1,8,8,1,8,8,8,8,8,1,8,8,8,8,2], - [1,1,1,8,8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,8,8,1,1,1], - [8,8,1,8,8,1,8,8,1,8,8,8,8,8,8,8,8,1,8,8,1,8,8,1,8,8], - [8,8,1,8,8,1,8,8,1,8,8,8,8,8,8,8,8,1,8,8,1,8,8,1,8,8], - [1,1,1,1,1,1,8,8,1,1,1,1,8,8,1,1,1,1,8,8,1,1,1,1,1,1], - [1,8,8,8,8,8,8,8,8,8,8,1,8,8,1,8,8,8,8,8,8,8,8,8,8,1], - [1,8,8,8,8,8,8,8,8,8,8,1,8,8,1,8,8,8,8,8,8,8,8,8,8,1], - [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], -] |> List.foldi( - y, l, (food, walls) -> - List.foldi( - x, v, (food, walls) -> - if v == 1 then (Set.add(~{x y}, food), walls) - else if v == 8 then (food, Set.add(~{x y}, walls)) - else (food, walls), - l, (food, walls)), - _, (Set.empty:set(Base.pos), Set.empty:set(Base.pos))) - @client Food = {{ draw(ctx:Canvas.context) = diff --git a/src/ghost.opa b/src/ghost.opa index 4a93a6c..797931e 100644 --- a/src/ghost.opa +++ b/src/ghost.opa @@ -1,19 +1,5 @@ @client Ghost = {{ - make(ai, x, y, dir, color, eye_color) = { - ~ai ~color ~eye_color - base = Base.make(x, y, dir, 10) - eye_step = 0 - eye_steps = 32 - } : Ghost.t - - default = [ - make({dumb}, 5, 4, {right}, Color.orange, Color.crimson), - make({guard}, 20, 4, {down}, Color.darkred, Color.gold), - make({dumb}, 20, 22, {left}, Color.purple, Color.silver), - make({guard}, 5, 22, {up}, Color.green, Color.navy), - ] : list(Ghost.t) - draw_one(ctx:Canvas.context, g:Ghost.t) = w = base_size diff --git a/src/opacman.opa b/src/opacman.opa index c6a6b89..128a953 100644 --- a/src/opacman.opa +++ b/src/opacman.opa @@ -2,15 +2,15 @@ fps = 60 base_size = 32 -grid_width = 26 -grid_heigth = 29 +grid_width = List.length(List.head(grid)) +grid_heigth = List.length(grid) /* Defaults */ -@client default_game = { - pacman = Pacman.default - ghosts = Ghost.default - food = initial_food +default_game = { + pacman = Default.pacman + ghosts = Default.ghosts + food = Default.food score = 0 } : Game.status diff --git a/src/pacman.opa b/src/pacman.opa index f1a9f92..281ffca 100644 --- a/src/pacman.opa +++ b/src/pacman.opa @@ -1,13 +1,5 @@ @client Pacman = {{ - default = { - base = Base.make(0, 0, {right}, 10) - next_dir = {right} - mouth_step = 0 - mouth_incr = 1 - mouth_steps = 10 - } : Pacman.t - draw(ctx:Canvas.context) = g = game.get() p = g.pacman @@ -74,7 +66,7 @@ else if Set.mem(pos, g.food) then food = Set.remove(pos, g.food) - if food == Set.empty then (initial_food, g.score+1010) + if food == Set.empty then (Default.food, g.score+1010) else (food, g.score+10) else (g.food, g.score) diff --git a/src/wall.opa b/src/wall.opa index 9cb74ca..aed7901 100644 --- a/src/wall.opa +++ b/src/wall.opa @@ -6,13 +6,13 @@ do Canvas.set_fill_style(ctx, {color=Color.darkblue}) do Set.iter( ~{x y} -> Canvas.fill_rect(ctx, 1+x*w, 1+y*w, w, w), - walls) + Default.walls) do Canvas.restore(ctx) void at(x, y) = x >= grid_width || y >= grid_heigth || x < 0 || y < 0 - || Set.mem(~{x y}, walls) + || Set.mem(~{x y}, Default.walls) }}