/* Project: 3x3x3 Mono LED Cube (r3). * File: MVSnake.cpp * Description: * Movie -- mv_Snake * * Copyright (C) 2014 Marc Symonds * All rights reserved. * * This software may be used and redistributed, with or without * modification, as long as it is understood that this software * is provided as-is without any explicit or implied warranties * of merchantablity or fitness of purpose. */ #include "Arduino.h" #include "Movies.h" #include "DisplayFrame.h" #define SNAKELEN 4 void mv_Snake() { unsigned long ts, t; int ss, sl, sp, i, j, k; byte nx, ny, nz; ts = millis() + DEF_MOVIE_TIME; points[0] = Point((byte)random(0, 3), (byte)random(0, 3), (byte)random(0, 3)); sl = 1; ss = 0; displayFrame[points[0].Pos()] = MAXBRIGHTNESSLEVEL; sp = random(100, 500); while (millis() < ts) { t = millis() + sp; do { nx = points[ss].x; ny = points[ss].y; nz = points[ss].z; switch(random(0, 3)) { case 0: nx = nx + random(0, 3) - 1; break; case 1: ny = ny + random(0, 3) - 1; break; default: nz = nz + random(0, 3) - 1; break; } } while (millis() < t && (nx > 2 || ny > 2 || nz > 2 || displayFrame[POS(nx, ny, nz)] != 0)); while (millis() < t) ; if (nx > 2 || ny > 2 || nz > 2 || displayFrame[POS(nx, ny, nz)] != 0) { for (i = 0 ; i < 27 ; i++) displayFrame[i] = MAXBRIGHTNESSLEVEL; delay(50); df_clear(); points[0] = Point((byte)random(0, 3), (byte)random(0, 3), (byte)random(0, 3)); sl = 1; ss = 0; displayFrame[points[ss].Pos()] = MAXBRIGHTNESSLEVEL; } else { i = ss; for (j = 0 ; j < sl ; j++) { k = points[i].Pos(); if (displayFrame[k] > 0) --displayFrame[k]; if (displayFrame[k] > 0) --displayFrame[k]; if (i == 0) i = SNAKELEN - 1; else --i; } if (++ss >= SNAKELEN) ss = 0; points[ss].Set(nx, ny, nz); displayFrame[points[ss].Pos()] = MAXBRIGHTNESSLEVEL; if (sl < SNAKELEN) ++sl; } } df_clear(); }
Recent Comments