{"id":897,"date":"2026-02-13T09:14:39","date_gmt":"2026-02-13T01:14:39","guid":{"rendered":"https:\/\/edu.circuspi.com\/?p=897"},"modified":"2026-03-09T15:56:17","modified_gmt":"2026-03-09T07:56:17","slug":"8-2-understanding-random-generation","status":"publish","type":"post","link":"https:\/\/edu.circuspi.com\/index.php\/gamebit-en\/8-2-understanding-random-generation\/","title":{"rendered":"8.2 Understanding Random Generation"},"content":{"rendered":"\n<p>Since the <strong>&#8220;Burger&#8221;<\/strong> is gone after being eaten, we need it to reappear. The method is simple: just create another burger. Besides setting its position, remember to set its &#8220;<strong>Kind<\/strong>&#8221; correctly so it meets the &#8220;<strong>Overlap Conditions<\/strong>.&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"776\" height=\"368\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_01.png\" alt=\"\" class=\"wp-image-1200\"\/><\/figure>\n\n\n\n<p>However, you will notice the burger always appears in a fixed spot. In a normal game, it should be &#8220;<strong>Randomly Generated<\/strong>&#8221; in different places. We can find the convenient block &#8220;<strong>pick random ( 0 ) to ( 10 )<\/strong>&#8221; within the &#8220;<strong>Math<\/strong>&#8221; category in the <strong>Block Toolbox<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"859\" height=\"522\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_02.png\" alt=\"\" class=\"wp-image-1201\"\/><\/figure>\n\n\n\n<p>We can fill in two numbers, and it will randomly generate a new number between them. Quick thinkers might already know what to use this for!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1600\" height=\"672\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_03.png\" alt=\"\" class=\"wp-image-1202\"\/><\/figure>\n\n\n\n<p>That&#8217;s right! We can use it to &#8220;<strong>Generate Coordinates<\/strong>.&#8221; Place this block into the <strong>x<\/strong> and <strong>y<\/strong> coordinate fields respectively. Fill in the ranges: <strong>0~160<\/strong> for the x-coordinate and <strong>0~120<\/strong> for the y-coordinate (based on screen pixel size).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"935\" height=\"379\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_04.png\" alt=\"\" class=\"wp-image-1203\"\/><\/figure>\n\n\n\n<p>After finishing, run it in the <strong>Game Simulator<\/strong>. Test it a few times, and you might notice that sometimes the burger disappears or gets stuck, as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"329\" height=\"459\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_05.png\" alt=\"\" class=\"wp-image-1204\"\/><\/figure>\n\n\n\n<p>This is because we didn&#8217;t restrict it to appear strictly <em>within<\/em> the visible screen area. We can use the &#8220;<strong>set [mySprite] stay in screen &lt;ON&gt;<\/strong>&#8221; block found in the &#8220;<strong>Sprites<\/strong>&#8221; category.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"681\" height=\"312\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_06.png\" alt=\"\" class=\"wp-image-1205\"\/><\/figure>\n\n\n\n<p>Place this block <strong>after<\/strong> the &#8220;create sprite&#8221; block. This adds a restriction immediately when the burger is created, so it won&#8217;t go out of bounds when randomly positioning. <\/p>\n\n\n\n<p><em>Remember to select the correct <strong>Variable Name<\/strong> (the target) at the beginning of the block.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"966\" height=\"427\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_07.png\" alt=\"\" class=\"wp-image-1206\"\/><\/figure>\n\n\n\n<p>Test it in the <strong>Game Simulator<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"338\" height=\"476\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_08.gif\" alt=\"\" class=\"wp-image-1207\"\/><\/figure>\n\n\n\n<p>We have completed the scoring and random spawning functions, but the game feels a bit empty. Let&#8217;s add some sound! Specifically, when the burger is eaten. Add the &#8220;<strong>Music<\/strong>&#8221; block we learned earlier to the destruction event. You can find a suitable sound effect or design one yourself.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"882\" height=\"467\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_09.png\" alt=\"\" class=\"wp-image-1208\"\/><\/figure>\n\n\n\n<p>While you&#8217;re at it, add some background music to enrich the game presentation. Remember to set the &#8220;<strong>looping<\/strong>&#8221; option to &#8220;<strong>true<\/strong>&#8221; for continuous playback.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"656\" height=\"496\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_10.png\" alt=\"\" class=\"wp-image-1209\"\/><\/figure>\n\n\n\n<p><strong>Adding a Challenge: The Countdown<\/strong> Gaining points endlessly without a result isn&#8217;t a game. Let&#8217;s add an element of excitement: a &#8220;<strong>Countdown Timer<\/strong>.&#8221; We will set a rule: if the score doesn&#8217;t meet the target when time runs out, the player loses.<\/p>\n\n\n\n<p>Drag out the &#8220;<strong>start countdown ( 10 ) seconds<\/strong>&#8221; block from the &#8220;<strong>Info<\/strong>&#8221; category.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"454\" height=\"475\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_11.png\" alt=\"\" class=\"wp-image-1210\"\/><\/figure>\n\n\n\n<p>Place this block at the very bottom of the &#8220;<strong>on start<\/strong>&#8221; block. This means the countdown begins just as the game starts. Feel free to set the duration.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"494\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_12.png\" alt=\"\" class=\"wp-image-1211\"\/><\/figure>\n\n\n\n<p>Then, we need to determine the result when the timer ends. Drag out the &#8220;<strong>on countdown end<\/strong>&#8221; block from &#8220;<strong>Info<\/strong>&#8221; and place it in the <strong>Workspace<\/strong> (empty area).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"449\" height=\"400\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_13.png\" alt=\"\" class=\"wp-image-1212\"\/><\/figure>\n\n\n\n<p>Next, we need to judge if our score meets the target. Drag out the &#8220;<strong>if&#8230; then&#8230; else<\/strong>&#8221; block from &#8220;<strong>Logic<\/strong>&#8221; and place it inside &#8220;<strong>on countdown end<\/strong>.&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"379\" height=\"429\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_14.png\" alt=\"\" class=\"wp-image-1213\"\/><\/figure>\n\n\n\n<p>Drag out the &#8220;<strong>score<\/strong>&#8221; block (the oval shape) from &#8220;<strong>Info<\/strong>.&#8221; This block stores our current score.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"447\" height=\"401\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_15.png\" alt=\"\" class=\"wp-image-1214\"\/><\/figure>\n\n\n\n<p>To compare the score, we need a &#8220;<strong>Comparison<\/strong>&#8221; block. Drag out the &#8220;<strong>&lt; ( 0 ) = ( 0 ) &gt;<\/strong>&#8221; block from &#8220;<strong>Logic<\/strong>.&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"284\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_16.png\" alt=\"\" class=\"wp-image-1215\"\/><\/figure>\n\n\n\n<p>Combine these three blocks. In the dropdown menu of the comparison block, select &#8220;<strong>&gt;<\/strong>&#8221; (greater than).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"386\" height=\"331\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_17.png\" alt=\"\" class=\"wp-image-1216\"\/><\/figure>\n\n\n\n<p>Choose your own target score.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"407\" height=\"323\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_18.png\" alt=\"\" class=\"wp-image-1217\"\/><\/figure>\n\n\n\n<p>Finally, the most critical block: select &#8220;<strong>game over &lt;LOSE&gt;<\/strong>&#8221; from the &#8220;<strong>Game<\/strong>&#8221; category. The option at the end determines whether the game is won or lost.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"702\" height=\"354\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_19.png\" alt=\"\" class=\"wp-image-1218\"\/><\/figure>\n\n\n\n<p>Add the different &#8220;<strong>Results<\/strong>&#8221; (Win\/Lose) into the condition slots sequentially. Now, an exciting and fun game is complete!!\ud83c\udf89\ud83c\udf89\ud83c\udf89<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"468\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-08-2_20.png\" alt=\"\" class=\"wp-image-1219\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Since the &#8220;Burger&#8221; is gone after being eate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":426,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,23],"tags":[],"table_tags":[],"class_list":{"0":"post-897","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-gamebit-en","8":"category-freeinfo-en","9":"czr-hentry"},"_links":{"self":[{"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/comments?post=897"}],"version-history":[{"count":2,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/897\/revisions"}],"predecessor-version":[{"id":1223,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/897\/revisions\/1223"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/media\/426"}],"wp:attachment":[{"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/media?parent=897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/categories?post=897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/tags?post=897"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/table_tags?post=897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}