This is usually done with reinforcement learning. For this you would need an environment which is able to react on your networks predictions. The environment must provide the current state (e.g. an image of the current game situation) which can be fed to the CNN and must accept actions (the NNs predictions) and calculate a reward for the given action. The optimization goal would be to maximize the sequence of states without being game over (and maybe a higher reward for an apple).
You could have a look at the environments of openai-gym 15. Even if they don’t have such an environment you could look at other games or the cartpole environment as a beginning.