nSnake
A ncurses implementation of the classic Snake game
Defines | Enumerations | Functions | Variables
src/engine.c File Reference

Definition of the graphical engine functions. More...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ncurses.h>
#include "engine.h"
#include "fruit.h"
#include "player.h"
#include "hscores.h"
Include dependency graph for engine.c:

Defines

#define FIXED_WIDTH   80
#define FIXED_HEIGHT   24
#define REFRESH_DELAY   (20000 + ((9 - game.level) * 25000))
 This formula determines a time (in microseconds) the game must wait between each screen refresh.

Enumerations

enum  Colors {
  BLACK_WHITE = 1, CYAN_BLACK, BLUE_BLACK, WHITE_BLACK,
  GREEN_BLACK, RED_BLACK
}
 Simple enum to make the colors easier to read: FOREGROUND_BACKGROUND.

Functions

void draw_background ()
 Just erases everything to black.
void draw_borders ()
 Draws the window border, according to the game.mode.
void draw_fruit ()
 Draws the current fruit on screen.
void draw_fruit_bonus ()
 Draws the bonus value of the current fruit.
void draw_player ()
 Draws the snake - from the head to the whole body.
void draw_score ()
 Prints the current score.
void engine_exit ()
 Exits and dealocates the memory required by ncurses.
void engine_get_game_input ()
 Get the user input during game and make the right decisions.
void engine_init ()
 Starts the game engine.
void engine_show_game_over ()
 Draws the Game Over screen.
void engine_show_main_menu ()
 Displays the main menu and gets the user input from it.
void engine_show_pause ()
 Prints the pause message.
void engine_show_screen ()
 Completely draws the screen during game.
void get_game_over_input ()
 Waits for an user action during the 'Game Over' screen.
int get_main_menu_input (int *speed_cur_option)
 Gets the input for the main menu.
void get_pause_input ()
 Just waits untill the user either unpauses the game or exits.
void start_atrribute (int attr)
 Wrapper to the attron() function, in case the current terminal doesn't support colors.

Variables

struct screen_t screen
 Global screen structure.

Detailed Description

Definition of the graphical engine functions.

This file presents the nCurses specifical implementation of the game`s graphics.


Define Documentation

#define REFRESH_DELAY   (20000 + ((9 - game.level) * 25000))

This formula determines a time (in microseconds) the game must wait between each screen refresh.

It is based on the current game level, so the higher its value, the lowest is the delay and faster the game will appear to be.

Note:
1,000,000 microseconds == 1 second

Function Documentation

void engine_init ( )

Starts the game engine.

Initializes all the stuff related to ncurses.

Note:
If some engine-specific initialization fails, the game aborts.
Todo:
There must have a game init function or something where i could put this

Draws the Game Over screen.

Besides drawing 'Game Over', it highlights where the player died.

Displays the main menu and gets the user input from it.

This function blocks the game execution and waits for user input, refreshing the main menu screen according to the options selected.

Completely draws the screen during game.

The usleep() function interrupts the program for 'n' microseconds. It was difficult to get a stable value for the game progression.

Note:
This is the main function of this file because it shows logically how the process of drawing the screen sould be
 All Data Structures Files Functions Variables Enumerations Defines