Refresh page in for loop

edited May 2015 in How To...

first let me say, sorry for english, i'am french and beguiner in programation. So here , I have a problem with my program . It is a "color memory" game . It only remains for me one thing to do is to display the colors one by one to remember . Except that I do this with a "for" loop , and the loop "for" refreshed the screen only after having finished . So I wanted to know if there was a way to force the refresh ? ( If you want to see all my code called me ) , thank you in advance.

Tagged:

Answers

  • no, the screen is only refreshed once at the end of draw()

    to animate something use the fact that draw() in itself loops all the time

    so put the for-loop var in an extra var and just increment it at the end of draw (unteil it reached the upper bound)

  • I thinks i understand the idea , i try to make this now ! Merci beaucoup !!!

  • Your advice works very well , but now I have a new problem .... when I have two same color in a row , I can not seem to make the" blinking effect" ... if you had an other idea , I listen carefully :)

    • You should implement "states" in your sketch.
    • Each state determines which action to take inside draw().
    • In your case, you need DISPLAY & BLINK states.
    • Take a look at this recent discussion to see what I mean:

    http://forum.processing.org/two/discussion/10674/counting-keypressed-and-using-this#latest

  • Thank you for the info, but I 'm trying , but it still does not working .... like I said I'm not very good in programing ... I can show you what I have do now? ( if yes , please tell me how to put my code to make it as more readable for you )

  • edited May 2015

    i can't show all my code in one time...

  • edited May 2015

    You need to simplify your code just for demonstrating what's wrong.

  • edited May 2015

    int m, i, c, p, click, level, n, boule, s, w, temps, a, k; int[] y = new int[20]; int[] x = new int[20]; int[] etat= {0, 0, 0, 0}; void setup() { frameRate(60) ; size(700, 700); p=-1; click=0; level=1; n=-1; boule=0; w=10; temps=0; a=1; k=5; background(25, 25, 25); fill(150, 0, 0); rect(140, 140, 200, 200); fill(0, 150, 0); rect(140, 360, 200, 200); fill(138, 150, 0); rect(360, 140, 200, 200); fill(0, 0, 150); rect(360, 360, 200, 200); choixdescouleurs(); } void draw() { if (etat[0] == 0) { fill(155, 0, 0); rect(140, 140, 200, 200); } else { fill(255, 0, 0); rect(140, 140, 200, 200); } if (etat[1] == 0) { fill(138, 150, 0); rect(360, 140, 200, 200); } else { fill(235, 255, 0); rect(360, 140, 200, 200); } if (etat[2] == 0) { fill(0, 150, 0); rect(140, 360, 200, 200); } else { fill(0, 255, 0); rect(140, 360, 200, 200); } if (etat[3] == 0) { fill(0, 0, 155); rect(360, 360, 200, 200); } else { fill(0, 0, 255); rect(360, 360, 200, 200); } if ((level==a )&&(i<=k+1)) { etat[0]=0; etat[1]=0; etat[2]=0; etat[3]=0; switch(x[i]) { case 1: etat[0]=1; break; case 2 : etat[1]=1; break; case 3: etat[2]=1; break; case 4 : etat[3]=1; break; } i++; delay(700); } } void delay(int delay) { int time = millis(); while (millis () - time <= delay); } void choixdescouleurs() { // fonction permettant de choisir des couleurs aléatoire pour chaque niveau delay(100); if (level==1 ) { for (int i = 0; i<5; i++) { x[i] = 1 + int( random(4)); print (x[i]); } } }

  • sorry i can't make better ...

  • Placing the code between `` isn't the right way to post formatted code. :-\"
    In order to make your code logic easier, you should consider implementing a class for your colored rect().

  • sorry , like i say i'am a beguiner in programming , i make this program for the final exam of shcool ( le BAC ) , so if i make class , i should be able to present it ^^

    and sorry for my code posting ^^

  • i think you need to use background(0); at the beginning of draw()

    also when you set a rect as ON / show its color say stroke (255); when it's not on, say noStroke(); directly before the code line with rect

    not tested

    ;-)

  • Sorry I forgot to notify you that it was good, I make what I wanted :) . Thank you all for your help, is very friendly. and hello from France : p

  • edited May 2015

    I was doing a sample, but haven't finished yet.
    Since you say you've made it, just wanna post it the way it is now: :D

    static final int FPS = 30, ON = FPS, OFF = FPS/2;
    static final int BUTTONS = 4;
    
    final Button[] btns = new Button[BUTTONS];
    boolean isBlinking, isCPU = true;
    
    void setup() {
      size(700, 700, JAVA2D);
    
      smooth(4);
      frameRate(FPS);
      rectMode(CORNER);
    
      strokeWeight(2);
      stroke(0);
      background(050);
    
      btns[0] = new Button(140, 140, #FF0000, #A00000);
      btns[1] = new Button(360, 140, #C0FF00, #90A000);
      btns[2] = new Button(140, 360, #00FF00, #00A000);
      btns[3] = new Button(360, 360, #0000FF, #0000A0);
    }
    
    void draw() {
      for (Button b : btns) {
        if (b.timeToTurnOff())  b.isOn = false;
        b.display();
      }
    }
    
    void mousePressed() {
    }
    
    class Button {
      static final int DIAM = 200;
    
      int x, y;
      color on, off;
      boolean isOn;
      int endFrame;
    
      Button(int $x, int $y, color $on, color $off) {
        x = $x;
        y = $y;
        on = $on;
        off = $off;
      }
    
      void display() {
        fill(isOn? on : off);
        rect(x, y, DIAM, DIAM);
      }
    
      boolean timeToTurnOff() {
        return isOn & frameCount >= endFrame;
      }
    
      boolean mouseOver() {
        return mouseX > x & mouseX < x+DIAM & mouseY > y & mouseY < y+DIAM;
      }
    }
    
  • edited May 2015

    Bonjour Thom_Geek, je parle français et l'anglais très bien, et si vous voulez que je traduis des choses je peux. Aussi je peut t'aider avec ton project! Je regarde ton code maintenant et je ne comprend pas vraiment ce que tu veux faire. Est-ce que le program fait un pattern des coulours et le utilisateur doit se souvenir? Si tu veut me contacter personnellement : ryan@rywestech.com. Je veut te aider.

  • haha thank you anyway GoTooLoop ;)

    Ben je n'ai plus de problème , j'ai réussi à le résoudre. Alors en effet le joueur doit mémoriser des couleurs dans un certain ordre. Mais merci pour la proposition de traduction :).

  • Now I have another problem . I can not put it under a form of aplication since I adding the sound , someone have an idea?

  • Answer ✓

    I can not put it under a form of aplication

    what do you mean? In a website?

Sign In or Register to comment.