Advertisement
Guest User

Untitled

a guest
Oct 13th, 2010
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. # Include the PySFML extension
  2. from PySFML import sf
  3. import sys
  4.  
  5. class ImageManager():
  6.     def __init__(self):
  7.         self.__imageDict = {}
  8.        
  9.     def getImage(self, path):
  10.         '''
  11.        Search image in ImageManager dictionary  and return it's reference.
  12.        If no image is found in __imageDict load it insert in __imageDict and return it's reference.
  13.        
  14.        Args:
  15.            Image File relative path
  16.        Returns:
  17.            sf.Image object
  18.        '''
  19.         try:
  20.             return self.__imageDict[path]
  21.         except KeyError:
  22.             image = sf.Image()
  23.             image.LoadFromFile(path)
  24.             self.__imageDict[path] = image
  25.             return image
  26.  
  27. # Create the main window
  28. window = sf.RenderWindow(sf.VideoMode(640, 480, 32), "PySFML test")
  29. # Create the SFML event handler
  30. event = sf.Event()
  31.  
  32. # Create and position SFML shape
  33. box = sf.Shape.Rectangle(0, 0, 50, 50, sf.Color(127, 0, 0, 255))
  34. box.SetPosition(100,100)
  35.  
  36. # Create image manager
  37. imagemanager = ImageManager()
  38.  
  39. # Load and setup the sprite
  40. sprite = sf.Sprite(imagemanager.getImage('01.png'))
  41. sprite.SetPosition(400, 400)
  42. sprite.SetCenter(15, 15)
  43.  
  44. #Setup game speed
  45. speed = 400
  46.  
  47. # Start the game loop
  48. while window.IsOpened():
  49.     # Process events
  50.     while window.GetEvent(event):
  51.         # Close window exit
  52.         if event.Type == sf.Event.Closed:
  53.             window.Close()
  54.         # Key esc exit
  55.         if event.Type == sf.Event.KeyPressed and event.Key.Code == sf.Key.Escape:
  56.             window.Close()
  57.     # Clear the screen
  58.     window.Clear(sf.Color(255,255,255))
  59.     # Move the sprite
  60.     if window.GetInput().IsKeyDown(sf.Key.Left):
  61.         sprite.Move(-speed * window.GetFrameTime(), 0)
  62.         sprite.Rotate(speed * window.GetFrameTime())
  63.     elif window.GetInput().IsKeyDown(sf.Key.Right):
  64.         sprite.Move(speed * window.GetFrameTime(), 0)
  65.         sprite.Rotate(-speed * window.GetFrameTime())
  66.     if window.GetInput().IsKeyDown(sf.Key.Up):
  67.         sprite.Move(0, -speed * window.GetFrameTime())
  68.     elif window.GetInput().IsKeyDown(sf.Key.Down):
  69.         sprite.Move(0, speed * window.GetFrameTime())
  70.  
  71.     # Draw sprite and render the scene    
  72.     window.Draw(sprite)
  73.     window.Display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement