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(){
DrawLine(BORDER_GAP, BORDER_GAP, BORDER_GAP, 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){
if(sq != NULL){
DrawRectangle(BORDER_GAP + sq->posX*SQUARE_SIZE,
BORDER_GAP + sq->posY*SQUARE_SIZE,
void drawSquare(int x, int y, Color color){
DrawRectangle(BORDER_GAP + x*SQUARE_SIZE,
BORDER_GAP + y*SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE,
sq->color);
}
color);
}
void drawPiece(Piece p){
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){
for(int i = 0; i < g.width; i++){
for(int j = 0; j < g.height; j++){
drawSquare(g.grid[i][j]);
void drawGrid(Grid g){
for(int x = 0; x < g.width; x++){
for(int y = 0; y < g.height; y++){
if(g.grid[x][y] != NULL){
drawSquare(x, y, g.grid[x][y]->color);
}
//drawSquare(g.grid[x][y]);
}
}
}
......
......@@ -12,8 +12,8 @@
void InitDisplay();
void InitGame();
void drawSquare(Square sq);
void drawSquare(int x, int y, Color color);
void drawPiece(Piece p);
void displayGrid(Grid g);
void drawGrid(Grid g);
void gameInstructions(Grid grid, Piece* pList);
\ No newline at end of file
......@@ -37,8 +37,12 @@ int main(void){
}
// Déplacements saisis au clavier
if(IsKeyPressed(KEY_LEFT)) movePieceHztl(p, -1);
if(IsKeyPressed(KEY_RIGHT)) movePieceHztl(p, 1);
if(IsKeyPressed(KEY_LEFT) && canMoveHztl(grid, 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_Z)) rotatePiece(p, true);
......@@ -52,17 +56,16 @@ int main(void){
isFixed = true;
removeLinesIfCompleted(grid);
}
}
drawPiece(p);
drawGrid(grid);
displayGrid(grid);
frameCounter++;
EndDrawing();
}
CloseWindow();
......
No preview for this file type
......@@ -279,6 +279,18 @@ void movePieceVert(Piece piece, int shift){
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){
sq->posX += shift;
}
......@@ -289,29 +301,53 @@ void movePieceHztl(Piece p, int shift){
}
}
void removeLine(Grid g, int lineIndex){
int i = lineIndex;
while(i > 0){
i--;
void removeLinesIfCompleted(Grid grid){
bool isLineCompleted;
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;
}
}
if(isLineCompleted){
int lineToRemove = y;
for(int j = 0; j < g.width; j++){
g.grid[j][i+1] = g.grid[j][i];
//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];
}
for(int k = 0; k < g.width; k++){
g.grid[k][0] = NULL;
}
//initTop
for(int x = 0; x < grid.width; x++){
grid.grid[x][0] = malloc(sizeof(square_s));
grid.grid[x][0] = NULL;
}
}
else{
y++;
}
}
}
void removeLinesIfCompleted(Grid grid){
for(int k = 0; k < grid.height; k++){
int j = 0;
while(j < grid.width && grid.grid[j][k] != NULL){
j++;
void displayColumn(Square* line, int index, int height){
for(int y = 0; y < height; y++){
if(line[y] != NULL){
DrawText("1", 500 + 30*index, 20 + 30*y, 20, RAYWHITE);
}
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);
void moveSquareVert(Square sq, int shift);
void movePieceVert(Piece piece, int shift);
bool canMoveHztl(Grid g, Piece p, int moveNum);
void moveSquareHztl(Square sq, int shift);
void movePieceHztl(Piece p, int shift);
void removeLine(Grid g, int lineIndex);
void removeLinesIfCompleted(Grid grid);
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