Re: [Tutor] font/text in pygame

2005-04-27 Thread Jacob S.
def displaybalance():
for score, name in mylist:
slip = 30 - len(name)
slip_amt = slip* 
print %s%s%s % (name,slip_amt,score)
(I did this with the print command to make sure it would produce what
I wanted, three strings for the three sample scores I put in this
dummy list).
Yup, you did that. And actually, what happened was that string formatting 
was first used to create a string, and then this string was sent to the 
print command, which displayed it. Your last line is equivalent to:

foo = %s%s%s % (name,slip_amt,score)
print foo
The only magic thing you can only do with print is the print a, b, c 
syntax.
Uck.
Try this.
for score, name in mylist:
   name = name.ljust(30)
   print %s%s % (name,score)
Or, if you prefer...
print \n.join(%-30s%s%(name,score) for name,score in mylist)
If you don't have python 2.4
print \n.join([---]) # with the hyphens replaced with the above 
comprehension

Oh, and there is another cool thing you can do with print.
First off, print uses sys.stdout, so if you change it, print changes where 
it prints.
Second, if you use the syntax print output,item0,item1,item2
you can redirect manually where things are printed to. output has to be an 
object which has a write method.

i.e.
a = open(myfile.txt,w)
b = This is the string I want to print out.
print a,b
a.close()
gives you a file in the current directory with the filename myfile.txt 
with the contents This is the string I want to print out.

Okay, I'm done again,
Jacob 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] font/text in pygame

2005-04-27 Thread D. Hartley
Jacob (et al):

Unfortunately at this point I'm rendering the text, not printing it
(using text = font.render).  So I need to know if there is a hard
character or something I can type, since 50*  (space) isnt lining
them up right.

Thanks!
~Denise

On 4/27/05, Jacob S. [EMAIL PROTECTED] wrote:
  def displaybalance():
  for score, name in mylist:
  slip = 30 - len(name)
  slip_amt = slip* 
  print %s%s%s % (name,slip_amt,score)
 
  (I did this with the print command to make sure it would produce what
  I wanted, three strings for the three sample scores I put in this
  dummy list).
 
  Yup, you did that. And actually, what happened was that string formatting
  was first used to create a string, and then this string was sent to the
  print command, which displayed it. Your last line is equivalent to:
 
  foo = %s%s%s % (name,slip_amt,score)
  print foo
 
  The only magic thing you can only do with print is the print a, b, c
  syntax.
 
 Uck.
 
 Try this.
 
 for score, name in mylist:
name = name.ljust(30)
print %s%s % (name,score)
 
 Or, if you prefer...
 
 print \n.join(%-30s%s%(name,score) for name,score in mylist)
 
 If you don't have python 2.4
 
 print \n.join([---]) # with the hyphens replaced with the above
 comprehension
 
 Oh, and there is another cool thing you can do with print.
 First off, print uses sys.stdout, so if you change it, print changes where
 it prints.
 Second, if you use the syntax print output,item0,item1,item2
 you can redirect manually where things are printed to. output has to be an
 object which has a write method.
 
 i.e.
 
 a = open(myfile.txt,w)
 
 b = This is the string I want to print out.
 print a,b
 a.close()
 
 gives you a file in the current directory with the filename myfile.txt
 with the contents This is the string I want to print out.
 
 Okay, I'm done again,
 Jacob
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] font/text in pygame

2005-04-26 Thread Max Noel
On Apr 26, 2005, at 23:57, D. Hartley wrote:
But in any case, font/text will only take strings - i cant pass in a
list, or an index to an item in a list (which is, in this case, a
tuple), and since the items in the list will be changed and updated
obviously i cant just type in the items as strings myself.
any ideas? does this question even make sense the way it's worded?
It's a last-ditch effort to get my high score list onto the graphics
window before i have to abandon it :)
	The idea, of course, is to convert the things you want to display to 
strings. You may be interested in the following things:

- the str() function -- converts anything it's passed into a string. 
str(1) returns 1. str((2, 3, 4)) returns (2, 3, 4).
- Basic string concatenation -- use the + operator. Example: a + b 
returns ab.
- the string.join method -- joins a list or tuple of strings. Example: 
'xx'.join([foo, bar, baz]) returns fooxxbarxxbaz.
- string formatting. You ever programmed in C? It's basically the same 
thing as printf, only better. Example:
Hi, my name is %s. I am %s years old. I come from %s. % (Max, 21, 
France)
returns Hi, my name is Max. I am 21 years old. I come from France.

	By using some of these (string formatting and str() come to mind), you 
