Document stub.png This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (May 2020)
Reason: Needs more descriptions

A driving engine is used in a variety of racing games, and can be very easy to replicate. There are two main ways that top-down driving works- in a non-scrollable world and a scrollable world. For the non-scrollable driving engine, only 1 script is needed.

Non-scrollable Engine

when gf clicked
go to x: (0) y: (0) // reset position
point in direction (90) // reset direction
set [turn v] to (0) // reset on flag click
set [velocity v] to (0) // reset on flag click
forever
    if <(0) < (velocity)> then // if sprite moving forward
        if <key (left arrow v) pressed?> then
            change [turn v] by (-0.75) // affect direction negatively
            change [velocity v] by (-0.15) // slow down forward movement on turn
        end
        if <key (right arrow v) pressed?> then
            change [turn v] by (.75) // affect direction positively
            change [velocity v] by (-0.15) // slow down forward movement on turn
        end
    else // if movement speed negative
        if <key (left arrow v) pressed?> then
            change [turn v] by (.75) // turn opposite direction, as with real cars
            change [velocity v] by (.15) // slow down backward movement on turn
        end
        if <key (right arrow v) pressed?> then
            change [turn v] by (-0.75) // turn opposite direction, as with real cars
            change [velocity v] by (.15) // slow down backward movement on turn
        end
    end
    if <key (up arrow v) pressed?> then
        change [velocity v] by (.5) // move forward
    end
    if <key (down arrow v) pressed?> then
        change [velocity v] by (-0.5) // move backward
    end
    set [turn v] to  ((turn) * (.9)) // gradually slow down, as in real physics
    turn right (turn) degrees // set turn to control the sprite's direction
    set [velocity v] to  ((velocity) * (.9)) // gradually slow down, as in real physics
    move (velocity) steps // set velocity to control the sprite's position
end

Scrollable Engine

The main difference between the scrollable driving engine and the non-scrollable driving engine is that a non-scrollable driving engine is good for one computer, two player games, while a scrollable engine is primarily used in one player or online, cloud-based games.The scrollable driving engine requires two sprites- car and road- and as such, two separate scripts. The first one is very similar to the non-scrollable script.

when gf clicked
go to x: (0) y: (0) // reset position
point in direction (90) // reset direction
set [turn v] to (0) // reset on flag click
set [velocity v] to (0) // reset on flag click
forever
    if <(0) < (velocity)> then // if sprite moving forward
        if <key (left arrow v) pressed?> then
            change [turn v] by (.75) // affect direction positively
            change [velocity v] by (-0.15) // slow down forward movement on turn
        end
        if <key (right arrow v) pressed?> then
            change [turn v] by (-0.75) // affect direction negatively
            change [velocity v] by (-0.15) // slow down forward movement on turn
        end
    else // if movement speed negative
        if <key (left arrow v) pressed?> then
            change [turn v] by (-0.75) // turn opposite direction, as with real cars
            change [velocity v] by (.15) // slow down backward movement on turn
        end
        if <key (right arrow v) pressed?> then
            change [turn v] by (.75) // turn opposite direction, as with real cars
            change [velocity v] by (.15) // slow down backward movement on turn
        end
    end
    if <key (up arrow v) pressed?> then
        change [velocity v] by (.5) // move forward
    end
    if <key (down arrow v) pressed?> then
        change [velocity v] by (-0.5) // move backward
    end
    set [turn v] to  ((turn) * (.9)) // gradually slow down, as in real physics
    set [velocity v] to  ((velocity) * (.9)) // gradually slow down, as in real physics
    move (velocity) steps // set velocity to control the sprite's position
end

Here is the script that controls the movement of the 'road' the car is driving on.

Note Note: Remember that it must be a sprite, not a backdrop.
when gf clicked
set rotation style [don't rotate v]
go to x: (0) y: (0) // reset position
forever
    move ((0) - (velocity)) steps// move opposite direction of velocity
    point in direction ([direction v] of (car v)) // point in direction of car
end

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.