Add HUD, including lives and score keeping
authorsam@talyn.home.samhart.net
Thu Aug 16 19:46:13 2007 -0400 (4 years ago)
changeset 37d24a25111a12
parent 367b3b84106678
child 381f7b8c80d062
Add HUD, including lives and score keeping
fgij-prinv.py
font/DejaVuSansMono.ttf
font/copyright
       1 --- a/fgij-prinv.py	Thu Aug 16 18:16:18 2007 -0400
       2 +++ b/fgij-prinv.py	Thu Aug 16 19:46:13 2007 -0400
       3 @@ -34,6 +34,8 @@
       4  
       5  music_filename = "audio/ping_island.ogg"
       6  asplode_sound_filename = "audio/Explosion.wav"
       7 +
       8 +font_filename = "font/DejaVuSansMono.ttf"
       9  
      10  class space:
      11      """
      12 @@ -220,6 +222,7 @@
      13          self.tall = -1
      14          self.plasma_object = None
      15          self.alive = False
      16 +        self.hud = None
      17          self._setup()
      18  
      19      def _setup(self):
      20 @@ -236,6 +239,11 @@
      21          """
      22          self.people = people
      23  
      24 +    def set_hud(self, hud):
      25 +        """
      26 +        """
      27 +        self.hud = hud
      28 +
      29      def set_asplode_sound(self, asplode_sound):
      30          """
      31          """
      32 @@ -259,6 +267,7 @@
      33                      i.asplode()
      34                      self.alive = False
      35                      self.asplode_sound.play()
      36 +                    self.hud.add_points(i.person.score)
      37                      break
      38  
      39  class space_ship:
      40 @@ -274,6 +283,7 @@
      41          self.ship_object = None
      42          self.plasmas = []
      43          self.max_plasmas = 3
      44 +        self.hud = None
      45          self._setup()
      46  
      47      def set_people(self, people):
      48 @@ -289,6 +299,13 @@
      49          self.asplode_sound = asplode_sound
      50          for p in self.plasmas:
      51              p.set_asplode_sound(asplode_sound)
      52 +
      53 +    def set_hud(self, hud):
      54 +        """
      55 +        """
      56 +        self.hud = hud
      57 +        for p in self.plasmas:
      58 +            p.set_hud(hud)
      59  
      60      def _setup(self):
      61          """
      62 @@ -327,6 +344,53 @@
      63                  p.y = self.max_y
      64                  break
      65  
      66 +class hud_object:
      67 +    def __init__(self, screen, ship_object, size, lives, color):
      68 +        self.screen = screen
      69 +        self.max_x = -1
      70 +        self.max_y = -1
      71 +        self.ms_w = -1
      72 +        self.ms_h = -1
      73 +        self.ship_object = ship_object
      74 +        self.font_size = size
      75 +        self.font = None
      76 +        self.mini_ship = None
      77 +        self.lives = lives
      78 +        self.color = color
      79 +        self.score = 0
      80 +        self.score_len = 10
      81 +        self._setup()
      82 +
      83 +    def _setup(self):
      84 +        self.font = pygame.font.Font(font_filename, self.font_size)
      85 +        h2 = self.font_size
      86 +        w1, h1 = self.ship_object.ship_object.get_size()
      87 +        w2 = int(float(h2) * (float(w1)/float(h1))) #BAH MAN!
      88 +        self.mini_ship = pygame.transform.scale(self.ship_object.ship_object, (w2, h2))
      89 +        self.ms_w, self.ms_h = self.mini_ship.get_size()
      90 +        self.max_x, self.max_y = self.screen.get_size()
      91 +
      92 +    def blit(self):
      93 +        """
      94 +        """
      95 +        # blit the lives left
      96 +        y = 1
      97 +        for i in range(self.lives):
      98 +            x = self.max_x - ((i+1) * self.ms_w)
      99 +            self.screen.blit(self.mini_ship, [x, y])
     100 +
     101 +        score_format = "%%0%ii" % self.score_len
     102 +        score_text = score_format % self.score
     103 +        score_img = self.font.render(score_text, True, self.color)
     104 +        self.screen.blit(score_img,[1,1])
     105 +
     106 +    def add_points(self, points):
     107 +        """
     108 +        """
     109 +        self.score = self.score + points
     110 +        # FIXME!
     111 +        # We don't account for looping
     112 +
     113  class ProdInvader:
     114      def __init__(self):
     115          self.space_obj = None
     116 @@ -338,9 +402,13 @@
     117          self.max_x = 800
     118          self.max_y = 600
     119          self.fps = 30
     120 +        self.font_size = 40
     121 +        self.lives = 4
     122 +        self.score_color = (46, 229, 236)
     123          self.enemy_object = []
     124          self.ship_object = None
     125          self.clock = None
     126 +        self.hud = None
     127          self._setup()
     128  
     129      def _setup(self):
     130 @@ -379,6 +447,10 @@
     131          self.ship_object.set_asplode_sound(self.asplode_sound)
     132  
     133          anykey = pygame.image.load(anykey_filename)
     134 +
     135 +        # Prepare the HUD
     136 +        self.hud = hud_object(self.screen, self.ship_object, self.font_size, self.lives, self.score_color)
     137 +        self.ship_object.set_hud(self.hud)
     138  
     139          # Get the clock object
     140          self.clock = pygame.time.Clock()
     141 @@ -439,6 +511,7 @@
     142                  e.blit()
     143              self.screen.blit(self.enemy_screen, [0,0])
     144              self.ship_object.blit()
     145 +            self.hud.blit()
     146              for event in pygame.event.get():
     147                  if event.type == QUIT:
     148                      return self.clock.get_fps()
     1.1 Binary file font/DejaVuSansMono.ttf has changed
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/font/copyright	Thu Aug 16 19:46:13 2007 -0400
     2.3 @@ -0,0 +1,58 @@
     2.4 +This package was debianized by Peter Cernak <pce@users.sourceforge.net> on
     2.5 +Sun,  5 Sep 2004 17:10:26 +0200.
     2.6 +
     2.7 +It was downloaded from http://dejavu.sourceforge.net/
     2.8 +
     2.9 +Upstream Authors: Stepan Roh <src@users.sourceforge.net> (original author),
    2.10 +                  see /usr/share/doc/ttf-dejavu/AUTHORS for full list
    2.11 +
    2.12 +Copyright:
    2.13 +
    2.14 +Fonts are (c) Bitstream (see below). DejaVu changes are in public domain.
    2.15 +
    2.16 +Bitstream Vera Fonts Copyright
    2.17 +------------------------------
    2.18 +
    2.19 +Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is
    2.20 +a trademark of Bitstream, Inc.
    2.21 +
    2.22 +Permission is hereby granted, free of charge, to any person obtaining a copy
    2.23 +of the fonts accompanying this license ("Fonts") and associated
    2.24 +documentation files (the "Font Software"), to reproduce and distribute the
    2.25 +Font Software, including without limitation the rights to use, copy, merge,
    2.26 +publish, distribute, and/or sell copies of the Font Software, and to permit
    2.27 +persons to whom the Font Software is furnished to do so, subject to the
    2.28 +following conditions:
    2.29 +
    2.30 +The above copyright and trademark notices and this permission notice shall
    2.31 +be included in all copies of one or more of the Font Software typefaces.
    2.32 +
    2.33 +The Font Software may be modified, altered, or added to, and in particular
    2.34 +the designs of glyphs or characters in the Fonts may be modified and
    2.35 +additional glyphs or characters may be added to the Fonts, only if the fonts
    2.36 +are renamed to names not containing either the words "Bitstream" or the word
    2.37 +"Vera".
    2.38 +
    2.39 +This License becomes null and void to the extent applicable to Fonts or Font
    2.40 +Software that has been modified and is distributed under the "Bitstream
    2.41 +Vera" names.
    2.42 +
    2.43 +The Font Software may be sold as part of a larger software package but no
    2.44 +copy of one or more of the Font Software typefaces may be sold by itself.
    2.45 +
    2.46 +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    2.47 +OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY,
    2.48 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT,
    2.49 +TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME
    2.50 +FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING
    2.51 +ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
    2.52 +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
    2.53 +THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE
    2.54 +FONT SOFTWARE.
    2.55 +
    2.56 +Except as contained in this notice, the names of Gnome, the Gnome
    2.57 +Foundation, and Bitstream Inc., shall not be used in advertising or
    2.58 +otherwise to promote the sale, use or other dealings in this Font Software
    2.59 +without prior written authorization from the Gnome Foundation or Bitstream
    2.60 +Inc., respectively. For further information, contact: fonts at gnome dot
    2.61 +org.