[Tutor] pygame blinking text

2011-12-15 Thread Cranky Frankie
First of all let me thank Timo, Lie Ryan, and Ramit, for their
constructive answers to my pygame/livewires blinking text question.

I wasn't able to use any of their suggestions, but I finally figured
out a method to get blinking text in a simple pygame program.

What I realized was that since it let's you put a background image on
the screen, then all I had to do was create another background image
with the text I wanted to blink on it, then simply use an animation
object to quickly cycle the images, and there you go, blinking text.

I'm sure there's a way to do this that's better, but all I'm trying to
do here is display a blinking "Thank You!" message at the end of my
Python presentation, using this little trick as a way to show the
beginnings of game programming. I'm not trying to write an actual
game, just using this to show a little bit of pygame as an example of
the different things you can do in Python.

Here's what I wound up with:

# thank_you.py
# Demonstrates creating an animation

from livewires import games

games.init(screen_width = 640, screen_height = 480, fps = 50)

screen_image = games.load_image("AEMUG640x480.JPG", transparent = 0)
games.screen.background = screen_image

screen_file = ["AEMUG640x480TY.JPG",
   "AEMUG640x480.JPG"]

screen_refresh = games.Animation(images = screen_file,
x = games.screen.width/2,
y = games.screen.height/2,
n_repeats = 0,
repeat_interval = 50)

games.screen.add(screen_refresh)

games.screen.mainloop()


-- 
Frank L. "Cranky Frankie" Palmeri
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pygame blinking text

2011-12-12 Thread Prasad, Ramit
>This is a common problem when people start GUI/graphics programming; 
>once you enter the GUI mainloop, you're no longer in control of the 
>program flow. Instead the mainloop will process events and callbacks; 
>therefore if you want a blinking text, you had to arrange such that your 
>blinker function will be called every second.

Since you are using livewires, you can subclass games.Timer 
and then create an instance of that subclass at the end and then have 
the tick function check for some Boolean value and either hide or 
show the text. I am not familiar with livewires or pygame, so I hope that helps.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pygame blinking text

2011-12-12 Thread Lie Ryan

On 12/13/2011 01:01 AM, Cranky Frankie wrote:

I tried putting the ty_message block in a WHILE TRUE loop, and that
didn't work. Then I tried the same with the
games.screen.add(ty_message) line and that didn't work either. I think
what might work is if I can find a way to delete the ty_message and
readd it, over and over in a loop with a delay, bu:


This is a common problem when people start GUI/graphics programming; 
once you enter the GUI mainloop, you're no longer in control of the 
program flow. Instead the mainloop will process events and callbacks; 
therefore if you want a blinking text, you had to arrange such that your 
blinker function will be called every second.


In pygame, you can do the following:

#!/usr/bin/env python

import pygame

# initialize pygame, this must be called before
# doing anything with pygame
pygame.init()

# create a screen
screen = pygame.display.set_mode((400, 400))

# setup the text
font = pygame.font.Font(None, 36)
text = font.render("Hello World", True, (100, 100, 100))

display = True

# the main loop
while pygame.time.get_ticks() < 1: # run the program for 10 seconds
# empty the screen
screen.fill((255, 255, 255))

display = not display

# draw the text to the screen only if display is True
if display:
screen.blit(text, (100, 100))

# update the actual screen
pygame.display.flip()

# wait for half second
pygame.time.wait(500)


You can do this in pygame since pygame do not provide a mainloop.

I took a quick glance at livewires, their main loop implementation is 
very simple; however it discards all events except for mouse, keyboard, 
and the quit event. In typical game engine, you usually will register an 
event to the event handler to be executed every second that will 
draw/undraw the text. However, in livewires event handling system you 
can't do that. Therefore, if you want to do it, you'd have to override 
the event handling method in the Screen class.


However, for a quick hack, I think you can abuse the after_death= 
parameter; you can register a function that will games.screen.add() a 
Message, then after_death, it registers an invisible message which 
registers a Message after its death which registers the visible message 
after death which ... . In short, the following (I haven't tested it, 
although I think it should work):


stop = False
def add_visible_ty():
if stop: return
message = games.Message(
value = "Thank You!",
size = 100,
color = color.red,
x = games.screen.width/2,
y = games.screen.height/2,
lifetime = 500,
after_death = add_invisible_ty
)
games.screen.add(message)

