Oppure

Loading
26/06/14 9:20
pbe
Sto creando un gioco a tempo nel quale hai l'obiettivo di prendere 10 palline nere entro lo scadere del tempo, evitando una pallina verde che si muove in giro per lo schermo.

Il problema qual è? E' che quando si perde per aver avuto contatto con la pallina verde si torna al menu, e se si fa un altra partita il tempo è quello della partita precedentemente non conclusa e non riesco a farlo resettare.
Questo problema è accentuato anche perchè mentre si sta nel menu il tempo inizia già e non aspetta che parta il gioco.

Ecco una versione demo: benzocode.altervista.org/pages/sketch_140624d/…

Ed ecco il codice:

boolean keyup = false;
boolean keyright = false;
boolean keyleft = false;
boolean keydown = false;
 
float x,y,x2,y2,circaX,circaY,enemyX,enemyY,enemyX2,enemyY2,enemyX3,enemyY3,movX,movY,circaXx,circaYy;
int i,p,c=0,t, interval = 60;
PFont font;
String s= "Gioco fatto da P.Be", v="Hai Vinto!", time = "60";

void setup() {
  size(640,360);  
  font = createFont("Arial", 30);
  
}
 
void draw() {
    if (c == 0) {
     time="60";
     i=0;
     p=10;
     x = width/2;
     y = height/2;
     x2=random(16,624);
     y2=random(16,344);
    
     enemyY=50;
     enemyX=20;
     enemyY2=100;
     enemyX2=400;
     enemyY3=300;
     enemyX3=150;
     movX=1;
     movY=1;
    background (255);    
    stroke (0);
    fill (0);
    textSize (14);
    text ("INIZIA A GIOCARE! ", 210,30);

    rect (210, 120, 100, 50);
    stroke (255);
    fill (255);    
    text ("INIZIA", 237, 150);
    stroke (51);
    fill (51);
    line (20,200,560,200);
    textSize (14);
    
    text (s,250,260,300,400);
    if ((((mouseX >= 210) && (mouseX <= 310)) && ((mouseY >= 120) && (mouseY <= 170))) && (mousePressed && (mouseButton == LEFT))) {   // se il mouse viene premuto (all'interno del rettangolo) il gioco termine e la finestra viene chiusa      
      c = 5;
    }
  }
  if (c == 5) {
  time="60";
  background(195);
    fill(0);
   ellipse(x2,y2,16,16);
        fill(123,213,123);
          ellipse(enemyX,enemyY,16,16);
    fill(255);
  ellipse(x,y,16,16);
  fill(0);
  if(i<10){
  interval = 60;

   t = interval-int(millis()/1000);
    time = nf(t , 3);
    if(t == 0){
    text("GAME OVER",210,30);
    rect (210, 70, 100, 50);
    fill (255);    
    text ("MENU", 235, 100);
     i=10;
    v="";
    p=0;
    if ((((mouseX >= 210) && (mouseX <= 310)) && ((mouseY >= 70) && (mouseY <= 120))) && (mousePressed && (mouseButton == LEFT))) {   // se il mouse viene premuto (all'interno del rettangolo) il gioco termine e la finestra viene chiusa      
     c=0;
    }

    }
     else
   {
   moveEnemy(); 
   text("Secondi rimanenti: "+time, 10, 20);
}
  }

  text("Pallini rimasti: "+p, 400, 20);
  if(i<10)
  {
  if (keyup) y--;
  if (keydown) y++;
  if (keyleft) x--;
  if (keyright) x++;
  }
  circaX=x-x2;
  circaY=y-y2;
  if(i<10)
  {
   if(((circaX<=16)&&(circaX>=-16))&&((circaY<16)&&(circaY>=-16)))
   {
    p=p-1;
    i++;
    x2=random(16,624);
    y2=random(16,344);
   } 
  }
  else
  {
   time="";
   text(v, 10, 30);  
  }
  circaXx=x-enemyX;
  circaYy=y-enemyY;
   if((((circaXx<=16)&&(circaXx>=-16))&&((circaYy<16)&&(circaYy>=-16)))||t==0)
   {
    text("GAME OVER",210,30);
    rect (210, 70, 100, 50);
    fill (255);    
    text ("MENU", 235, 100);
    if ((((mouseX >= 210) && (mouseX <= 310)) && ((mouseY >= 70) && (mouseY <= 120))) && (mousePressed && (mouseButton == LEFT))) {   // se il mouse viene premuto (all'interno del rettangolo) il gioco termine e la finestra viene chiusa      
     c=0;
    }
    i=10;
    v="";
    p=0;
   } 
   else
   {
   moveEnemy();
   }
  
 }
}
 
