
New Timer( speed, timerActionListener GridPanelĭimension dim = new Dimension( COLS * CELL_SIZE + 1, ROWS * CELL_SIZE + 1 ) New Timer( speed, timerActionListener ).start() If( speed = SPEED_START & totalLength > (ROWS*COLS/2) ) Public void actionPerformed( ActionEvent e ) TimerActionListener = new ActionListener() GrowthRemaining += cells.get( headPosition ).n Įlse if( ! ( cells.get( headPosition ) instanceof Empt圜ell ) )Ĭt( new HeadCell( headPosition, headDirection ) ) If( cells.get( headPosition ) instanceof NumberCell ) TailPosition = us( tailDirection.getIncr() ) Ĭt( new Bod圜ell( prevHead, headDirection, prevDirection ) ) ().beep() ĭirection prevDirection = cells.get( prevHead ).direction įtTitle( String.format( "Length: %,d", totalLength ) ) ĭirection prevTailDirection = cells.get( tailPosition ).direction Ĭt( new Empt圜ell( tailPosition ) ) If( headPosition.isOutOfBounds( 0, 0, COLS, ROWS ) ||Ĭells.get( headPosition ) instanceof Bod圜ell )įtTitle( String.format( "GAME OVER! Length: %,d", totalLength ) ) HeadPosition = us( headDirection.getIncr() ) Next = next.minus( headDirection.getIncr() ) Ĭt( new Bod圜ell( next, headDirection, headDirection ) ) įor( int y = 0 y list = getListOfEmpt圜ells() Ĭt( new NumberCell( list.get( i ), RAND.nextInt(9)+1 ) ) Private static final long serialVersionUID = void actionPerformed( ActionEvent e ) Private static final long serialVersionUID = void actionPerformed( ActionEvent arg0 )ĮtAction( new AbstractAction( "Exit" ) tAction( new AbstractAction( "New Game" ) -įtDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) Private Direction( int incrX, int incrY ) Private static final Font FONT_DIGIT = new Font( "ARIAL", Font.BOLD, 26 ) Private static final Random RAND = new Random() Private static final Color COLOR_NUMBER = Color.black Private static final Color COLOR_GRID_LINES = Color.blue

Private static final Color COLOR_BODY = new Color( 0x7777ff ) // light blue Private static final Color COLOR_HEAD = Color.blue Private static final int SPEED_FASTER = 500

Private static final int SPEED_START = 700 // milliseconds Private static final int QTY_NUMBERS = 5

Private static final int INIT_BODY_LEN = 5 * that game could be re-played at a higher speed. * Another "future" would be to record "events" during play so * difficulties on slower machines (perhaps not?). Paint currently works on my machine but may have * Use a backing image for grid to make paint operation more * When the length gets to 200 the speed increases slightly.

This works well so I post this just for you to enjoy and perhaps learn from. I played the Snake game on an ASCII terminal 40 years ago.
