Pages: 1
When I tried to create a scratch game where player cars raced against computer cars, I had to work out how to get the computer cars to steer around the track. I wanted the cars to work on ANY track without modifying the script so had to work out how - so I've done the Show, here's the Tell ...
The car is controlled by two sprites. Each which look like identical cars except for a single dot, for one sprite in front and to the left of the car, and for the other in front and to the right. The dot is the same colour as the track, so it can't be seen (although if you wanted to show the car driving at night, the dot's would look like cool headlights !)
The cars always move forwards, so if the car with dot on the left is touching the border colour outside the track, then it's the dot that's touching the colour and the car needs to turn right, so it sets it's variable "turndirection" to 2. If it's not touching the colour outside the track, it set's "turndirection to 0".
If the car with dot on the right is touching the border colour outside the track, then the car needs to turn left, so it sets it's variable "turndirection" to -2, otherwise it is set to 0.
The variable turndirection is local to each car (to make sure one script doesn't override the other).The left-dot car sets the position for both cars. Within a loop, until the race is over, it moves forward according to it's speed variable (starting with a value of about 4), and turns according to the script ;
turn ((turndirection) + ("turndirection" of s[Cmp1SenseR])) degreesThe whole movement script for the right-dot car is as follows, with the left-dot car turn sensing block exactly the same as the if touching colour ... block.
when I receive [NextRace] set (turndirection) to [0] show repeat (until (RaceOver) = [1]) set (Speed) to ( (Speed) of [Cmp1SenseL] ) if (((touching color c[00CE42]?) and (((turndirection) of [Cmp1SenseL v] ) =[0])) and ((turncnt) < [360])) set (turndirection) to ((Speed) * [-2]) change (turncnt) by [1] else change (turncnt) by -[1] set (turndirection) to [0] End point in direction ( (direction) of [Cmp1SenseL v] ) set (x) to ( (x position) of [Cmp1SenseL v] ) set (y) to ( (y position) of [Cmp1SenseL v] ) End hide endNote how the right car is set to point in the same direction as the left car, and it's X and Y position are set to exactly the same track position so that we only actually see one car.
Offline
Pages: 1