This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.
  • Index
  •  » Show and tell
  •  » Getting computer cars to drive themselves around a track (My tell...)

#1 2012-07-21 03:11:27

AddInnovation
Scratcher
Registered: 2011-11-16
Posts: 57

Getting computer cars to drive themselves around a track (My tell...)

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])) degrees
The 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
    end
Note 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.

The final game is at the link below - please feel free to download it or remix it.

http://scratch.mit.edu/projects/AddInnovation/2355265

I created some additional features to go with the track in the form of water features - they were only supposed to be interesting things going on (e.g. there was a diver which almost got eaten by a shark), but I added a speedboat going around the centre of one track (including a ramp to jump) using exactly the same scripts.
If I wanted to expand "Wacky Races", I'd add one or more races in water in speedboats, and then maybe add some other automatic bikes / cars / snails / whatever !

Let me know if you have any questions !

Offline

 
  • Index
  •  » Show and tell
  •  » Getting computer cars to drive themselves around a track (My tell...)

Board footer