Serpentone sull’NXT

import javax.microedition.lcdui.Graphics;
import lejos.nxt.Button;
import lejos.util.Delay;

/*
 *              1 = su
 *  4 = sinistra       2 = destra
 *              3 = giù
 */

public class Serpentone {

	public static void main(String[] args) {
		Graphics g = new Graphics();

		int p = 1, x = 49, y = 63; // direzione e posizione iniziale

		do {
			// se bottone sinistro, sterza a sinistra
			if (Button.LEFT.isDown()) {
				p -= 1;
				if (p == 0)
					p = 4;
			}
			// se bottone destro, sterza a destra
			if (Button.RIGHT.isDown()) {
				p += 1;
				if (p == 5)
					p = 1;
			}

			// se sta andando in su ed esce rientra da sotto
			if (p == 1) {
				y -= 1;
				if (y == -1)
					y = 63;
			}
			// se sta andando in giù ed esce rientra da sopra
			if (p == 3) {
				y += 1;
				if (y == 64)
					y = 0;
			}
			// se sta andando a destra ed esce rientra da sinistra
			if (p == 2) {
				x += 1;
				if (x == 100)
					x = 0;
			}
			// se sta andando a sinistra ed esce rientra da destra
			if (p == 4) {
				x -= 1;
				if (x == -1)
					x = 99;
			}
			g.drawLine(x, y, x, y);

			Delay.msDelay(1000);
		} while (!Button.ESCAPE.isDown());
	}

}