LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2008, 11:49 AM   #1
xadrith
Member
 
Registered: Oct 2007
Distribution: Slack 12.1
Posts: 43

Rep: Reputation: 15
printing a string that unfolds with Python


I'm currently designing a text-based game with Python and I was
wondering if there was a way to "unfold" text in a string one
letter at a time. I wanted to use this for certain dramatic
effect, such as unfolding particular parts of the plotline.

An example would be...

As you move deeper into the darkness of the room, the smell of
death becomes too much to bear.

Instead of this being static and all at once, I want each letter
to be drawn one at a time, one after another.

If this is possible, I'd gratefully appreciate any information
to get this working... also, if it IS possible, is there also a
way to set the pace for scrolling the characters?

Thanks for ANY reply, whatsoever!!!

x.D
 
Old 07-23-2008, 12:51 PM   #2
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
Create a function like such:

Code:
def unfold_message(msg,delay):
    try:
        delay=float(delay)
    except ValueError:
        delay=0.100
    for char in msg:
        time.sleep(delay)
        sys.stdout.write(char)
        sys.stdout.flush()
    print
then call it like such:

Code:
my_message = """As you move deeper into the darkness of the room, 
the smell of death becomes too much to bear."""

unfold_message(my_message, 0.100)
This will add a slight delay (one-tenth of a second, though you can modify that to suit your needs) to each character.

Last edited by rocket357; 07-23-2008 at 12:56 PM.
 
Old 07-23-2008, 12:53 PM   #3
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
Of course you can, just use a loop to print one character at a time, something like:

Code:
for c in some_string:
    print c,
    delay()
(obviously you want to flesh this out a bit.)

Or if you need to do other stuff while it's printing (e.g. check for input), maybe maintain an index.

Code:
i=0
while whatever_condition:
    ... do some stuff...
    print some_string[i],
    i+=1
    ... do some more stuff...
edit:
Bah, beaten to it, and with a superior implementation.

edit2:
One thing to note, if you expect (hope?) that people will play the game more than once, you might want to add some way to break out of this delay (e.g. pressing escape will break the loop and just print the message), since once you know what to expect, you tend to just want to get on with it. Of course, this depends on how long the message is...

Last edited by smoked kipper; 07-23-2008 at 01:00 PM.
 
Old 07-23-2008, 01:05 PM   #4
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
Quote:
Originally Posted by smoked kipper View Post
One thing to note, if you expect (hope?) that people will play the game more than once, you might want to add some way to break out of this delay (e.g. pressing escape will break the loop and just print the message), since once you know what to expect, you tend to just want to get on with it. Of course, this depends on how long the message is...
Excellent point that I hadn't considered =)
 
Old 07-23-2008, 01:13 PM   #5
xadrith
Member
 
Registered: Oct 2007
Distribution: Slack 12.1
Posts: 43

Original Poster
Rep: Reputation: 15
Thank you both for your prompt replies and very helpful information. I was looking all over google for something like this and I just didn't know quite how to word it, many thanks.

Quote:
Originally Posted by smoked kipper View Post
edit2:
One thing to note, if you expect (hope?) that people will play the game more than once, you might want to add some way to break out of this delay (e.g. pressing escape will break the loop and just print the message), since once you know what to expect, you tend to just want to get on with it. Of course, this depends on how long the message is...
Also, smoked kipper, that second note will be good to keep in mind, definitely. The main goal of designing this game is to apply knowledge gained from this new language to a fun and engaging project, and as I learn more, I will go back and tweak it until, well, I'm happy with it, and... it's fun to play.

The ability to skip through dialogue is a feature no good game should do without. User freedom is paramount.

x.D
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract a number from a complicated string in python horacioemilio Programming 2 12-25-2007 09:09 PM
Python: search for string in a list or file chess Programming 3 08-22-2007 04:22 PM
LXer: LinuxWorld San Francisco unfolds next week LXer Syndicated Linux News 0 08-12-2006 09:21 PM
python: full string isn't making it through socket cs-cam Programming 1 11-26-2005 11:20 PM
convert string to integer with python Kanaflloric Programming 2 05-27-2005 11:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration