nSnake
A ncurses implementation of the classic Snake game
 All Data Structures Files Functions Variables Enumerations Macros Pages
Functions | Variables
player.c File Reference

Definition of the player functions. More...

#include <stdlib.h>
#include "nsnake.h"
#include "engine.h"
#include "player.h"
#include "fruit.h"
Include dependency graph for player.c:

Go to the source code of this file.

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. More...
 
void player_exit ()
 Free all memory associated with the player.
 
int player_hit_borders ()
 Checks collision between the snake and the walls. More...
 
int player_hit_fruit ()
 Checks if the snake has eaten the fruit. More...
 
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. More...
 
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. More...
 

Variables

struct player_t snake
 Global player structure.
 

Detailed Description

Definition of the player functions.

Definition in file player.c.

Function Documentation

int player_collided_with_borders ( )

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.

Definition at line 70 of file player.c.

int player_hit_borders ( )

Checks collision between the snake and the walls.

Warning
Behind you!

Definition at line 101 of file player.c.

int player_hit_fruit ( )

Checks if the snake has eaten the fruit.

Returns
1 if yes, 0 if no.

Definition at line 116 of file player.c.

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.

Definition at line 159 of file player.c.

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.

Definition at line 219 of file player.c.