void keyPressed() {
 
  if (key == CODED) {
    if (keyCode == UP) keyup = true; 
    if (keyCode == DOWN) keydown = true; 
    if (keyCode == LEFT) keyleft = true; 
    if (keyCode == RIGHT) keyright = true; 

  }
}
 
void keyReleased() {
  if (key == CODED) {
    if (keyCode == UP) keyup = false; 
    if (keyCode == DOWN) keydown = false; 
    if (keyCode == LEFT) keyleft = false; 
    if (keyCode == RIGHT) keyright = false; 
  }
}

void moveEnemy()
{
  enemyX+=movX;
  enemyY+=movY;
  if(enemyX==16)
  {
    movX=1;
  } 
  if(enemyX==624)
  {
    movX=-1;
  } 
  if(enemyY==16)
  {
    movY=1;
  } 
  if(enemyY==344)
  {
    movY=-1;
  } 
}

Spero che mi possiate aiutare prima possibile.
Grazie in anticipo.:k:
aaa
26/06/14 17:01
Roby94
Come ti ho suggerito in precedenza dovresti prima riordinare il codice, si denota uno schema molto casuale e caotico in questo che rende difficile individuare anche un errore cosi basilare.
aaa
26/06/14 18:05
pbe
Forse non ho capito bene come fare a sintetizzare il codice.
Quale può essere un buon metodo? Consigli?
aaa
26/06/14 18:08
pbe
Mi avevi consigliato di usare delle classi ma non saprei in che modo applicarle (anche avendo iniziato da pochi giorni con Processing).
Qualcuno mi potrebbe illuminare?
Grazie mille
aaa
26/06/14 22:08
Roby94
Non hai nessuna esperienza per quanto riguarda la OOP?
it.wikipedia.org/wiki/…
aaa
27/06/14 7:45
pbe
Oggi ci provo , sistemo , e poi ci risentiamo
aaa
02/07/14 9:35
pbe
Eccomi qua di nuovo, ho cercato di sistemare il mio listato e ho aggiunto alcuni oggetti e classi per sistemare il codice.

Il sorgente secondo me è già più semplificato come da richiesta, prima di mostrarvelo ricordo i bug del mini-gioco: ho messo un timer nel gioco ma il problema è che parte già dal menù e quando finisce una partita e inizia un'altra il timer prosegue senza ritornare a 60 secondi ma va in negativo (-001, -002, -003,...etc). Secondo me la causa sta tutta nella funzione "millis()" la quale tiene il tempo dall'inizio del programma e non dall'inizio della partita.

Come faccio a sistemarlo?

Ecco il listato:

boolean keyup = false;
boolean keyright = false;
boolean keyleft = false;
boolean keydown = false;
boolean eMove=true;
boolean goTime=false;
boolean move=true;

//PEDINE
Player player;
Food food;
Enemy enemy;

float circaX,circaY,movX,movY,circaXx,circaYy;
int i,p=10,c=0,m;
String s= "Gioco fatto da P.Be", v="Hai Vinto!";

//VARIABILI PER IL  COUNTDOWN
PFont font;
String time="060";
int t, interval=60;
int start;

void setup() {
  size(640,360);  
  font = createFont("Arial", 30);
  player = new Player(width/2, height/2, 16);
  food = new Food(random(16,624), random(16,344), 16);
  enemy = new Enemy(20,50,16);
}
 
