Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
MLOD Mini Projet
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yoann Bordin
MLOD Mini Projet
Commits
6e78304a
Commit
6e78304a
authored
Oct 30, 2020
by
Yoann Bordin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Edited struct, added functions
parent
76f29de8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
124 additions
and
39 deletions
+124
-39
tetris-main.c
tetris-main.c
+26
-0
tetris-struct.c
tetris-struct.c
+71
-20
tetris-struct.h
tetris-struct.h
+27
-19
No files found.
tetris-main.c
View file @
6e78304a
#include "tetris-struct.h"
#include "raylib.h"
#include <stdio.h>
int
main
(){
const
int
screenWidth
=
1280
;
const
int
screenHeight
=
720
;
InitWindow
(
screenWidth
,
screenHeight
,
"Tetris"
);
SetTargetFPS
(
60
);
while
(
!
WindowShouldClose
()){
BeginDrawing
();
ClearBackground
(
RAYWHITE
);
EndDrawing
();
}
CloseWindow
();
Grid
grid
=
gridInit
();
while
(
!
gameOver
(
grid
)){
Piece
piece
=
pieceInit
();
while
(
canMove
(
piece
,
grid
)){
move
(
piece
);
}
addPieceToGrid
(
piece
,
grid
);
}
}
\ No newline at end of file
tetris-struct.c
View file @
6e78304a
...
...
@@ -4,43 +4,94 @@
Grid
gridInit
(){
Grid
g
;
g
.
matrix
=
calloc
(
g
.
width
,
sizeof
(
Square
*
));
g
.
grid
=
calloc
(
g
.
width
,
sizeof
(
Square
*
));
for
(
int
i
=
0
;
i
<
g
.
width
;
i
++
){
g
.
matrix
[
i
]
=
calloc
(
g
.
height
,
sizeof
(
Square
));
g
.
grid
[
i
]
=
calloc
(
g
.
height
,
sizeof
(
Square
));
}
return
g
;
}
g
.
matrix
=
squareMatrixInit
(
g
.
matrix
,
g
.
width
,
g
.
height
);
Piece
pieceInit
(){
Piece
p
;
p
.
squares
=
calloc
(
p
.
size
,
sizeof
(
Square
));
return
g
;
return
p
;
}
bool
gameOver
(
Grid
grid
){
return
height
(
grid
)
==
grid
.
height
;
}
int
height
(
Grid
g
){
int
height
=
0
;
int
height_p
;
for
(
int
i
=
0
;
i
<
g
.
width
;
i
++
){
height_p
=
heightColumn
(
g
.
grid
[
i
],
g
.
height
);
if
(
height_p
>
height
){
height
=
height_p
;
}
}
}
Square
squareInit
(
){
Square
sq
;
sq
.
isFilled
=
false
;
int
heightColumn
(
Square
*
column
,
int
size
){
int
height
=
0
;
int
y
;
return
sq
;
for
(
int
i
=
0
;
i
<
size
;
i
++
){
if
(
column
[
i
]
!=
NULL
){
y
=
column
[
i
]
->
position
[
1
];
if
(
y
+
1
>
height
){
height
=
y
+
1
;
}
}
}
return
height
;
}
Square
**
squareMatrixInit
(
Square
**
matrix
,
int
width
,
int
height
){
for
(
int
i
=
0
;
i
<
width
;
i
++
){
for
(
int
j
=
0
;
j
<
height
;
j
++
){
matrix
[
i
][
j
]
=
squareInit
();
bool
canMove
(
Piece
piece
,
Grid
grid
){
int
x
,
y
;
for
(
int
i
=
0
;
i
<
piece
.
size
;
i
++
){
if
(
!
canMoveSquare
(
piece
.
squares
[
i
],
grid
)){
return
false
;
}
}
return
matrix
;
return
true
;
}
Piece
pieceInit
(){
Piece
p
;
p
.
matrix
=
calloc
(
p
.
size
,
sizeof
(
Square
*
));
bool
canMoveSquare
(
Square
sq
,
Grid
g
){
int
x
=
sq
->
position
[
0
];
int
y
=
sq
->
position
[
1
];
return
(
y
!=
0
||
y
==
heightColumn
(
g
.
grid
[
x
],
g
.
height
));
}
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
p
.
matrix
[
i
]
=
calloc
(
p
.
size
,
sizeof
(
Square
));
void
move
(
Piece
piece
){
for
(
int
i
=
0
;
i
<
piece
.
size
;
i
++
){
moveSquare
(
piece
.
squares
[
i
]);
}
}
p
.
matrix
=
squareMatrixInit
(
p
.
matrix
,
p
.
size
,
p
.
size
);
void
moveSquare
(
Square
sq
){
sq
->
position
[
1
]
--
;
}
return
p
;
void
addPieceToGrid
(
Piece
piece
,
Grid
grid
){
for
(
int
i
=
0
;
i
<
piece
.
size
;
i
++
){
addSquareToGrid
(
piece
.
squares
[
i
],
grid
);
}
}
void
addSquareToGrid
(
Square
sq
,
Grid
g
){
int
x
=
sq
->
position
[
0
];
int
y
=
sq
->
position
[
1
];
g
.
grid
[
x
][
y
]
=
sq
;
}
\ No newline at end of file
tetris-struct.h
View file @
6e78304a
#include <stdbool.h>
typedef
struct
Square
{
int
size
;
bool
isFilled
;
typedef
struct
square_s
{
int
*
position
;
int
coordSize
;
}
Square
;
int
color
;
typedef
struct
Grid
{
int
height
=
20
;
int
width
=
10
;
}
square_s
;
typedef
square_s
*
Square
;
Square
**
matrix
;
}
G
rid
;
typedef
struct
Grid
{
Square
**
g
rid
;
typedef
struct
Piece
{
int
size
=
4
;
int
width
;
int
height
;
}
Grid
;
Square
**
matrix
;
typedef
struct
Piece
{
Square
*
squares
;
int
size
;
}
Piece
;
Grid
gridInit
();
Square
squareInit
();
Square
**
squareMatrixInit
(
Square
**
matrix
,
int
width
,
int
height
);
Grid
initSquares
(
Grid
g
);
Piece
pieceInit
();
void
down
();
void
move
();
void
rotate
();
\ No newline at end of file
bool
gameOver
(
Grid
grid
);
int
height
(
Grid
g
);
int
heightColumn
(
Square
*
column
,
int
size
);
bool
canMove
(
Piece
piece
,
Grid
grid
);
bool
canMoveSquare
(
Square
sq
,
Grid
g
);
void
move
(
Piece
piece
);
void
moveSquare
(
Square
sq
);
void
addPieceToGrid
(
Piece
piece
,
Grid
grid
);
void
addSquareToGrid
(
Square
sq
,
Grid
g
);
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment