nSnake
A ncurses implementation of the classic Snake game
|
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"
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. |
Definition of the graphical engine functions.
This file presents the nCurses specifical implementation of the game`s graphics.
#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.
void engine_init | ( | ) |
Starts the game engine.
Initializes all the stuff related to ncurses.
void engine_show_game_over | ( | ) |
Draws the Game Over screen.
Besides drawing 'Game Over', it highlights where the player died.
void engine_show_main_menu | ( | ) |
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.
void engine_show_screen | ( | ) |
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.