Now, Now!
Patience, I will show you why you got the path /data/... it is because the
OS.Path gives you the location you are in and that is the unprinted default and
adds onto that the print-out you got on the screen for that was the join part
of the path and only the join part of the path...
fullname = os.path.join('data', name)
self.image, self.rect = load_image('chimp.bmp', -1)
NOTE:
Then name in parenthesis is the chimp.bmp. In other words the variable with
that name inside, pointing to it.
SO:
os.path.join(... will be the path you reside in at the time you ran your
program. In other words that will be the c:\python25\ directory. Or if you are
in examples it would be c:\python25\examples. But since you can not find that
file name it is because you are not in the examples folder.
NOW:
Since you have placed the Chimp.py file in your Python25 directory you
must now locate the data directory which is mentioned in the program below and
copied above for this example. Remember the only thing mentioned in the program
after the join statement is /dat/ which is correct for the file is in the data
folder, but, you are not in the examples folder where the data folder resides.
You are in the Python25 folder or directory where the examples directory or
folder resides.
Thus, you either change the join statement to include the examples folder
or move into the examples folder and run from there. Which is what the batch
file does. It places you into the examples folder to run the Chimp.py module.
Which when running it is running from the examples folder and not the Python25
folder, so the os.path will have inside of it c:/Python25/examples/ and the
join will append onto it, join, the path inside the '...' or data/
Now my example or batch file is placing you into the folder you wish to be
in. The examples folder has example programs. The data folder has data for
these games/programs/modules.
so the batch file starts you in the Python25 folder, jumps you into the
examples folder, then runs the Chimp.py from that folder. Once you exit the
game you go back inside the batch file which has a CD .. or CD c:\python25
command to move back to the previous directory or the the python directory...
In other words the command CD .. means move back one directory, or folder.
FINALLY:
I gave you the batch file to use that you can use anywhere inside any
folder. A batch file with no path and the name for that batch file can be named
to correspond to what you wish to run. Like EX.BAT for example programs and
will jump into examples folder if you wish to start and remain inside the
Python25 folder. Having no path inside of the batch file allows you to be
anywhere and go anywhere. By deleting the CD .. at the end means you wish to
stay in that folder so it is dependent on what you intend to do.
Now in the batch file I added %1 %2 %3 ... these correspond to any added
switches or commands placed on the command line before hitting enter to run the
program from the batch file. If they are blank then nothing gets passed. But %1
must be there for the name of the file you wish to run in Python.
ALSO:
The downloadable program he mentions is a program that gets placed into
your MY Computer or Explorer file menu or context menu. It says Open Command
Here when you cursor down the file or context menu. Click on it when hovering
over the folder, such as the examples folder, it will take you to DOS right at
that point and you will be right inside the examples folder.
This is an easy way as long as the DOS folder you wish to be in is located
by the system path for the Python25 executable. But that should not be a
problem as long as the system path points to the Python25.exe file. He did give
you an example to set it and I think you probably do have it already for the
downloads now automatically set it up for you. but if not, then change it as he
suggested.
For the system path is needed to run programs and when using the download
he gives you will be able to go into any folder you desire to run DOS from.
When you type exit you just go back to the last Windows command or window you
were in, unless you closed it to clear the screen.
So please decide on what you wish to do. My batch file below if copied into
the folder you wish to be in then give it any name to correspond to what you
wish to do. My batch file below has to reside in the folder you have the .py
file in. The other example I will add below it and that will allow you to be in
the Python25 folder and jump into any folder beneath it or if you wish to go
somewhere else then you will have to add the path to it. The second example
will just jump you into the example folder and back out or up to the Python25
folder above it. Or parent of the child folder.
The name you place on the batch file can be like I did below P.BAT for a Python
load and run, or EX.BAT to load Python25 and run a .py name you give on the
command line from the Python25 folder into the examples folder. I used short
names just to make it easier for you to type and get into your program to run.
I hope this helps, Bruce
I am thoroughly confused at this point. I guess I need to change the path. You
guys really are nerds, goddamit!
On 10/31/07, RR4CLB < [EMAIL PROTECTED]> wrote:
Hi Both!
I also downloaded it and it is an easy way to get to folders from the
desk top. FOr if I have Python25 folder on my desk top then I click on it. Then
go to the examples folder and hit the context menu key. It is a nice way to do
it and quick.
Also to have the least amount of work then he can make up a batch file
for the loading of Python25 or run a program using it.
Batch file:
Either copy the text and paste it into Notepad or
Type each and hit enter, <enter>, at the end of each line below:
1) P>BAT batch file:
copy con: P.BAT <enter>
@echo off <enter>
python25 %1.py %2 %3 %4 %5 <enter>
@echo on <enter>
ctrl Z <enter>
Screen says: '1 file(s) copied'
2) EX.BAT file placed in Python25 folder:
copy con: EX.BAT <enter>
@echo off <enter>
CD EXAMPLE
python25 %1.py %2 %3 %4 %5 <enter>
CD .. <enter>
@echo on <enter>
ctrl Z <enter>
Screen says: '1 file(s) copied'
#!/usr/bin/env python
"""
This simple example is used for the line-by-line tutorial
that comes with pygame. It is based on a 'popular' web banner.
Note there are comments here, but for the full explanation,
follow along in the tutorial.
"""
#Import Modules
import os, pygame
from pygame.locals import *
if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'
#functions to create our resources
def load_image(name, colorkey=None):
fullname = os.path.join('data', name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print 'Cannot load image:', fullname
raise SystemExit, message
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
def load_sound(name):
class NoneSound:
def play(self): pass
if not pygame.mixer or not pygame.mixer.get_init():
return NoneSound()
fullname = os.path.join('data', name)
try:
sound = pygame.mixer.Sound(fullname)
except pygame.error, message:
print 'Cannot load sound:', fullname
raise SystemExit, message
return sound
#classes for our game objects
class Fist(pygame.sprite.Sprite):
"""moves a clenched fist on the screen, following the mouse"""
def __init__(self):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image, self.rect = load_image('fist.bmp', -1)
self.punching = 0
def update(self):
"move the fist based on the mouse position"
pos = pygame.mouse.get_pos()
self.rect.midtop = pos
if self.punching:
self.rect.move_ip(5, 10)
def punch(self, target):
"returns true if the fist collides with the target"
if not self.punching:
self.punching = 1
hitbox = self.rect.inflate(-5, -5)
return hitbox.colliderect(target.rect)
def unpunch(self):
"called to pull the fist back"
self.punching = 0
class Chimp(pygame.sprite.Sprite):
"""moves a monkey critter across the screen. it can spin the
monkey when it is punched."""
def __init__(self):
pygame.sprite.Sprite.__init__(self) #call Sprite intializer
self.image, self.rect = load_image('chimp.bmp', -1)
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.rect.topleft = 10, 10
self.move = 9
self.dizzy = 0
def update(self):
"walk or spin, depending on the monkeys state"
if self.dizzy:
self._spin()
else:
self._walk()
def _walk(self):
"move the monkey across the screen, and turn at the ends"
newpos = self.rect.move((self.move, 0))
if self.rect.left < self.area.left or \
self.rect.right > self.area.right:
self.move = -self.move
newpos = self.rect.move((self.move, 0))
self.image = pygame.transform.flip(self.image, 1, 0)
self.rect = newpos
def _spin(self):
"spin the monkey image"
center = self.rect.center
self.dizzy = self.dizzy + 12
if self.dizzy >= 360:
self.dizzy = 0
self.image = self.original
else:
rotate = pygame.transform.rotate
self.image = rotate(self.original, self.dizzy)
self.rect = self.image.get_rect(center=center)
def punched(self):
"this will cause the monkey to start spinning"
if not self.dizzy:
self.dizzy = 1
self.original = self.image
def main():
"""this function is called when the program starts.
it initializes everything it needs, then runs in
a loop until the function returns."""
#Initialize Everything
pygame.init()
screen = pygame.display.set_mode((468, 60))
pygame.display.set_caption('Monkey Fever')
pygame.mouse.set_visible(0)
#Create The Backgound
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
#Put Text On The Background, Centered
if pygame.font:
font = pygame.font.Font(None, 36)
text = font.render("Pummel The Chimp, And Win $$$", 1, (10, 10, 10))
textpos = text.get_rect(centerx=background.get_width()/2)
background.blit(text, textpos)
#Display The Background
screen.blit(background, (0, 0))
pygame.display.flip()
#Prepare Game Objects
clock = pygame.time.Clock()
whiff_sound = load_sound('whiff.wav')
punch_sound = load_sound('punch.wav')
chimp = Chimp()
fist = Fist()
allsprites = pygame.sprite.RenderPlain((fist, chimp))
#Main Loop
while 1:
clock.tick(60)
#Handle Input Events
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
elif event.type == MOUSEBUTTONDOWN:
if fist.punch(chimp):
punch_sound.play() #punch
chimp.punched()
else:
whiff_sound.play() #miss
elif event.type == MOUSEBUTTONUP:
fist.unpunch()
allsprites.update()
#Draw Everything
screen.blit(background, (0, 0))
allsprites.draw(screen)
pygame.display.flip()
#Game Over
#this calls the 'main' function when this script is executed
if __name__ == '__main__': main()