void draw() {
    if (c == 0) {
     start=millis();
     enemy.x=20;
     enemy.y=50;
     food.x=random(16,624);
     food.y=random(16,344);
     player.x=width/2;
     player.y=height/2;
     
     eMove=true;
     goTime=false;
     move=true;
     i=0;
     p=10;
     movX=2;
     movY=2;
    background (255);    
    stroke (0);
    fill (0);
    textSize (14);
    text ("INIZIA A GIOCARE! ", 210,30);

    rect (210, 120, 100, 50);
    stroke (255);
    fill (255);    
    text ("INIZIA", 237, 150);
    stroke (51);
    fill (51);
    line (20,200,560,200);
    textSize (14);
    
    text (s,250,260,300,400);
    if ((((mouseX >= 210) && (mouseX <= 310)) && ((mouseY >= 120) && (mouseY <= 170))) && (mousePressed && (mouseButton == LEFT))) {   
      c = 5;
      i=0;
      p=10;
      goTime=true;
    }
  }
  if (c == 5) {
    
     i=0;
     interval=60;
     
  background(195);    
  fill(0);
  
  food.display();
  player.display();
  enemy.display();
  enemy.moveEnemy(); 
  
  if((i<10)&&(c==5)){
   if(goTime==true)
   {
    t = interval-int(millis()/1000);
    time = nf(t,3);
    if(t == 0){
    interval=interval+60;
    fill(255);
    text("GAME OVER",210,30);
    fill(0);
    rect (210, 70, 100, 50);
    fill (255);    
    text ("MENU", 235, 100);
    i=10;
    v="";
    p=0;
    move=false;
    eMove=false;
    goTime=false;
    if ((((mouseX >= 210) && (mouseX <= 310)) && ((mouseY >= 70) && (mouseY <= 120))) && (mousePressed && (mouseButton == LEFT))) {   // se il mouse viene premuto (all'interno del rettangolo) il gioco termine e la finestra viene chiusa      
     c=0;
    }
    }
    }

   fill(0);
   text("Secondi rimanenti: "+time, 10, 20);
   
  }
  fill(0);
  text("Pallini rimasti: "+p, 400, 20);
  if(i<10)
  {
   if(move==true)
   {
    if (keyup) player.y--;
    if (keydown) player.y++;
    if (keyleft) player.x--;
    if (keyright) player.x++;
   }
  }
  circaX=player.x-food.x;
  circaY=player.y-food.y;
  if(i<10)
  {
   if(((circaX<=15)&&(circaX>=-15))&&((circaY<15)&&(circaY>=-15)))
   {
    p=p-1;
    i++;
    food.x=random(16,624);
    food.y=random(16,344);
   } 
  }
  else
  {
   text(v, 10, 30);  
  }
  circaXx=player.x-enemy.x;
  circaYy=player.y-enemy.y;
   if((((circaXx<=15)&&(circaXx>=-15))&&((circaYy<15)&&(circaYy>=-15)))||(t==0))
   {
    move=false;
    eMove=false;
    goTime=false;
    i=10;
    v="";
    fill(0);
    text("GAME OVER",210,30);
    fill(0);
    rect (210, 70, 100, 50);
    fill (255);    
    text ("MENU", 235, 100);
    if ((((mouseX >= 210) && (mouseX <= 310)) && ((mouseY >= 70) && (mouseY <= 120))) && (mousePressed && (mouseButton == LEFT))) {   // se il mouse viene premuto (all'interno del rettangolo) il gioco termine e la finestra viene chiusa      
     c=0;
    }
   } 
  
 }
}
 
void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) keyup = true; 
    if (keyCode == DOWN) keydown = true; 
    if (keyCode == LEFT) keyleft = true; 
    if (keyCode == RIGHT) keyright = true; 
  
 }
}
 
void keyReleased() {
  if (key == CODED) {
    if (keyCode == UP) keyup = false; 
    if (keyCode == DOWN) keydown = false; 
    if (keyCode == LEFT) keyleft = false; 
    if (keyCode == RIGHT) keyright = false; 
  }
}


class Enemy{
  float x,y,d;
  
  Enemy(float xE, float yE, float diam)
  {
    x=xE;
    y=yE;
    d=diam;
  }
  
  void display()
  {
    fill(123,213,123);
    ellipse(x,y,d,d);
  }
  
  void moveEnemy()
  {
   if(eMove==true)
   {
    x+=movX;
    y+=movY;
    if(x==16)
    {
      movX=2;
    } 
    if(x==624)
    {
      movX=-2;
    } 
    if(y==16)
    {
      movY=2;
    } 
    if(y==344)
    {
      movY=-2;
    } 
  }
 }
}

class Player{
  float x,y,d;
  
  Player(float xx, float yy, float diam){
    x=xx;
    y=yy;
    d=diam;
  }
  
  void display(){
    fill(255);
    ellipse(x,y,d,d);
  }
}

class Food{
 float x,y,d;
 
 Food(float xF, float yF, float diam){
     x=xF;
     y=yF;
     d=diam;
 }
 
 void display(){
  fill(0);
  ellipse(x,y,d,d);
 }
} 



Attendo risposte prima possibile :k: Grazie
aaa
02/07/14 9:50
TheDarkJuster
puoi fare un classe game per ordinare meglio e rendere quel codice più mantenibile. Comunque secondo me non hai rimesso a 60 interval...............
aaa