{"id":906,"date":"2026-02-13T09:28:09","date_gmt":"2026-02-13T01:28:09","guid":{"rendered":"https:\/\/edu.circuspi.com\/?p=906"},"modified":"2026-03-09T16:06:07","modified_gmt":"2026-03-09T08:06:07","slug":"9-4-making-sprites-move-automatically-enemy-patterns","status":"publish","type":"post","link":"https:\/\/edu.circuspi.com\/index.php\/gamebit-en\/9-4-making-sprites-move-automatically-enemy-patterns\/","title":{"rendered":"9.4 Making Sprites Move Automatically \u2013 Enemy Patterns"},"content":{"rendered":"\n<p>Before making the villain move, we need to adjust everyone&#8217;s initial positions to avoid losing health the moment the game starts.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"428\" height=\"339\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-09-4.png\" alt=\"\" class=\"wp-image-1237\"\/><\/figure>\n\n\n\n<p>Common enemy behavior patterns fall into two types: &#8220;<strong>Chasing the Player<\/strong>&#8221; and &#8220;<strong>Teleporting<\/strong>.&#8221; Let&#8217;s introduce them in order.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9.4.1 Chasing the Player<\/h3>\n\n\n\n<p>Before we explain, you might have thought: &#8220;Since it needs to chase the player, do we need to know the &#8216;Player&#8217;s coordinates&#8217; and the &#8216;Enemy&#8217;s coordinates,&#8217; calculate the angle of the shortest distance, and then pursue?&#8221;<\/p>\n\n\n\n<p>No, no, no. While theoretically correct, practical operation doesn&#8217;t need to be that complex. MakeCode Arcade has already figured it out for us.<\/p>\n\n\n\n<p>Let&#8217;s click on &#8220;<strong>Sprites<\/strong>.&#8221; Inside the &#8220;<strong>Physics<\/strong>&#8221; section of the list, you can find the &#8220;<strong>set [myEnemy] follow [mySprite]<\/strong>&#8221; block.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"702\" height=\"358\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-09-4-1_01.png\" alt=\"\" class=\"wp-image-1238\"\/><\/figure>\n\n\n\n<p>Drag this block into &#8220;<strong>on start<\/strong>.&#8221; Click the &#8220;<strong>+<\/strong>&#8221; sign at the end of the block to see the &#8220;<strong>Speed<\/strong>&#8221; setting field. The default speed of 100 makes the enemy as fast as the player\u2014playing at this speed is extremely difficult! You&#8217;ll be caught in the blink of an eye. So, please change it to a lower number.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"496\" height=\"295\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-09-4-1_02.png\" alt=\"\" class=\"wp-image-1239\"\/><\/figure>\n\n\n\n<p>After setting it, test it with the <strong>Game Simulator<\/strong> and repeatedly adjust it to a speed you feel is appropriate.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"404\" height=\"546\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-09-4-1_03.gif\" alt=\"\" class=\"wp-image-1240\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9.4.2 Teleporting<\/h3>\n\n\n\n<p>Another method is to make the enemy instantly &#8220;<strong>Change Position<\/strong>&#8221; at a &#8220;<strong>Fixed Frequency<\/strong>.&#8221; For example, setting the enemy to change position every two seconds creates a thrill when the enemy suddenly appears in front of you during gameplay.<\/p>\n\n\n\n<p>We learned how to randomly change positions in previous units. The key is: <em>How do we automatically change the position after a specific interval?<\/em><\/p>\n\n\n\n<p>We can use the &#8220;<strong>on game update every ( 500 ) ms<\/strong>&#8221; block found in the &#8220;<strong>Game<\/strong>&#8221; category.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"568\" height=\"323\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-09-4-2_01.png\" alt=\"\" class=\"wp-image-1241\"\/><\/figure>\n\n\n\n<p>When we set the &#8220;<strong>Interval<\/strong>,&#8221; this block will continuously run the code inside it according to that interval. We want to change the enemy&#8217;s position every 2 seconds, so enter <strong>2000<\/strong> (since the unit is milliseconds) or select <strong>2 seconds<\/strong> from the dropdown menu.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"298\" height=\"147\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-09-4-2_02.png\" alt=\"\" class=\"wp-image-1242\"\/><\/figure>\n\n\n\n<p>Finally, place the &#8220;<strong>Random Position<\/strong>&#8221; method inside this block. <strong>Remember to select the Enemy&#8217;s variable name.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"596\" height=\"177\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-09-4-2_03.png\" alt=\"\" class=\"wp-image-1243\"\/><\/figure>\n\n\n\n<p>Once finished, try playing 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=\"266\" height=\"362\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-09-4-2_04.gif\" alt=\"\" class=\"wp-image-1244\"\/><\/figure>\n\n\n\n<p>In this unit, we learned about &#8220;<strong>Life (Health)<\/strong>&#8221; and &#8220;<strong>Animating Sprites<\/strong>.&#8221; In addition to the original tension of the time limit, the addition of the villain adds a lot of excitement and fun to the process. Currently, the gameplay mainly involves using the joystick to dodge.<\/p>\n\n\n\n<p>Next, we will add the ability for the character to attack, just like the fighter jet in the &#8220;Space Destroyer&#8221; example, which can fire beams to destroy meteors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before making the villain move, we need to adjust every [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":455,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,23],"tags":[],"table_tags":[],"class_list":{"0":"post-906","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\/906","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=906"}],"version-history":[{"count":2,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/906\/revisions"}],"predecessor-version":[{"id":1249,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/906\/revisions\/1249"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/media\/455"}],"wp:attachment":[{"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/media?parent=906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/categories?post=906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/tags?post=906"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/table_tags?post=906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}