direction !left !down direction !up direction left elif (event key =k_right or event key =k_dand direction right elif (event key =k_up or event key =k_wand direction direction up elif (event key =k_down or event key =k_sand direction down elif event key =k_escapeterminate(line is the start of the main game loop and line is the start of the event handling loop if the event is quit eventthen we call terminate((which we've defined the same as the terminate(function in the previous game programsotherwiseif the event is keydown eventthen we check if the key that was pressed down is an arrow key or wasd key we want an additional check so that the worm does not turn in on itself for exampleif the worm is moving leftthen if the player accidentally presses the right arrow keythe worm would immediate start going right and crash into itself that is why we have this check for the current value of the direction variable that wayif the player accidentally presses an arrow key that would cause them to immediately crash the wormwe just ignore that key press collision detection check if the worm has hit itself or the edge if wormcoords[head][' '=- or wormcoords[head][' '=cellwidth or wormcoords[head][' '=- or wormcoords[head][' '=cellheight return game over for wormbody in wormcoords[ :] if wormbody[' '=wormcoords[head][' 'and wormbody[' '=wormcoords[head][' '] return game over the worm has crashed when the head has moved off the edge of the grid or when the head moves onto cell that is already occupied by another body segment we can check if the head has moved off the edge of the grid by seeing if either the coordinate of the head (which is stored in wormcoords[head][' ']is - (which is past the left edge email questions to the authoral@inventwithpython com