Commits

Radomir Dopieralski committed d4ab011

Make the code in tutorial more pythonic

* Make the style follow PEP8 more closely,
* fix the import names to be more consistent with the names in text,
* remove the distracting and useless ImportError handling,
* get rid of "import *",
* get rid of unneeded parentheses,
* replace the ugly backslash line continuations with parentheses,
* simplify the checks for collection emptiness.

Comments (0)

Files changed (2)

doc/tutorial/helloworld.rst

 window. ::
 
     import sys
+    import sdl2.ext
 
-    try:
-        import sdl2.ext as sdl2ext
-    except ImportError:
-        import traceback
-        traceback.print_exc()
-        sys.exit(1)
+    RESOURCES = sdl2.ext.Resources(__file__, "resources")
 
-    from sdl2.ext import Resources
-    RESOURCES = Resources(__file__, "resources")
-
-At first, we try to import the :mod:`sdl2.ext` module, which is
-necessary for displaying the window and image. :mod:`sdl2.ext` requires
-:mod:`sdl2`, so in case it could not load the SDL2 bindings, we will
-print the exact error stack information and exit with a failure.
-
-We also need some resources from the ``resources`` folder, so
-that we have a test image around to display on the window later on. In
-your own applications, it is unlikely that you will ever need to import
-them, but we need them here, so we use the :mod:`sdl2.ext.Resources`
-class to have them available.
+We need some resources from the ``resources`` folder, so that we have a test
+image around to display on the window later on. In your own applications, it is
+unlikely that you will ever need to import them, but we need them here, so we
+use the :mod:`sdl2.ext.Resources` class to have them available.
 
 Window creation and image loading
 ---------------------------------
 access to the screen, so we can display the logo and thus represent it
 to the user. ::
 
-    sdl2ext.init ()
+    sdl2.ext.init()
 
-    window = sdl2ext.Window("Hello World!", size=(640, 480))
+    window = sdl2.ext.Window("Hello World!", size=(640, 480))
     window.show()
 
-    factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)
+    factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE)
     sprite = factory.from_image(RESOURCES.get_path("hello.bmp"))
 
     spriterenderer = factory.create_sprite_renderer(window)
     spriterenderer.render(sprite)
 
 First, we initialise the :mod:`sdl2.ext` internals to gain access to the
-screen and and to be able to create windows on top of it. Once done with that,
+screen and to be able to create windows on top of it. Once done with that,
 :class:`sdl2.ext.Window` will create the window for us and we
 supply a title to be shown on the window's border along with its initial size.
 Since :class:`sdl2.ext.Window` instances are not shown by default,
 window is focused by the user and - as long as the event loop is
 running - will keep the window shown on the screen. ::
 
-    processor = sdl2ext.TestEventProcessor()
+    processor = sdl2.ext.TestEventProcessor()
     processor.run(window)
 
 Since this is a very first tutorial, we keep things simple here and use a
 it will exit from the event loop. Once it exits, we should clean up the
 video internals, we initialised at the beginning. Thus, a final call to ::
 
-    sdl2ext.quit()
+    sdl2.ext.quit()
 
 should be made.

doc/tutorial/pong.rst

 to close the window and exit the game. ::
 
     import sys
-    try:
-        from sdl2 import *
-        import sdl2.ext as sdl2ext
-    except ImportError:
-        import traceback
-        traceback.print_exc()
-        sys.exit(1)
+    import sdl2 import
+    import sdl2.ext
 
     def run():
-        sdl2ext.init()
-        window = sdl2ext.Window("The Pong Game", size=(800, 600))
+        sdl2.ext.init()
+        window = sdl2.ext.Window("The Pong Game", size=(800, 600))
         window.show()
         running = True
         while running:
-            events = sdl2ext.get_events()
+            events = sdl2.ext.get_events()
             for event in events:
-                if event.type == SDL_QUIT:
+                if event.type == sdl2.SDL_QUIT:
                     running = False
                     break
             window.refresh()
 
     running = True
     while running:
-        events = sdl2ext.get_events()
+        events = sdl2.ext.get_events()
         for event in events:
-            if event.type == SDL_QUIT:
+            if event.type == sdl2.SDL_QUIT:
                 running = False
                 break
         window.refresh()
 
