Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Altri Linguaggi - Gioco a tempo | Processing
Forum - Altri Linguaggi - Gioco a tempo | Processing

Avatar
pbe (Normal User)
Pro


Messaggi: 131
Iscritto: 24/05/2014

Segnala al moderatore
Postato alle 11:20
Giovedì, 26/06/2014
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: http://benzocode.altervista.org/pages/sketch_140624d/gioco ...

Ed ecco il codice:

Codice sorgente - presumibilmente Java

  1. boolean keyup = false;
  2. boolean keyright = false;
  3. boolean keyleft = false;
  4. boolean keydown = false;
  5.  
  6. float x,y,x2,y2,circaX,circaY,enemyX,enemyY,enemyX2,enemyY2,enemyX3,enemyY3,movX,movY,circaXx,circaYy;
  7. int i,p,c=0,t, interval = 60;
  8. PFont font;
  9. String s= "Gioco fatto da P.Be", v="Hai Vinto!", time = "60";
  10.  
  11. void setup() {
  12.   size(640,360);  
  13.   font = createFont("Arial", 30);
  14.  
  15. }
  16.  
  17. void draw() {
  18.     if (c == 0) {
  19.      time="60";
  20.      i=0;
  21.      p=10;
  22.      x = width/2;
  23.      y = height/2;
  24.      x2=random(16,624);
  25.      y2=random(16,344);
  26.    
  27.      enemyY=50;
  28.      enemyX=20;
  29.      enemyY2=100;
  30.      enemyX2=400;
  31.      enemyY3=300;
  32.      enemyX3=150;
  33.      movX=1;
  34.      movY=1;
  35.     background (255);    
  36.     stroke (0);
  37.     fill (0);
  38.     textSize (14);
  39.     text ("INIZIA A GIOCARE! ", 210,30);
  40.  
  41.     rect (210, 120, 100, 50);
  42.     stroke (255);
  43.     fill (255);    
  44.     text ("INIZIA", 237, 150);
  45.     stroke (51);
  46.     fill (51);
  47.     line (20,200,560,200);
  48.     textSize (14);
  49.    
  50.     text (s,250,260,300,400);
  51.     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      
  52.       c = 5;
  53.     }
  54.   }
  55.   if (c == 5) {
  56.   time="60";
  57.   background(195);
  58.     fill(0);
  59.    ellipse(x2,y2,16,16);
  60.         fill(123,213,123);
  61.           ellipse(enemyX,enemyY,16,16);
  62.     fill(255);
  63.   ellipse(x,y,16,16);
  64.   fill(0);
  65.   if(i<10){
  66.   interval = 60;
  67.  
  68.    t = interval-int(millis()/1000);
  69.     time = nf(t , 3);
  70.     if(t == 0){
  71.     text("GAME OVER",210,30);
  72.     rect (210, 70, 100, 50);
  73.     fill (255);    
  74.     text ("MENU", 235, 100);
  75.      i=10;
  76.     v="";
  77.     p=0;
  78.     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      
  79.      c=0;
  80.     }
  81.  
  82.     }
  83.      else
  84.    {
  85.    moveEnemy();
  86.    text("Secondi rimanenti: "+time, 10, 20);
  87. }
  88.   }
  89.  
  90.   text("Pallini rimasti: "+p, 400, 20);
  91.   if(i<10)
  92.   {
  93.   if (keyup) y--;
  94.   if (keydown) y++;
  95.   if (keyleft) x--;
  96.   if (keyright) x++;
  97.   }
  98.   circaX=x-x2;
  99.   circaY=y-y2;
  100.   if(i<10)
  101.   {
  102.    if(((circaX<=16)&&(circaX>=-16))&&((circaY<16)&&(circaY>=-16)))
  103.    {
  104.     p=p-1;
  105.     i++;
  106.     x2=random(16,624);
  107.     y2=random(16,344);
  108.    }
  109.   }
  110.   else
  111.   {
  112.    time="";
  113.    text(v, 10, 30);  
  114.   }
  115.   circaXx=x-enemyX;
  116.   circaYy=y-enemyY;
  117.    if((((circaXx<=16)&&(circaXx>=-16))&&((circaYy<16)&&(circaYy>=-16)))||t==0)
  118.    {
  119.     text("GAME OVER",210,30);
  120.     rect (210, 70, 100, 50);
  121.     fill (255);    
  122.     text ("MENU", 235, 100);
  123.     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      
  124.      c=0;
  125.     }
  126.     i=10;
  127.     v="";
  128.     p=0;
  129.    }
  130.    else
  131.    {
  132.    moveEnemy();
  133.    }
  134.  
  135.  }
  136. }
  137.  
  138. void keyPressed() {
  139.  
  140.   if (key == CODED) {
  141.     if (keyCode == UP) keyup = true;
  142.     if (keyCode == DOWN) keydown = true;
  143.     if (keyCode == LEFT) keyleft = true;
  144.     if (keyCode == RIGHT) keyright = true;
  145.  
  146.   }
  147. }
  148.  
  149. void keyReleased() {
  150.   if (key == CODED) {
  151.     if (keyCode == UP) keyup = false;
  152.     if (keyCode == DOWN) keydown = false;
  153.     if (keyCode == LEFT) keyleft = false;
  154.     if (keyCode == RIGHT) keyright = false;
  155.   }
  156. }
  157.  
  158. void moveEnemy()
  159. {
  160.   enemyX+=movX;
  161.   enemyY+=movY;
  162.   if(enemyX==16)
  163.   {
  164.     movX=1;
  165.   }
  166.   if(enemyX==624)
  167.   {
  168.     movX=-1;
  169.   }
  170.   if(enemyY==16)
  171.   {
  172.     movY=1;
  173.   }
  174.   if(enemyY==344)
  175.   {
  176.     movY=-1;
  177.   }
  178. }



