CONNEXION
  • RetourJeux
    • Sorties
    • Hit Parade
    • Les + populaires
    • Les + attendus
    • Soluces
    • Tous les Jeux
    • Gaming
  • RetourActu Gaming
    • News
    • Astuces
    • Tests
    • Previews
    • Toute l'actu gaming
  • RetourBons plans
    • Bons plans
    • Bons plans Smartphone
    • Bons plans Hardware
    • Bons plans Image et Son
    • Bons plans Amazon
    • Bons plans Cdiscount
    • Bons plans Decathlon
    • Bons plans Fnac
    • Tous les Bons plans
  • RetourJVTech
    • Actus High-Tech
    • Intelligence Artificielle
    • Smartphones
    • Mobilité urbaine
    • Hardware
    • Image et son
    • Tutoriels
    • Tests produits High-Tech
    • Guides d'achat High-Tech
    • JVTech
  • RetourCulture
    • Actus Culture
    • Culture
  • RetourVidéos
    • A la une
    • Gaming Live
    • Vidéos Tests
    • Vidéos Previews
    • Gameplay
    • Trailers
    • Chroniques
    • Replay Web TV
    • Toutes les vidéos
  • RetourForums
    • Hardware PC
    • PS5
    • Switch 2
    • Xbox Series
    • Switch
    • Pokemon pocket
    • FC 25 Ultimate Team
    • League of Legends
    • Tous les Forums
  • PC
  • PS5
  • Xbox Series
  • Switch 2
  • PS4
  • One
  • Switch
  • iOS
  • Android
  • MMO
  • RPG
  • FPS
En ce moment Genshin Impact Valhalla Breath of the wild Animal Crossing GTA 5 Red dead 2
Liste des sujets

[Java]Aidez moi a comprendre ce code svp

Cemninho
Cemninho
Niveau 2
14 mai 2005 à 18:26:43

