• Bruce

Let's create an MPAGD game: Part 27: Making your controls redefinable

I thought I'd take a look at something a bit different in this part, and that's how can we make the controls for the game redefinable. As standard, MPAGD will generate an Intro Menu for your game that offers the player a choice of controls:


  1. KEYBOARD

  2. KEMPSTON JOYSTICK

  3. SINCLAIR JOYSTICK

  4. REDEFINE


But, pressing 4 for REDEFINE only allows the player to define a PAUSE key. So far, our game has keys for:


UP

DOWN

LEFT

RIGHT JUMP

FART


So, how do we make them redefinable?


First, let's recall how we set the default keys for the game. We do this in the KEYBOARD CONTROLS in the EDITORS menu:



So these are the keys that the player will use by default.


If we want to make them redefinable we need to add some code to the INTRO MENU EVENT. This is what ours looks like so far:



EVENT INTROMENU

BORDER 0                    ; black border
COLOUR 5                    ; cyan ink on black paper
LET CONTROL = 99            ; initialise control with 99
WHILE CONTROL >= 99         ; for as long as control is >= 99
    CLS                     ; clear the screen
    PRINTMODE 1             ; print the following text in double height
    AT 4 10                 ; at line 4 column 10
    MESSAGE 0               ; display the game title
    CHR 33                  ; '!'
    PRINTMODE 0             ; revert back to normal height text.
	
    AT 8 10                 ; display the menu:                      
    PRINT "1. KEYBOARD"
    AT 10 10
    PRINT "2. KEMPSTON"
    AT 12 10
    PRINT "3. SINCLAIR"
    AT 14 10
    PRINT "4. REDEFINE"
    LET CONTROL = 99        
    WHILE CONTROL = 99
        IF KEY OPTION1      ; if the 1 key is pressed
            LET CONTROL = 0 ; control 0 = KEYBOARD
        ENDIF
        IF KEY OPTION2      ; if  the 2 key is pressed
            LET CONTROL = 1 ; control 1 = KEMPSTON
        ENDIF
        IF KEY OPTION3      ; if the 3  key is pressed
            LET CONTROL = 2 ; control 3 is sinclair joystick
        ENDIF
        IF KEY OPTION4      ; if the 4 Key is pressed
            CLS             ; clear the screen
            AT 3 9
            PRINT "PRESS KEY FOR:"
            AT 6 12
            WHILE KEY OPTION4 ; wait for the 4 key to be released
            ENDWHILE          
            PRINT "PAUSE"     
            DEFINEKEY FIRE3   ; the key pressed will be the new FIRE3 key
            LET CONTROL = 100 ; set control to 100 which will end this
        ENDIF
    ENDWHILE
ENDWHILE
CLS                           ; clear the screen

CONTROL is a special MPAGD variable the defines which of the control methods the player will be using, the only values that make a difference are 0 (Keyboard) 1 (Kempston Joystick) or 2 (Sinclair Joystick)


That's why the code above initialises the CONTROL with the value 99, and the player wont leave the intro menu until they press either the 1 key (sets control to 0 for Keyboard), 2 key (sets the control to 1 for Kempston) or 3 key (sets the control to 2 for sinclair joystick)


The IF KEY statements are looking for OPTION1 (which if you look up at the Keyboard controls screenshot above is by default the '1' key), OPTION2, OPTION3 or OPTION4 key being pressed.


So, if you change your keyboard controls in the editor such that OPTION1 is the A key you would need to edit your menu so that it said A. KEYBOARD.


Confused? Hopefully not!


Back to the code.


The part we need to change is the IF KEY OPTION4 statement, change it to the code below (highlighted)



EVENT INTROMENU

