Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Overloading Render for Sprites  (Read 7664 times)

0 Members and 1 Guest are viewing this topic.

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Overloading Render for Sprites
« 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.

hugoruscitti

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • http://www.losersjuegos.com.ar
Overloading Render for Sprites
« Reply #1 on: July 15, 2009, 02:56:27 am »
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:



source code:

Code: [Select]

# -*- 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.

remi.k2620

  • Full Member
  • ***
  • Posts: 186
    • View Profile
    • http://remi.tuxfamily.org
Overloading Render for Sprites
« Reply #2 on: July 16, 2009, 05:27:10 pm »
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:
Code: [Select]

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)

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Overloading Render for Sprites
« Reply #3 on: July 17, 2009, 11:16:06 pm »
Quote from: "remi.k2620"
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:
Code: [Select]

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.

 

anything