summaryrefslogtreecommitdiff
path: root/src/draw.c
blob: 607c7864e2ff1074ac22270ec47cd40402e1f871 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "config.h"

#include <errno.h>
#include <math.h>
#include <ncurses.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include "include/body.h"

#ifdef MKDIR_TAKES_ONE_ARG   /* MinGW32 */
#undef mkdir
#define mkdir(a, b) mkdir(a)
#endif

#define START_SIZE 4
//TODO consider having speed be affected by window size
#define SPEED 50000 //global refresh rate (for usleep())
#define TIMEOUT 1

//#define DEBUG 1

void updateScore(int which, int score);
void updateScreen(SNAKE *sp, int dir, bool *collision, int *fruit_loc, bool *fruit, int *score);
int loadScore(void);
void saveScore(int highscore);

#ifdef DEBUG
void count_up(void);
#endif

int main(){
	int score = 0;
	int highscore;
	int input;
	int dir;
	bool collision;
	int fruit_loc[2]; //index 0 is x, 1 is y
	bool fruit_bool;
	bool new_hs_flag;
	int i;

	//TODO consider removing score2 altogether

	srand(time(NULL));

	initscr();
	cbreak();
	keypad(stdscr, true);
	noecho();
	curs_set(0);
	set_escdelay(1);

	while(input != 'q'){
		clear();
		
		//reset variables
		collision = false;
		fruit_bool = false;
		score = 0;
		new_hs_flag = false;
		highscore = loadScore();


		//draw current score, and high score
		updateScore(0, score);
		updateScore(1, highscore);

		//the snake will be initially drawn without calling to body.h
		//functions in body.h will update the snake and check for collision
		//updateScreen() will actually draw the snake on each update (and update the scoreboard)

		//initialize and draw starting snake
		SNAKE *player = makeSnake(START_SIZE+2, getmaxy(stdscr)/2, START_SIZE);
		//TODO consider various choices for drawing the snake (the below sets reverse video and draws spaces)
		attron(A_REVERSE);
		mvaddch(getmaxy(stdscr)/2, START_SIZE+2, ':');
		for(i = 1; START_SIZE-i > 0; i++){
			mvaddch(getmaxy(stdscr)/2, START_SIZE+2-i, ' ');
		}
		attroff(A_REVERSE);

		//TODO add something like "press any key to start!"
		getch();

		//turn timeout on and set the initial direction to "right"
		timeout(TIMEOUT);
		dir = 1;
		while(!collision){
			input = getch();
			
			//update dir based on received input (check that the input is valid as well)
			switch(input){
				case KEY_UP:
					if(dir % 2) dir = 0;
					break;
				case KEY_RIGHT:
					if(!(dir % 2)) dir = 1;
					break;
				case KEY_DOWN:
					if(dir % 2) dir = 2;
					break;
				case KEY_LEFT:
					if(!(dir % 2)) dir = 3;
			}

			updateScore(0, score);
			if(score > highscore){
				highscore = score;
				new_hs_flag = true;
			}
			updateScore(1, highscore);
			updateScreen(player, dir, &collision, fruit_loc, &fruit_bool, &score);
			//TODO consider changing SPEED depending on if moving horizontally or vertically
			refresh();
			usleep(SPEED);
		}

		deleteSnake(player);
		if(new_hs_flag) saveScore(highscore);

		//turn timeout off (wait for input again)
		timeout(-1);

		//TODO add something like "game over"
		while(input != 'q' && input != ' ' && input != 10) input = getch();

		//DEBUG
#ifdef DEBUG
		//count_up();
		getch();
#endif
	}

	endwin();
	return 0;
}

// 0 for p1 score, 1 for highscore, 2 for p2 score
void updateScore(int which, int score){
	char *title;
	int start;

	switch(which){
		case 0: //p1 score
			title = "Score";
			start = 2;
			break;

		case 1: //high score
			title = "Highscore";
			start = getmaxx(stdscr)/2 - 5;
			break;

		default:
			exit(1);
	}

	//draw the title and the score in the right location
	mvprintw(0, start, "%s", title);
	mvprintw(1, start + (which % 2 ? 5 : 3) - (!score ? 0 : (int)log10(score)), "%d", score);

	return;
}

