{"id":884,"date":"2026-02-13T08:55:52","date_gmt":"2026-02-13T00:55:52","guid":{"rendered":"https:\/\/edu.circuspi.com\/?p=884"},"modified":"2026-03-09T15:47:25","modified_gmt":"2026-03-09T07:47:25","slug":"7-using-variables-to-set-up-game-collision-events","status":"publish","type":"post","link":"https:\/\/edu.circuspi.com\/index.php\/gamebit-en\/7-using-variables-to-set-up-game-collision-events\/","title":{"rendered":"7. Using &#8220;Variables&#8221; to Set Up Game Collision Events"},"content":{"rendered":"\n<p>We have sprites, atmosphere, art, and music. Now it&#8217;s time to learn a crucial programming concept: <strong>Variables<\/strong>.<\/p>\n\n\n\n<p>Those who have learned programming before are surely familiar with variables. If you haven&#8217;t, don&#8217;t worry! You have actually used them in previous units; you just didn&#8217;t realize they were &#8220;<strong>Variables<\/strong>.&#8221;<\/p>\n\n\n\n<p>Besides explaining variables, we will also learn about handling &#8220;<strong>Collision Events<\/strong>&#8221; in this unit\u2014that is, programming what happens when two sprites touch. For example, in the &#8220;Snake&#8221; game, when the snake touches food, the food disappears, and the snake grows. Or in our &#8220;Space Destroyer&#8221; example, when a meteor hits the fighter jet, the jet explodes, and health decreases.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"1125\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2023\/09\/07-01.png\" alt=\"\" class=\"wp-image-381\"\/><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">7.1 Understanding Variables<\/h2>\n\n\n\n<p>A &#8220;<strong>Variable<\/strong>&#8221; is a medium used to store data. You can imagine a variable as a <strong>box<\/strong>. This box can store any type of data. We can put the Orange Dinosaur inside, or we can put pizza or fried chicken in it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1140\" height=\"758\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-1_01.png\" alt=\"\" class=\"wp-image-1166\"\/><\/figure>\n\n\n\n<p>This &#8220;<strong>Variable<\/strong>&#8221; box can be passed around. The recipient can open the box to retrieve the data inside or replace it with new data.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"1185\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2023\/09\/07-1_02.png\" alt=\"\" class=\"wp-image-383\"\/><\/figure>\n\n\n\n<p>Once data is stored, it remains in its last saved state unless we change it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"1136\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2023\/09\/07-1_03.png\" alt=\"\" class=\"wp-image-384\"\/><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">7.2 How to Use Variables<\/h2>\n\n\n\n<p>Let&#8217;s return to the code. During the sprite creation process, we placed our designed sprite into a &#8220;<strong>Variable<\/strong>.&#8221; Just like our understanding of the box metaphor: once the sprite is stored, the data won&#8217;t change unless we modify it.<\/p>\n\n\n\n<p>You can see that this variable has a name. To avoid picking up the wrong &#8220;box&#8221; when passing variables around, we give it a name. By default, the program names it &#8220;<strong>mySprite<\/strong>.&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1666\" height=\"644\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-2_01.png\" alt=\"\" class=\"wp-image-1167\"\/><\/figure>\n\n\n\n<p>We can understand that the variable &#8220;<strong>mySprite<\/strong>&#8221; is equivalent to the &#8220;<strong>Sprite (Orange Dinosaur)<\/strong>.&#8221; So, when we set the position of &#8220;<strong>mySprite<\/strong>&#8221; or use the controller to move &#8220;<strong>mySprite<\/strong>,&#8221; we are effectively controlling the &#8220;<strong>Sprite (Orange Dinosaur)<\/strong>.&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1638\" height=\"654\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-2_02.png\" alt=\"\" class=\"wp-image-1168\"\/><\/figure>\n\n\n\n<p>At this point, quick thinkers might ask: &#8220;Why can&#8217;t I just do this?&#8221; (referring to using the &#8220;create sprite&#8221; block directly in every command).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"626\" height=\"264\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-2_03.png\" alt=\"\" class=\"wp-image-1169\"\/><\/figure>\n\n\n\n<p>If you write the code as shown in the hypothetical image above, running it will reveal a surprise: there are <strong>two<\/strong> sprites! One is controllable, and the other is not.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1158\" height=\"732\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-2_04.png\" alt=\"\" class=\"wp-image-1171\"\/><\/figure>\n\n\n\n<p>This is because the block &#8220;<strong>sprite [ ] of kind (Player)<\/strong>&#8221; essentially means &#8220;<strong>Create a NEW Sprite<\/strong>.&#8221; If you use this block again in a movement command, you are creating a second sprite\u2014one positioned at (x: 80, y: 60), and another separate one that moves with the controller. This is why you see a &#8220;shadow clone&#8221; (duplicate) in the <strong>Game Simulator<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"402\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-2_05.png\" alt=\"\" class=\"wp-image-1172\"\/><\/figure>\n\n\n\n<p>Although creating a variable seems like an extra step, think of it this way: the named variable is like giving your sprite a specific name. When you want to control it, you call it by its name.<\/p>\n\n\n\n<p>Click on the variable name within the block, and select &#8220;<strong>Rename variable&#8230;<\/strong>&#8221; from the dropdown menu to give it a new name.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"829\" height=\"402\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-2_06.png\" alt=\"\" class=\"wp-image-1173\"\/><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">7.3 Creating Another Sprite<\/h2>\n\n\n\n<p>Next, let&#8217;s create a second sprite. Click on &#8220;<strong>Sprites<\/strong>&#8221; in the <strong>Block Toolbox<\/strong>, use the create sprite block, place it inside &#8220;<strong>on start<\/strong>,&#8221; and design its appearance (in this example, we selected a <strong>Burger<\/strong> from the Gallery).<\/p>\n\n\n\n<p>You will notice that the default &#8220;<strong>Variable Name<\/strong>&#8221; has changed automatically. The editor does this to prevent errors.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"678\" height=\"417\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_01.png\" alt=\"\" class=\"wp-image-1174\"\/><\/figure>\n\n\n\n<p>Let&#8217;s give these variables meaningful names! The sprite generated by default appears in the center of the screen. Let&#8217;s use the &#8220;<strong>set [mySprite] position to x ( 0 ) y ( 0 )<\/strong>&#8221; block we learned earlier to change the positions of these two sprites.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"775\" height=\"341\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_02.png\" alt=\"\" class=\"wp-image-1175\"\/><\/figure>\n\n\n\n<p>We can categorize sprites into groups to facilitate group control later. In this example, we set the Orange Dinosaur&#8217;s kind to &#8220;<strong>Player<\/strong>&#8221; and the Burger&#8217;s kind to &#8220;<strong>Food<\/strong>.&#8221; You can define these <strong>Kind<\/strong> names yourself.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"863\" height=\"583\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_03.png\" alt=\"\" class=\"wp-image-1176\"\/><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">7.4 Sprite Overlaps<\/h2>\n\n\n\n<p>If we want the &#8220;<strong>Orange Dinosaur<\/strong>&#8221; to eat the &#8220;<strong>Burger<\/strong>&#8220;\u2014meaning the Burger disappears when the Dinosaur &#8220;<strong>collides<\/strong>&#8221; with it\u2014we need to use the concept of &#8220;<strong>Overlaps<\/strong>.&#8221; This allows us to program subsequent events when the images of two sprites overlap.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"950\" height=\"762\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_04.gif\" alt=\"\" class=\"wp-image-1177\"\/><\/figure>\n\n\n\n<p>Please click on &#8220;<strong>Sprites<\/strong>&#8221; in the <strong>Block Toolbox<\/strong>, scroll down to find blocks related to &#8220;<strong>Overlaps<\/strong>,&#8221; and drag out the first block: &#8220;<strong>on sprite of kind (Player) overlaps otherSprite of kind (Player)<\/strong>.&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"756\" height=\"273\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_05.png\" alt=\"\" class=\"wp-image-1178\"\/><\/figure>\n\n\n\n<p>Place this block in the <strong>Workspace<\/strong> (empty area). Since this block functions similarly to &#8220;<strong>on start<\/strong>&#8221; (it is an event handler), it cannot be placed inside other blocks, but you can place other blocks inside it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"890\" height=\"642\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_06.png\" alt=\"\" class=\"wp-image-1179\"\/><\/figure>\n\n\n\n<p>Let&#8217;s look closely at this block. It states that when a certain Kind &#8220;<strong>overlaps<\/strong>&#8221; with another Kind, it triggers the blocks inside it.<\/p>\n\n\n\n<p>So, if we want the Burger to &#8220;<strong>disappear<\/strong>&#8221; when the &#8220;<strong>Orange Dinosaur<\/strong>&#8221; overlaps with the &#8220;<strong>Burger<\/strong>,&#8221; we can place the &#8220;<strong>destroy<\/strong>&#8221; block inside.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"933\" height=\"498\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_07.png\" alt=\"\" class=\"wp-image-1180\"\/><\/figure>\n\n\n\n<p>Making a specific sprite disappear is easy. In the &#8220;<strong>Sprites<\/strong>&#8221; list, there is a &#8220;<strong>destroy [mySprite]<\/strong>&#8221; block. This block will help us make the Burger vanish.<\/p>\n\n\n\n<p><strong>Crucial Step:<\/strong> We need to change the logic in the overlap block to: &#8220;<strong>on sprite of kind (Player) overlaps otherSprite of kind (Food)<\/strong>.&#8221;<\/p>\n\n\n\n<p>Then, inside the block, drag the &#8220;<strong>otherSprite<\/strong>&#8221; value (the red rounded block representing the specific Food that was hit) into the destroy block.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"757\" height=\"479\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_08.png\" alt=\"\" class=\"wp-image-1181\"\/><\/figure>\n\n\n\n<p>Or, ensure you select the correct variable name for the burger; otherwise, nothing will happen!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"835\" height=\"219\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_09.png\" alt=\"\" class=\"wp-image-1182\"\/><\/figure>\n\n\n\n<p>Once finished, run 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=\"964\" height=\"800\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_10.gif\" alt=\"\" class=\"wp-image-1183\"\/><\/figure>\n\n\n\n<p>The &#8220;<strong>destroy [mySprite]<\/strong>&#8221; block also allows you to adjust the special effect upon destruction. Click the &#8220;<strong>+<\/strong>&#8221; symbol at the end of the block to select an &#8220;<strong>effect<\/strong>&#8221; (like fire, spray) and adjust the &#8220;<strong>duration<\/strong>.&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"395\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_10.png\" alt=\"\" class=\"wp-image-1184\"\/><\/figure>\n\n\n\n<p>Test it again in the <strong>Game Simulator<\/strong>, try switching between different effects, and find the one you like best! (*\u00b4\u2200`)~\u2665<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"636\" src=\"https:\/\/edu.circuspi.com\/wp-content\/uploads\/2026\/02\/eng-07-3_12.gif\" alt=\"\" class=\"wp-image-1185\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>We have sprites, atmosphere, art, and music. Now it&#038;#82 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":381,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,23],"tags":[],"table_tags":[],"class_list":{"0":"post-884","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\/884","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=884"}],"version-history":[{"count":2,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/884\/revisions"}],"predecessor-version":[{"id":1186,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/884\/revisions\/1186"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/media\/381"}],"wp:attachment":[{"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/media?parent=884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/categories?post=884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/tags?post=884"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/edu.circuspi.com\/index.php\/wp-json\/wp\/v2\/table_tags?post=884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}