Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    2D Physics - Geometry Bounce Game (Difficulty: 4)

    Tutorials
    1
    1
    1031
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

      Introduction

      Geometry Dash is a fun and popular game. The Scratch version of this game created by griffpatch has over 100 million views. However, that game is built with thousands of blocks and hundreds of costumes, which is a big challenge for most Scratch learners.

      In this tutorial, you will learn to build a similar game with only about 100 blocks on the CreatiCode playground. The player box will be gliding in both directions, jumping to avoid obstacles in the same way as Geometry Dash.

      e4.gif

      You can easily design the game map and change the game mechanics yourself afterward. There are two key reasons why it is much easier:

      1. We will use a much larger canvas, allowing you to design the entire level using one costume manually.

      2. We will use the 2D physics engine, which makes it much easier to handle the movement and collision of objects.

       
       

      Step 1 - Remix the Project Template

       

      First, please open this link and remix it as your own project.

      https://play.creaticode.com/projects/6571e0dead3b29181d637adb

       
       

      Step 2 - Review the Project Template

       

      The project template is prepared in these two steps:

      Different Stage and Canvas Size

      You can click the “Edit” menu and observe that the viewport window (the stage) is 720 wide and 360 tall. This makes it easier for the player to view the upcoming obstacles and traps. The canvas size is much larger (2000 by 2000), which allows us to design the entire level map in it.

      2a16e590-aa33-4573-a23b-f7f4cce56364-image.png

      Player Box

      The Box sprite contains a yellow square costume. It will be controlled by the player. Feel free to replace the costume with your own.

      Initial Map

      In the Platforms sprite, if you open the costume editor, you will see an initial map with some teal boxes. You will need to zoom out to view the entire map:

      e.gif

       
      These rectangular boxes will serve as the platforms for the player’s box to glide on. There is nothing special about these boxes. You can duplicate them or add new ones yourself after you finish the tutorial. However, to make sure you can follow this tutorial, you should not change them for now.

       
       

      A Special Note on Changing Costumes

       

      Since the project uses the 2D physics engine, we will generate a hidden physics body for each shape in the costume. If you change the costume in any way, you need to make sure no object is selected in the costume, and click the “Center” button to shift the center of all objects to the center of the canvas. This is crucial to ensure the physics bodies of these shapes are aligned with the shapes themselves.

       
      e.gif

       
       

      Step 3 - Add a Dark Backdrop

       

      Next, switch to the Stage, and open the Backdrop editor. We will need a dark background, and as the player box moves across the background, it should show some color changes. One simple solution is to add a big rectangle and then use a gradient color pattern to fill it.
       
      e.gif

       
       

      Step 4 - Initialize 2D Physics

       

      Now we are ready to start coding. First of all, since we will be using the 2D physics engine, we should initialize it. The best place to do it is in the Stage, since the engine is used by all sprites:

      105693ed-7669-4cc2-8836-6f7ab7b79ee9-image.png

       
      We set the gravity at -1200, so the player box will fall vertically. This number can be adjusted. If you want the box to fall faster, use a stronger gravity like -1500.

      We are also setting the 4 world borders around the map (2000 by 2000 canvas) to have 100% friction and 100% restitution. That’ll ensure the player box rebounds from these borders without losing any momentum.

       
       

      Step 5 - Broadcast and Receive the “start” Message

       

      After the physics engine is initialized, we should broadcast a new message “start”, and receive that message in every sprite. This is a very commonly used pattern, which makes sure the sprites only use the physics engine after it has been initialized.

      • Add to Stage:

      d89696f2-e3ff-4290-948a-a72b90de2aa5-image.png

      • Add to Both Box and Platforms Sprites:

      5cea7137-e5a5-4175-bfa5-aded1a521dfd-image.png

       
       

      Step 6 - Initialize the Platforms

       

      In the Platforms sprite, we can use the “behave as” block to create a (hidden) physics body for every shape in the costume.
       
      b9207f99-e0a1-4337-9c1d-8f4e904c2735-image.png

       
      A few notes on this block:

      • We need to use the “behave as compound shape” block because the costume contains multiple rectangles. Each rectangle will have its own physics body of the same size/shape/position.
      • All these physics objects are “fixed”, which means they will never move.
      • The “object” type is selected to ensure these physics bodies will collide with the player box. If the “sensor” type is selected, then they will only report touching events but will not actually collide with other objects.
      • Since all the shapes are simple with straight edges, we can use a relatively large value for the curve tolerance and point distance.

       
      Lastly, to confirm the physics objects are created properly, you can temporarily set the “debug” option to “yes” so that the physics bodies will be drawn with a red outline.

      f96e52e4-ad1d-4a87-b966-6c3eacb64401-image.png

       
      If these outlines are not aligned with the platform shapes (see below), then you need to center the costume using the “Center” button as described above.

      078e28e5-d11c-4fd8-b2af-8156ece1adea-image.png

       
       

      Step 7 - Configure Platforms

       

      Next, we need to configure the physics properties of these platforms:

      5c8b0058-f103-430a-b72c-e80142ca43bd-image.png

       
      The density doesn’t matter since these platforms are fixed. The friction is 0%, so the player box will smoothly glide over these platforms. The restitution is set to -100%, which guarantees the player box will not rebound when it lands on the platforms. We need to use a negative number because even when it is 0%, we still see the player box rebound a bit.

       
       

      Step 8 - Initial Position of the Player Box

       

      For the player box, we would like it to start at the top left corner of the map. To find a good position, we can switch to the Platforms sprite, open the costume editor, and move the mouse pointer to where we want the player box to start. The x and y positions of the mouse pointer will be shown at the bottom:

      e.gif

       
      In the Box sprite, we can use x of -960 and y of 326 as a starting point:

      c8a94d8f-6e58-494f-a2ee-ef0caabbc0e3-image.png

       
      Note that we won’t see the box yet when we run the program. That’s because the box’s position is already outside the stage window’s range, which is 720 by 360.

       
       

      Step 9 - Lock the Viewport at the Player Box

       

      To always show the player box on the stage, we need to lock the viewport to the Box sprite. That way, the viewport will always try to center at the player box. Whenever the canvas size is larger than the viewport (stage) size, you almost always need to lock the viewport to the sprite that the player controls.

      539d8efa-ccdb-450b-8d61-2a4c0164f727-image.png

       
      Now if you run the program again, you will see the player box appearing in the top left corner, right above the top platform. Note that since this is already at the left edge of the canvas, the player box is not at the center of the viewport (the viewport can not move left further).

      1fb2f98c-4427-412b-a9e1-14e249922e74-image.png

       
       

      Step 10 - Attach a Physics Body to the Player Box

       

      Similar to the platforms, we need to attach a physics body to the player box. That hidden body will be managed by the physics engine to run simulations, and the sprite will be moved accordingly.

      c2475834-9e3c-4c53-b430-34f5c1147824-image.png

       
      Here are some notes on these 2 blocks:

      • The box will have a “dynamic” body, which means it can be moved by forces like gravity
      • The box’s type is “object” (as opposed to the “sensor”), so it will collide with other objects
      • The friction is 0, so the box will glide smoothly on the platforms without friction
      • The restitution is 100%, so when the box hits the border walls, it will bounce back at the same speed.

       
      Now, the box will fall due to gravity and land on the platform:

      e.gif

       
       

      Step 12 - Make the Box Move

       

      The player box will be constantly moving left or right. It changes direction when it hits the 2 side walls. At the start, we will set its x speed to 200, so it glides to the right:

      2a650e91-1f7f-46b0-981d-3415203e52b9-image.png

       

      Now, our box is moving around and landing on the platforms, and the viewport is always following the box as well:

      e4.gif

       
       

      Step 13 - Press To Jump

       

      To make the box jump, we simply need to give it an initial upward speed in the y direction, then it will gradually slow down and fall due to gravity. To check for key press or mouse click, we can use a forever loop like this:

      0bbae510-0717-4462-8d0d-33e4fb541941-image.png

       
      Now, we can make the box jump:
       

      e4.gif

       
       

      Step 14 - Avoid Jump In Air

       

      Our program has a logical issue, the box can make a jump even when it is still in the air. We need to check if it is touching the platform before setting its y speed:

      5c8dc3da-c5a8-4533-b2be-a09558b4e868-image.png

       
      However, after adding this check, the box can no longer jump when we press the SPACE key. It is as if the box is not touching the platform, though it is gliding on it. Can you think about how to fix this problem?

       
       

      Step 15 - Fix Touching Platform Issue

       

      To fix the issue, we first need to understand why it is happening. As shown, when the box is standing on the platform, the “touching [Platforms]” block returns false.

      e4.gif

       
      That is because the physics engine is doing a really good job. Though the box is falling due to gravity, the physics engine ensures the box is always above the platform, and there is no overlap between them.

      To fix this issue, there is a very simple solution. We can make the box costume slightly larger than its physics body. The physics body ensures the box doesn’t fall into the platform, and the costume image is used to check if the box is touching the platform costume.

      Here is how to do it in the Box sprite. We set its size to 100% before adding its physics body so that its physics body is at 100% size. Then after, we enlarge the box’s costume to 130%, so the bottom part of the box will overlap with the platform:

      d56b8f36-968f-41c3-9e6e-d8991d472f9a-image.png

       
      To verify the physics body is indeed smaller, we can set “debug” to “Yes” temporarily for the box, and we will see the red outline of the body is smaller than the box:

      02eea6ac-b652-4b63-a5c2-442c6eb0ffb7-image.png

       
      After this change, the box can only jump when it is touching the platform.

       
       

      Step 16 - Fix Slow Down Issue

       

      There is another issue we need to fix. When the box collides with the border, it tends to slow down, even though we have set their restitution to 100%.

      e4.gif

       
      Since we want the box to stay at the same horizontal speed of 200 all the time, we can simply update its speed repeatedly. Note that the box may be moving left or right, so we have to check its current direction before resetting the speed.

      827a9cf9-be1d-48bc-b023-6b6efc803036-image.png

       
      Note that if the speed is too small (between -10 and 10), then most likely the box has been stopped by some obstacle, so we should count that as a failure, and send a “retry” message to restart the game. We will handle that message later.

      With this change, the box will keep moving at a speed of 200 in both directions.

       
       

      Step 17 - Add a Red Triangle

       

      Now let’s add a red triangle obstacle. When the player box touches it, the game will restart. You can directly edit the costume of the Platforms sprite. Select color of 0, saturation of 100, and brightness of 100. Note that you should set the outline to none to make sure the player box touches the red color:

      e4.gif

       
      Now the box will be bounced back by the red triangle.

      e4.gif

       
       

      Step 18 - Retry if Touching Red

       

      If the box touches any red obstacle, we should count that as a failure, and send the “retry” message. You need to set the color to be the same as the obstacle (color = 0, saturation = 100, brightness = 100).

      41dd5268-3e3e-4d71-af72-253530ad7bdb-image.png

       
       

      Step 19 - Retry Logic

       

      When the Box sprite receives the “retry” message, we need to move the box back to its start point, and make it move right from there:

      df52d0a8-e1af-48e7-845b-201f090e4cb4-image.png

       
      This way, the game will repeatedly restart by itself upon any failure:

      e4.gif

       
       

      Step 20 - Add More Red Obstacles

       

      At this point, you can add more red obstacles to the map any way you like. For example, let’s add 2 triangles and a rectangle in the Platforms sprite:

      3e19098c-c89e-45ff-b83d-d4692b8438f8-image.png

       
       

      Step 21 - Green Jumping Board

       

      We can also introduce objects of other colors for different effects. For example, we can add a green box to represent a jumping board:

      ad659db1-4b3f-418f-850f-852acb24db3a-image.png

       
      And then in the Box sprite, we can add logic to make the box jump higher when it touches this green color:

      7acb6fbd-d125-4b28-955b-5559640bfc13-image.png

       
      Now our game is already playable!

      e4.gif

       
       

      Step 22 - Rotate the Box

       

      When the box jumps up, it would be much more fun to make it rotate as well. Let’s define a new block “add rotation” for that purpose. Depending on whether the box is jumping to the right or left, we need to set the rotation in different directions:

      b9730250-820c-433a-9d60-47be88001ef4-image.png

       
      We can use this block wherever we are setting the y speed:

      28c341b7-5f9b-4146-a45a-9dbad2e546d6-image.png

       
      Now it looks like this when the box jumps:

      e4.gif

       
       

      Step 23 - Add a Goal Sprite

       

      We still haven’t defined how the player can win this game. Let’s add a new empty sprite named “Goal”. Edit its costume to add a green bar that represents a destination area. You can use some gradient and transparency to make it look cool:

      e4.gif

       
       

      Step 24 - Place the Goal Sprite

       

      To find out where we should place the Goal sprite, switch to the Platforms sprite and look at its costume. Move the mouse pointer over the map to find the x and y positions you need. For example, suppose we want to place the Goal at the end of the first row, we will get its position (x = 985, y = 321) this way:

      e4.gif

       
      We can use the control panel to set this position for the Goal sprite:

      e4.gif

       
       

      Step 25 - Player Success

       

      When the player box touches the Goal sprite, the player has won, and we should broadcast a “success” message:

      05db3d7c-4497-4b5d-aa54-aae01b9883cd-image.png

       
       

      Step 26 - Make the Box Spin at the End

       

      When the player box touches the Goal, it should no longer move due to the physics engine. Therefore, we should remove its physics. Instead, to make it fun, we can make the box spin forever:

      7ba561f9-3e42-4081-906b-449d85531f7c-image.png

       
      This is what it looks like:

      e4.gif

       
       

      Step 27 - End Game Message

       

      When the player has succeeded, we should also show a message like “You won” to indicate that’s the end of the game. We can also add a new sound from the library to play at the end. This can be done in the Stage when we receive the “success” message:

      99e52743-2b35-43cd-b49c-53635fab8289-image.png

       
       

      Step 28 - Falling Off the Platform

       

      If the player box falls too low, we can also count that as a failure and make the player start from the beginning. In this example, the goal is at the top level, so if the player box falls off the top level, its y position will be less than 200. So we can add that as an additional failure condition in the Box sprite:

      60b953f5-2d3e-4427-9881-14823adb19c1-image.png

       
      We can test it like this:

      e4.gif

       
      By now the game is complete. Although it is very simple, it includes all important logic components:

      e4.gif

       
       

      Enhancements

       

      Congratulations on completing a fairly complex game. Next, it’s your turn to make a different version of this game. Here are some ideas for you to consider:

      1. Design different level maps: You can edit the costume of the Platforms sprite to change the platforms and the red/green objects. Make sure you test-play your game before publishing it.

      2. Add additional color types. You can use other colors to trigger interesting actions. For example, when the box touches a yellow shape, its size doubles, or when it touches a blue shape, its moving speed changes from 200 to 400. The possibilities are endless.

      3. Add a background music

      4. Create an automated demo: you can write a little script that makes the box jump at the right time so that it completes the game automatically. This can serve as a demo for the players if they don’t know how to pass a level.

      1 Reply Last reply Reply Quote 0
      • Pinned by  info-creaticode info-creaticode 
      • First post
        Last post