I think a lot of motivation has to do with completion/success. If you're coding a game/learning to code, and you feel like you're getting nowhere, it can be easy to lose motivation. To get around this, I think sometimes it is as easy as finding something else to complete/accomplish.