-The while loop above is the main event loop of our application. It deals
-with all kinds of input events that can occur when working with the
-window, such as mouse movements, key strokes, resizing operations and so
-on. SDL handles a lot for us when it comes to events, so all we need to
-do is to check, if there are any events, retrieve each event one by
-one, and handle it, if necessary. For now, we will just handle the
-``SDL_QUIT`` event, which is raised when the window is about to be closed.
+The while loop above is the main event loop of our application. It deals with
+all kinds of input events that can occur when working with the window, such as
+mouse movements, key strokes, resizing operations and so on. SDL handles a lot
+for us when it comes to events, so all we need to do is to check, if there are
+any events, retrieve each event one by one, and handle it, if necessary. For
+now, we will just handle the ``sdl2.SDL_QUIT`` event, which is raised when the
+window is about to be closed.
 
 In any other case we will just refresh the window's graphics buffer, so
 it is updated and visible on-screen.
 
     [...]
 
-    WHITE = sdl2ext.Color(255, 255, 255)
+    WHITE = sdl2.ext.Color(255, 255, 255)
 
-    class SoftwareRenderer(sdl2ext.SoftwareSpriteRenderer):
+    class SoftwareRenderer(sdl2.ext.SoftwareSpriteRenderer):
         def __init__(self, window):
             super(SoftwareRenderer, self).__init__(window)
 
         def render(self, components):
-            sdl2ext.fill(self.surface, sdl2ext.Color(0, 0, 0))
+            sdl2ext.fill(self.surface, sdl2.ext.Color(0, 0, 0))
             super(SoftwareRenderer, self).render(components)
 
 
-    class Player(sdl2ext.Entity):
+    class Player(sdl2.ext.Entity):
         def __init__(self, world, sprite, posx=0, posy=0):
             self.sprite = sprite
             self.sprite.position = posx, posy
     def run():
         ...
 
-        world = sdl2ext.World()
+        world = sdl2.ext.World()
 
         spriterenderer = SoftwareRenderer(window)
         world.add_system(spriterenderer)
 
-        factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)
+        factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE)
         sp_paddle1 = factory.from_color(WHITE, size=(20, 100))
         sp_paddle2 = factory.from_color(WHITE, size=(20, 100))
 
 
         running = True
         while running:
-            events = sdl2ext.get_events()
+            events = sdl2.ext.get_events()
             for event in events:
-                if event.type == SDL_QUIT:
+                if event.type == sdl2.SDL_QUIT:
                     running = False
                     break
             world.process()
 within the window boundaries. ::
 
     [...]
-    class MovementSystem(sdl2ext.Applicator):
+    class MovementSystem(sdl2.ext.Applicator):
         def __init__(self, minx, miny, maxx, maxy):
             super(MovementSystem, self).__init__()
-            self.componenttypes = (Velocity, sdl2ext.Sprite)
+            self.componenttypes = (Velocity, sdl2.ext.Sprite)
             self.minx = minx
             self.miny = miny
             self.maxx = maxx
             self.vy = 0
 
 
-    class Player(sdl2ext.Entity):
+    class Player(sdl2.ext.Entity):
         def __init__(self, world, posx=0, posy=0):
             [...]
             self.velocity = Velocity()
 
 
-    class Ball(sdl2ext.Entity):
+    class Ball(sdl2.ext.Entity):
         def __init__(self, world, sprite, posx=0, posy=0):
             self.sprite = sprite
             self.sprite.position = posx, posy
 on colliding with the walls or the player paddles. ::
 
     [...]
-    class CollisionSystem(sdl2ext.Applicator):
+    class CollisionSystem(sdl2.ext.Applicator):
         def __init__(self, minx, miny, maxx, maxy):
             super(CollisionSystem, self).__init__()
-            self.componenttypes = (Velocity, sdl2ext.Sprite)
+            self.componenttypes = Velocity, sdl2.ext.Sprite
             self.ball = None
             self.minx = minx
             self.miny = miny
             self.maxy = maxy
 
         def _overlap(self, item):
-            pos, sprite = item[0], item[1]
+            pos, sprite = item
             if sprite == self.ball.sprite:
                 return False
 
             left, top, right, bottom = sprite.area
             bleft, btop, bright, bbottom = self.ball.sprite.area
 
-            return bleft < right and bright > left and \
-                btop < bottom and bbottom > top
+            return (bleft < right and bright > left and
+                    btop < bottom and bbottom > top)
 
         def process(self, world, componentsets):
             collitems = [comp for comp in componentsets if self._overlap(comp)]
-            if len(collitems) != 0:
+            if collitems:
                 self.ball.velocity.vx = -self.ball.velocity.vx
 
 
 
         running = True
         while running:
-            events = sdl2ext.get_events()
+            events = sdl2.ext.get_events()
             for event in events:
-                if event.type == SDL_QUIT:
+                if event.type == sdl2.SDL_QUIT:
                     running = False
                     break
-            SDL_Delay(10)
+            sdl2.SDL_Delay(10)
             world.process()
 
     if __name__ == "__main__":
         [...]
         running = True
         while running:
-            events = sdl2ext.get_events()
+            events = sdl2.ext.get_events()
             for event in events:
-                if event.type == SDL_QUIT:
+                if event.type == sdl2.SDL_QUIT:
                     running = False
                     break
-                if event.type == SDL_KEYDOWN:
-                    if event.key.keysym.sym == SDLK_UP:
+                if event.type == sdl2.SDL_KEYDOWN:
+                    if event.key.keysym.sym == sdl2.SDLK_UP:
                         player1.velocity.vy = -3
-                    elif event.key.keysym.sym == SDLK_DOWN:
+                    elif event.key.keysym.sym == sdl2.SDLK_DOWN:
                         player1.velocity.vy = 3
-                elif event.type == SDL_KEYUP:
-                    if event.key.keysym.sym in (SDLK_UP, SDLK_DOWN):
+                elif event.type == sdl2.SDL_KEYUP:
+                    if event.key.keysym.sym in (sdl2.SDLK_UP, sdl2.SDLK_DOWN):
                         player1.velocity.vy = 0
-            SDL_Delay(10)
+            sdl2.SDL_Delay(10)
             world.process()
 
     if __name__ == "__main__":
         sys.exit(run())
 
-Every event that can occur and that is supported by SDL2 can be identified
-by a static event type code. This allows us to check for a key stroke, mouse
-button press, and so on. First, we have to check for ``SDL_KEYDOWN`` and
-``SDL_KEYUP`` events, so we can start and stop the paddle movement on demand.
-Once we identified such events, we need to check, whether the pressed or
-released key is actually the Up or Down key, so that we do not start or
-stop moving the paddle, if the user presses R or G or whatever.
+Every event that can occur and that is supported by SDL2 can be identified by a
+static event type code. This allows us to check for a key stroke, mouse button
+press, and so on. First, we have to check for ``sdl2.SDL_KEYDOWN`` and
+``sdl2.SDL_KEYUP`` events, so we can start and stop the paddle movement on
+demand.  Once we identified such events, we need to check, whether the pressed
+or released key is actually the Up or Down key, so that we do not start or stop
+moving the paddle, if the user presses R or G or whatever.
 
 Whenever the Up or Down key are pressed down, we allow the left player
 paddle to move by changing its velocity information for the vertical
 of the paddle it will bounce back with some angle close to 90 degrees to
 its initial movement direction. ::
 
-    class CollisionSystem(sdl2ext.Applicator):
+    class CollisionSystem(sdl2.ext.Applicator):
         [...]
 
         def process(self, world, componentsets):
             collitems = [comp for comp in componentsets if self._overlap(comp)]
-            if len(collitems) != 0:
+            if collitems:
                 self.ball.velocity.vx = -self.ball.velocity.vx
 
                 sprite = collitems[0][1]
 This means that we just need to invert the vertical velocity, once the
 ball hits the top or bottom. ::
 
-    class CollisionSystem(sdl2ext.Applicator):
+    class CollisionSystem(sdl2.ext.Applicator):
         [...]
 
         def process(self, world, componentsets):
             [...]
 
-            if self.ball.sprite.y <= self.miny or \
-                    self.ball.sprite.y + self.ball.sprite.size[1] >= self.maxy:
+            if (self.ball.sprite.y <= self.miny or
+                self.ball.sprite.y + self.ball.sprite.size[1] >= self.maxy):
                 self.ball.velocity.vy = - self.ball.velocity.vy
 
-            if self.ball.sprite.x <= self.minx or \
-                    self.ball.sprite.x + self.ball.sprite.size[0] >= self.maxx:
+            if (self.ball.sprite.x <= self.minx or
+                self.ball.sprite.x + self.ball.sprite.size[0] >= self.maxx):
                 self.ball.velocity.vx = - self.ball.velocity.vx
 
 Creating an enemy
 create a simple computer-controlled player that tries to hit the ball
 back to us, which sounds more interesting. ::
 
-    class TrackingAIController(sdl2ext.Applicator):
+    class TrackingAIController(sdl2.ext.Applicator):
         def __init__(self, miny, maxy):
             super(TrackingAIController, self).__init__()
-            self.componenttypes = (PlayerData, Velocity, sdl2ext.Sprite)
+            self.componenttypes = PlayerData, Velocity, sdl2.ext.Sprite
             self.miny = miny
             self.maxy = maxy
             self.ball = None
             self.ai = False
 
 
-    class Player(sdl2ext.Entity):
+    class Player(sdl2.ext.Entity):
         def __init__(self, world, sprite, posx=0, posy=0, ai=False):
             self.sprite = sprite
             self.sprite.position = posx, posy