Game engines

A game engine is an interface that assists in building and playing games, including
◼
  • graphics
  • ◼
  • physics
  • ◼
  • user input
  • ◼
  • collision detection
  • ◼
  • animation, sound, etc.
  • The tools to create games can be built up with the Wolfram Language and the games run in a Notebook.

    Past R&D talks
    

    Dynamic is the driver

    Basic principles

    The goal of
    Dynamic[expr]
    is to display
    expr
    as it is right now.
    In[]:=
    x=0.5;
    In[]:=
    Dynamic[x]
    In[]:=
    Slider[Dynamic[x],{0,4}]

    Progress Quest!

    Out[]=
    Welcome to Progress Quest!
    Lvl:
    0
    Exp:
    0
    Start new quest
    Resume quest

    What’s happening:
    

    Graphics

    Our game takes place in a Graphics expression with geometric primitives.
    In[]:=
    Graphics[​​{Purple,Disk[{2,4},0.5],Rectangle[{1,1},{2,2}],AbsoluteThickness[3],Line[{{4,3},{4,1}}]},​​Background->LightBlue,Frame->True,GridLines->Automatic,ImageSize->300,PlotRange->{{0,5},{0,5}}]
    //InputForm

    Physics: time advancement

    Apparent motion

    Let's programmatically to move a disk from left to right as smoothly as possible.
    In[]:=
    DynamicModule{x1 = 1, x2 = 1, $TimeFactor = 0.005, vel = 1},​​ Column​​ LabeledSlider[Dynamic[$TimeFactor], {{1, 0.1, 0.05, 0.01, 0.005, 0.001}}],​​ Graphics​​ ​​
    Vertical lines
    ,​​ {Purple, Disk[{Dynamic[If[x1 < 5, x1 += vel*$TimeFactor, x1 = 1]], -0.5}, 0.1]},​​ {Red, Disk[{Dynamic[If[x2 < 5, x2 += 2*vel*$TimeFactor, x2 = 1]], 0.5}, 0.1]},​​
    Graphics options
    
    In[]:=
    Column"Example Output",ListAnimate
    

    Frame rate math

    How many times per second can the front end update a value?
    In[]:=
    DynamicModule[{x = 0, t = 0},​​ Column[​​ Animator[Dynamic[t], {0, 1}, AnimationRate -> 1, AnimationRepetitions -> 1],​​ Dynamic[If[t < 1, x++, {x, 1./x}]]]]
    In[]:=
    Column[{"Example Output",Pane@
    }]
    If we want the velocity to be one unit per second then all time calculations need to include this factor.
    1unit
    second
    *
    second
    200frames

    User Input

    ControllerInformation

    In[]:=
    ControllerInformation[]
    In[]:=
    Column"Example Output",Pane@
    

    ControllerState

    In[]:=
    Dynamic[{ControllerState["X Axis"],ControllerState["Y Axis"]}]
    In[]:=
    Column[{"Example Output",Pane@
    }]

    controllerReadyQ
    

    User Input + Physics

    Control apparent motion with a gamepad

    In[]:=
    DynamicModule{pX1 = 1, pX2 = 1, $TimeFactor = 0.005},​​ Column​​
    $TimeFactor slider
    ,​​ Graphics​​ ​​
    Vertical lines
    ,​​ {Purple, Disk[{Dynamic[pX1 += Round[ControllerState["X Axis"], 0.1]*$TimeFactor], -0.5}, 0.1]},​​ {Red, Disk[{Dynamic[pX2 += 2*Round[ControllerState["X Axis"], 0.1]*$TimeFactor], 0.5}, 0.1]},​​
    Graphics options
     // controllerReadyQ
    Out[]=
    Example Output

    Physics: motion along each coordinate axis

    Example

    Collision Detection: scene boundary

    Example

    Collision Detection: rectangles

    Collision Detection: multiple objects

    Example

    Tool: obstacle builder

    Add or remove Rectangle primitives, locked to an integer grid.

    See it in action first...

    Physics: changes to velocity

    This could be due to gravity or a direct impulse control.

    See it in action first...

    2D platformer: mechanics

    Example

    2D platformer: side-scrolling

    Example

    Graphics: adding artwork

    Inset

    Raster

    In image coordinates, the y-axis is inverted compared to graphics coordinates.

    Tool: draw blocks

    Example

    Animation: artwork

    Sprite sheet

    I’m starting with a sprite map from opengameart.org by www.kenney.nl.
    Nice maps have pixel locations and descriptions of each frame:

    Animation: adding to the scene

    Use Raster instead of Rectangle

    2D platformer: create a level

    Combining it all together...

    Bonus: Castlevania demo

    See https://github.com/KMDaily/CastlevaniaDemo_WolframLanguage.

    Bonus: games that directly affect velocity

    Fishing minigame

    Bonus: pixel painter tool

    See https://community.wolfram.com/groups/-/m/t/1286303.

    CITE THIS NOTEBOOK

    Wolfram R&D LIVE: Building a video game with Wolfram notebooks​
    by Kevin Daily​
    Wolfram Community, STAFF PICKS, May 12 2023
    ​https://community.wolfram.com/groups/-/m/t/2918746