SFML community forums
Bindings - other languages => Python => Topic started by: Kingdom of Fish on July 13, 2009, 10:44:24 pm
-
Hi, I'm kind of new to the python binding for SFML but I'm trying to implement a menu system and want to be able to change the color of a sf.String sprite each time it's drawn depending on a set of flags. But I can't seem to figure och what (if any) function is called on each draw.
-
Hi, I think that you can create a new sprite object and
set the behavior that change text's color easy, but handling
events manually:
This examples show how to create a simple menu and change
the text's color with mouse:
(http://www.losersjuegos.com.ar/incoming/descargas/20090714/font.png)
source code:
# -*- encoding: utf-8 -*-
from PySFML import sf
import sys
class MenuTextSprite(sf.String):
def __init__(self, text, x, y):
sf.String.__init__(self, text)
self.default_color = sf.Color(200, 200, 200)
self.over_color = sf.Color.White
self.SetColor(self.default_color)
self.SetPosition(x, y)
self.my_rect = self.GetRect()
def on_mouse_move_event(self, mouse_move_event):
x, y = mouse_move_event.X, mouse_move_event.Y
# Check if the mouse are over my.
if self.my_rect.Contains(x, y):
self.SetColor(self.over_color)
else:
self.SetColor(self.default_color)
app = sf.RenderWindow()
app.Create(sf.VideoMode(320, 240), "Menu")
#create all text items
new_game_text = MenuTextSprite("New Game", 80, 50)
about_text = MenuTextSprite("About this game", 50, 100)
quit_text = MenuTextSprite("Quit", 120, 150)
menu_items_list = [new_game_text, about_text, quit_text]
event = sf.Event()
input = app.GetInput()
background_color = sf.Color(100, 100, 100)
while True:
app.Clear(background_color)
for text_item in menu_items_list:
app.Draw(text_item)
while app.GetEvent(event):
x = input.GetMouseX()
y = input.GetMouseY()
if event.Type == sf.Event.Closed:
app.Close()
sys.exit(0)
if event.Type == sf.Event.MouseMoved:
for text_item in menu_items_list:
text_item.on_mouse_move_event(event.MouseMove)
app.Display()
You can change other String's attributes like font style, size or
rotation too, only change the "on_mouse_move_event" method.
Bye.
-
If you want to override the Render function you have to make your class inherit from sf.Drawable, it won't work if it inherits from sf.String.
For example you can do:
class MyString(sf.Drawable):
def __init__(self):
self.str = sf.String("Test")
def Render(self, win):
if myflag == something:
self.str.SetColor(sf.Color.Red)
win.Draw(self.str)
-
If you want to override the Render function you have to make your class inherit from sf.Drawable, it won't work if it inherits from sf.String.
For example you can do:
class MyString(sf.Drawable):
def __init__(self):
self.str = sf.String("Test")
def Render(self, win):
if myflag == something:
self.str.SetColor(sf.Color.Red)
win.Draw(self.str)
Most excellent :D Thanks for the help.