Commit f3c8496d authored by Yoann Bordin's avatar Yoann Bordin

Score gestion

parent b84b5014
...@@ -22,8 +22,6 @@ void drawSquare(int x, int y, Color color){ ...@@ -22,8 +22,6 @@ void drawSquare(int x, int y, Color color){
SQUARE_SIZE, SQUARE_SIZE,
SQUARE_SIZE, SQUARE_SIZE,
color); color);
} }
void drawPiece(Piece p){ void drawPiece(Piece p){
...@@ -47,14 +45,40 @@ void displayScore(int score){ ...@@ -47,14 +45,40 @@ void displayScore(int score){
DrawText(TextFormat("%4i", score), 430, 80, 20, RAYWHITE); DrawText(TextFormat("%4i", score), 430, 80, 20, RAYWHITE);
} }
void gameInstructions(Grid grid, Piece* pList){ void displaySquare(Square sq, int pIndex, int pListIndex){
Piece piece = generePiece(pList); char text0[5];
char text1[5];
drawPiece(piece); itoa(sq->posX, text0, 10);
itoa(sq->posY, text1, 10);
DrawText(text0, 20 + 80*pIndex, 20 + 80*pListIndex, 20, RAYWHITE);
DrawText(text1, 60 + 80*pIndex, 20 + 80*pListIndex, 20, RAYWHITE);
}
while(canMove(piece, grid)){ void displayPiece(Piece p, int pListIndex){
movePieceVert(piece, 1); for(int i = 0; i < p.size; i ++){
displaySquare(p.squares[i], i, pListIndex);
} }
}
addPieceToGrid(piece, grid); void displayPieceList(Piece* pList, int size){
for(int i = 0; i < size; i++){
displayPiece(pList[i], i);
}
} }
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);
}
}
}
void displayGrid(Grid g){
for(int x = 0; x < g.width; x++){
displayColumn(g.grid[x], x, g.height);
}
}
...@@ -9,13 +9,21 @@ ...@@ -9,13 +9,21 @@
#define GRID_PWIDTH SQUARE_SIZE*GRID_WIDTH #define GRID_PWIDTH SQUARE_SIZE*GRID_WIDTH
#define GRID_PHEIGHT SQUARE_SIZE*GRID_HEIGHT #define GRID_PHEIGHT SQUARE_SIZE*GRID_HEIGHT
// Init functions
void InitDisplay(); void InitDisplay();
void InitGame(); void InitGame();
// Draw functions
void drawSquare(int x, int y, Color color); void drawSquare(int x, int y, Color color);
void drawPiece(Piece p); void drawPiece(Piece p);
void drawGrid(Grid g); void drawGrid(Grid g);
// Display functions
void displayScore(int score); void displayScore(int score);
void gameInstructions(Grid grid, Piece* pList); // Display functions (debug only)
\ No newline at end of file void displaySquare(Square sq, int pIndex, int pListIndex);
void displayPiece(Piece p, int pListIndex);
void displayPieceList(Piece* pList, int size);
void displayColumn(Square* line, int index, int height);
void displayGrid(Grid g);
\ No newline at end of file
...@@ -10,6 +10,7 @@ int main(void){ ...@@ -10,6 +10,7 @@ int main(void){
bool isFixed = true; bool isFixed = true;
int score = 0; int score = 0;
int level = 1;
int frameCounter = 0; int frameCounter = 0;
...@@ -34,7 +35,7 @@ int main(void){ ...@@ -34,7 +35,7 @@ int main(void){
// Nouvelles pièces // Nouvelles pièces
if(isFixed){ if(isFixed){
p = generePiece(pieceList); p = spawnPiece(pieceList);
movePieceHztl(p, 3); movePieceHztl(p, 3);
isFixed = false; isFixed = false;
} }
...@@ -46,15 +47,15 @@ int main(void){ ...@@ -46,15 +47,15 @@ int main(void){
if(IsKeyPressed(KEY_RIGHT) && canMoveHztl(grid, p, 1)){ if(IsKeyPressed(KEY_RIGHT) && canMoveHztl(grid, p, 1)){
movePieceHztl(p, 1); movePieceHztl(p, 1);
} }
if(IsKeyDown(KEY_DOWN) && canMoveDown(grid, p, 1)){ if(IsKeyDown(KEY_DOWN) && canMoveVert(grid, p, 1)){
movePieceVert(p, 1); movePieceVert(p, 1);
} }
if(IsKeyPressed(KEY_Q)){ // A en AZERTY if(IsKeyPressed(KEY_Q)){ // A en AZERTY
while(rotateBorderLeft(p, false)){ while(rotateCollideLeft(p, false)){
movePieceHztl(p, 1); movePieceHztl(p, 1);
} }
while(rotateBorderRight(p, false)){ while(rotateCollideRight(p, false)){
movePieceHztl(p, -1); movePieceHztl(p, -1);
} }
while(rotateCollideGrid(grid, p, false)){ while(rotateCollideGrid(grid, p, false)){
...@@ -63,11 +64,12 @@ int main(void){ ...@@ -63,11 +64,12 @@ int main(void){
rotatePiece(grid, p, false); rotatePiece(grid, p, false);
} }
if(IsKeyPressed(KEY_W)){ // Z en AZERTY if(IsKeyPressed(KEY_W)){ // Z en AZERTY
while(rotateBorderLeft(p, true)){ while(rotateCollideLeft(p, true)){
movePieceHztl(p, 1); movePieceHztl(p, 1);
} }
while(rotateBorderRight(p, true)){ while(rotateCollideRight(p, true)){
movePieceHztl(p, -1); movePieceHztl(p, -1);
} }
while(rotateCollideGrid(grid, p, true)){ while(rotateCollideGrid(grid, p, true)){
...@@ -77,9 +79,10 @@ int main(void){ ...@@ -77,9 +79,10 @@ int main(void){
rotatePiece(grid, p, true); rotatePiece(grid, p, true);
} }
int score_prec = score;
// Déplacer pièce toutes les 1s // Déplacer pièce toutes les 1s
if(frameCounter%20 == 0){ if(frameCounter%(60/level) == 0){
if(canMove(p, grid)){ if(canMoveVert(grid, p, 1)){
movePieceVert(p, 1); movePieceVert(p, 1);
} }
else{ else{
...@@ -91,6 +94,11 @@ int main(void){ ...@@ -91,6 +94,11 @@ int main(void){
} }
} }
// Gestion des niveaux
if(score_prec%10 > score%10 && level < 10){
level++;
}
drawPiece(p); drawPiece(p);
drawGrid(grid); drawGrid(grid);
displayGrid(grid); displayGrid(grid);
...@@ -99,6 +107,20 @@ int main(void){ ...@@ -99,6 +107,20 @@ int main(void){
EndDrawing(); EndDrawing();
} }
for(int x = 0; x < grid.width; x++){
for(int y = 0; y < grid.height; y++){
free(grid.grid[x][y]);
}
free(grid.grid[x]);
}
free(grid.grid);
for(int i = 0; i < p.size; i++){
free(p.squares[i]);
}
free(p.squares);
CloseWindow(); CloseWindow();
return EXIT_SUCCESS; return EXIT_SUCCESS;
......
No preview for this file type
This diff is collapsed.
...@@ -30,48 +30,55 @@ typedef struct Piece{ ...@@ -30,48 +30,55 @@ typedef struct Piece{
int size; int size;
} Piece; } Piece;
// Init functions
Grid gridInit(); Grid gridInit();
Grid initSquares(Grid g);
Piece pieceInit(); Piece pieceInit();
Square squareInit();
// Copy functions
Square copySquare(Square sq); Square copySquare(Square sq);
bool gameOver(Grid grid); // Set functions
int height(Grid g); void setSquareColor(Square sq, int colorIndex);
int heightColumn(Square* column, int size); void setPieceColor(Piece p, int colorIndex);
bool canMove(Piece piece, Grid grid);
bool canMoveSquare(Square sq, Grid g);
void rotateSquare(Square sq, Square ref, bool rotation);
void rotatePiece(Grid g, Piece p, bool rotation); // False makes a counterclockwise rotation and true makes a clockwise rotation
void addPieceToGrid(Piece piece, Grid grid); // Piece generation and data reading
void addSquareToGrid(Square sq, Grid g);
int randInt(int lower, int upper);
Square getSquare(char* line, int index); Square getSquare(char* line, int index);
Piece getPieceFromLine(char* line); Piece getPieceFromLine(char* line);
Piece* getPiecesData(char* fileName); Piece* getPiecesData(char* fileName);
Piece generePiece(Piece* pieceList);
void displaySquare(Square sq, int pIndex, int pListIndex); int randInt(int lower, int upper);
void displayPiece(Piece p, int pListIndex); Piece spawnPiece(Piece* pieceList);
void displayPieceList(Piece* pList, int size);
// Bool verifications for moving
bool canMoveVert(Grid g, Piece p, int moveNum);
bool canMoveHztl(Grid g, Piece p, int moveNum);
bool canMoveDown(Grid g, Piece p, int moveNum); // Moving functions
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);
int removeLinesIfCompleted(Grid grid); // Bool verifications for rotating
bool rotateCollideLeft(Piece p, bool rotation);
bool rotateCollideRight(Piece p, bool rotation);
bool rotateCollideTop(Piece p, bool rotation); // To debug
bool rotateCollideGrid(Grid g, Piece p, bool rotation);
void displayColumn(Square* line, int index, int height); // Rotating functions
void displayGrid(Grid g); void rotateSquare(Square sq, Square ref, bool rotation);
void rotatePiece(Grid g, Piece p, bool rotation); // False makes a counterclockwise rotation and true makes a clockwise rotation
bool rotateBorderLeft(Piece p, bool rotation); // Grid adding functions
bool rotateBorderRight(Piece p, bool rotation); void addSquareToGrid(Square sq, Grid g);
bool rotateCollideGrid(Grid g, Piece p, bool rotation); void addPieceToGrid(Piece piece, Grid grid);
\ No newline at end of file
// Line completed detection
int removeLinesIfCompleted(Grid grid); // Returns the number of lines completed
// Game over detection
int heightColumn(Square* column, int size);
int height(Grid g);
bool gameOver(Grid grid);
\ 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