3

I'd like to make a little animation in a console app and to do this, I've created a ASCII art:

                       ,.:b-'::'     ,`::-/,._
                  _.\\,,-/` /| |     ,' .`| ,_: ,.
               ` -_,'[ ._-|/,'Y      ` `  /  -/,-.:`.
             ,'/[(o _.. _,".`':| ___  ::/|.`][,'/,,/,'`.
           `:-\-. `','\`,-' [:_,.....'__ ^..,|-.'-  ,,\ :.
         ,'-\-.` _ `[.-  --'             '-..`'-[''\ .L-",`.
       ,L< ':],, ,` ,-                       '- , .''Y"-'.-'`
     _'_,::''/-]._,'        _ ..     .._         `.`]'. -,-:,^.
    _'/,[``.-'/'-       _ :].-''`'''''--.[         -.  .|/:L:| \
    '[' ]>] ' /'       _,'                `-  .      ``\'` ,u[<L'
   /,`.  [-' /        ,`                    ,. `.     `.`,,``..| |
  P.P.`[o'),      ,''`__-.                 ,' /'\,      .\'( ; .:]
 /`/  `  .'/XXX  `,'    . -.             ,' ,'   `.       \.>  :,`
 | \.u[/\'|X   X/\'      'v ^,         ,' ,'       \     \ `^/:.-, |
.:-<.. [||/ XXX '/         `\ `\     ,' /'         .\     || \]::`.'
| |::>,-b|     ||            ^. '---' ,'           Y|     || [./.'/\
| /_,[| ||     ||              | ,.  /             ||     || |> --||
Y=]L__`=[|     ||              | ,-  '             ||     | ::_,==:
| |i .| |'.     \            /' ,..._ `'           ||     || /L. ,
|_]L,.'-] |     [\         ,- ,'     ,' '.         ||     'P..L, / |
 v...-L\ \\     `/.      ,' ,'         `. -_      /      /|'Y\-.`::'
 \  -:,'\..,     \`\  _,' /'             ^,      /,        .[-` ,'
  )_  '   '.\     `[`,  /'                 `.'\,''       /`..:-  |
   <`...,`./ `      `[.=                    `= /        ,_/-/.` ''
    \`[,-'.v'\`.      ".`._               _.:'         '-' '.>/ '
      [,_,L!`, [`       '  `-...______.,-'           v /'''|]:.'
      ``\ //'. .`.`.         ' --- -- ''          ',-|' '  b''
       `.Yv-]-, ',-.'-                          ,-'''\ [,.-,'
         `['./,L|'`.],..  .__           _    ,.',. .[ ]'v']
           `v-/|  <." ..`\-.. ::_   |: ,.-\\',.\'-  ' '','
             `.:`[/   / ' > .:',.||::_|/--'. ``,\ '/: '
                -',:.\`[| .'/,[/ L',]   ^  /``,] ``-`
                    -\|, -,: |`-|||/ |``.,..\`_,-'
                       ''--.' ==='|::::\'-''

(Yeah, it's a roulette)

I created the same ASCII arts with the ball in different positions (like also the cross on the roulette).

My problem is, I think, quite tricky: I'd like to print the roulette to the console something like 4 times per second (maybe more, do not exactly know) at the same place in order that it's looks like a gif. I've no problem to load the files and transform them to string, but I didn't find a way to rewrite at the same place (the \r won't work for multiline text).

It would be great, if I could do it without using any library like curses (although I'm a big fan of ncurses in Python!).

Henry Twist
  • 5,666
  • 3
  • 19
  • 44
Laurent Meyer
  • 2,766
  • 3
  • 33
  • 57
  • So I did a draft version, so if you want to take a look: https://github.com/lolobosse/CurseRoulette (works on OSX and Linux, didn't test it on Windows) – Laurent Meyer Nov 12 '14 at 11:01

2 Answers2

3

I think curses or something like it is going to be necessary.

As I remember it (curses was a long time ago), there were two points to curses -- different terminals, and efficiency on slow terminal connections. You're going to face both problems, depending on how terminals are connected and how often you end up wanting to repaint the screen.

Curses analyzed the desired output to determine the most efficient way to draw stuff -- should it position the cursor three times and draw things separately, or output multiple lines and just draw them once? And what were the character sequences necessary to position the cursor?

Good luck with it, regardless.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Ok thanks, it wasn't the answer I was expecting but if it's the only way... And could it work with ascii codes ? – Laurent Meyer Nov 01 '14 at 16:05
  • Not sure what you mean -- do you mean, can you encode a string as ASCII? You can output a string to a stream as ASCII, look up encoding of streams for details. You cannot store an internal string as ASCII in Java, but then I don't know any situation where you would need to. – arcy Nov 01 '14 at 19:03
  • No these very old code with Escape caracters to move the cursor but it's not a problem, I'm using lanterna (curses) and it's working. I keep you updated :) – Laurent Meyer Nov 01 '14 at 19:51
1

In order that you have something to start with, this code prints an outer thick circle, an inner thin circle, and a kross in the center of your shape with random ASCII punctuation symbols:

                            *$%,'"++-(                            
                      (,%,(#)&"("%#!'/%*.*)&                      
                %%%&"&'$#'*#/,)$-,/,$/'..*+/*%--#)                
              &.#'''&.(,%)""$'-/.&.".(+)$##,)%.*))'$              
            )-.+,#*'*./&+)"$#,,$+')*%*.")"$(%-)&-$/(-&            
          $(%#/)'*-!%)."#'              *%..&**,*.!%.+/'          
        ('&!"%!-$/#,*/                      !.-&.)+!#!."/!        
      *+#/!)&$'+-$                              &$//)$*/.#(*      
    .$-+&'+).'!%          .'/+,-&#&!%,,%          ($$'),-,&*!'    
    '*"#!/(.&(        /"$%!'%%'+$+$/&-/!(+/!        %".%).%&//    
    $+''.)/&.#      **""**              )&/(*.      /,-%-*(,'&    
  +&*//##+.&      ,'&*&$                  ,+$$)-      $)%$#))!..  
  /'"#!$%.'&      +#/)  (&              -.  ".--      &(/+'#,)%*  
  '$.#-,//      $&'!      &+          "#      !/,'      ,-+&/$#,  
%'%+)&,,$'      ".$(        .!      (-        +$'"      /(&'.*,(&*
!#/%"/.-+)      $+,.          "&  .-          +($#      )'.#!&"(./
%&$"%%/).$      -%"#            &%            $%'$      ,#"$,(-#$.
!$"$#*(#*)      +*&#          &*  $#          ###&      *'$/#,.)(.
"$(*-'.*!(      $""/        ),      %,        $/&(      %*&$$)'-,&
  /,#-$-**      +**-      %$          $-      (.%,      %$(%*)+'  
  #*,**,&(%*      /"*"  &/              #-  '&##      ,./+'$&,&(  
  &!!&*--*+%      /#-.)&                  !"#/%-      ',".&-#!+*  
    '$'"!"*"$"      !.)(,"              .,&,.)      -+)$//$/*+    
    '/#&"+%%-"        ',+*,&&)/!**-$,$/(*!+'        .!#-$'..$(    
    -#&%!-%),"--          --+#-,$!#-#$/+          +/'#*#*#*+./    
      ()$"**!$//'/                              +"+(+/,)%)*"      
        "+#'.$'-!*&"&$                      )/!..,(-&.*/#(        
          )-#%/#!!!.$..#)*              $'/%-/+#+)%.-*'.          
            /'#-+.%#$),)$/**%%&++%#,)"(+/,&((-($*'"+'*            
              *!**!+,#&-&-%"&&..,*/(&'!/&##-/&*+%%,"              
                /."($&$'!!))/&$#%))"/*&&)!"'$%/,+-                
                      (+$.-)%#*#!$.)*###/*&(                      
                            #"*'"*#+(!                            

Java 8:

int r = 16;
IntStream.rangeClosed(-r, r)
        .map(Math::abs)
        .peek(i -> IntStream.rangeClosed(-r, r)
                .map(Math::abs)
                // printing a kind of thick circle,
                // thin circle and cross in the center
                .mapToObj(j -> i == j && i < r/3 || // cross
                        // outer thick circle
                        Math.abs(i*i + j*j - r*r/1.3) < 4*r ||
                        // inner thin circle
                        Math.abs(i*i + j*j - r*r/4) < r
                        // cast to String
                        ? ""
                        // two random ASCII punctuation
                        // symbols: !"#$%&'()*+,-./
                        + (char) (33 + Math.random() * 15)
                        + (char) (33 + Math.random() * 15)
                        // two whitespaces
                        : "  ")
                .forEach(System.out::print))
        .forEach(i -> System.out.println());

See also: Print out an ASCII circle and axes