nSnake
A ncurses implementation of the classic Snake game
Data Structures | Enumerations | Functions | Variables
src/player.h File Reference

Declaration of the player functions, defines and globals. More...

#include "nsnake.h"
Include dependency graph for player.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  player_pieces
 The structure of each part of the player. More...
struct  player_t
 Represent the snake itself. More...

Enumerations

enum  directions { UP = 0, LEFT, DOWN, RIGHT }
 Simple enum to make the directions of the player easier to read.

Functions

void player_change_direction (int new_direction)
 Changes the snake direction based on the input received.
int player_collided_with_borders ()
 Depending on the game mode, the player may be able to hit borders or not.
void player_exit ()
 Free all memory associated with the player.
int player_hit_borders ()
 Checks collision between the snake and the walls.
int player_hit_fruit ()
 Checks if the snake has eaten the fruit.
int player_hit_self ()
 Checks if the snake have collided with itself.
void player_increase_score (int add)
 Simply increases the score by the value sent as the parameter.
void player_increase_size (int size)
 Increases the snake size.
void player_init ()
 Starts the player-related variables.
void player_teleport_borders ()
 Teleports the player around the borders.
void player_update ()
 Updates the player position, one piece at a time.

Variables

struct player_t snake
 Global player structure.

Detailed Description

Declaration of the player functions, defines and globals.


Function Documentation

Depending on the game mode, the player may be able to hit borders or not.

If it is possible, we call player_hit_borders().

See also:
player_hit_borders()
Returns:
FALSE, if the player didn't hit the borders, and TRUE if it did.

Checks collision between the snake and the walls.

Warning:
Behind you!

Checks if the snake has eaten the fruit.

Returns:
1 if yes, 0 if no.
void player_increase_size ( int  size)

Increases the snake size.

Here we have the core function of this file. Each time the snake increases its size, we expand the array that represents its body.

Note:
This is the main function of this file, because it shows how i've implemented the snake. In the future, we could use a linked list instead of an array.
void player_update ( )

Updates the player position, one piece at a time.

Start by moving the snake pieces one at a time from the last to the first, and then moves the head according to its direction.

 All Data Structures Files Functions Variables Enumerations Defines