[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] possible bug when w/ sprites and 2 surfaces



I'm trying to create a pygame program to display text
messages, with a message title and then the message
body below, with the message body moving down the
screen.

Hi;

I think this does what you want:

# scroller1.py
import pygame
from pygame.locals import QUIT
import time
pygame.display.init()
pygame.font.init()

class LineOfText(pygame.sprite.Sprite):
def __init__(self, text_rendered, x, y, containers):
pygame.sprite.Sprite.__init__(self, containers)
self.image = text_rendered
self.rect = text_rendered.get_rect()
self.rect.y = y
self.rect.x = x

def update(self):
self.rect.move_ip(0, 1)

screen = pygame.display.set_mode((640, 480))

bg = pygame.Surface((640, 480))

title_size = 36

title_font = pygame.font.Font(None, title_size)
title_rendered = title_font.render('title', 1, (255, 0, 0 ))
bg.blit(title_rendered, (200, 50))
screen.blit(bg, ( 0, 0 ))
pygame.display.update()

roll_surf = pygame.Surface((640, 480))

all = pygame.sprite.RenderUpdates()
x = 10
y = 0
for line in ['abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJLMNOPQRSTUVWXYZ']:
text_font = pygame.font.Font(None, 32)
text_rendered = text_font.render(line, 1, (255, 255, 255))

LineOfText(text_rendered, x, y, all)
y = y + 32

pygame.display.flip()

done = 0
while not done:
for event in pygame.event.get():
if event.type == QUIT:
done = 1

#screen.blit(bg, (0, 0))
#bg.blit(roll_surf, (0, title_size + 200))
#pygame.display.flip()

### I think this is the problem... screen has the title
### on it, so when you use it to "clear" the title will
### show up. Make sure the background used to clear is
### the same size as the foreground surface, and that
### they are lined up.

all.clear(screen, bg) # clear the roll surface
all.update()

d = all.draw(screen) # render all sprites (ie Text Lines) onto roll_surface
pygame.display.update(d)


time.sleep (0.2)



This is another solution, using pygsear (http://www.nongnu.org/pygsear/)

# scroller2.py
import pygame
from pygsear.Drawable import String, Stationary, Layer, Image
from pygsear import Util
from pygsear.Game import Game
from pygsear.locals import TRANSPARENT


g = Game()

title = String(message='Title', fontSize=40, color=(255, 0, 0))
title.center(y=20)
affix = Stationary(sprite=title)
affix.draw()

layer = g.addLayer(size=(250, 300))
layer.image.set_colorkey(TRANSPARENT)
layer.center(y=200)

msg = """This is a very long message
which can contain newlines
and will be word wrapped automatically if the line \
gets to be too long to stay on one line like this one..."""

rct = pygame.Rect((0, 0, 250, 400))
text = Util.render_textrect(msg, rct, bgcolor=TRANSPARENT)
scroll = Image(image=text)
affix = Stationary(w=layer, sprite=scroll)
affix.draw()

g.sprites.add(layer, level=1)
layer.path.set_velocity(vy=-20)
g.mainloop()

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963