BORDER 0                    ; black border
COLOUR 5                    ; cyan ink on black paper
LET CONTROL = 99            ; initialise control with 99
WHILE CONTROL >= 99         ; for as long as control is >= 99
    CLS                     ; clear the screen
    PRINTMODE 1             ; print the following text in double height
    AT 4 10                 ; at line 4 column 10
    MESSAGE 0               ; display the game title
    CHR 33                  ; '!'
    PRINTMODE 0             ; revert back to normal height text.
	
    AT 8 10                 ; display the menu:                      
    PRINT "1. KEYBOARD"
    AT 10 10
    PRINT "2. KEMPSTON"
    AT 12 10
    PRINT "3. SINCLAIR"
    AT 14 10
    PRINT "4. REDEFINE"
    LET CONTROL = 99        
    WHILE CONTROL = 99
        IF KEY OPTION1      ; if the 1 key is pressed
            LET CONTROL = 0 ; control 0 = KEYBOARD
        ENDIF
        IF KEY OPTION2      ; if  the 2 key is pressed
            LET CONTROL = 1 ; control 1 = KEMPSTON
        ENDIF
        IF KEY OPTION3      ; if the 3  key is pressed
            LET CONTROL = 2 ; control 3 is sinclair joystick
        ENDIF
        IF KEY OPTION4      ; if the 4 Key is pressed
            CLS             ; clear the screen
            AT 3 9
            PRINT "PRESS KEY FOR:"
            AT 6 12
            WHILE KEY OPTION4 ; wait for the 4 key to be released
            ENDWHILE          
            PRINT "UP"     
            DEFINEKEY UP   ; the key pressed will be the new UP key
            AT 8 12        
            PRINT "DOWN"     
            DEFINEKEY DOWN   ; the key pressed will be the new down key
            AT 10 12
            PRINT "LEFT"
            DEFINEKEY LEFT   ; the key pressed will be the new left key
            AT 12 12
            PRINT "RIGHT"
            DEFINEKEY RIGHT   ; the key pressed will be the new RIGHT key
            AT 14 12
            PRINT "JUMP"
            DEFINEKEY FIRE1  ; the key pressed will be the new JUMP key
            AT 16 12
            MESSAGE 3        ; message #3 is FART 
            DEFINEKEY FIRE2  ; the key pressed will be the new FART key
            AT 18 12
            PRINT "PAUSE"
            DEFINEKEY FIRE3  ; this will be the PAUSE key
            LET CONTROL = 100 ; set control to 100 which will end this and will return us to the menu
        ENDIF
    ENDWHILE
ENDWHILE
CLS                           ; clear the screen


So here, we use the DEFINEKEY command to ask for a keypress and whatever key is pressed becomes the new key for whichever key type we require. We are limited to only using and redefining the 11 key types that MPAGD allows:

  • UP

  • DOWN

  • LEFT

  • RIGHT

  • FIRE1

  • FIRE2

  • FIRE3

  • OPTION1

  • OPTION2

  • OPTION3

  • OPTION4

Note that if you make the OPTIONx keys redefinable they WILL affect your control selection menu! so if your menu displays 1.KEYBOARD but someone redefines OPTION1 to say the T key, when they try to select keyboard with the 1 key, it wont work, they would have to press T!


Hopefully you also noticed that rather than use PRINT "FART" I used MESSAGE 3 as I already have the word FART stored as a reusable message...just saved you a few bytes there!


Let's give it a test...


Cool, now we have redefinable keys!

69 views0 comments

Want to support my work?....Buy my games!

aboutME

Hello, I'm Bruce and I write games for old 8bit computers using Jonathan Cauldwell's excellent Multi-Platform Arcade Games Designer (MPAGD)

I've written a few successful* games for the Sinclair ZX Spectrum and MSX platforms that have been (largely) well received with my most recent game - Twenty Four Hour Parsley People scoring a 10 out of 10 on Planeta Sinclair.

In my blog I am sharing lots of the code that I wrote for my games, in a way that you can use in your own games.   I've commented it so that you'll learn some of the techniques I use to create interesting new mechanics and help your games stand out from the pack.

MPAGD includes lots of standard scripts, they're great to get you started, but if you're new (or just rusty) when it comes to writing code, hopefully my tutorials will help you get started and  turn your imagination into awesome 8 bit games!

All my code is free to use and do with as you please, but if you find them useful please feel free to buy me a coffee ...or better still - buy or download my games :)

*successful is a very relative term in 8bit computer games