Driving Engine

Материал из Поле цифровой дидактики

Шаблон:Wiki Standards 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

whenclickedgotox:0y:0resetpositionpointindirection90resetdirectionsetturnto0resetonflagclicksetvelocityto0resetonflagclickforeverif0<velocitythenifspritemovingforwardifkeyleftarrowpressed?thenchangeturnby-0.75affectdirectionnegativelychangevelocityby-0.15slowdownforwardmovementonturnendifkeyrightarrowpressed?thenchangeturnby.75affectdirectionpositivelychangevelocityby-0.15slowdownforwardmovementonturnendelseifmovementspeednegativeifkeyleftarrowpressed?thenchangeturnby.75turnoppositedirection,aswithrealcarschangevelocityby.15slowdownbackwardmovementonturnendifkeyrightarrowpressed?thenchangeturnby-0.75turnoppositedirection,aswithrealcarschangevelocityby.15slowdownbackwardmovementonturnendendifkeyuparrowpressed?thenchangevelocityby.5moveforwardendifkeydownarrowpressed?thenchangevelocityby-0.5movebackwardendsetturntoturn*.9graduallyslowdown,asinrealphysicsturnturndegreessetturntocontrolthesprite'sdirectionsetvelocitytovelocity*.9graduallyslowdown,asinrealphysicsmovevelocitystepssetvelocitytocontrolthesprite'spositionend

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.

whenclickedgotox:0y:0resetpositionpointindirection90resetdirectionsetturnto0resetonflagclicksetvelocityto0resetonflagclickforeverif0<velocitythenifspritemovingforwardifkeyleftarrowpressed?thenchangeturnby.75affectdirectionpositivelychangevelocityby-0.15slowdownforwardmovementonturnendifkeyrightarrowpressed?thenchangeturnby-0.75affectdirectionnegativelychangevelocityby-0.15slowdownforwardmovementonturnendelseifmovementspeednegativeifkeyleftarrowpressed?thenchangeturnby-0.75turnoppositedirection,aswithrealcarschangevelocityby.15slowdownbackwardmovementonturnendifkeyrightarrowpressed?thenchangeturnby.75turnoppositedirection,aswithrealcarschangevelocityby.15slowdownbackwardmovementonturnendendifkeyuparrowpressed?thenchangevelocityby.5moveforwardendifkeydownarrowpressed?thenchangevelocityby-0.5movebackwardendsetturntoturn*.9graduallyslowdown,asinrealphysicssetvelocitytovelocity*.9graduallyslowdown,asinrealphysicsmovevelocitystepssetvelocitytocontrolthesprite'spositionend

Here is the script that controls the movement of the 'road' the car is driving on. Шаблон:Note

whenclickedsetrotationstyledon'trotategotox:0y:0resetpositionforevermove0-velocitystepsmoveoppositedirectionofvelocitypointindirectiondirectionofcarpointindirectionofcarend

See Also