-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
184 lines (161 loc) · 4.59 KB
/
game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "game.h"
#include "file.h"
int game(){
Map gameMap;
int ch = 0;
int go = 1;
int move;
init_pair(MARIO, COLOR_BLACK, COLOR_RED);
initMap(&gameMap);
keypad(gameMap.win , true);
while (go == 1 && ch != 27)
{
switch (ch)
{
case KEY_DOWN:
move = DOWN;
break;
case KEY_UP:
move = UP;
break;
case KEY_LEFT:
move = LEFT;
break;
case KEY_RIGHT:
move = RIGHT;
break;
case 27:
go = 0;
break;
default:
move = RIEN;
break;
}
mouveM(&gameMap, move);
print_map(&gameMap);
wrefresh(gameMap.win);
ch = getch();
}
freeMap(&gameMap);
}
void initMap(Map *map){
for(int x = 0; x < NB_BLOC_X; x++){
for(int y = 0; y < NB_BLOC_Y; y++){
map->map[x][y] = VIDE;
}
}
loadFile(map);
map->win = newwin(NB_BLOC_Y, NB_BLOC_X, 0, 0);
}
void freeMap(Map* map){
for(int x = 0; x < NB_BLOC_X; x++){
for(int y = 0; y < NB_BLOC_Y; y++){
mvwaddch(map->win, y, x, ' ');
}
}
wrefresh(map->win);
delwin(map->win);
}
void print_map(Map *map){
for(int x = 0; x < NB_BLOC_X; x++){
for(int y = 0; y < NB_BLOC_Y; y++){
switch (map->map[x][y])
{
case MUR:
wattron(map->win, A_REVERSE);
mvwaddch(map->win, y, x, ' ');
wattroff(map->win, A_REVERSE);
break;
case MARIO:
wattron(map->win, COLOR_PAIR(MARIO));
mvwaddch(map->win, y, x, ' ');
wattroff(map->win, COLOR_PAIR(MARIO));
break;
case VIDE:
mvwaddch(map->win, y, x, ' ');
break;
case OBJECTIF:
mvwaddch(map->win, y, x, '*');
break;
case CAISSE:
mvwaddch(map->win, y, x, 'Q');
break;
case CAISSE_OK:
mvwaddch(map->win, y, x, 'O');
break;
default:
break;
}
}
}
}
void mouveM(Map *map, int move){
Coordonne temp = map->positionM;
map->map[temp.x][temp.y] = VIDE;
switch (move)
{
case RIGHT:
if(map->map[temp.x + 1][temp.y] == VIDE){
map->positionM.x ++;
}else if (map->map[temp.x + 1][temp.y] == CAISSE)
{
if(map->map[temp.x + 2][temp.y] == VIDE){
map->map[temp.x + 2][temp.y] = CAISSE;
map->positionM.x ++;
}else if (map->map[temp.x + 2][temp.y] == OBJECTIF)
{
map->map[temp.x + 2][temp.y] = CAISSE_OK;
map->positionM.x ++;
}
}
break;
case LEFT:
if(map->map[temp.x - 1][temp.y] == VIDE){
map->positionM.x --;
}else if (map->map[temp.x - 1][temp.y] == CAISSE)
{
if(map->map[temp.x - 2][temp.y] == VIDE){
map->map[temp.x - 2][temp.y] = CAISSE;
map->positionM.x --;
}else if (map->map[temp.x - 2][temp.y] == OBJECTIF)
{
map->map[temp.x - 2][temp.y] = CAISSE_OK;
map->positionM.x --;
}
}
break;
case UP:
if(map->map[temp.x][temp.y - 1] == VIDE){
map->positionM.y --;
}else if (map->map[temp.x][temp.y - 1] == CAISSE)
{
if(map->map[temp.x][temp.y - 2] == VIDE){
map->map[temp.x][temp.y - 2] = CAISSE;
map->positionM.y --;
}else if (map->map[temp.x][temp.y - 2] == OBJECTIF)
{
map->map[temp.x][temp.y - 2] = CAISSE_OK;
map->positionM.y --;
}
}
break;
case DOWN:
if(map->map[temp.x][temp.y + 1] == VIDE){
map->positionM.y ++;
}else if (map->map[temp.x][temp.y + 1] == CAISSE)
{
if(map->map[temp.x][temp.y + 2] == VIDE){
map->map[temp.x][temp.y + 2] = CAISSE;
map->positionM.y ++;
}else if (map->map[temp.x][temp.y + 2] == OBJECTIF)
{
map->map[temp.x][temp.y + 2] = CAISSE_OK;
map->positionM.y ++;
}
}
break;
default:
break;
}
map->map[map->positionM.x][map->positionM.y] = MARIO;
}