need help with game

Raseliarison nirinA nirina at mail.blueline.mg
Sun Apr 6 18:25:09 EDT 2003


"BBFoto" <bbfoto2002 at netscape.net> wrote in news:
3e8b1b0f.8380222 at news-server...
> I did a search on the PySol directory.  Nowhere is there a file called
> "PySol.py".  There is a series of files in sequence named
> "PySol_15.pyc"  "PySol_16.pyc"  "PySol_20..pyc"  "PySol_21.pyc" and
> "PySol_22.pyc".

Hmmm.... usually you do not have to deal with compiled files, but you can
try the following suggestion. Do this from the Python interpreter (the
prompt begins with >>> ) and not from dos prompt.

>>> yourdir='c:/program files/pysol/pysol-4.80/data'

to short cut the name directory, then

>>> import os
>>> os.chdir(yourdir)

This changes directory to yourdir. To confirm that, you can get the current
working directory by typing:

>>> os.getcwd()

You have also to set the path to yourdir, so Python finds correctly his way.
You do that with sys module:

>>> import sys
>>> sys.path

This prints a list of all paths Python needs to run properly, you append the
path to yourdir to this list with:

>>> sys.path.append(yourdir)

and yourdir appears in the list when you type again:

>>> sys.path

Now, your are able to load those pyc files with, for example:

>>> import PySol_22

If this succeds, your game should start or the prompt should return to

>>>

without complain. Typing:

>>> PySol_22

should return something like:

<module 'PySol_22' from 'c:/program
files/pysol/pysol-4.80/data\PySol_22.pyc'>

To abstract what you've done, one can say that you have loaded into the
interpreter an OBJECT called 'PySol_22' with all its ATTRIBUTES (which
attributes are originally defined in the file 'PySol_22.py' ). Get the list
of attributes with the built-in function dir:

>>> dir(PySol_22)

this returns a list like:

['__builtins__', '__doc__', '__file__', '__name__', 'afunction', 'aclass',
'another', 'main']

To use them, remember how to use os and sys modules. In this hypothetical
list, you can output some doc and request help with:

>>> print PySol_22.__doc__

>>> help(PySol_22)

and

>>> help(PySol_22.main)

i speculate 'main' is the function to execute your game, so:

>>> PySol_22.main()

should start it. That is one way, to run your game.
You may wonder why compiled Python files are unusual?
Ordinary, when creating Python program, one writes Python scripts in a text
editor (a new window with idle, edit with dos, notepad with w98 ...) then
saves it in a file with .py extension. Let's try:

save as 'hello.py' the following line:

print "hello, Python world!"

and as 'list.py' in the same directory the next 4 lines :

from os import listdir, getcwd
for file in listdir(getcwd()):
    if file.endswith('py') or file.endswith('pyc'):
        print file

To run a Python program, start a dos session, change to the directory where
you have saved the files above and type at the prompt (you will type exactly
the same thing even if you are in a bash or csh shell on Lunix box), for
example:

python list.py

to run 'list.py'. This will display at the screen all files which end with
'py' and 'pyc' and among them 'hello.py' and 'list.py'. Note that there is
no compiled file, no .pyc file, associated with them (for the moment!). I
let you run 'hello.py' and see what it does.
If you just type

python

at the dos prompt, a Python shell, an interpreter, starts. On my machine,
the prompt turns to this:

Python 2.3a1 (#38, Dec 31 2002, 17:53:59) [MSC v.1200 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license" for more information.
>>>

Now, to execute a Python program, you can use the built-in function
execfile. Below, i copy some code and their predicted behavior:

>>> execfile('list.py')
list.py
hello.py
>>> execfile('hello.py')
hello, Python world!
>>> execfile('list.py')
list.py
hello.py

Note again, there is no file with .pyc extension associated with 'list.py'
or 'hello.py'.
Another way to run Python program is to import them, e.g.:

>>> import hello
hello, Python world!

But now, if you try first:

>>> execfile('list.py')

then:

>>> import list

you will see where compiled Python files come from. Typing the module name
tells you from where the module is loaded.

>>> list
<module 'list' from 'list.py'>

Exit now the Python shell and return to dos session:

>>> import sys; sys.exit()

So, when importing module the machine creates compiled Python files. Can
they be used without 'pure' Python file?
Create another directory, copy there those .pyc files and change into this
directory; from your dos prompt, type:

mkdir pyc
copy *.pyc pyc
cd pyc

If you try to execute a compiled file with

python hello.pyc

the program may run but return error like:

run_pyc_file: nested_scopes: 0
or
RuntimeError : Bad magic number in .pyc file

In this new directory, start again a Python shell. Of course, built-in
function execfile does not work now as it expects a human readable code, and
a .pyc file is machine readable code, hence the syntax error you
experienced. But import works fine:

>>> import hello
hello, Python world!
>>> import list
list.pyc
hello.pyc
>>> hello
<module 'hello' from 'hello.pyc'>
>>> list
<module 'list' from 'list.pyc'>

ouf, that becomes too long.
I hope this helps you to clarify your first contact with Python.
Distributing Python program in compiled package is not (in my own and humble
opinion) the spirit of Python, Python is open source and free. To encourage
you to go on, below you find a program you can use, modify, improve... Cut
and paste it and save as 'sfkb.py' or whatever you want. To run, type as
usual at a dos prompt:

python sfkb.py

or double-click on the icon.
The program uses winsound module, so it works only on win32 platorm.

Python is very well documented, but do not hesitate to ask the list, you are
in the Python world!

Don't forget, Python is free!
I 'll be glad if blind people can hear the sound from keyboard

i do not pretend to be a Python expert, i'm just a Python user
all other people in the Python-list are more authoritative than i.

Sincerly yours
nirinA

### -------cut here --------------
try:
    import winsound
except ImportError:
    print "this script requires winsound module"

import os
import math
import wave
from Tkinter import *

print "This may take a while...(about 6 minutes on my machine)\n\
When the 25 wave files are build, two windows will appear\n\
activate the one with title Keyboard\n\
and you can play sounds by pressing alphabetical keys.\n\
\n\
Next time you use this program, comment out wave generator code\n\
to avoid rebuilding 25 wave files.\n\
\n\
Create a directory named /images and copy on it some GIF images,\n\
uncomment out canvas example code to demonstrate some Tkinter features\n\
Have fun!"

wavename = []
parameters = (1,2,44100,44100,'NONE','not compressed')

for frequence in range(100,725,25):
    wn = 'f' + str(frequence) + '.wav'
    wavename.append(wn)

###comment out wave generator code when wave files are created
###--wave generator--
    wo = wave.open(wn, 'w')
    wo.setparams(parameters)

    for i in range(1, 44101):
        amplitude = int(30000*abs(math.sin(math.pi*i/frequence)*
                                  math.sin(math.pi*i/(frequence-5.))))
        wp = wave.struct.pack('h', amplitude)
        wo.writeframes(wp)

    wo.close()
###--wave generator--

root = Tk()
root.title("Monty Python's Flying Circus")
Label(root, text='  Python  '*10, fg='blue').pack()

def call(event):
    print "ni here, this is a sound generator"
def press_a(event):
    winsound.PlaySound(wavename[0],winsound.SND_ASYNC)
    return
def press_b(event):
    winsound.PlaySound(wavename[1],winsound.SND_ASYNC)
    return
def press_c(event):
    winsound.PlaySound(wavename[2],winsound.SND_ASYNC)
    return
def press_d(event):
    winsound.PlaySound(wavename[3],winsound.SND_ASYNC)
    return
def press_e(event):
    winsound.PlaySound(wavename[4],winsound.SND_ASYNC)
    return
def press_f(event):
    winsound.PlaySound(wavename[5],winsound.SND_ASYNC)
    return
def press_g(event):
    winsound.PlaySound(wavename[6],winsound.SND_ASYNC)
    return
def press_h(event):
    winsound.PlaySound(wavename[7],winsound.SND_ASYNC)
    return
def press_i(event):
    winsound.PlaySound(wavename[8],winsound.SND_ASYNC)
    return
def press_j(event):
    winsound.PlaySound(wavename[9],winsound.SND_ASYNC)
    return
def press_k(event):
    winsound.PlaySound(wavename[10],winsound.SND_ASYNC)
    return
def press_l(event):
    winsound.PlaySound(wavename[11],winsound.SND_ASYNC)
    return
def press_m(event):
    winsound.PlaySound(wavename[12],winsound.SND_ASYNC)
    return
def press_n(event):
    winsound.PlaySound(wavename[13],winsound.SND_ASYNC)
    return
def press_o(event):
    winsound.PlaySound(wavename[14],winsound.SND_ASYNC)
    return
def press_p(event):
    winsound.PlaySound(wavename[15],winsound.SND_ASYNC)
    return
def press_q(event):
    winsound.PlaySound(wavename[16],winsound.SND_ASYNC)
    return
def press_r(event):
    winsound.PlaySound(wavename[17],winsound.SND_ASYNC)
    return
def press_s(event):
    winsound.PlaySound(wavename[18],winsound.SND_ASYNC)
    return
def press_t(event):
    winsound.PlaySound(wavename[19],winsound.SND_ASYNC)
    return
def press_u(event):
    winsound.PlaySound(wavename[20],winsound.SND_ASYNC)
    return
def press_v(event):
    winsound.PlaySound(wavename[21],winsound.SND_ASYNC)
    return
def press_w(event):
    winsound.PlaySound(wavename[22],winsound.SND_ASYNC)
    return
def press_x(event):
    winsound.PlaySound(wavename[23],winsound.SND_ASYNC)
    return
def press_y(event):
    winsound.PlaySound(wavename[24],winsound.SND_ASYNC)
    return

### z key plays windows sound

def press_z(event):
    winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
    return

###--canvas example
##list_image = os.listdir('./images/')
##from random import choice
##image_name = choice(list_image)
##img = PhotoImage(file='./images/'+image_name)
##canvas = Canvas(root, width=img.width(), height=img.height())
##canvas.bind("<Button-1>",call)
##canvas.create_image(0,0,anchor=NW,image=img)
##canvas.pack()
###--canvas example

top = Toplevel()
Label(top, text='Sound From KeyBoard',
      font=('Helvetica',20), fg='cyan').pack()
Label(top, text='Python is Cooooool!', fg='green').pack()
Label(top, text='Nirvana of the Snake', fg='yellow').pack()
top.title('Keyboard')
top.bind("<a>",press_a)
top.bind("<b>",press_b)
top.bind("<c>",press_c)
top.bind("<d>",press_d)
top.bind("<e>",press_e)
top.bind("<f>",press_f)
top.bind("<g>",press_g)
top.bind("<h>",press_h)
top.bind("<i>",press_i)
top.bind("<j>",press_j)
top.bind("<k>",press_k)
top.bind("<l>",press_l)
top.bind("<m>",press_m)
top.bind("<n>",press_n)
top.bind("<o>",press_o)
top.bind("<p>",press_p)
top.bind("<q>",press_q)
top.bind("<r>",press_r)
top.bind("<s>",press_s)
top.bind("<t>",press_t)
top.bind("<u>",press_u)
top.bind("<v>",press_v)
top.bind("<w>",press_w)
top.bind("<x>",press_x)
top.bind("<y>",press_y)
top.bind("<z>",press_z)

Button(top, text='destroy',
       fg ='red', command=top.destroy).pack()
Button(root, text='quit',
       fg ='red', command=root.quit).pack()
root.mainloop()

###----end here ----------------------------










More information about the Python-list mailing list