Navigation

    CreatiCode Scratch Forum

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

    2D Physics - Marble Maze (Difficulty: 3)

    Tutorials
    1
    1
    1197
    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

      In this tutorial, you will learn to use the 2D physics engine to build a marble maze. The player needs to rotate the maze left and right to move the marble to the goal area.

      m9.gif

       
       

      Step 1 - Create a New Project

      Please create a new project on the CreatiCode platform, and remove the sprite with the dog. Rename the empty sprite to “Maze”, then add the “ball” sprite from the library. We will only need these 2 sprites in this project.

      901bfe95-8293-45cb-82d5-04aee11e6804-image.png

       
       

      Step 2 - Draw a Simple Maze

      In the Maze sprite, switch to the costume editor, and draw a maze using shapes (not lines). Here is how to draw a maze using 6 boxes. Of course, feel free to draw a more complex maze.

      m1.gif

       
      Note that it is very important that you select all shapes at the end, and align them to the center of the costume. It will make sure the maze is correctly handled by the 2D physics engine.

       
       

      Step 3 - Initialize the 2D Physics World

      To use the physics engine, we first need to initialize the physics world in the Maze sprite.

      022c9137-8436-4f3f-bd2f-488594c7ad83-image.png

       
      The gravity is set to -1000 along the Y direction, which will make the marble fall toward the bottom of the screen pretty quickly.

       
       

      Step 4 - Add the Maze to the Physics World

      At this time the physics engine does not know about the maze yet. To add the maze to the physics world, we first reset its position and direction, then tell the physics engine to create a physics body for it.

      Since the maze is a fairly complex shape, we will need to represent it as a compound shape. The maze will be rotating, so we can not set it to be “fixed”; also, the maze will not fall due to gravity, so we should not set it to “dynamic” either. Therefore, it is set to be a “movable” object.

      e395809a-7737-43bf-86ef-ba5dfcc2f99a-image.png

       
      You can toggle the “debug” option to “Yes” to see that the physics engine will be using 6 6 boxes to represent this maze:

      m2.gif

       
       

      Step 5 - The “add ball” message

      Now we are ready to add the marble ball. In the Maze sprite, broadcast a new message named “add ball”:

      e72dac88-3ef8-476e-a0e9-b075a571bd2a-image.png

       
      Then in the Ball sprite, when it receives this message, we will move the ball to a starting position, such as x = 50 and y = 50:

      af679332-e331-4cd4-bd6a-f72ecae48889-image.png

       
       

      Step 6 - Enable Physics for the Ball

      Next, we need to tell the physics engine about the ball, so the engine will manage the ball’s movement for us. Since the ball would just behave like a real marble, we can set it as a “dynamic” object.

      a2c219e6-c9ae-4a4f-b06b-ded49cf91b40-image.png

       
      Now if we run the project, the ball should fall onto the maze:

      m4.gif

       
       

      Step 7 - Press Keys to Rotate the Maze

      Next, in the Maze sprite, we will use a “forever” loop to check for key presses repeatedly. If the “a” key is pressed, we will rotate the maze left, and if “d” is pressed, we will rotate the maze right. If neither key is pressed, we set the rotation speed to 0, so the maze stops rotating.

      b5212ab3-8d7a-47d1-a84d-1f229e2a24bb-image.png

       
      Now we can already play with the maze like this:

      m5.gif

       
       

      Step 8 - Add a New Costume with the Goal Area

      Now we are going to add a goal area for the ball. Name the existing maze costume as “maze”, then duplicate it into a new costume named “maze with goal”. In this new costume, add a new blue square to serve as the goal area.

      m7.gif

       
       

      Step 9 - Switch Costume

      This next step is a bit unusual. We are going to switch to the “maze” costume, add it to the physics world, then switch to the “maze with goal” costume. Basically, you need to insert 2 “switch costume” blocks like this:

      90bd8507-cacc-4878-ac17-e89ed82533e9-image.png

       
      Here is the reason for doing this. When the physics world creates a body for the maze, we do not want it to use the goal area (the new blue square), because we do not want the ball to collide with this blue square. After the maze has been added to the physics world, we want to switch back to showing the blue square, so that when the ball touches it, we will know the game is over.

      If you toggle “Debug” to “Yes” temporarily, you will see that the blue square is not used as part of the compound body of the maze. Therefore, the ball won’t collide with it.

      d09a4f5e-fe03-4feb-8400-ae12211a6b2b-image.png

       
      We can verify that the ball won’t collide with the blue square:

      m8.gif

       
       

      Step 10 - Wait Until Touching the Blue Color

      Now we can easily tell if the game is over by checking if the ball is touching the blue color.

      In the Ball sprite, we will wait until the ball is touching the blue color, then show a label to declare the player has won the game:

      4303f550-6f30-4184-8947-47558daad2aa-image.png

       
      This is the final demo of the game:

      m9.gif

       
       

      Creative Ideas

      There are many ways you can enhance this game. Here are some example ideas:

      • A More Complex Map: clearly the current map is too simple. You can design any map you like using all the shapes in the costume editor (not lines). Don’t forget to align the costume to the center when you are done.

      • Teleporting: you can easily add additional areas with different colors to trigger some special movement. For example, add a green circle (similar to how the blue square is added), and if the ball touches this green color, teleport it to a different part of the maze.

      • Reward Items: you can also add other items in the maze, and when the ball picks up that item, it gains some specify power, such as becoming really bouncy.

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