Spero che mi possiate aiutare prima possibile.
Grazie in anticipo.:k:

PM Quote
Avatar
Roby94 (Member)
Guru


Messaggi: 1170
Iscritto: 28/12/2009

Segnala al moderatore
Postato alle 19:01
Giovedì, 26/06/2014
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.

PM Quote
Avatar
pbe (Normal User)
Pro


Messaggi: 131
Iscritto: 24/05/2014

Segnala al moderatore
Postato alle 20:05
Giovedì, 26/06/2014
Forse non ho capito bene come fare a sintetizzare il codice.
Quale può essere un buon metodo? Consigli?

PM Quote
Avatar
pbe (Normal User)
Pro


Messaggi: 131
Iscritto: 24/05/2014

Segnala al moderatore
Postato alle 20:08
Giovedì, 26/06/2014
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

PM Quote
Avatar
Roby94 (Member)
Guru


Messaggi: 1170
Iscritto: 28/12/2009

Segnala al moderatore
Postato alle 0:08
Venerdì, 27/06/2014
Non hai nessuna esperienza per quanto riguarda la OOP?
http://it.wikipedia.org/wiki/Programmazione_orientata_agli ...

PM Quote
Avatar
pbe (Normal User)
Pro


Messaggi: 131
Iscritto: 24/05/2014

Segnala al moderatore
Postato alle 9:45
Venerdì, 27/06/2014
Oggi ci provo , sistemo , e poi ci risentiamo

PM Quote
Avatar
pbe (Normal User)
Pro


Messaggi: 131
Iscritto: 24/05/2014

Segnala al moderatore
Postato alle 11:35
Mercoledì, 02/07/2014
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:

