direction right start the apple in random place apple getrandomlocation( while truemain game loop for event in pygame event get()event handling loop if event type =quit terminate( elif event type =keydown if (event key =k_left or event key =k_aand direction !right direction left elif (event key =k_right or event key =k_dand direction !left direction right elif (event key =k_up or event key =k_wand direction !down direction up elif (event key =k_down or event key =k_sand direction !up direction down elif event key =k_escape terminate( 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 check if worm has eaten an apply if wormcoords[head][' '=apple[' 'and wormcoords[head][' '=apple[' '] don' remove worm' tail segment apple getrandomlocation(set new apple somewhere else del wormcoords[- remove worm' tail segment move the worm by adding segment in the direction it is moving if direction =up