Voila une classe d´un Tetris que j´essaie de comprendre. Les méthodes ci dessous intéragissent entre-elles au point que je n´arrive plus a comprendre qui fait quoi ! ! J´aimerais comprendre l´utilité des méthodes de cette classe(addBlock,moveX,....). J´ai passé 4heures dessus je n´y arrive toujours pas... je desespère...aidez moi please ! ! Merci
class TetrisPiece
{
GameCallback gameCallback;//déclaration de l´interface GameCallback
TetrisBlock gameBlocks[][];
int x;
int y;
Color color;
int nb_block[][];//Pour définir la taille de la pièce en fonction de " size"
int size;//Taille de la pièce définie
boolean rotated = false;
boolean piecePrete = false;
int BonusChute = 0;

TetrisPiece(GameCallback gameCallback, TetrisBlock gameBlocks[][], PieceDescription piece, int x, int y)
{
this.gameCallback = gameCallback;
this.gameBlocks = gameBlocks;
this.x = x;
this.y = y;
this.size = piece.size;
this.color = piece.color;

nb_block = new int[size][size];//Taille de la pièce
copyArray(piece.nb_block, nb_block, size);
}

void addBlock()
{
boolean atTop = false;
for ( int i = 0; i < size; i++)
for ( int j = 0; j < size; j++)
if ( nb_block[i][j] == 1)
{
if ( y < 2)
atTop = true;
else
gameBlocks[x + i][y + j] = new TetrisBlock(color);
}
gameCallback.blockLanded(BonusChute, atTop);
}

void copyArray(int inArr[][], int outArr[][], int arSize)
{
for ( int i = 0; i < arSize; i++)
for ( int j = 0; j < arSize; j++)
outArr[i][j] = inArr[i][j];
}

void copyArrayShifted(int inArr[][], int outArr[][], int arSize, int xShift, int yShift)
{
if ( xShift == -1)
{
for ( int i = 0; i < arSize - 1; i++)
for ( int j = 0; j < arSize; j++)
outArr[i][j] = inArr[i + 1][j];
for ( int j = 0; j < arSize; j++)
outArr[arSize - 1][j] = 0;
}
if ( xShift == 1)
{
for ( int i = 1; i < arSize; i++)
for ( int j = 0; j < arSize; j++)
outArr[i][j] = inArr[i - 1][j];
for ( int j = 0; j < arSize; j++)
outArr[0][j] = 0;
}
if ( yShift == 1)
{
for ( int i = 0; i < arSize; i++)
for ( int j = 1; j < arSize; j++)
outArr[i][j] = inArr[i][j - 1];
for ( int i = 0; i < arSize; i++)
outArr[i][0] = 0;
}
}

void rotateArray(int inArr[][], int rotArr[][], boolean rotateRight)
{
for ( int i = 0; i < size; i++)
for ( int j = 0; j < size; j++)
if ( rotateRight)
rotArr[i][j] = inArr[size - j - 1][i];//On crée la rotation dans le tableau représentant la pièce
else
rotArr[i][j] = inArr[j][size - i - 1];
}

/ *
**
** The methods that move a piece must be synchronized, since there
** are two threads that can both move a piece. Therefore, it is
** important to check to see if the piece is already landed ( piecePrete) on
** entry into these synchronized methods, since they could be blocking
** while the other thread lands the piece.
**
*/
synchronized void rotatePiece(boolean rotateRight)
{
if ( piecePrete)
return;

int rotArr[][] = new int[size][size];
rotateArray(nb_block, rotArr, rotateRight);
if ( !CheckForCollision(rotArr, size))
{
/ / draw new piece before erasing old parts
drawPiece(rotArr, size, false);
erasePieceDif(nb_block, rotArr, size);
copyArray(rotArr, nb_block, size);
}
}

synchronized void chute()
{
if ( piecePrete)
return;
while ( moveDown())
BonusChute += TetrisGameCanvas.scbonus;//On augmente la valeur du bonus en cas de chute de la piece
}

synchronized boolean moveDown()
{
if ( piecePrete)
return(false);
y++;
boolean moved = ! CheckForCollision(nb_block, size);
if ( !moved)
{
y--;
addBlock();
}
else
{
int shiftedArr[][] = new int[size][size];
copyArrayShifted(nb_block, shiftedArr, size, 0, 1);
/ / draw new piece before erasing old parts
drawPiece(false);
y--;
erasePieceDif(nb_block, shiftedArr, size);
y++;
}
return(moved);
}

synchronized void moveX(int dx)
{
if ( piecePrete)
return;
x += dx;
boolean moved = ! CheckForCollision(nb_block, size);
if ( moved)
{
int shiftedArr[][] = new int[size][size];
copyArrayShifted(nb_block, shiftedArr, size, dx, 0);
/ / draw new piece before erasing old parts
drawPiece(false);
x -= dx;
erasePieceDif(nb_block, shiftedArr, size);
x += dx;
}
else
x -= dx;
}

boolean CheckForCollision(int inArray[][], int arSize)
{
for ( int i = 0; i < arSize; i++)
for ( int j = 0; j < arSize; j++)
if ( inArray[i][j] == 1)
for ( int a = 0; a < Tetris.worldWidth + 2; a++)
for ( int b = 0; b < Tetris.worldHeight + 3; b++)
if ( ( gameBlocks[a][b] ! = null) && ( x + i == a) && ( y + j == b) )
return(true);
return(false);
}

void erasePieceDif(int oldArr[][], int newArr[][], int arSize)
{
int lasti = -1;
int lastj = -1;
for ( int i = 0; i < arSize; i++)
for ( int j = 0; j < arSize; j++)
if ( ( oldArr[i][j] == 1) && ( newArr[i][j] == 0) )
{
lasti = i;
lastj = j;
gameCallback.DessBloc(color, x + i, y + j, true);
}
if ( y < 2)
{
for ( int i = 0; i < arSize; i++)
for ( int j = 0; j < arSize; j++)
if ( ( oldArr[i][j] == 1) && ( newArr[i][j] == 0) )
gameCallback.DessBloc(color, x + i, y + j, true);
}
else if ( ( lasti ! = -1) && ( lastj ! = -1) )
gameCallback.DessBloc(color, x + lasti, y + lastj, true);
/ / end bug workaround
}

void drawPiece(boolean erase)
{
drawPiece(nb_block, size, erase);
}

void drawPiece(int inArr[][], int arSize, boolean erase)
{
for ( int i = 0; i < arSize; i++)
for ( int j = 0; j < arSize; j++)
if ( inArr[i][j] == 1)
gameCallback.DessBloc(color, x + i, y + j, erase);
}
}

linkinonline
linkinonline
Niveau 9
14 mai 2005 à 19:08:58

bas ca taffiche un jeu :
alors tu dosi aligné , les carré pour pouvoir cassé le mur, c´est tout simple!

:dehors:

Sous forums
  • Aide à l'achat Mac
  • Macintosh
  • Création de Jeux
  • Programmation
  • Création de sites web
  • Linux
  • Internet
  • Steam Deck
  • Hardware
La vidéo du moment