nSnake
A ncurses implementation of the classic Snake game
 All Data Structures Files Functions Variables Enumerations Macros Pages
fruit.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 #include <time.h>
31 
32 #include "fruit.h"
33 #include "engine.h"
34 #include "player.h"
35 
36 
37 struct fruit_t fruit;
38 
39 
44 void fruit_init ()
45 {
46  int top = 50;
47  int bottom = 10;
48  int valid_fruit = FALSE;
49 
50 
51  // Create a seed for generating random numbers based on the time
52  srand (time (NULL));
53 
54  while (valid_fruit == FALSE)
55  {
56  // Generate a random number between the screen limits
57  fruit.x = rand() % ((screen.width-2) - 1 + 1) + 1;
58  fruit.y = rand() % ((screen.height-2) - 2 + 2) + 2;
59 
60  // Generates a random value between 'top' and 'bottom'
61  fruit.bonus += rand() % (top - bottom + 1) + bottom;
62 
63  if (fruit.y != screen.height-1)
64  valid_fruit = TRUE;
65 
66  // Lets make sure the fruit doesnt start at the snake body
67  int i;
68  for (i = 0; i < (snake.size-1); i++)
69  {
70  if ((fruit.x == snake.body[i].x) &&
71  (fruit.y == snake.body[i].y))
72  {
73  valid_fruit = FALSE;
74  break;
75  }
76  }
77  }
78 }
79 
80 
84 {
85  if (fruit.bonus > 0)
86  fruit.bonus--;
87 }