def add_invisible_ty():
if stop: return
message = games.Message(
value = "",
size = 100,
color = color.red,
x = games.screen.width/2,
y = games.screen.height/2,
lifetime = 500,
after_death = add_visible_ty
)
games.screen.add(message)




games.screen.delete(ty_message)

doesn't work - delete is not a valid method.


There are two rendering style for graphics programming; one is called 
the "retained mode", in which the libraries retain a complete model of 
the objects to be rendered and graphic calls do not directly cause 
actual rendering but instead update an internal model, the other is the 
"immediate mode" in which graphic calls directly cause rendering of 
graphics objects to the display.


pygame falls into the latter; pygame do not keep track of objects that 
it has drawn on screen, therefore if you need to "delete" an object from 
the screen, you'd need to redraw the whole screen except the object you 
had deleted. At the very least, you'd need to redraw the part of the 
screen that had changed due to the deletion.


livewires falls into the former; in livewires there is an internal 
object that represent each screen object and it keep tracks of which 
objects and which part of the screen that are "dirty".


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pygame blinking text

2011-12-12 Thread Steven D'Aprano

Cranky Frankie wrote:
[...]

Is there any way to get blinking text with pygame? This is not a deal
breaker, but I thought the blinking text would be neat.


To punish your users for completing the game?


Ha ha only serious.



--
Steven


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pygame blinking text

2011-12-12 Thread Timo

Op 12-12-11 15:01, Cranky Frankie schreef:

My Python presentation is just about complete. As a nice ending I want
to do a pygame program that displays the group's .jpg, with the words
"Thank You!" blinking, say on for a second and off for a second.
Unfortunatley, I can't get the works to blink, but I can get them to
appear for a short time:

from livewires import games, color
Looks like you're not using pygame, but livewires. Why not try their 
mailinglist, I bet their knowledge is better than this *Python 
beginners* list.


Cheers,
Timo



games.init(screen_width = 640, screen_height = 480, fps = 50)

bg_image = games.load_image("AEMUG640x480.JPG", transparent = False)
games.screen.background = bg_image


ty_message = games.Message(value = "Thank You!",
  size = 100,
  color = color.red,
  x = games.screen.width/2,
  y = games.screen.height/2,
  lifetime = 100,
  after_death = 0)

games.screen.add(ty_message)

games.screen.mainloop()



I tried putting the ty_message block in a WHILE TRUE loop, and that
didn't work. Then I tried the same with the
games.screen.add(ty_message) line and that didn't work either. I think
what might work is if I can find a way to delete the ty_message and
readd it, over and over in a loop with a delay, bu:

games.screen.delete(ty_message)

doesn't work - delete is not a valid method.

Is there any way to get blinking text with pygame? This is not a deal
breaker, but I thought the blinking text would be neat.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] pygame blinking text

2011-12-12 Thread Cranky Frankie
My Python presentation is just about complete. As a nice ending I want
to do a pygame program that displays the group's .jpg, with the words
"Thank You!" blinking, say on for a second and off for a second.
Unfortunatley, I can't get the works to blink, but I can get them to
appear for a short time:

from livewires import games, color

games.init(screen_width = 640, screen_height = 480, fps = 50)

bg_image = games.load_image("AEMUG640x480.JPG", transparent = False)
games.screen.background = bg_image


ty_message = games.Message(value = "Thank You!",
 size = 100,
 color = color.red,
 x = games.screen.width/2,
 y = games.screen.height/2,
 lifetime = 100,
 after_death = 0)

games.screen.add(ty_message)

games.screen.mainloop()



I tried putting the ty_message block in a WHILE TRUE loop, and that
didn't work. Then I tried the same with the
games.screen.add(ty_message) line and that didn't work either. I think
what might work is if I can find a way to delete the ty_message and
readd it, over and over in a loop with a delay, bu:

games.screen.delete(ty_message)

doesn't work - delete is not a valid method.

Is there any way to get blinking text with pygame? This is not a deal
breaker, but I thought the blinking text would be neat.

-- 
Frank L. "Cranky Frankie" Palmeri
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor