Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
terminal_gui/main.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
79 lines (61 sloc)
2.11 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "world.h" | |
#include "textbox.h" | |
#include "keyboard.h" | |
int main() | |
{ | |
// === create the world === | |
World world( 100, 100 ); // 100 tiles by 100 | |
View display( world, 60, 30 ); // create a view window of 60 by 30 tiles | |
// === create a player === | |
Entity player( '#', 20, 20, Terminal::Color::FG_RED, Terminal::Color::BG_GREEN ); | |
int playerHealth = 100; | |
int playerMoves = 100; | |
// === create some next boxes === | |
TextBox healthBox( 65, 10, 15 ); | |
TextBox moveBox( 65, 15, 15 ); | |
display.add( healthBox ); | |
display.add( moveBox ); | |
// === populate the world === | |
world.fill( Tile('-', Terminal::Color::FG_WHITE, Terminal::Color::BG_GREEN ) ); | |
Tile water( 'w', Terminal::Color::FG_BLUE, Terminal::Color::BG_BLUE ); | |
world.get( 10, 10 ) = water; | |
world.get( 20, 35 ) = water; | |
world.add( player ); | |
// === set up the screen === | |
Terminal::clear(); // wipe it | |
Terminal::cursor(false); // turn cursor off | |
Keyboard keys; | |
keys.grab(); // dont display key presses on screen | |
char c = 0; | |
while( c != 'q' ) | |
{ | |
// display player stats | |
healthBox.clear(); | |
healthBox << "Health: " << playerHealth; | |
moveBox.clear(); | |
moveBox << "Moves: " << playerMoves; | |
// move the viewpoint so it centers on the player | |
display.x = player.x - display.width/2; | |
display.y = player.y - display.height/2; | |
display.draw(); | |
c = keys.get(); // get keyboard input | |
bool moved = true; | |
switch( c ) | |
{ | |
case 'w': player.up(); break; | |
case 's': player.down(); break; | |
case 'a': player.left(); break; | |
case 'd': player.right(); break; | |
default: moved = false; break; | |
} | |
if( moved ) | |
{ | |
playerMoves -= 1; | |
if( world.get( player.x, player.y ).contents == 'w' ) // player is in water | |
playerHealth -= 1; | |
} | |
} | |
keys.release(); | |
Terminal::cursor(true); | |
return 0; | |
} |