You defined your TEX array to be 8x8. That means you can't put numbers higher than 7 in there. Try changing line 95 to T=(T+SLICE) MOD 8 Multiplying by 8 almost guarantees that T will be over 7.