nSnake
A ncurses implementation of the classic Snake game
 All Data Structures Files Functions Variables Enumerations Macros Pages
player.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2  * nSnake - The classic snake game with ncurses. *
3  * Copyright (C) 2011-2012 Alexandre Dantas (kure) *
4  * *
5  * This file is part of nSnake. *
6  * *
7  * nSnake is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  * homepage: http://sourceforge.net/projects/nsnake/ *
21  * *
22 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
23 
29 #include <stdlib.h>
30 
31 #include "nsnake.h"
32 #include "engine.h"
33 #include "player.h"
34 #include "fruit.h"
35 
36 
37 struct player_t snake;
38 
39 
42 void player_change_direction (int new_direction)
43 {
44  if ((new_direction == UP) && (snake.direction != DOWN) &&
45  (snake.direction != UP) )
46  snake.direction = UP;
47 
48 
49  else if ((new_direction == LEFT) && (snake.direction != RIGHT) &&
50  (snake.direction != LEFT) )
51  snake.direction = LEFT;
52 
53  else if ((new_direction == DOWN) && (snake.direction != UP) &&
54  (snake.direction != DOWN) )
55  snake.direction = DOWN;
56 
57  else if ((new_direction == RIGHT) && (snake.direction != LEFT) &&
58  (snake.direction != RIGHT) )
59  snake.direction = RIGHT;
60 }
61 
62 
71 {
72  if (game.mode == BORDERS_ON)
73  return player_hit_borders ();
74 
75  else if (game.mode == BORDERS_OFF)
77 
78  else if (game.mode == FREE_MODE) //secret debugging-mode
79  {}
80 
81  return FALSE;
82 }
83 
84 
87 void player_exit ()
88 {
89  if (snake.body != NULL)
90  {
91  free (snake.body);
92  snake.body = NULL;
93  }
94 }
95 
96 
102 {
103  if ((snake.body[0].x < 1) || (snake.body[0].x > (screen.width-2)) ||
104  (snake.body[0].y < 2) || (snake.body[0].y > (screen.height-2)))
105  {
106  return TRUE;
107  }
108  return FALSE;
109 }
110 
111 
117 {
118  if ((snake.body[0].x == fruit.x) && (snake.body[0].y == fruit.y))
119  return TRUE;
120  else
121  return FALSE;
122 }
123 
124 
128 {
129  int i;
130  for (i = (snake.size-1); i > 1; i--)
131  {
132  if ((snake.body[0].x == snake.body[i].x) &&
133  (snake.body[0].y == snake.body[i].y))
134  {
135  return TRUE;
136  }
137  }
138  return FALSE;
139 }
140 
141 
144 void player_increase_score (int add)
145 {
146  snake.score += add;
147 }
148 
149 
160 {
161  int piece_size = sizeof (struct player_pieces);
162 
163  snake.size += size;
164 
165  snake.body = realloc (snake.body, (snake.size * piece_size));
166  if (snake.body == NULL)
167  nsnake_abort ("Memory Error!\n");
168 }
169 
170 
173 void player_init ()
174 {
175  snake.body = NULL;
176 
177  snake.is_alive = TRUE;
178  snake.speed = 1;
179  snake.score = 0;
180  snake.direction = RIGHT;
181 
182  snake.size = 3;
183  snake.body = malloc (snake.size * sizeof (struct player_pieces));
184  if (snake.body == NULL)
185  nsnake_abort ("Memory Error!\n");
186 
187  int i;
188  for (i = 0; i < snake.size; i++)
189  {
190  snake.body[i].x = screen.width/2;
191  snake.body[i].y = screen.height/2;
192  }
193 }
194 
195 
199 {
200  if (snake.body[0].x < 1)
201  snake.body[0].x = screen.width-2;
202 
203  if (snake.body[0].y < 2)
204  snake.body[0].y = screen.height-2;
205 
206  if (snake.body[0].x > (screen.width-2))
207  snake.body[0].x = 1;
208 
209  if (snake.body[0].y > (screen.height-2))
210  snake.body[0].y = 2;
211 }
212 
213 
220 {
221 // body
222  int i;
223  for (i = (snake.size-1); i > 0; i--)
224  {
225  snake.body[i].x = snake.body[i-1].x;
226  snake.body[i].y = snake.body[i-1].y;
227  }
228 
229 // head
230  if (snake.direction == UP)
231  snake.body[0].y -= snake.speed;
232 
233  else if (snake.direction == LEFT)
234  snake.body[0].x -= snake.speed;
235 
236  else if (snake.direction == DOWN)
237  snake.body[0].y += snake.speed;
238 
239  else if (snake.direction == RIGHT)
240  snake.body[0].x += snake.speed;
241 }