Commit 1d27412a authored by Yoann Bordin's avatar Yoann Bordin

Bug fixes

parent 69fbf0f9
{
"files.associations": {
"*.m": "c"
}
}
\ No newline at end of file
...@@ -13,30 +13,32 @@ void InitDisplay(){ ...@@ -13,30 +13,32 @@ void InitDisplay(){
DrawLine(BORDER_GAP, BORDER_GAP, BORDER_GAP, posY, RAYWHITE); DrawLine(BORDER_GAP, BORDER_GAP, BORDER_GAP, posY, RAYWHITE);
DrawLine(BORDER_GAP, posY, posX, posY, RAYWHITE); DrawLine(BORDER_GAP, posY, posX, posY, RAYWHITE);
DrawLine(posX, posY, posX, BORDER_GAP, RAYWHITE); DrawLine(posX+1, posY, posX+1, BORDER_GAP, RAYWHITE);
} }
void drawSquare(Square sq){ void drawSquare(int x, int y, Color color){
if(sq != NULL){ DrawRectangle(BORDER_GAP + x*SQUARE_SIZE,
DrawRectangle(BORDER_GAP + sq->posX*SQUARE_SIZE, BORDER_GAP + y*SQUARE_SIZE,
BORDER_GAP + sq->posY*SQUARE_SIZE, SQUARE_SIZE,
SQUARE_SIZE, SQUARE_SIZE,
SQUARE_SIZE, color);
sq->color);
}
} }
void drawPiece(Piece p){ void drawPiece(Piece p){
for(int i = 0; i < p.size; i++){ for(int i = 0; i < p.size; i++){
drawSquare(p.squares[i]); drawSquare(p.squares[i]->posX, p.squares[i]->posY, p.squares[i]->color);
} }
} }
void displayGrid(Grid g){ void drawGrid(Grid g){
for(int i = 0; i < g.width; i++){ for(int x = 0; x < g.width; x++){
for(int j = 0; j < g.height; j++){ for(int y = 0; y < g.height; y++){
drawSquare(g.grid[i][j]); if(g.grid[x][y] != NULL){
drawSquare(x, y, g.grid[x][y]->color);
}
//drawSquare(g.grid[x][y]);
} }
} }
} }
......
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
void InitDisplay(); void InitDisplay();
void InitGame(); void InitGame();
void drawSquare(Square sq); void drawSquare(int x, int y, Color color);
void drawPiece(Piece p); void drawPiece(Piece p);
void displayGrid(Grid g); void drawGrid(Grid g);
void gameInstructions(Grid grid, Piece* pList); void gameInstructions(Grid grid, Piece* pList);
\ No newline at end of file
...@@ -37,8 +37,12 @@ int main(void){ ...@@ -37,8 +37,12 @@ int main(void){
} }
// Déplacements saisis au clavier // Déplacements saisis au clavier
if(IsKeyPressed(KEY_LEFT)) movePieceHztl(p, -1); if(IsKeyPressed(KEY_LEFT) && canMoveHztl(grid, p, -1)){
if(IsKeyPressed(KEY_RIGHT)) movePieceHztl(p, 1); movePieceHztl(p, -1);
}
if(IsKeyPressed(KEY_RIGHT) && canMoveHztl(grid, p, 1)){
movePieceHztl(p, 1);
}
if(IsKeyPressed(KEY_A)) rotatePiece(p, false); if(IsKeyPressed(KEY_A)) rotatePiece(p, false);
if(IsKeyPressed(KEY_Z)) rotatePiece(p, true); if(IsKeyPressed(KEY_Z)) rotatePiece(p, true);
...@@ -52,16 +56,15 @@ int main(void){ ...@@ -52,16 +56,15 @@ int main(void){
isFixed = true; isFixed = true;
removeLinesIfCompleted(grid); removeLinesIfCompleted(grid);
} }
} }
drawPiece(p); drawPiece(p);
drawGrid(grid);
displayGrid(grid); displayGrid(grid);
frameCounter++; frameCounter++;
EndDrawing(); EndDrawing();
} }
......
No preview for this file type
...@@ -279,6 +279,18 @@ void movePieceVert(Piece piece, int shift){ ...@@ -279,6 +279,18 @@ void movePieceVert(Piece piece, int shift){
DrawText(text3, 450, 20, 20, RED); DrawText(text3, 450, 20, 20, RED);
} }
bool canMoveHztl(Grid g, Piece p, int moveNum){
for(int i = 0; i < p.size; i++){
int x = p.squares[i]->posX + moveNum;
int y = p.squares[i]->posY;
if(x < 0 || x >= g.width || g.grid[x][y] != NULL){
return false;
}
}
return true;
}
void moveSquareHztl(Square sq, int shift){ void moveSquareHztl(Square sq, int shift){
sq->posX += shift; sq->posX += shift;
} }
...@@ -289,29 +301,53 @@ void movePieceHztl(Piece p, int shift){ ...@@ -289,29 +301,53 @@ void movePieceHztl(Piece p, int shift){
} }
} }
void removeLine(Grid g, int lineIndex){ void removeLinesIfCompleted(Grid grid){
int i = lineIndex; bool isLineCompleted;
while(i > 0){
i--; int y = 0;
while(y < grid.height){
isLineCompleted = true;
for(int x = 0; x < grid.width; x++){
if(grid.grid[x][y] == NULL){
isLineCompleted = false;
}
}
for(int j = 0; j < g.width; j++){ if(isLineCompleted){
g.grid[j][i+1] = g.grid[j][i]; int lineToRemove = y;
//free
for(int x = 0; x < grid.width; x++){
free(grid.grid[x][lineToRemove]);
}
//shift
for(int x = 0; x < grid.width; x++){
for(int i = lineToRemove; i > 0; i--){
grid.grid[x][i] = grid.grid[x][i-1];
}
}
//initTop
for(int x = 0; x < grid.width; x++){
grid.grid[x][0] = malloc(sizeof(square_s));
grid.grid[x][0] = NULL;
}
} }
for(int k = 0; k < g.width; k++){ else{
g.grid[k][0] = NULL; y++;
} }
} }
} }
void removeLinesIfCompleted(Grid grid){ void displayColumn(Square* line, int index, int height){
for(int k = 0; k < grid.height; k++){ for(int y = 0; y < height; y++){
int j = 0; if(line[y] != NULL){
while(j < grid.width && grid.grid[j][k] != NULL){ DrawText("1", 500 + 30*index, 20 + 30*y, 20, RAYWHITE);
j++;
} }
if (j == grid.width){ }
removeLine(grid, k); }
}
void displayGrid(Grid g){
for(int x = 0; x < g.width; x++){
displayColumn(g.grid[x], x, g.height);
} }
} }
\ No newline at end of file
...@@ -61,8 +61,12 @@ void displayPieceList(Piece* pList, int size); ...@@ -61,8 +61,12 @@ void displayPieceList(Piece* pList, int size);
void moveSquareVert(Square sq, int shift); void moveSquareVert(Square sq, int shift);
void movePieceVert(Piece piece, int shift); void movePieceVert(Piece piece, int shift);
bool canMoveHztl(Grid g, Piece p, int moveNum);
void moveSquareHztl(Square sq, int shift); void moveSquareHztl(Square sq, int shift);
void movePieceHztl(Piece p, int shift); void movePieceHztl(Piece p, int shift);
void removeLine(Grid g, int lineIndex); void removeLinesIfCompleted(Grid grid);
void removeLinesIfCompleted(Grid grid);
\ No newline at end of file void displayColumn(Square* line, int index, int height);
void displayGrid(Grid g);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment