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

Author Topic: pySFML Example Incompatibilities  (Read 9157 times)

0 Members and 1 Guest are viewing this topic.

Flon

  • Newbie
  • *
  • Posts: 1
    • View Profile
pySFML Example Incompatibilities
« on: March 18, 2017, 04:28:50 pm »
Hi everyone, long time lurker here as I usually manage to figure things out eventually but I'm hitting a brick wall here. I am recently getting back into python programming after only using it breifly for small projects as part of a university degree. I have alot more experience with c++ but I can really see the benefits for prototyping in python.

When looking for a  multimedia library for python I quickly came accross pySFML and was initially very happy with this discovery as SFML is by far my favoured library when Im working in c++. But, I am having endless headaches trying to get this to work.

My issues mainly seem to revolve around the built in window event system, with a good example cropping up in the very first complete example of the "Getting started" page here:
https://python-sfml.org/gettingstarted.html

from sfml import sf


# create the main window
window = sf.RenderWindow(sf.VideoMode(640, 480), "pySFML Window")

try:
   # load a sprite to display
   texture = sf.Texture.from_file("cute_image.png")
   sprite = sf.Sprite(texture)

   # create some graphical text to display
   font = sf.Font.from_file("arial.ttf")
   text = sf.Text("Hello SFML", font, 50)

   # load music to play
   music = sf.Music.from_file("nice_music.ogg")

except IOError: exit(1)

# play the music
music.play()

# start the game loop
while window.is_open:
   # process events
   for event in window.events:
      # close window: exit
      if type(event) is sf.CloseEvent:
         window.close()

   window.clear() # clear screen
   window.draw(sprite) # draw the sprite
   window.draw(text) # draw the string
   window.display() # update the window

When running this code I get the error referring to line 27:
Quote
AttributeError: module 'sfml.sf' has no attribute 'CloseEvent'

With some trial and error I managed to change this section to:
if event.type == sf.Event.CLOSED:
                    window.close()

Which regains the proper functionality (click x to close).

However I now want to add some keyboard input and again I am hit with similar inconsistencies.

For example in the following example, constructed as per the documentation, nothing will ever be printed out no matter the input:

for event in window.events:
if event == sf.KeyEvent:
            if event.pressed:
                print("pressed", flush=True)
                window.close()
            elif event.released:
                print("released", flush=True)
                window.close()
 

Any Ideas as to what is causing this, is the documentation really outdated? As I have installed everything using their provided prepackaged binaries I am out of ideas.

-Edit: Looking at the github repo I have found a telling commit
"Rewrote event API"
  * Removed previous ugly API (silly things I made years ago)
  * Event attributes match C++ API (position becomes x and y, etc)
  * Implemented __repr__() in each events
  * Updated the examples

I will try building from the latest source and see if my issues are resolved. If so I guess something got mixed up in the binaries on the site.

-Edit 2: The examples on the github page appear to be up to date and working so I will base my code on those for the time being. it seems that the documentation has not been updated in quite a while which is leading to these inconsistencies
« Last Edit: March 18, 2017, 06:45:11 pm by Flon »