Honestly, the answer is “familiarity and practice”. When you wrote your question to me, you probably weren’t thinking about how to spell each word, or where the key for each letter is on the keyboard, or how to put the sentence together in a coherent fashion. For someone unfamiliar with the language and keyboard layout, it could take hours or even days to compose a paragraph. You’ve already internalized many of those concepts such that you don’t even need to think about it anymore - the rules and the vocabulary for composing and expressing your question just came naturally because of how familiar you are with the means of expression.
Much like learning a new language or how to use a particular tool, writing code and coming up with game logic requires a lot of familiarity and practice. This is because they are all essentially ways to express ideas. In order to convey the idea of how a character can move in a game to another person, you would need to use words, concepts, and maybe illustrations that the person understands to express that idea. Similarly, if you wanted to convey an idea using a computer, you would need to use words, concepts, and data the computer understands to express that idea through the computer. The more familiar you are with how the computer works, the easier this becomes. The way you gain familiarity with something is through practice.
Got a burning question you want answered?