|

Code A Racing Car Game with Scratch and Lego Wedo 2.0 (Part 1)

Share this!

Liam has just finished building his first interactive game by pairing Lego Wedo 2.0 with Scratch. It’s a racing car game, complete with a Lego steering wheel controller.

Code a racing car game with scratch and lego wedo

The inspiration for this game comes from Mi 2 Tom, who has some fantastic projects up on YouTube. Liam’s version is similar, but all the elements have been created from scratch, so he’s got a great understanding of how the game is created.

Breakdown of this post

This game has a lot more components to it than others Liam has created. To make it easier to follow, we’ve broken it down into a few parts.

Part 1

  • Building the steering wheel with Lego and Wedo
  • Drawing the background for the game
  • Creating the sprites you’ll need

Part 2

  • Creating the code for all the sprites, including how to end the game and keep score

Associated posts

  • How to create the racing car sprite (in more detail)
  • Modifying existing sprites from the Scratch library

Building the Lego steering wheel

Modifying the base and frame

The steering wheel, as built by Mi 2 Tom, looks to be made from the Lego Wedo 2.0 kit. Liam has the Wedo sensors, hub, motor, and most of the components, but not the full kit, so we needed some substitution. It’s often quite a good thing, because he has to think outside the box to find solutions. The original base is built on top of the smarthub. That will generally work really well, but Liam is a multi-tasker. He tends to have several projects on the go at any one time. Right now, for example, there are three projects ‘mostly built’ and a fourth one in the planning stages. As a result, we need to be able to switch the smarthub between projects when he’s ready to test one.

For the modification of the base for the steering wheel, Liam built a structure out of Lego to hold the steering wheel frame, and left an empty space in the middle to slot the smarthub into. When he’s ready to try the racing car game, he puts the smarthub in, connects the motor, and it’s ready to go.

Strengthening the steering wheel

For the steering wheel itself, there’s been a lot of substitution. Liam did his best to follow Mi 2 Tom’s build process, and it looked very similar, but something wasn’t right. Liam’s version wasn’t very stable. Where the Technic bars and right angles are attached with pins, they were quite loose and the steering wheel didn’t hold its shape. You’ll see in the photos below that it’s been braced front and back in several places. With the extra bracing, it works really well. Liam is a fairly enthusiastic young driver, and hasn’t done any harm to the steering wheel yet.

Connecting the steering wheel to the frame

The connection of the steering wheel to the shaft is still a bit insecure. It fixes to a round 2×2 brick, which is a fairly small connection doing a lot of the heavy lifting. On a regular basis, the steering wheel detaches from the shaft, and Liam finds himself driving in mid-air. Fortunately, it doesn’t affect the interaction between Lego Wedo and Scratch, so the race can just keep going. We can reattach it between races.

Steering wheel connection to shaft
The steering wheel often lets go of the shaft

Where the shaft connects to the frame, the rubber band is a critical component. Without it, the shaft would drop through the frame and get stuck. With the rubber band secured around the pulley wheel and the top of the frame, it keeps the shaft in place. The second function of the rubber band is that it helps the steering wheel return to upright when it’s released. The benefit to the racing car game is that the steering wheel will always prefer to drive straight, keeping you in your lane.

Rubber band holding the wheel centred
Rubber band holding the wheel centred

Setting the scene for the racing car game in Scratch

The road background

In a similar way to Liam’s volcanic eruption project, the background for this game is hand-drawn. Liam normally starts by looking through the Scratch library for a pre-made background that he likes. In this case though, he couldn’t find the right thing, so decided to create one himself. If you’re making this game yourself and aren’t sure about your drawing skills, you can also upload your own images, so maybe look for a free stock photo that you like.

Backdrop for the racing car game in Scratch

For this game, the background refers to everything that doesn’t move. Liam needed to add the grey road surface, the central white line, and the blue space either side. The background doesn’t include the dashed lane markers, because they travel down the screen. For this game, the lane markers are sprites, and you’ll see more about them below.

How to create your own sprites in Scratch

There are several Sprites in this game, which were all created individually (with help from Grandma for the details):

  • The racing car
  • Oncoming traffic
  • Lane dividers

The racing car Sprite

The racing car for the game is mostly made from rectangles, so it’s easier than it looks. We’ve put together some slides below that show you how it’s done. For a more detailed explanation, we’ve put together a post which breaks down the steps for making the car.

  • Sprite menu
  • Select the Costume tab
  • Choose the rectangle tool
  • Start by drawing a rectangle
  • Choose the reshape tool
  • Use the reshape tool to drag the lines
  • Layer the rectangles
  • Car beginning to take shape
  • Add lines to mark the bonnet and boot
  • Add a spoiler
  • Use the back button to shift the spoiler
  • Finished racing car sprite
Costumes for the racing car

Costumes for the racing car

Once you’ve created your racing car, you’ll need to create three costumes for it – facing straight ahead, turning left, and turning right. It’s the exact same car, just rotated and saved as another costume. You’ve already done costume one, by creating the car facing straight ahead.

It’s very easy to create these additional costumes. Right-click on the thumbnail of the sprite on the left-hand side, and select ‘duplicate’. Rotate the car, and you’re done. Duplicate it again to create the third sprite, and rotate it the other way. In the section below about coding the racing car, you’ll see why these different costumes are important.

Other traffic – trucks and cars

The sprites for rest of the traffic were created in the same way as the racing car – start with a rectangle, and modify it as you go.

Even though it seems like there are a lot of different vehicles on the road, there are actually only two sprites. One sprite holds the code for the left-hand lanes, and the other for the right-hand lanes. The different colours and shapes have been made by adding multiple costumes to each sprite.

In a similar way to the racing car sprite, duplicate the first sprite. Select the body of the car, and change the colour. Do the same for the roof, and you’ve created a new sprite very quickly. In the coding for the traffic, we’ll look at how to vary the vehicles that appear on the road.

Lane dividers

The lane dividers are super simple, but very important. These are the dashed white lines that move down the screen while the car is driving. They’ve got some simple coding behind them, but from the costume perspective, it’s just a white rectangle set in the centre of the canvas. Liam needed two sprites, one for each side of the road.

How are you getting on?

How are you going so far? Have you built your steering wheel and created your sprites? If you have, check out Part 2 to start coding.

If there’s something you got stuck on that we didn’t mention here, tell us that too so we can add some extra info and help others!

Make a racing car game with scratch and lego wedo

Share this!

Similar Posts

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *