As the numbers get bigger the further they are from the centre, we can see that if random number ‘1’ is generated, the traffic will be in the left-most lane. At this point, we are using coordinates again, on the horizontal or x-axis to set up the traffic in the centre of one of the lanes. Within each part of the if/else code, there is an orange line which says ‘set Xcarposition to…’. Because there are two lanes of traffic, we can set up the traffic position based on whether number 1 or 2 comes through each time. The ‘if’ statement says “if the number equals 1, then do the first action, if not, do the second action”. So the green line is saying “pick a number between 1 and 2”. It’s embedded in an if/else statement, which gives different instructions for two different outcomes. This is the green line about halfway down the code sequence. There’s a way around this, using a random number generator. But, you don’t want to alternate, or put a pattern in place, because the game will get boring if it’s predictable. Select which lane the traffic will appear inĮach side of the road has two lanes, and both lanes need traffic. That will be on the left-hand side at the top. Looking at the starting coordinates for the traffic on the left, the coordinates are -152,170. A positive number will be in the top half of the screen, and a negative number at the bottom. So, an x-coordinate of a negative number will be on the left of centre, and a positive number will be on the right. Coordinate 0,0 (x,y) is in the centre of the screen. The starting coordinates, on the blue line at the top, tell the sprite where to begin. Coding for the traffic (in both directions) When the tilt sensor is more than 20 degrees to the right, the car will change to the diagonally-right costume, and move 10 pixels horizontally to the right. The third condition does the same as the second, but to the right. This moves the car 10 pixels to the left, along the horizontal. That’s what the blue line of code is for – change x by -10. At the same time, you want the car to move sideways so it can change lanes. When the condition is met, costume 2 will show, where the car is facing diagonally left. (Vertical = less than 20 degrees, Left = more than 20 degrees.) If you change the vertical range in Condition 1, make sure you update Conditions 2 and 3 to match. The degrees selected should be the same as the condition for vertical, so that there’s no overlap or gap in the code. The second condition applies when the tilt sensor is more than 20 degrees to the left. We found that 20 degrees either side was good, but you may prefer something different. If the range is too small, you’ll find it hard to hold the steering wheel close enough to vertical, and the racing car will veer off to the side when you don’t want it to. Very important for this part of the code is to nominate a suitable range either side of vertical. Under those conditions, costume 1 will show, which is when the car is facing straight ahead. The first part of this code applies when the tilt sensor is close to vertical – less than 20 degrees either left or right. All three are inside a forever loop, so the sprite will constantly check which condition is applicable. That’s one condition for each costume, so that the sprite knows what to do. Notice that there are three ‘if’ conditions in this section of code. At the same time, we want the car to move left or right when we turn the steering wheel, so the blue lines of code tell the car to move along the horizontal axis.įinal part of the code for the racing car, using Lego Wedo tilt sensor blocks It’s controlled by the tilt angle of the sensor, either left, right, or vertical. The first is to control which costume is used for the racing car sprite. In this code, the tilt sensor is used to instruct two different actions simultaneously.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |