How to make a worm game

Let’s have some nostalgia here, we’ll take a look, how to make a simple worm game. The example codes have been coded in Monkey X Pro, but can be compiled with free Monkey X to desktop and HTML5. Perhaps someone will develop a fun worm game with Monkey X Pro to Android. 🙂

 

First, simple code where the player can just control the worm, that moves the very old traditional way:

Import mojo

Function Main()
    New MyApp
End

Class MyApp Extends App

    Const BG_WIDTH:Int = 640
    Const BG_HEIGHT:Int = 480

    Global devWidth:Float, devHeight:Float, scaleX:Float, scaleY:Float
    Global wormLength:Int = 8
    Global wormX:Int[8], wormY:Int[8]
    Global dx:Int, dy:Int

    Method OnCreate()

        devWidth = DeviceWidth()
        devHeight = DeviceHeight()
        
        scaleX = devWidth / BG_WIDTH
        scaleY = devHeight / BG_HEIGHT

        ' Init worm
        For Local w:Int = 0 To 7
            wormX[w] = 16*7 - w * 16
            wormY[w] = 0
        Next
        
        dx = 1
        dy = 0

        SetUpdateRate(10)
    End

    Method OnUpdate()

        If KeyHit(KEY_DOWN) Then
            dx = 0
            dy = 1
        Endif
        
        If KeyHit(KEY_UP) Then
            dx = 0
            dy = -1
        Endif
        
        If KeyHit(KEY_LEFT) Then
            dx = -1
            dy = 0
        Endif
        
        If KeyHit(KEY_RIGHT) Then
            dx = 1
            dy = 0
        Endif

        For Local w:Int = wormLength - 1 To 1 Step -1
            wormX[w] = wormX[w-1]
            wormY[w] = wormY[w-1]
        Next

        wormX[0] = wormX[0] + dx * 16
        wormY[0] = wormY[0] + dy * 16
    End
    

    Method OnRender()
        
        ' Scale the graphics
        PushMatrix()
        Scale (scaleX,scaleY)

        SetColor 180,0,180        
        DrawRect 0,0,BG_WIDTH,BG_HEIGHT

        SetColor 0,255,0

        For Local w:Int = 1 To wormLength - 1
            DrawOval wormX[w],wormY[w],16,16
        Next
                
        SetColor 255,0,0
        DrawOval wormX[0],wormY[0],16,16

        PopMatrix()
    End

End Class

Then a bit more advanced code, where the worm moves with step of one pixel, but one part of the worm is an oval that’s width and height are 16 pixels. This version is also a little game where eating the food lengthens the worm.

Import mojo

Function Main:Int()
    New MyApp
    Return 0
End

Class MyApp Extends App

    Const STARTSCREEN = 0
    Const PLAY = 1

    Const BG_WIDTH:Int = 640
    Const BG_HEIGHT:Int = 480

    Global devWidth:Float, devHeight:Float, scaleX:Float, scaleY:Float
    Global wormLength:Int = 8
    Global wormX:Int[wormLength * 16], wormY:Int[wormLength * 16]
    Global dx, dy:Int
    Global foodX:Int, foodY:Int
    Global drawFood:Bool
    Global gameState:Int
    
    Method OnCreate()

        devWidth = DeviceWidth()
        devHeight = DeviceHeight()
        
        scaleX = devWidth / BG_WIDTH
        scaleY = devHeight / BG_HEIGHT

        wormX[0] = 200
        ' Init worm
        For Local w:Int = 0 To 7 * 16
            wormX[w] = 200-w
            wormY[w] = 10*16
        Next
        
        dx = 1
        dy = 0
        
        drawFood = True
        SetUpdateRate(60)
    End

    Method OnUpdate()
        
        ' Left mouse button starts the "game"
        If MouseHit(0) > 0 Then gameState = PLAY
        
        If gameState = STARTSCREEN Then Return
        
        If drawFood = True Then
            foodX = Rnd()*(639-16)
            foodY = Rnd()*(479-16)
            drawFood = False
        Endif
        
        If KeyHit(KEY_DOWN) Then
            dx = 0
            dy = 1
        Endif
        
        If KeyHit(KEY_UP) Then
            dx = 0
            dy = -1
        Endif
        
        If KeyHit(KEY_LEFT) Then
            dx = -1
            dy = 0
        Endif
        
        If KeyHit(KEY_RIGHT) Then
            dx = 1
            dy = 0
        Endif
        
        If Abs(wormX[0] - foodX) < 16 And Abs(wormY[0] - foodY) < 16 Then
            wormLength = wormLength + 1
            ' Dynamically resize the arrays
            wormX = wormX.Resize(wormLength * 16)
            wormY = wormY.Resize(wormLength * 16)

            For Local w:Int = 0 To 15
                wormX[(wormLength-1)*16-w] = wormX[(wormLength-2)*16-w]
                wormY[(wormLength-1)*16-w] = wormY[(wormLength-2)*16-w]
            Next
            drawFood = True
        Endif
        
        For Local w:Int = (wormLength - 1) * 16 To 1 Step -1
            wormX[w] = wormX[w-1]
            wormY[w] = wormY[w-1]
        Next    

        wormX[0] = wormX[0] + dx
        wormY[0] = wormY[0] + dy
    End
    

    Method OnRender()
        
        Local alpha:Float
        
        ' Scale the graphics
        PushMatrix()
        Scale (scaleX,scaleY)

        SetColor 180,0,180        
        DrawRect 0,0,BG_WIDTH,BG_HEIGHT

        Select gameState
            Case STARTSCREEN
                SetColor 255,255,255
                SetBlend AdditiveBlend
                
                DrawText "Simple worm game",(640 - TextWidth("Simple worm game")) / 2,10
                DrawText "Use cursor keys to control",(640 - TextWidth("Use cursor keys to control")) / 2,40
                DrawText "Press left mousebutton to start",(640 - TextWidth("Press left mousebutton to start")) / 2,70
    
            Case PLAY
                SetColor 255,255,255
                SetBlend AdditiveBlend
                DrawText "Worm Length: " + wormLength,0,0
                SetColor 0,255,0
                
                SetBlend AlphaBlend
                SetAlpha 1
                SetColor 0,255,0

                For Local w:Int = 0 To wormLength - 1
                    DrawOval wormX[w * 16],wormY[w * 16],16,16
                Next
                
                SetColor 255,0,0
    
                DrawOval wormX[0],wormY[0],16,16

                SetColor 255,255,0
                DrawOval foodX,foodY,16,16
        End Select
        
        PopMatrix()
    End

End Class

If you wan’t to see how above code works in practice, click here to play (HTML5 game). Feel free to use these codes.

%d bloggers like this: