Adjusted PySFML to work with the current SFML2 branch.

Note that it's just compatible. A lot of the new functionality is still in the pipeline.

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1308 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
tankbo 2009-12-07 11:53:38 +00:00
parent fb7470cbc3
commit 839c80556d
29 changed files with 544 additions and 354 deletions

Binary file not shown.

View file

@ -0,0 +1,98 @@
# coding=utf-8
from PySFML import sf
import random
# Simple class for an apple.
class Apple:
sprite = None
speed = (2, 2)
rotationstep = 1
def __init__( self, image ):
self.sprite = sf.Sprite( image )
self.sprite.SetOrigin( image.GetWidth() / 2, image.GetHeight() / 2 )
# Set resolution and create the window.
Resolution = (800, 600)
wnd = sf.RenderWindow( sf.VideoMode( Resolution[0], Resolution[1], 32 ), "Hello SFML!" )
wnd.UseVerticalSync( True )
# Load a fancy font.
cheese = sf.Font()
cheese.LoadFromFile( "data/cheeseburger.ttf", 50 )
# Create a text.
text = sf.Text( u"Hello SFML from Python!", cheese, 50 )
text.SetOrigin( text.GetRect().GetSize()[0] / 2, text.GetRect().GetSize()[1] / 2 )
text.SetPosition( 400, 300 )
text.SetColor( sf.Color( 0, 100, 0, 100 ) )
# Create a text for FPS display.
fpstext = sf.Text( u"FPS: --", cheese )
fpstext.SetColor( sf.Color( 0, 0, 0 ) )
currentfps = 0
fpsclock = sf.Clock()
# Load apple image from file.
appleimage = sf.Image()
appleimage.LoadFromFile( "data/apple.png" )
# Create some apples with random position, speed, rotation and color.
apples = [Apple( appleimage ) for num in range( 0, 100 )]
for apple in apples:
apple.sprite.SetOrigin( appleimage.GetWidth() / 2, appleimage.GetHeight() / 2 )
apple.sprite.SetPosition(
random.randint( apple.sprite.GetOrigin()[0], Resolution[0] - apple.sprite.GetOrigin()[0] ),
random.randint( apple.sprite.GetOrigin()[1], Resolution[1] - apple.sprite.GetOrigin()[1] )
)
apple.sprite.SetColor( sf.Color( random.randint( 100, 255 ), random.randint( 100, 255 ), random.randint( 100, 255 ) ) )
randx = random.randint( -3, 3 )
randy = random.randint( -3, 3 )
apple.speed = (1 if randx == 0 else randx, 1 if randy == 0 else randy)
apple.rotationstep = random.uniform( 1.0, 20.0 ) - 10.0
event = sf.Event()
# Main loop.
while wnd.IsOpened():
# Fetch all pending events and process them.
while wnd.GetEvent( event ):
# Quit when window has been closed or Escape has been pressed.
if event.Type == sf.Event.Closed:
wnd.Close()
elif event.Type == sf.Event.KeyPressed and event.Key.Code == sf.Key.Escape:
wnd.Close()
# Clear window to white color.
wnd.Clear( sf.Color( 255, 255, 255 ) )
# Draw all apples and texts.
for apple in apples:
wnd.Draw( apple.sprite )
wnd.Draw( text )
wnd.Draw( fpstext )
wnd.Display() # Display everything.
# Count FPS.
currentfps += 1
if fpsclock.GetElapsedTime() >= 1.0:
fpsclock.Reset()
fpstext.SetString( u"FPS: " + unicode( currentfps ) )
currentfps = 0
# Update apples (for the "bounce effect").
for apple in apples:
apple.sprite.Move( apple.speed[0], apple.speed[1] )
apple.sprite.Rotate( apple.rotationstep )
realpos = (apple.sprite.GetPosition()[0] - apple.sprite.GetOrigin()[0], apple.sprite.GetPosition()[1] - apple.sprite.GetOrigin()[1])
if (apple.speed[0] > 0 and realpos[0] >= Resolution[0] - appleimage.GetWidth()) or (apple.speed[0] < 0 and realpos[0] <= 0):
apple.speed = (apple.speed[0] * -1, apple.speed[1])
if (apple.speed[1] > 0 and realpos[1] >= Resolution[1] - appleimage.GetWidth()) or (apple.speed[1] < 0 and realpos[1] <= 0):
apple.speed = (apple.speed[0], apple.speed[1] * -1)

View file

@ -11,7 +11,7 @@ def main():
# Create main window
App = sf.RenderWindow(sf.VideoMode(800, 600), "SFML OpenGL")
App.PreserveOpenGLStates(True)
App.SetActive()
# Create a sprite for the background
BackgroundImage = sf.Image()
@ -73,7 +73,11 @@ def main():
# Draw background
App.Draw(Background)
App.Flush()
# Active window to be able to perform OpenGL commands.
App.SetActive()
# Clear depth buffer
glClear(GL_DEPTH_BUFFER_BIT)
@ -145,7 +149,7 @@ def main():
glEnd()
# Draw some text on top of our OpenGL object
Text = sf.String("This is a rotating cube")
Text = sf.Text("This is a rotating cube")
Text.SetPosition(230., 300.)
Text.SetColor(sf.Color(128, 0, 128))
App.Draw(Text)

View file

@ -3,40 +3,40 @@
# You can notice that here we use PySFML.sf instead of just PySFML
# Therefore it won't be needed to put sf. in front of SFML classes
from PySFML.sf import *
from PySFML import sf
def Main():
Buffer = SoundBuffer()
Buffer = sf.SoundBuffer()
if not Buffer.LoadFromFile("data/fart.wav"): # Loads the sound
return
Fart = Sound(Buffer, False)
Fart = sf.Sound(Buffer, False)
WindowWidth, WindowHeight = 640, 480
App = RenderWindow(VideoMode(WindowWidth,WindowHeight,32), "Sound with PySFML", Style.Close, WindowSettings(24,8,0))
App = sf.RenderWindow(sf.VideoMode(WindowWidth,WindowHeight,32), "Sound with PySFML", sf.Style.Close, sf.ContextSettings(24,8,0))
App.SetFramerateLimit(30)
EventHandler = Event()
EventHandler = sf.Event()
InputHandler = App.GetInput()
Text = String("Turn the sound on.\nClick anywhere on the screen.\nMove the mouse. Click again.\nTry clicking in the corners.")
Text = sf.Text("Turn the sound on.\nClick anywhere on the screen.\nMove the mouse. Click again.\nTry clicking in the corners.")
Text.SetX(30.)
Text.SetY(20.)
Text.SetColor(Color(150, 100, 10, 255))
Text.SetColor(sf.Color(150, 100, 10, 255))
while App.IsOpened(): # Main loop
while App.GetEvent(EventHandler): # Event Handler
if EventHandler.Type == Event.Closed:
if EventHandler.Type == sf.Event.Closed:
App.Close()
if EventHandler.Type == Event.KeyPressed and EventHandler.Key.Code == Key.Escape:
if EventHandler.Type == sf.Event.KeyPressed and EventHandler.Key.Code == sf.Key.Escape:
App.Close()
if EventHandler.Type == Event.MouseButtonPressed and EventHandler.MouseButton.Button == Mouse.Left:
if EventHandler.Type == sf.Event.MouseButtonPressed and EventHandler.MouseButton.Button == sf.Mouse.Left:
Fart.SetPitch(1.5 - 1.*InputHandler.GetMouseY()/WindowHeight)
Fart.SetPosition( 1.*(InputHandler.GetMouseX() - WindowWidth/2)/(WindowWidth/20), 2., -2.)
Fart.Play()
App.Draw(Text)
App.Display()
App.Clear(Color.Black)
App.Clear(sf.Color.Black)
Main()

View file

@ -4,7 +4,7 @@ from PySFML import sf
def Main():
# Check that the device can capture audio
if sf.SoundRecorder.CanCapture() == False:
if sf.SoundRecorder.IsAvailable() == False:
print "Sorry, audio capture is not supported by your system"
return

View file

@ -10,7 +10,7 @@ class Menu:
text_color = sf.Color(220, 220, 20, 255)
self.spacing = screen_height/7
self.title = sf.String("PyWorm!")
self.title = sf.Text("PyWorm!")
self.title.SetColor(text_color)
self.title.SetPosition(screen_width/2-80., self.spacing)
@ -18,7 +18,7 @@ class Menu:
x_align = [-80., -50., -70., -50.]
self.strings = []
for i in range(0, 4):
string = sf.String(levels[i])
string = sf.Text(levels[i])
string.SetColor(text_color)
string.SetPosition(screen_width/2+x_align[i], (2+i)*self.spacing+20)
self.strings.append(string)
@ -47,7 +47,7 @@ class Apple(sf.Sprite):
pass
# print "Could not load data/apple.png"
sf.Sprite.__init__(self, apple_img)
self.SetCenter(apple_img.GetWidth()/2, apple_img.GetHeight()/2)
self.SetOrigin(apple_img.GetWidth()/2, apple_img.GetHeight()/2)
self.size = apple_img.GetWidth()
def random_move(self, arena):
@ -62,10 +62,10 @@ class Arena(dict):
def __init__(self, window_width, window_height):
self.window_width = window_width
self.arena_bottom, self.exit_left, self.exit_right = window_height-80, window_width/2 - 50, window_width/2 + 50
self['level_str'] = sf.String()
self['level_str'] = sf.Text()
self['level_str'].SetColor(sf.Color.White)
self['level_str'].SetPosition(60., window_height-60)
self['score_str'] = sf.String()
self['score_str'] = sf.Text()
self['score_str'].SetColor(sf.Color.White)
self['score_str'].SetPosition(260., window_height-60)
self.exit_rect = sf.Shape.Rectangle(self.exit_left, 0, self.exit_right, self.arena_top, sf.Color.Black)
@ -78,16 +78,16 @@ class Arena(dict):
def reset(self):
self.level, self.score, self.arena_left, self.arena_right = 1, 0, self.shrink_value, self.window_width-self.shrink_value
self.update_arena_rect()
self['level_str'].SetText("Level: 1")
self['score_str'].SetText("Score: 0")
self['level_str'].SetString("Level: 1")
self['score_str'].SetString("Score: 0")
def update_score(self):
self.score += 1
self['score_str'].SetText("Score: " + str(self.score))
self['score_str'].SetString("Score: " + str(self.score))
def next_level(self):
self.level += 1
self['level_str'].SetText("Level: " + str(self.level))
self['level_str'].SetString("Level: " + str(self.level))
self.arena_left += self.shrink_value
self.arena_right -= self.shrink_value
self.update_arena_rect()
@ -97,7 +97,7 @@ class Arena(dict):
class Part(sf.Sprite):
def __init__(self, rond, x, y):
sf.Sprite.__init__(self, rond)
self.SetCenter(rond.GetWidth()/2, rond.GetHeight()/2)
self.SetOrigin(rond.GetWidth()/2, rond.GetHeight()/2)
self.SetPosition(x, y)
class Worm(list):