-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-arcade.html
200 lines (186 loc) · 6.03 KB
/
02-arcade.html
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<html lang="en">
<head>
<!--
Taken from https://arcade.makecode.com/blocks-embed
Using as starting point.
-->
<title>Blocks Embedding Test Page</title>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<style>
html {font-family: Arial, Helvetica, sans-serif}
.boxdiv {padding:5px; display:inline-block; border: solid; background-color: lightgray}
</style>
</head>
<body>
<h1>Blocks Embedding Test Page</h1>
<h2>Try embedding some blocks...</h2>
<p>Render a simple example.</p>
<div class="boxdiv">
<pre class="blocks"><code>
let counter = 0
game.onUpdateInterval(5000, function () {
console.log("Entering the `on game update every 5000 ms` block.")
counter += 1
console.logValue("Number of times run", counter)
})
</code></pre>
</div>
<p>Render <i>Chase the Pizza</i> tutorial project:</p>
<div class="boxdiv">
<pre class="blocks"><code>
sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, function (sprite, otherSprite) {
info.changeScoreBy(1)
pizza.setPosition(randint(10, 150), randint(10, 110))
info.startCountdown(10)
})
let pizza: Sprite = null
scene.setBackgroundColor(9)
let mySprite = sprites.create(img`
. . . . . . . . . . b 5 b . . .
. . . . . . . . . b 5 b . . . .
. . . . . . b b b b b b . . . .
. . . . . b b 5 5 5 5 5 b . . .
. . . . b b 5 d 1 f 5 5 d f . .
. . . . b 5 5 1 f f 5 d 4 c . .
. . . . b 5 5 d f b d d 4 4 . .
. b b b d 5 5 5 5 5 4 4 4 4 4 b
b d d d b b d 5 5 4 4 4 4 4 b .
b b d 5 5 5 b 5 5 5 5 5 5 b . .
c d c 5 5 5 5 d 5 5 5 5 5 5 b .
c b d c d 5 5 b 5 5 5 5 5 5 b .
. c d d c c b d 5 5 5 5 5 d b .
. . c b d d d d d 5 5 5 b b . .
. . . c c c c c c c c b b . . .
. . . . . . . . . . . . . . . .
`, SpriteKind.Player)
controller.moveSprite(mySprite)
pizza = sprites.create(img`
. . . . . . b b b b . . . . . .
. . . . . . b 4 4 4 b . . . . .
. . . . . . b b 4 4 4 b . . . .
. . . . . b 4 b b b 4 4 b . . .
. . . . b d 5 5 5 4 b 4 4 b . .
. . . . b 3 2 3 5 5 4 e 4 4 b .
. . . b d 2 2 2 5 7 5 4 e 4 4 e
. . . b 5 3 2 3 5 5 5 5 e e e e
. . b d 7 5 5 5 3 2 3 5 5 e e e
. . b 5 5 5 5 5 2 2 2 5 5 d e e
. b 3 2 3 5 7 5 3 2 3 5 d d e 4
. b 2 2 2 5 5 5 5 5 5 d d e 4 .
b d 3 2 d 5 5 5 d d d 4 4 . . .
b 5 5 5 5 d d 4 4 4 4 . . . . .
4 d d d 4 4 4 . . . . . . . . .
4 4 4 4 . . . . . . . . . . . .
`, SpriteKind.Food)
</code></pre>
</div>
<p>The Fibonacci example not rendered:</p>
<div class="boxdiv">
<pre><code>
sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, function (sprite, otherSprite) {
info.changeScoreBy(1)
pizza.setPosition(randint(10, 150), randint(10, 110))
info.startCountdown(10)
})
let pizza: Sprite = null
scene.setBackgroundColor(9)
let mySprite = sprites.create(img`
. . . . . . . . . . b 5 b . . .
. . . . . . . . . b 5 b . . . .
. . . . . . b b b b b b . . . .
. . . . . b b 5 5 5 5 5 b . . .
. . . . b b 5 d 1 f 5 5 d f . .
. . . . b 5 5 1 f f 5 d 4 c . .
. . . . b 5 5 d f b d d 4 4 . .
. b b b d 5 5 5 5 5 4 4 4 4 4 b
b d d d b b d 5 5 4 4 4 4 4 b .
b b d 5 5 5 b 5 5 5 5 5 5 b . .
c d c 5 5 5 5 d 5 5 5 5 5 5 b .
c b d c d 5 5 b 5 5 5 5 5 5 b .
. c d d c c b d 5 5 5 5 5 d b .
. . c b d d d d d 5 5 5 b b . .
. . . c c c c c c c c b b . . .
. . . . . . . . . . . . . . . .
`, SpriteKind.Player)
controller.moveSprite(mySprite)
pizza = sprites.create(img`
. . . . . . b b b b . . . . . .
. . . . . . b 4 4 4 b . . . . .
. . . . . . b b 4 4 4 b . . . .
. . . . . b 4 b b b 4 4 b . . .
. . . . b d 5 5 5 4 b 4 4 b . .
. . . . b 3 2 3 5 5 4 e 4 4 b .
. . . b d 2 2 2 5 7 5 4 e 4 4 e
. . . b 5 3 2 3 5 5 5 5 e e e e
. . b d 7 5 5 5 3 2 3 5 5 e e e
. . b 5 5 5 5 5 2 2 2 5 5 d e e
. b 3 2 3 5 7 5 3 2 3 5 d d e 4
. b 2 2 2 5 5 5 5 5 5 d d e 4 .
b d 3 2 d 5 5 5 d d d 4 4 . . .
b 5 5 5 5 d d 4 4 4 4 . . . . .
4 d d d 4 4 4 . . . . . . . . .
4 4 4 4 . . . . . . . . . . . .
`, SpriteKind.Food)
</code></pre>
</div>
<script>
var makecodeUrl = "https://arcade.makecode.com/";
var blocksClass = "blocks";
var injectRenderer = function () {
var f = $("<iframe>", {
id: "makecoderenderer",
src: makecodeUrl + "--docs?render=1&lang=" + ($('html').attr('lang') || "en")
});
f.css("position", "absolute");
f.css("left", 0);
f.css("bottom", 0);
f.css("width", "1px");
f.css("height", "1px");
$('body').append(f);
}
function makeCodeRenderPre(pre) {
var f = document.getElementById("makecoderenderer");
f.contentWindow.postMessage({
type: "renderblocks",
id: pre.id,
code: pre.innerText,
options: {
snippetMode: false,
},
}, makecodeUrl);
}
var attachBlocksListener = function () {
var blockId = 0;
window.addEventListener("message", function (ev) {
var msg = ev.data;
if (msg.source != "makecode") return;
switch (msg.type) {
case "renderready":
$("." + blocksClass).each(function () {
var snippet = $(this)[0];
snippet.id = "pxt-blocks-" + (blockId++);
makeCodeRenderPre(snippet);
});
break;
case "renderblocks":
var svg = msg.svg; // this is a string containing SVG
var id = msg.id; // this is the id you sent
// replace text with svg
var img = document.createElement("img");
img.src = msg.uri;
img.width = msg.width;
img.height = msg.height;
var pre = document.getElementById(id)
pre.parentNode.insertBefore(img, pre)
pre.parentNode.removeChild(pre);
break;
}
}, false);
}
$(function () {
injectRenderer();
attachBlocksListener();
});
</script>
</body>
</html>