Re: Python, input, and breaking the program

oo yay! I'm the first to offer a technical explanation for once. Squeeee!
OK so, here goes:
There are two types of program, GUI and command line.
command line programs are programs that run, you guessed it, in yhe command line. This is that funny window that has the name "c:\windows\bla bla bla"
A GUI program is a program that has a graphical user interface, or, a separate window, with all those wonderful buttons and key press events and stuff!

So:
raw_input(), this is a command line function. It is designed to get input from the command line, and is what is called a blocking function. This means that if you have a loop like this:
n=0
while(n<10):
if(n==5):
  print("tell me something!");
  print(raw_input())
n=n+1

the loop will run super fast until n=5, then when n=5, it will stop while it waits for the user to finish writing, and as soon as the player hits enter, it will continue.

Simulating this in an audio game, however, is difficult. Or at least, more difficult than simply writing: raw_input().

Pygame, to the best of my knowledge, has no text box or similar.
Sure, you could gut your program, move to wx python, and have a lot of trouble with sound, but why!
Pygame gives you everything you need for an audio game right there, and with only a few tweaks, we can give it our own text box.
I'm not a fan of writing peoples code for them, so the below is just seudo code, meaning, it is not real and just outlines things:
textfield=""
in_text_field=0
def key_pressed(event):
if(key is enter):
   in_text_field=2
else:
  textfield=textfield+event.letter

don't use event.letter, that's not a thing.
then, in your game loop:
if(in_text_field==1)
do nothing because we're waiting for text to be finished.
elif(in_text_field==2):
aha! we have some text, and a text box was recently closed! do something with it, such as:
speak("Mua ha ha ha ha! I know what you typed! you typed: "+textfield+". Clever!")
textfield=""
in_text_field=0

then in your pygame event loop, simply put a call to game_loop, and make sure not to include while loops in that game loop because otherwise it sticks, and besides the event loop is already a game loop.

Few! ok this got a lot more wordy than I meant it to, and the code is only seudo code, but that's the basic idea.
I'll probably end up covering something like this in my next batch of tutorials, but I hope that helps!

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : keithwipf1 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Lucas1853 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Lucas1853 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dardar via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Lucas1853 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Lucas1853 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Lucas1853 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : kianoosh via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Lucas1853 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector

Reply via email to