[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] placement of graphics code



> The MVC thing makes perfect sense to me on a very very very high and
> abstract level. Applying it in the real world I find difficult. My
> long term goal is an animated roguelike game. That's way beyond my
> current skill level, so I'm settling for attempting an animated
> version of this old favorite:
> 

A rougelike game has a level of complexity where i'd definetly do a MVC,
event-driven implementation, but for your guessing game example, i'd
probably do something like this:

class PromptSprite(Sprite):
  """the sprite to prompt the player for his guess"""
  def __init__( ... ):
      ...
      font = pygame.font.Font(None, 30)
      text = "What's your guess?"
      self.image = font.render( text, 1, (255,0,0))
      self.rect  = self.image.get_rect()


class ResultSprite(Sprite):
  """the sprite to show the player if he was correct"""
  def __init__( ... ):
      ...
      font = pygame.font.Font(None, 30)
      self.responses = { 'low': "That's too low",
                         'high': "That's too high",
                         'right': "You got it",
                         'empty': " ",
                       }

      self.response = self.responses['empty']

  def update():
      self.image = font.render( self.response, 1, (255,0,0))
      self.rect  = self.image.get_rect()

class Game:
  def __init__( responseSprite ):
      from random import Random
      rng = Random()
      self.x = rng.randrange( 0, 9 )

  def Guess( guessInteger ):
      """see where the guess lies and send out a USEREVENT
         accordingly"""

class EventManager:
  """... this guy checks pygame events, if it's a keypress and numeric,
     it can call Game.Guess().  If it's a USEREVENT, send it to the
     ResponseSprite, so it can update it's response"""

that's just a rough idea.

> import random
> x=random.randint(0,9)
> guess = None
> while (guess != x):
>     guess = int( raw_input( "guess?" ))
>     if (guess > x):
>         print "Too high"
>     elif (guess < x):
>         print "Too low"
>     else:
>         print "you got it"
> 

-- 
+-------------------------------------------------------------+
| Shandy Brown                               Digital Thinkery |
| shandy@geeky.net             http://www.digitalthinkery.com |
+-------------------------------------------------------------+