void updateScreen(SNAKE *sp, int dir, bool *collision, int *fruit_loc, bool *fruit, int *score){
	char eyes;

	//switch case to decide "eyes" char
	switch(dir){
		case 0:
			eyes = '"';
			break;
		case 2:
			eyes = '.';
			break;
		default:
			eyes = ':';
	}

	//turn off "back"
	mvaddch(getSnakeHeady(sp), getSnakeHeadx(sp), ' ');
	//fix previous "front" (no more eyes)
	attron(A_REVERSE);
	mvaddch(getSnakeTaily(sp), getSnakeTailx(sp), ' ');
	//update the snake position
	updateSnake(sp, dir, getmaxx(stdscr), getmaxy(stdscr), collision, fruit_loc, fruit, score);
	//turn on front
	mvaddch(getSnakeTaily(sp), getSnakeTailx(sp), eyes);
	attroff(A_REVERSE);

	//TODO add logic to prevent fruit from spawning inside snake
	//draw a new fruit, if necessary (do not draw it over scoreboard)
	if(!(*fruit)){
		*fruit_loc = rand() % getmaxx(stdscr);
		*(fruit_loc+1) = rand() % (getmaxy(stdscr)-2) + 2;
		attron(A_BOLD);
		mvaddch(*(fruit_loc+1), *fruit_loc, '@');
		attroff(A_BOLD);
		*fruit = true;
	}

	return;
}

int loadScore(){
	FILE *savedata;
	char *path = malloc(sizeof(char) * 256);
	int hs = 0;

	if(getenv("HOME") != NULL) 
		sprintf(path, "%s/.local/share/simple-snake/save.bin", getenv("HOME"));
	else if(getenv("APPDATA") != NULL) 
		sprintf(path, "%s/simple-snake/save.bin", getenv("APPDATA"));
	savedata = fopen(path, "rb");

	if(savedata != NULL){
		fread(&hs, sizeof(int), 1, savedata);
		fclose(savedata);
	}

	free(path);
	return hs;
}

void saveScore(int highscore){
	FILE *savedata;
	char *path = malloc(sizeof(char) * 256);
	bool has_appdata = false; //set to true if appdata is defined (implying WIN32)

	if(getenv("HOME") != NULL) 
		sprintf(path, "%s/.local/share/simple-snake/save.bin", getenv("HOME"));
	else if(getenv("APPDATA") != NULL){
		sprintf(path, "%s/simple-snake/save.bin", getenv("APPDATA"));
		has_appdata = true;
	}
	else return;
	savedata = fopen(path, "wb");

	//if the file does not exist, create it
	if(savedata == NULL){
		if(!has_appdata){
			sprintf(path, "%s/.local/", getenv("HOME"));
			mkdir(path, 0700);

			sprintf(path, "%s/.local/share/", getenv("HOME"));
			mkdir(path, 0755);

			sprintf(path, "%s/.local/share/simple-snake/", getenv("HOME"));
			mkdir(path, 0755);

			sprintf(path, "%s/.local/share/simple-snake/save.bin", getenv("HOME"));
			savedata = fopen(path, "wb");
		}

		else {
			sprintf(path, "%s/simple-snake/", getenv("APPDATA"));
			mkdir(path, 0755);

			sprintf(path, "%s/simple-snake/save.bin", getenv("APPDATA"));
			savedata = fopen(path, "wb");
		}

		if(savedata == NULL) return;
	}

	fwrite(&highscore, sizeof(int), 1, savedata);
	fclose(savedata);

	free(path);
	return;
}
		

//debug functions start here
#ifdef DEBUG
void count_up(){
	int score = 0;
	int score2 = 0;
	int highscore = 0;

	while(1){
		score++;
		score2++;
		highscore++;
		updateScore(0, score);
		updateScore(1, highscore);
		updateScore(2, score2);
		refresh();
		usleep(50000);
	}

	return;
}
#endif