should be able to do what you want.

Hope that helps,
-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] font/text in pygame

2005-04-26 Thread D. Hartley
Max (et al.):

I tried to do it that way (score, name, posY etc) and it told me
too many values to unpack.  But I did get it to work the following
way:


# Display some text
x, y = 230, 270
for score, name in mylist:
slip = 30 - len(name)
slip_amt = slip* 
font = pygame.font.Font(None, 25)
text = font.render(%s%s%s % (name,slip_amt,score), 1,
(10, 10, 10, 0))
background.blit(text, (x,y))
y += text.get_height()

I'd rather have it centered on the x (x = 230 is the beginning (left
end) of the string, and my strings are different lengths, so I'd
rather have it centered), but I was just thrilled to get it working in
the first place.   So if anyone knows something quick I can add to
that code to make it centerx rather than beginning x, that'd be great.
But it does at least work for now.

But then I tried to import this (working!) code into my game, and even
tho it executes the function within which it's contained BEFORE
waiting for the y/n another game response, it's not displaying the
score then (I just managed to glimpse it in the second after I hit n,
the score flashed on right before the window closed. So I know it's in
there and works! Just not at the right time.)

The code goes like this:

  if lives == 0:
### trying addscore
gameover.play()
showGameOver(screen, background_image)
pygame.display.flip()

def add_score():
high_scores = pickle.load(file(scores.pik))
score = total_enemy_hits
if score  high_scores[-1][0]:
print Ta da! You got, total_enemy_hits,
Ranch Delivery Devices!
name = read_string(You made the high score
list! What's your name? )
user_score = (score,name)
high_scores.append(user_score)
high_scores.sort(reverse=True)
del high_scores[-1]
pickle.dump(high_scores, file(scores.pik, w))
x, y = 230, 270
for score, name in high_scores:
slip = 30 - len(name)
slip_amt = slip* 
font = pygame.font.Font(None, 25)
text = font.render(%s%s%s %
(name,slip_amt,score), 1, (255, 255, 255, 0))
screen.blit(text, (x,y))
y += text.get_height()
else:
print Sorry, you only got, total_enemy_hits,
Ranch Delivery Devices.
print You didn't quite make the high score list!
x, y = 230, 270
for score, name in high_scores:
slip = 30 - len(name)
slip_amt = slip* 
font = pygame.font.Font(None, 25)
text = font.render(%s%s%s %
(name,slip_amt,score), 1, (255, 255, 255, 0))
screen.blit(text, (x,y))
y += text.get_height()
print Better luck next time!

#pdb.set_trace()
add_score()

answer = 
while not answer in (y,n):
   for event in pygame.event.get():
  if event.type == KEYDOWN:
 if event.key == K_n:
answer = n
 elif event.key == K_y:
answer = y
if answer == n:
running = 0
else:
return 1


#refresh the display
pygame.event.pump()
pygame.display.flip()

#well, nice playing with you...
screen = pygame.display.set_mode((640, 480))
return 0


See, it seems to me that I define and execute the add_score function
BEFORE the do you want to play again bit. But it doesnt display
until AFTER you hit that key, at which point the game goes onto the
next thing (i.e., either refreshes and starts the game over, or closes
the window).  Like I said I know it's in there, because I saw it flash
on after I hit the n key, right before the window closes.  I tried
moving the showgameover/flip, commenting out the different
flip/pump/etc commands (these are a little out of my league,
experience-wise), and tried rearranging things as best I could. (And
yes, I know defining the add_score function right there is probably
not elegant, but if I dont do it that way I get other errors).  Am I
missing something glaringly obvious? If I define and use the add_score
function before the keystroke commands, why is it displaying after?

Thanks...

~Denise
___
Tutor maillist  - 

Re: [Tutor] font/text in pygame

2005-04-26 Thread D. Hartley
P.S.  I should also add that the print commands embedded within the
add_score function ARE displaying at the appropriate time (i.e.,
before I have to hit y/n).  Of course they're in the text/console
window, but it does tell me that it's executing that add_score
function at the right point in time.

So why is it waiting until after the y/n keystroke to display the text
I'm rendering on the game window?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor