nSnake
A ncurses implementation of the classic Snake game
 All Data Structures Files Functions Variables Enumerations Macros Pages
main.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 
30 #include "engine.h"
31 #include "fruit.h"
32 #include "nsnake.h"
33 #include "player.h"
34 #include "arguments.h"
35 #include "hscores.h"
36 
37 
45  int main (int argc, char* argv[])
46 {
47  int c = 0;
48  int wait = FALSE;
49 
50  if (argc > 1)
51  args_handle (argc, argv);
52 
53  engine_init ();
54  game.state = MAIN_MENU;
55 
56  while (TRUE == TRUE)
57  {
58  switch (game.state)
59  {
60  case MAIN_MENU:
62  game.state = GAME_INIT;
63  break;
64 
65  case GAME_INIT:
66  nsnake_init ();
67  game.state = GAME;
68  break;
69 
70  case GAME:
71  if (snake.is_alive == FALSE)
72  {
73  game.state = GAME_OVER;
74  break;
75  }
76 
77  switch (c = engine_get_game_input ())
78  {
79  case ERR:
80  // If we get no input
81  break;
82 
83  case KEY_UP: case 'w': case 'W': case 'k':
85  break;
86 
87  case KEY_LEFT: case 'a': case 'A': case 'h':
89  break;
90 
91  case KEY_DOWN: case 's': case 'S': case 'j':
93  break;
94 
95  case KEY_RIGHT: case 'd': case 'D': case 'l':
97  break;
98 
99  case 'q': case 'Q':
100  game.state = EXIT;
101  break;
102 
103  #ifdef DEBUG //debug key to increase score and size (gcc -DDEBUG)
104  case 'e': case 'E':
105  player_increase_score (100);
107  break;
108  #endif
109  case 'p': case 'P':
110  game.state = PAUSED;
111  break;
112 
113  default:
114  break;
115  }
116 
117  player_update ();
119 
120  if (player_hit_fruit () == TRUE)
121  {
122  // Is this score arbitrary?
125  fruit_init ();
126  }
127 
128  if (player_hit_self () == TRUE)
129  snake.is_alive = FALSE;
130 
132  snake.is_alive = FALSE;
133 
135  break;
136 
137  case PAUSED:
139 
140  wait = TRUE;
141  while (wait == TRUE)
142  {
143  int input = engine_get_game_input ();
144 
145  switch (input)
146  {
147  case 'p': case 'P':
148  wait = FALSE;
149  game.state = GAME;
150  break;
151 
152  case 'q': case 'Q':
153  wait = FALSE;
154  game.state = EXIT;
155  break;
156 
157  default:
158  break;
159  }
160 
162  }
163  break;
164 
165  case GAME_OVER:
166  if (game.mode == BORDERS_ON)
167  {
169  hscore_store ();
170  }
171  else if (game.mode == BORDERS_OFF)
172  {
174  hscore_store ();
175  }
176 
178 
179  wait = TRUE;
180  while (wait == TRUE)
181  {
182  switch (c = engine_get_game_input())
183  {
184  case 'q': case 'Q':
185  wait = FALSE;
186  game.state = EXIT;
187  break;
188 
189  case 'm': case 'M':
190  wait = FALSE;
191  game.state = MAIN_MENU;
192  break;
193 
194 #if OS_IS_WINDOWS
195  case ' ':
196 #else
197  case '\n':
198 #endif
199  wait = FALSE;
200  game.state = GAME_INIT;
201  break;
202 
203  default:
204  break;
205  }
206  }
207 
208  engine_clean_game_over();
209  break;
210 
211  case EXIT:
212  engine_exit ();
213  nsnake_exit ();
214  break;
215 
216  default:
217  break;
218  }
219  }
220 
221  // Even though we have this here, the game always quits during
222  // the main loop
223  engine_exit ();
224  nsnake_exit ();
225  return 0;
226 }