Typing text afte ra key is pressed

Hi everyone

I would like to ask you if you could help me to improve this code.

In this blendfile you will see a text, that contains a script. Take a look a the logic editor tab, you can see 2 properties, change the integer called number and press P, you will see the text will display different phrases.

I am currently working on the first stage Keisha, which is also the tutorial. If this step is done, i can then relase a good review of the game.

It should do these things step by step:

Enter = “Use Arrowkeys to move around”
LKey, UKey, RKey, DKey (each one is pressed at least once) = “Use C to Jump”
C x 1 = “Press C again in mid air for double jump”
C x 2 = “Good job, now you are going to learn how to shoot”

It is also the reason why i prepare the number property
I thank you in advance

Attachments

Textrun.blend (458 KB)

Textrun_KEYS.blend (3.37 MB)

I changed acouple of things, Textrun.py now runs in module mode so that I can have variable stored outside
the main loop, the Always sensor is not skipping anymore as the keyboard status I used in verifying key press
will become unreliable if skipped(even by just a few frames)

The 4 Arrowkeys must each be pressed once before it can proceed, check the console*

While implementing this I learned that I can still access your textlist(dict) placed outside the main loop,
which is helpful so I decided to leave it there… but I really don’t know how I’ve done it without the global keyword

To begin with, I’m not even sure how dictionary work? XD

from bge import events,logic

textlist= {}

textlist[0]      = ["6th Project - KEISHA incomplete" , events.ENTERKEY,31 ]
textlist[1]      = ["Use Arrowkeys to move around" , [events.UPARROWKEY,events.DOWNARROWKEY,events.LEFTARROWKEY,events.RIGHTARROWKEY],28 ]
textlist[2]     = ["Use C to Jump" , events.CKEY,13  ]
textlist[3]      = ["Press C again in mid air for double jump"  ,events.CKEY, 40]
_key_log = []

def main():
    #textlist= {}
    #textlist[0]      = ["6th Project - KEISHA incomplete" , events.ENTERKEY,31 ]
    #textlist[1]      = ["Use Arrowkeys to move around" , [events.UPARROWKEY,events.DOWNARROWKEY,events.LEFTARROWKEY,events.RIGHTARROWKEY],28 ]
    #textlist[2]     = ["Use C to Jump" , events.CKEY,13  ]
    #textlist[3]      = ["Press C again in mid air for double jump"  ,events.CKEY, 40]
    global _key_log
    cont =logic.getCurrentController()
    own = cont.owner 
    keyboard = logic.keyboard
    
    #ARROWS
    #enter = logic.KX_INPUT_ACTIVE == keyboard.events[events.ENTERKEY]
    #UKey = logic.KX_INPUT_ACTIVE == keyboard.events[events.UPARROWKEY]
    #DKey = logic.KX_INPUT_ACTIVE == keyboard.events[events.DOWNARROWKEY]
    #LKey = logic.KX_INPUT_ACTIVE == keyboard.events[events.LEFTARROWKEY]
    #RKey = logic.KX_INPUT_ACTIVE == keyboard.events[events.RIGHTARROWKEY] 
    #message = ""
    #mlen = len(message) # length of the string

    text,keys,count = textlist[own['number']]
    
    for key,state in keyboard.active_events.items(): # state is key status, 1=Pressed, 2=Still Active, 3=Released
        if own['number'] == 0:
            if key == keys and state == 3:
                own['number'] = 1
                own['counter'] = 0
        elif own['number'] == 1:
            if keys:
                if key in keys and key not in _key_log and state == 3:
                    _key_log.append(key) # log key that is already pressed
                    print(_key_log)
                if len(_key_log) == len(keys):
                    own['number'] = 2
                    own['counter'] = 0
                    _key_log = [] 
        elif own['number'] == 2:
            if key == keys and state == 3:  
                own['number'] = 3 
                own['counter'] = 0
        elif own['number'] == 3:
            if key == keys and state == 3: 
                own['number'] = 0 # repeat
                own['counter'] = 0
    
    if own['counter'] < count:
        own['counter'] += 1
        own.text = text[0 : own['counter']] # slice indices
        #print(own.text)

        if own['counter'] == count:
            own.text = text
            #own.text = ""
            #own['counter'] = 0
        
    #print(keys)
from bge import events,logic

textlist= {}

textlist[0]      = ["6th Project - KEISHA incomplete" , events.ENTERKEY,31 ]
textlist[1]      = ["Use Arrowkeys to move around" , [events.UPARROWKEY,events.DOWNARROWKEY,events.LEFTARROWKEY,events.RIGHTARROWKEY],28 ]
textlist[2]     = ["Use C to Jump" , events.CKEY,13  ]
textlist[3]      = ["Press C again in mid air for double jump"  ,events.CKEY, 40]
_key_log = []

def main():
    global _key_log
    cont =logic.getCurrentController()
    own = cont.owner 
    keyboard = logic.keyboard

    text,keys,count = textlist[own['number']]
    
    for key,state in keyboard.active_events.items():
        if own['number'] < len(textlist)-1:
            if isinstance(keys,int):      
                if key == keys and state == 3:
                    own['number'] += 1
                    own['counter'] = 0            
            elif isinstance(keys,list):
                if key in keys and key not in _key_log and state == 3:
                    _key_log.append(key)
                    print(_key_log)
                if len(_key_log) == len(keys):
                    own['number'] += 1
                    own['counter'] = 0
                    _key_log = [] 
        else:
            if key == keys and state == 3:
                own['number'] = 0
                own['counter'] = 0
                _key_log = [] 
    
    if own['counter'] < count:
        own['counter'] += 1
        own.text = text[0 : own['counter']] # slice indices
        #print(own.text)

        if own['counter'] == count:
            own.text = text
            #own.text = ""
            #own['counter'] = 0
        
    #print(keys)

A small changes to the code, everything is the same except when working with an ever expanding dictionary the previous version will require hundreds of “if”, an absolute nightmare to maintain, so now everything is automated, less is more so they say :yes:

wow thanks alot guramarx