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

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



I really appreciate your help.
The code you sent does work except that when I added
code to make the scrolling text repeat (it stays on
the same surface), it scrolls over the title.
How do I fix this?

I think with pygsear, I might just push the title up to
a higher layer (and probably add a background too...)

# scroller2a.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, RED, BLUE


g = Game()

t_layer = g.addLayer(size=(100, 40))
t_layer.image.set_colorkey(TRANSPARENT)
t_layer.center(y=200)

title = String(message='Title', fontSize=40, color=RED, bgcolor=BLUE)
#title.center(y=20)
affix = Stationary(w=t_layer, 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(t_layer, level=2)
g.sprites.add(layer, level=1)
layer.path.set_velocity(vy=-20)
g.mainloop()



Or, you can set the active clipping area for the screen...


# scroller1a.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()

clip = pygame.Rect(0, 100, 640, 430)
screen.set_clip(clip)

all = pygame.sprite.RenderUpdates()
x = 10
y = 0
for line in ['abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJLMNOPQRSTUVWXYZ',
'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

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)

_________________________________________________________________
Add photos to your e-mail with MSN 8. Get 2 months FREE*. http://join.msn.com/?page=features/featuredemail