playable with ncurses, no level management

This commit is contained in:
Gwenhael Le Moine 2011-07-01 16:32:15 +02:00
parent e0734e9cd8
commit a098bba42b

59
star.c
View file

@ -2,6 +2,8 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <ncurses.h>
/* levels have fixed, hardcoded dimensions */ /* levels have fixed, hardcoded dimensions */
#define LEVEL_HEIGHT 9 #define LEVEL_HEIGHT 9
#define LEVEL_WIDTH 16 #define LEVEL_WIDTH 16
@ -75,7 +77,7 @@ int won_or_not( struct state *s )
return( count_gifts( s ) == 0 ); return( count_gifts( s ) == 0 );
} }
void move( struct state *s, direction where ) void make_a_move( struct state *s, direction where )
{ {
int dx = 0, dy = 0, tmpx, tmpy, *item_coord; int dx = 0, dy = 0, tmpx, tmpy, *item_coord;
item_coord = malloc( sizeof( int ) * 2 ); item_coord = malloc( sizeof( int ) * 2 );
@ -382,37 +384,38 @@ void display_level( struct state *s )
{ {
int i, j, *ball, *cube; int i, j, *ball, *cube;
printf( "%i gifts left", count_gifts( s ) ); mvprintw( 0, 0, "%i gifts left, ", count_gifts( s ) );
if ( won_or_not( s ) ) { if ( won_or_not( s ) ) {
printf( ", You WON !\n"); mvprintw( 0, 17, "You WON !\n");
} }
else { else {
printf( ", go on.\n"); mvprintw( 0, 17, "go on.\n");
} }
for( i = 0 ; i < LEVEL_HEIGHT ; i++ ) { for( i = 0 ; i < LEVEL_HEIGHT ; i++ ) {
for( j = 0 ; j < LEVEL_WIDTH ; j++ ) { for( j = 0 ; j < LEVEL_WIDTH ; j++ ) {
switch( get_cell( s, j, i ) ) { switch( get_cell( s, j, i ) ) {
case WALL: case WALL:
printf( "##" ); mvprintw( i+1, j*2, "##" );
break; break;
case VOID: case VOID:
printf( " " ); mvprintw( i+1, j*2, " " );
break; break;
case BALL: case BALL:
printf( "()" ); mvprintw( i+1, j*2, "()" );
break; break;
case CUBE: case CUBE:
printf( "[]" ); mvprintw( i+1, j*2, "[]" );
break; break;
case GIFT: case GIFT:
printf( "<>" ); mvprintw( i+1, j*2, "<>" );
break; break;
default: break; /* ignore newlines */ default: break; /* ignore newlines */
} }
} }
printf( "\n" ); /* printf( "\n" ); */
} }
refresh();
} }
int main( int argc, char* argv[] ) int main( int argc, char* argv[] )
@ -420,6 +423,14 @@ int main( int argc, char* argv[] )
int i = 0; int i = 0;
struct state *s = malloc( sizeof( struct state ) ); struct state *s = malloc( sizeof( struct state ) );
/* ncurses */
WINDOW *w_main = initscr( );
cbreak();
noecho();
nonl();
intrflush( w_main, FALSE );
keypad( w_main, TRUE );
load_level( s, levels[ 0 ] ); load_level( s, levels[ 0 ] );
display_level( s ); display_level( s );
@ -427,13 +438,33 @@ int main( int argc, char* argv[] )
/* char* moves = "drdluruldrdlrulurudlurdul"; */ /* char* moves = "drdluruldrdlrulurudlurdul"; */
int key; int key;
do { do {
key = getchar();
move( s, key /* moves[ i ] */ );
display_level( s ); display_level( s );
i++; key = getch();
} while( ( ! won_or_not( s ) ) /* && ( moves[ i ] != '\0' ) */ ); switch( key ) {
case KEY_UP:
make_a_move( s, UP );
break;
case KEY_DOWN:
make_a_move( s, DOWN );
break;
case KEY_LEFT:
make_a_move( s, LEFT );
break;
case KEY_RIGHT:
make_a_move( s, RIGHT );
break;
default:
break;
}
/* make_a_move( s, key /\* moves[ i ] *\/ ); */
/* i++; */
} while( ( ! won_or_not( s ) ) && (( key != 'q' ) && ( key != 'Q' )) /* && ( moves[ i ] != '\0' ) */ );
display_level( s ); display_level( s );
free( s ); free( s );
echo();
nocbreak();
endwin();
return 0; return 0;
} }