1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
<!DOCTYPE html>
<html>
<head>
<title>Tau Prolog - Example: My little doge</title>
<meta charset="utf-8">
<link href="doge.css" rel="stylesheet">
<script type="text/javascript" src="../../modules/core.js"></script>
<script type="text/javascript" src="../../modules/dom.js"></script>
<script type="text/prolog" id="doge.pl">
:- use_module(library(dom)).
% move/3
% Move the doge
move(Doge, Axis, Incr) :-
get_by_id(board, Board),
style(Doge, Axis, px(X0)),
X1 is X0+Incr,
X1 >= -10,
(Axis = top -> X1 =< 310 ; X1 =< 720),
style(Doge, Axis, px(X1)).
% anim/2
% Change animation of the doge
anim(Doge, Dir) :-
atom_concat('res/', Dir, Src1),
atom_concat(Src1, '.gif', Src),
style(Doge, backgroundImage, url(Src)).
% clear_control/0
% Remove the focus class of any control
clear_controls :-
findall(X, (get_by_class(control, X), remove_class(X, focus)), _).
% remark_control/1
% Remark the pressed control
remark_control(Key) :-
clear_controls,
atom_concat('control-', Key, Id),
get_by_id(Id, Control),
add_class(Control, focus).
% action/2
% up
action(Doge, w) :-
anim(Doge, up),
move(Doge, top, -10).
% down
action(Doge, s) :-
anim(Doge, down),
move(Doge, top, 10).
% left
action(Doge, a) :-
anim(Doge, left),
move(Doge, left, -10).
% right
action(Doge, d) :-
anim(Doge, right),
move(Doge, left, 10).
% poop
action(Doge, p) :-
style(Doge, top, px(Y0)), style(Doge, left, px(X0)),
Y1 is Y0+50, X1 is X0+37,
create(div, Poop),
style(Poop, top, px(Y1)), style(Poop, left, px(X1)),
add_class(Poop, poop),
insert_before(Poop, Doge).
% init/0
% Initilize the game
init :-
get_by_id(doge, Doge),
get_by_tag(body, Body),
bind(Body, keyup, _, clear_controls),
bind(Body, keydown, Event, (
event_property(Event, key, Key),
remark_control(Key),
action(Doge, Key)
)).
</script>
</head>
<body>
<div id="board">
<div id="doge"></div>
<div id="controls">
<div class="control" id="control-w">w</div>
<div class="control" id="control-a">a</div>
<div class="control" id="control-s">s</div>
<div class="control" id="control-d">d</div>
<div class="control" id="control-p">p (poop)</div>
</div>
</div>
<script type="text/javascript">
// Create session
var session = pl.create(1000);
// Consult program
session.consult("doge.pl", {
success: function() {
// Query goal
session.query("init.", {
success: function() {
// Find answers
session.answer();
}
});
}
});
</script>
</body>
</html>
|