Codice sorgente - presumibilmente Java

  1. boolean keyup = false;
  2. boolean keyright = false;
  3. boolean keyleft = false;
  4. boolean keydown = false;
  5. boolean eMove=true;
  6. boolean goTime=false;
  7. boolean move=true;
  8.  
  9. //PEDINE
  10. Player player;
  11. Food food;
  12. Enemy enemy;
  13.  
  14. float circaX,circaY,movX,movY,circaXx,circaYy;
  15. int i,p=10,c=0,m;
  16. String s= "Gioco fatto da P.Be", v="Hai Vinto!";
  17.  
  18. //VARIABILI PER IL  COUNTDOWN
  19. PFont font;
  20. String time="060";
  21. int t, interval=60;
  22. int start;
  23.  
  24. void setup() {
  25.   size(640,360);  
  26.   font = createFont("Arial", 30);
  27.   player = new Player(width/2, height/2, 16);
  28.   food = new Food(random(16,624), random(16,344), 16);
  29.   enemy = new Enemy(20,50,16);
  30. }
  31.  
  32. void draw() {
  33.     if (c == 0) {
  34.      start=millis();
  35.      enemy.x=20;
  36.      enemy.y=50;
  37.      food.x=random(16,624);
  38.      food.y=random(16,344);
  39.      player.x=width/2;
  40.      player.y=height/2;
  41.      
  42.      eMove=true;
  43.      goTime=false;
  44.      move=true;
  45.      i=0;
  46.      p=10;
  47.      movX=2;
  48.      movY=2;
  49.     background (255);    
  50.     stroke (0);
  51.     fill (0);
  52.     textSize (14);
  53.     text ("INIZIA A GIOCARE! ", 210,30);
  54.  
  55.     rect (210, 120, 100, 50);
  56.     stroke (255);
  57.     fill (255);    
  58.     text ("INIZIA", 237, 150);
  59.     stroke (51);
  60.     fill (51);
  61.     line (20,200,560,200);
  62.     textSize (14);
  63.    
  64.     text (s,250,260,300,400);
  65.     if ((((mouseX >= 210) && (mouseX <= 310)) && ((mouseY >= 120) && (mouseY <= 170))) && (mousePressed && (mouseButton == LEFT))) {  
  66.       c = 5;
  67.       i=0;
  68.       p=10;
  69.       goTime=true;
  70.     }
  71.   }
  72.   if (c == 5) {
  73.    
  74.      i=0;
  75.      interval=60;
  76.      
  77.   background(195);    
  78.   fill(0);
  79.  
  80.   food.display();
  81.   player.display();
  82.   enemy.display();
  83.   enemy.moveEnemy();
  84.  
  85.   if((i<10)&&(c==5)){
  86.    if(goTime==true)
  87.    {
  88.     t = interval-int(millis()/1000);
  89.     time = nf(t,3);
  90.     if(t == 0){
  91.     interval=interval+60;
  92.     fill(255);
  93.     text("GAME OVER",210,30);
  94.     fill(0);
  95.     rect (210, 70, 100, 50);
  96.     fill (255);    
  97.     text ("MENU", 235, 100);
  98.     i=10;
  99.     v="";
  100.     p=0;
  101.     move=false;
  102.     eMove=false;
  103.     goTime=false;
  104.     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      
  105.      c=0;
  106.     }
  107.     }
  108.     }
  109.  
  110.    fill(0);
  111.    text("Secondi rimanenti: "+time, 10, 20);
  112.    
  113.   }
  114.   fill(0);
  115.   text("Pallini rimasti: "+p, 400, 20);
  116.   if(i<10)
  117.   {
  118.    if(move==true)
  119.    {
  120.     if (keyup) player.y--;
  121.     if (keydown) player.y++;
  122.     if (keyleft) player.x--;
  123.     if (keyright) player.x++;
  124.    }
  125.   }
  126.   circaX=player.x-food.x;
  127.   circaY=player.y-food.y;
  128.   if(i<10)
  129.   {
  130.    if(((circaX<=15)&&(circaX>=-15))&&((circaY<15)&&(circaY>=-15)))
  131.    {
  132.     p=p-1;
  133.     i++;
  134.     food.x=random(16,624);
  135.     food.y=random(16,344);
  136.    }
  137.   }
  138.   else
  139.   {
  140.    text(v, 10, 30);  
  141.   }
  142.   circaXx=player.x-enemy.x;
  143.   circaYy=player.y-enemy.y;
  144.    if((((circaXx<=15)&&(circaXx>=-15))&&((circaYy<15)&&(circaYy>=-15)))||(t==0))
  145.    {
  146.     move=false;
  147.     eMove=false;
  148.     goTime=false;
  149.     i=10;
  150.     v="";
  151.     fill(0);
  152.     text("GAME OVER",210,30);
  153.     fill(0);
  154.     rect (210, 70, 100, 50);
  155.     fill (255);    
  156.     text ("MENU", 235, 100);
  157.     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      
  158.      c=0;
  159.     }
  160.    }
  161.  
  162.  }
  163. }
  164.  
  165. void keyPressed() {
  166.   if (key == CODED) {
  167.     if (keyCode == UP) keyup = true;
  168.     if (keyCode == DOWN) keydown = true;
  169.     if (keyCode == LEFT) keyleft = true;
  170.     if (keyCode == RIGHT) keyright = true;
  171.  
  172.  }
  173. }
  174.  
  175. void keyReleased() {
  176.   if (key == CODED) {
  177.     if (keyCode == UP) keyup = false;
  178.     if (keyCode == DOWN) keydown = false;
  179.     if (keyCode == LEFT) keyleft = false;
  180.     if (keyCode == RIGHT) keyright = false;
  181.   }
  182. }
  183.  
  184.  
  185. class Enemy{
  186.   float x,y,d;
  187.  
  188.   Enemy(float xE, float yE, float diam)
  189.   {
  190.     x=xE;
  191.     y=yE;
  192.     d=diam;
  193.   }
  194.  
  195.   void display()
  196.   {
  197.     fill(123,213,123);
  198.     ellipse(x,y,d,d);
  199.   }
  200.  
  201.   void moveEnemy()
  202.   {
  203.    if(eMove==true)
  204.    {
  205.     x+=movX;
  206.     y+=movY;
  207.     if(x==16)
  208.     {
  209.       movX=2;
  210.     }
  211.     if(x==624)
  212.     {
  213.       movX=-2;
  214.     }
  215.     if(y==16)
  216.     {
  217.       movY=2;
  218.     }
  219.     if(y==344)
  220.     {
  221.       movY=-2;
  222.     }
  223.   }
  224.  }
  225. }
  226.  
  227. class Player{
  228.   float x,y,d;
  229.  
  230.   Player(float xx, float yy, float diam){
  231.     x=xx;
  232.     y=yy;
  233.     d=diam;
  234.   }
  235.  
  236.   void display(){
  237.     fill(255);
  238.     ellipse(x,y,d,d);
  239.   }
  240. }
  241.  
  242. class Food{
  243.  float x,y,d;
  244.  
  245.  Food(float xF, float yF, float diam){
  246.      x=xF;
  247.      y=yF;
  248.      d=diam;
  249.  }
  250.  
  251.  void display(){
  252.   fill(0);
  253.   ellipse(x,y,d,d);
  254.  }
  255. }



Attendo risposte prima possibile :k: Grazie

PM Quote
Avatar
TheDarkJuster (Member)
Guru^2


Messaggi: 1620
Iscritto: 27/09/2013

Segnala al moderatore
Postato alle 11:50
Mercoledì, 02/07/2014
puoi fare un classe game per ordinare meglio e rendere quel codice più mantenibile. Comunque secondo me non hai rimesso a 60 interval...............

PM Quote
Avatar
TheDarkJuster (Member)
Guru^2


Messaggi: 1620
Iscritto: 27/09/2013

Segnala al moderatore
Postato alle 11:50
Mercoledì, 02/07/2014
puoi fare un classe game per ordinare meglio e rendere quel codice più mantenibile. Comunque secondo me non hai rimesso a 60 interval...............

PM Quote