1111/**
1212 * A simple "Snake" game implementation for a 8x8 playing field.
1313 * <p>
14- * Hold the center key for one second to exit.
14+ * Press the select key or hold the center key for one second to exit.
1515 * <p>
1616 * This class doesn't include a main() method; please find an examples for wiring this up in the waveshare 14972
1717 * dispay hat demo.
@@ -42,6 +42,7 @@ public class Snake {
4242 private Entity [][] arena ;
4343 private List <Segment > body = new ArrayList <>();
4444 private long longPressStart = Long .MAX_VALUE ;
45+ private boolean exit = false ;
4546
4647 public Snake (GraphicsDisplay display , GameController controller ) {
4748 this .display = display ;
@@ -52,6 +53,7 @@ public Snake(GraphicsDisplay display, GameController controller) {
5253 assignKey (GameController .Key .LEFT , value -> processDirectionalKey (value , -1 , 0 ));
5354 assignKey (GameController .Key .RIGHT , value -> processDirectionalKey (value , 1 , 0 ));
5455 assignKey (GameController .Key .CENTER , value -> processCenterKey (value ));
56+ assignKey (GameController .Key .SELECT , _ -> { exit = true ; });
5557
5658 int displayWidth = display .getWidth ();
5759 int displayHeight = display .getHeight ();
@@ -122,10 +124,13 @@ private void renderColor(int x, int y, int color) {
122124
123125 public void run () {
124126 initialize ();
125- while (System . currentTimeMillis () - longPressStart < 1000 ) {
127+ while (! exit ) {
126128 deferredDelay .setDelayMillis (stepTimeMillis );
127129 step ();
128130 deferredDelay .materializeDelay ();
131+ if (System .currentTimeMillis () - longPressStart > 1000 ) {
132+ exit = true ;
133+ }
129134 }
130135 display .fillRect (0 , 0 , display .getWidth (), display .getHeight (), 0xff000000 );
131136 for (Map .Entry <ListenableOnOffRead <?>, Consumer <Boolean >> entry : keys .entrySet ()) {
0 commit comments