# How to reset a sketch after a certain amount of time?

Hello everyone, I’ve been trying to get a sketch to reset after one minute using millis in the code but i’m having a hard time figuring it out. Does anyone have any ideas?

``````
void setup()
{
zoom = 6;
ss = int(1280/zoom);
jj = int(1024/zoom);
game = new int[ss][jj][2];
running = true;
size(1280,1024, P2D);
frameRate(33);
background(89, 66, 244);
noStroke();
fill(200);
}

void draw()
{
decayTimer();
scale(zoom);
drawing();
life();
}

void life() {
if ( running ) {
for (int x = 0; x < ss; x=x+1) {
for (int y = 0; y < jj; y=y+1) {
int count = neighbors(x, y);
if (game[x][y][0] < 100) {
if (count == 3) {
game[x][y][1] = top;
}
}
else if (count < 2 || count > 3) {
game[x][y][1] -= decay;
}
}
}
}
}

void drawing() {
for (int x = 0; x < ss; x++) {
for (int y = 0; y < jj; y++) {
if (game[x][y][1] > lowThresh) {
game[x][y][0] = top;
fill(game[x][y][1]);
rect(x, y, 1, 1);
if(random(10)>randomDecay) game[x][y][0] -=6;
}
else {
if (game[x][y][0]>=0) game[x][y][0] -=3;
}
}
}
}

void decayTimer() {
if (millis() - lastDecayTime > decayInterval) {
if (decaySign) decay++;
else decay--;
if (decay<decayBottom || decay>decayTop) decaySign =  !decaySign;
lastDecayTime=millis();
println(decay);
}
}

int neighbors(int x, int y)
{
int total=0;
if (game[(x + 1) % ss][y][0] > highThresh) total++;
if (game[x][(y + 1) % jj][0] > highThresh) total++;
if (game[(x + ss - 1) % ss][y][0] > highThresh) total++;
if (game[x][(y + jj - 1) % jj][0] > highThresh) total++;
if (game[(x + 1) % ss][(y + 1) % jj][0] > highThresh) total++;
if (game[(x + ss - 1) % ss][(y + 1) % jj][0] > highThresh) total++;
if (game[(x + ss - 1) % ss][(y + jj - 1) % jj][0] > highThresh) total++;
if (game[(x + 1) % ss][(y + jj - 1) % jj][0] > highThresh) total++;
}
void keyPressed() {
if ( key == 'c' ) {

game = new int[ss][jj][2];
running = false;
}
else {

running = !running;
}

if ( running ) {
fill(top);
}
else {
fill(0);
}
}

int[][] pattern = {
{
0, 255, 255
}
,
{
255, 255, 0
}
,
{
0, 255, 0
}
};

void mouseDragged() {
if (mouseX>40 && mouseX < width-40 && mouseY > 40 && mouseY < height-40) {
for ( int x = 0; x < pattern[0].length; x++ ) {
for ( int y = 0; y < pattern.length; y++ ) {
game[int(mouseX/zoom+x)][int(mouseY/zoom+y)][1] = pattern[y][x];
}
}
}
}
``````
``````int time;

void setup(){
size(200,200);
reset();
}

void draw(){
if( time > millis() ){
reset();
}
// ...
}

void reset(){
// ...
time = millis() + 60000;
}
``````
1 Like