|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11782
|
[Ciclo 6] Pair Programming task 6.2.3 (fek vs cionci clash of the titans)
Implementare le animazioni in sprite cambiando frame ogni decimo di secondo. Al termine del'animazione si deve mostrare il frame di base per 3500 millisecondi prima di ripartire.
![]() Il task parla di scrivere in Sprite, ma guardando il codice ci pare piu' semplice scrivere in Gem, quindi proseguiamo per questa strada. Test list: - testare che una gemma appena creata abbia solo un frame di animazione - testare che all'aggiunta di un frame di animazione, la gemma ne abbia due - testare che all'update della gemma, cicli dal primo al secondo frame di animazione - testare che ogni frame abbia un tempo di durata - se la gemma si trova al frame 0 deve passare al frame 1 dopo 3500 ms - se la gemma si trova al frame X con X > 0 deve passare al frame X + 1 dopo 100 ms - se la gemma si trova al frame N-1 deve ritornare al frame 0 dopo 100 ms Per ora iniziamo con questi, ce ne verranno certamente in mente altri.
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11782
|
Primo test: una gemma appena creata ha un frame di animazione.
Codice:
package it.diamonds.tests; import it.diamonds.Gem; import junit.framework.TestCase; public class TestGemAnimation extends TestCase { public void TestNewGemHasOneFrame() { Gem gem = Gem.createForTesting(); assertEquals("A new gem must have only one frames", 1, gem.getNumberOfFrames()); } } ![]() (Codice minimo indispensabile per far passare il test)
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA Ultima modifica di fek : 11-12-2005 alle 10:32. |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Aggiunto questo codice a Gem:
Codice:
public int getNumberOfFrames() { return 1; } Ultima modifica di cionci : 11-12-2005 alle 10:32. |
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Nuovo test: aggiunta di un frame a Gem
Codice:
public void testAddingOneFrame() { Gem gem = Gem.createForTesting(); gem.addFrame(new Rectangle(10, 10, 20, 20)); assertEquals("gem must have two frames", 2, gem.getNumberOfFrames()); } |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11782
|
Dato che la dimensione dello sprite e' fissa, fornire quest'informazione anche in addFrame mi sembra una duplicazione (non supportiamo scalamenti per ora).
Modificherei il test cosi': Codice:
public void testAddingOneFrame() { Gem gem = Gem.createForTesting(); gem.addFrame(10, 10); assertEquals("gem must have two frames", 2, gem.getNumberOfFrames()); } Codice:
public void addFrame(int x, int y) { // TODO Auto-generated method stub } Lancio i test, compilano e il test fallisce: junit.framework.AssertionFailedError: gem must have two frames expected:<2> but was:<1> Il test si aspetta 2 ma riceve 1. Modifico il codice per far passare il test. Codice:
private int numberOfFrames; public int getNumberOfFrames() { return numberOfFrames; } public void addFrame(int x, int y) { numberOfFrames = 2; } junit.framework.AssertionFailedError: A new gem must have no frames expected:<1> but was:<0> Non sto barando, e' andata proprio cosi', ho dimenticato di inizializzare il campo numberOfFrames ![]() Per fortuna il primo test era li', pronto a ricordarmelo in caso di errore. Sto scrivendo mentre programmo. Correggo: Codice:
private int numberOfFrames = 1; Ora proseguo per triangolazione con un nuovo test: Codice:
public void testAddingTwoFrames() { Gem gem = Gem.createForTesting(); gem.addFrame(10, 10); gem.addFrame(10, 20); assertEquals("gem must have three frames", 3, gem.getNumberOfFrames()); } junit.framework.AssertionFailedError: gem must have three frames expected:<3> but was:<2> Cionci a te.
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Test passato modificando questo metodo:
Codice:
public void addFrame(int x, int y) { ++numberOfFrames; } |
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Il frame corrente deve essere 0 appena viene creata la gemma...
Codice:
public void testCurrentFrame() { Gem gem = Gem.createForTesting(); assertEquals("current frame must be 0", 0, gem.getCurrentFrame()); } Ultima modifica di cionci : 11-12-2005 alle 10:59. |
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11782
|
Ecco il metodo che fa passare il test.
Codice:
public int getCurrentFrame() { return 0; }
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
![]() |
![]() |
![]() |
#9 |
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11782
|
Ttestare che all'update della gemma, cicli dal primo al secondo frame di animazione.
Codice:
public void testUpdate() { Gem gem = Gem.createForTesting(); gem.addFrame(10, 10); gem.update(); assertEquals("current frame must be 1 after an update", 1, gem.getCurrentFrame()); } Cionci a te.
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA Ultima modifica di fek : 11-12-2005 alle 11:14. |
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Ho aggiunto:
private int currentFrame = 0; Codice:
public int getCurrentFrame() { return currentFrame; } public void update() { currentFrame = 1; } Ultima modifica di cionci : 11-12-2005 alle 11:17. |
![]() |
![]() |
![]() |
#11 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Il prossimo test ci impone di spostarci di un frame per ogni update che viene fatto...
Codice:
public void testUpdateWithThreeFrames() { Gem gem = Gem.createForTesting(); gem.addFrame(10, 10); gem.addFrame(20, 20); gem.update(); gem.update(); assertEquals("current frame must be 2 after two update", 2, gem.getCurrentFrame()); } |
![]() |
![]() |
![]() |
#12 |
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11782
|
Una sola riga da cambiare:
Codice:
public void update() { ++currentFrame; } ![]()
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
![]() |
![]() |
![]() |
#13 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
![]() |
![]() |
![]() |
![]() |
#14 |
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11782
|
Test: testare che ogni frame abbia un tempo di durata (in millisecondi)
Codice:
public void testFrameLengt() { gem.addFrame(10, 10, 1000); assertEquals("frame length is wrong", 1000, gem.getFrameLength(0)); }
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
![]() |
![]() |
![]() |
#15 |
Senior Member
Iscritto dal: Nov 2005
Messaggi: 1545
|
beh almeno io vi seguo passo passo se può consolarvi
![]() |
![]() |
![]() |
![]() |
#16 | |
Senior Member
Iscritto dal: Jul 2005
Città: Silent Hill
Messaggi: 1471
|
Quote:
![]() Azz... la nostra copertura è saltata...
__________________
DIAMOND CRUSH - Aut viam inveniam, aut faciam. |
|
![]() |
![]() |
![]() |
#17 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Aggiunto il codice che fa passare il test:
Codice:
public void addFrame(int x, int y, int delay) { ++numberOfFrames; } public int getFrameLength(int index) { return 1000; } Un saluto a tutti ![]() Ovviamente ho corretto tutti gli altri test per compilare senza problemi... Ultima modifica di cionci : 11-12-2005 alle 11:42. |
![]() |
![]() |
![]() |
#18 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Nuovo test e la situazione si fa interessante:
Codice:
public void testTwoFrameLengths() { gem.addFrame(10, 10, 1000); gem.addFrame(10, 10, 100); assertEquals("frame one length is wrong", 1000, gem.getFrameLength(0)); assertEquals("frame two length is wrong", 100, gem.getFrameLength(1)); } |
![]() |
![]() |
![]() |
#19 | |
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11782
|
Quote:
Ecco il test: Codice:
package it.diamonds.tests; import it.diamonds.Frame; import junit.framework.TestCase; public class TestFrame extends TestCase { public void testLength() { Frame frame = new Frame(10, 10, 1000); assertEquals("Frame length must be 1000", 1000, frame.getLength()); } } [code] package it.diamonds; public class Frame { public Frame(int x, int y, int length) { } public int getLength() { return 1000; } } [code] Vado avanti per triangolazione: Codice:
public void testLength() { Frame frame1 = new Frame(10, 10, 1000); assertEquals("Frame length must be 1000", 1000, frame1.getLength()); Frame frame2 = new Frame(10, 10, 2000); assertEquals("Frame length must be 2000", 2000, frame2.getLength()); } Ed ecco il codice che lo fa passare: Codice:
public class Frame { int length; public Frame(int x, int y, int length) { this.length = length; } public int getLength() { return length; } } E' il momento di fare un po' di refactoring: se sbaglio qualcosa ho i test che mi fermeranno. Torno a Gem. Aggiungo l'array di Frame. Codice:
private ArrayList<Frame> frames; Codice:
public void addFrame(int x, int y, int delay) { frames.add(new Frame(x, y, delay)); ++numberOfFrames; } Codice:
private ArrayList<Frame> frames = new ArrayList<Frame>(); Prossimo passo, proviamo a togliere numberOfFrames: Codice:
public int getNumberOfFrames() { return frames.size(); } ![]() Vediamo che dice JUnit: junit.framework.AssertionFailedError: A new gem must have one frame expected:<1> but was:<0> Ovvio, una nuova gemma deve avere un frame di animazione. Io me ne ero dimenticato, ma il test no. Vado nel costruttore e aggiungo questo: Codice:
addFrame(0, 0, 0); Codice:
public void addFrame(int x, int y, int delay) { frames.add(new Frame(x, y, delay)); } Codice:
public int getFrameLength(int index) { return frames.get(index).getLength(); } junit.framework.AssertionFailedError: frame length is wrong expected:<1000> but was:<0> Hmmm... il test si aspettava 1000 ma gli e' arrivato 0, pero' sono sicuro che se passo una durata alla classe Frame questa mi torna la durata giusta (quel test e' passato)... Fammi guardare il test: Codice:
public void testFrameLength() { gem.addFrame(10, 10, 1000); assertEquals("frame length is wrong", 1000, gem.getFrameLength(0)); } Ora lo cambio: Codice:
public void testFrameLength() { gem.addFrame(10, 10, 1000); assertEquals("frame length is wrong", 1000, gem.getFrameLength(1)); } Notate come ho fatto un sacco di errori mentre programmavo (sara' la fame ![]() Ci aggiorniamo a dopo pranzo.
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
![]() |
![]() |
![]() |
#20 |
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11782
|
Test: se la gemma si trova al frame 0 deve passare al frame 1 dopo 3500 ms
Codice:
public void testUpdateWithTimer() { gem.addFrame(10, 10, 1000); MockTimer timer = new MockTimer(0); gem.update(timer); assertEquals("current frame must be 0 after 0 milliseconds", 0, gem.getCurrentFrame()); timer.advance(3500); gem.update(timer); assertEquals("current frame must be 1 after 3500 milliseconds", 1, gem.getCurrentFrame()); }
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA Ultima modifica di fek : 11-12-2005 alle 13:29. |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 09:25.