SnakeGame -> GameFrame ( JFRAME) -> GamePanel(JPanel,ActionListener)
- SnakeGame constructs the Game frame
- Game frame add screen attributes and add the gamepanel
- Jpanel controls the game and defines the below
- screen size
- unit of each grid
- initial snake size
- key press delay
- arrays to hold snake body after eating apple
- key system based on QWES
- GamePanel constructs below
- init random
- init keyadapter inner class
- background
- start game
start game - > adds new apple in random x,y sets running to true starts timer for action listener
new apple will randomly set apple at x, y
paintcomponent -> calls super paint component and is responsible for drawing the components draws new apple on x,y draws body of snake draws score
move() will shift the array value one value over x[i] = x[i-1] also adds a switch case for direction ( QWES)
checkapple if head of snake is at apple x,y, then eat apple create new random apple
check collisons check if array is colliding inside itself or at the edges and set running as false stop the timer
game over -> add graphics to show strings
in overridden action Performed method check if running then move , cehck apples , check collison or game over -> repaint() ( super)
keypanel is added t listen to the keys and manage direction as specified above in QWES based on VK arrow keys