-
Notifications
You must be signed in to change notification settings - Fork 0
/
labyrinth.c
206 lines (181 loc) · 4.76 KB
/
labyrinth.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "labyrinth.h"
int main(void) {
// Recommended by man page
setlocale(LC_ALL, "");
//printf("Which level would you like to start from? (0-1)");
//scanf("%d", currentlvl);
initscr();
// Needs testing. Is it necessary to check for this?
if (!has_colors()) {
endwin();
printf("Your terminal does not support color.\n");
return 1;
}
// These dimensions are somewhat arbitrary, but they have to be bigger than the map we use
win = newwin(36, 72, 0, 0);
// Read a character at a time without waiting for newline or carriage return
cbreak();
// Don't print keypresses
noecho();
// Don't translate return key into newline
nonl();
// Don't flush tty driver queue contents if an interrupt key is pressed, as it messes up the screen contents
intrflush(win, FALSE);
// Enable use of the keypad
keypad(win, TRUE);
// Enable scrolling if moving past the terminal edge.
//idlok(win, TRUE);
//scrollok(win, TRUE);
startgame();
// Must be called before exiting so our terminal doesn't behave weirdly
endwin();
return 0;
}
int startgame() {
confcolors();
drawmap(levels[currentlvl]);
inputloop();
return 0;
}
int confcolors() {
start_color();
// In case we wanted to color our cursor, but let's leave this disabled for now
//printf("\e]12;%s\a", "green");
//fflush(stdout);
// Color used for one-directional blocks like <, >, v and ^
init_pair(1, COLOR_YELLOW, COLOR_BLACK);
// Color used for the goal block, *
init_pair(2, COLOR_BLACK, COLOR_GREEN);
// Color used by the wall block, 'o'
init_pair(3, COLOR_RED, COLOR_RED);
return 0;
}
int drawmap(filename) char* filename; {
wmove(win, 0, 0);
FILE *fp;
int c;
fp = fopen(filename, "r");
while (1) {
c = fgetc(fp);
if (feof(fp)) {
break;
}
switch (c) {
case '<':
case '>':
case '^':
case 'v':
wattron(win, COLOR_PAIR(1));
wprintw(win, "%c", c);
wattroff(win, COLOR_PAIR(1));
break;
case '*':
wattron(win, COLOR_PAIR(2));
wprintw(win, "%c", c);
wattroff(win, COLOR_PAIR(2));
break;
case 'o':
wattron(win, COLOR_PAIR(3));
wprintw(win, "%c", c);
wattroff(win, COLOR_PAIR(3));
break;
default:
wprintw(win, "%c", c);
break;
}
}
fclose(fp);
wmove(win, y, x);
return 0;
}
int inputloop() {
for (;;) {
int next_y = y;
int next_x = x;
int keypress = wgetch(win);
switch (keypress) {
case 'q':
return 0;
case 'h':
case 'a':
case KEY_LEFT:
next_x--;
break;
case 'j':
case 's':
case KEY_DOWN:
next_y++;
break;
case 'k':
case 'w':
case KEY_UP:
next_y--;
break;
case 'd':
case 'l':
case KEY_RIGHT:
next_x++;
break;
}
if (handlemovement(next_y, next_x)) {
break;
}
wrefresh(win);
}
return 0;
}
int handlemovement (int next_y, int next_x) {
chtype nextchar = mvwinch(win, next_y, next_x);
// We can't compare nextchar to '>' because '>' is colored. Using ints is a workaround.
switch (nextchar) {
// >
case 318:
if (next_x > x) {
x = next_x;
}
break;
// <
case 316:
if (next_x < x) {
x = next_x;
}
break;
// v
case 374:
if (next_y > y) {
y = next_y;
}
break;
// ^
case 350:
if (next_y < y) {
y = next_y;
}
break;
// *
case 554:
winlvl();
return 1;
break;
case ' ':
x = next_x;
y = next_y;
break;
default:
// Debug function for getting the code of the char we run into
//wprintw(win, "%d", nextchar);
break;
}
wmove(win, y, x);
return 0;
}
int winlvl() {
WINDOW* victorywin = newwin(3, 19, 1, 1);
wmove(victorywin, 1, 1);
box(victorywin, '|', '-');
wprintw(victorywin, "Level %d complete!", currentlvl);
wrefresh(victorywin);
wgetch(victorywin);
delwin(victorywin);
return 0;
}