Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 519a657156f8292188a5d8f560aa6169 > files > 47

pygame-doc-1.8.1-4mdv2010.0.i586.rpm

<html>
<head>
<title>~/src/pygame/examples/chimp.py.html</title>
<meta name="Generator" content="Vim/6.1">
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="#0000ff">#/usr/bin/env python</font>
&quot;&quot;&quot;
<font color="#ff00ff">This simple example is used for the line-by-line tutorial</font>
<font color="#ff00ff">that comes with pygame. It is based on a 'popular' web banner.</font>
<font color="#ff00ff">Note there are comments here, but for the full explanation, </font>
<font color="#ff00ff">follow along in the tutorial.</font>
&quot;&quot;&quot;


<font color="#0000ff">#Import Modules</font>
<font color="#a020f0">import</font> os, pygame
<font color="#a020f0">from</font> pygame.locals <font color="#a020f0">import</font> *

<font color="#a52a2a"><b>if</b></font> <font color="#a52a2a"><b>not</b></font> pygame.font: <font color="#a52a2a"><b>print</b></font> '<font color="#ff00ff">Warning, fonts disabled</font>'
<font color="#a52a2a"><b>if</b></font> <font color="#a52a2a"><b>not</b></font> pygame.mixer: <font color="#a52a2a"><b>print</b></font> '<font color="#ff00ff">Warning, sound disabled</font>'


<font color="#0000ff">#functions to create our resources</font>
<font color="#a52a2a"><b>def</b></font> <font color="#008b8b">load_image</font>(name, colorkey=None):
    fullname = os.path.join('<font color="#ff00ff">data</font>', name)
    <font color="#a52a2a"><b>try</b></font>:
        image = pygame.image.load(fullname)
    <font color="#a52a2a"><b>except</b></font> pygame.error, message:
        <font color="#a52a2a"><b>print</b></font> '<font color="#ff00ff">Cannot load image:</font>', fullname
        <font color="#a52a2a"><b>raise</b></font> SystemExit, message
    image = image.convert()
    <font color="#a52a2a"><b>if</b></font> colorkey <font color="#a52a2a"><b>is</b></font> <font color="#a52a2a"><b>not</b></font> None:
        <font color="#a52a2a"><b>if</b></font> colorkey <font color="#a52a2a"><b>is</b></font> -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    <font color="#a52a2a"><b>return</b></font> image, image.get_rect()

<font color="#a52a2a"><b>def</b></font> <font color="#008b8b">load_sound</font>(name):
    <font color="#a52a2a"><b>class</b></font> <font color="#008b8b">NoneSound</font>:
        <font color="#a52a2a"><b>def</b></font> <font color="#008b8b">play</font>(self): <font color="#a52a2a"><b>pass</b></font>
    <font color="#a52a2a"><b>if</b></font> <font color="#a52a2a"><b>not</b></font> pygame.mixer <font color="#a52a2a"><b>or</b></font> <font color="#a52a2a"><b>not</b></font> pygame.mixer.get_init():
        <font color="#a52a2a"><b>return</b></font> NoneSound()
    fullname = os.path.join('<font color="#ff00ff">data</font>', name)
    <font color="#a52a2a"><b>try</b></font>:
        sound = pygame.mixer.Sound(fullname)
    <font color="#a52a2a"><b>except</b></font> pygame.error, message:
        <font color="#a52a2a"><b>print</b></font> '<font color="#ff00ff">Cannot load sound:</font>', fullname
        <font color="#a52a2a"><b>raise</b></font> SystemExit, message
    <font color="#a52a2a"><b>return</b></font> sound


<font color="#0000ff">#classes for our game objects</font>
<font color="#a52a2a"><b>class</b></font> <font color="#008b8b">Fist</font>(pygame.sprite.Sprite):
    &quot;&quot;&quot;<font color="#ff00ff">moves a clenched fist on the screen, following the mouse</font>&quot;&quot;&quot;
    <font color="#a52a2a"><b>def</b></font> <font color="#008b8b">__init__</font>(self):
        pygame.sprite.Sprite.__init__(self) <font color="#0000ff">#call Sprite initializer</font>
        self.image, self.rect = load_image('<font color="#ff00ff">fist.bmp</font>', -1)
        self.punching = 0

    <font color="#a52a2a"><b>def</b></font> <font color="#008b8b">update</font>(self):
        &quot;<font color="#ff00ff">move the fist based on the mouse position</font>&quot;
        pos = pygame.mouse.get_pos()
        self.rect.midtop = pos
        <font color="#a52a2a"><b>if</b></font> self.punching:
            self.rect.move_ip(5, 10)

    <font color="#a52a2a"><b>def</b></font> <font color="#008b8b">punch</font>(self, target):
        &quot;<font color="#ff00ff">returns true if the fist collides with the target</font>&quot;
        <font color="#a52a2a"><b>if</b></font> <font color="#a52a2a"><b>not</b></font> self.punching:
            self.punching = 1
            hitbox = self.rect.inflate(-5, -5)
            <font color="#a52a2a"><b>return</b></font> hitbox.colliderect(target.rect)

    <font color="#a52a2a"><b>def</b></font> <font color="#008b8b">unpunch</font>(self):
        &quot;<font color="#ff00ff">called to pull the fist back</font>&quot;
        self.punching = 0


<font color="#a52a2a"><b>class</b></font> <font color="#008b8b">Chimp</font>(pygame.sprite.Sprite):
    &quot;&quot;&quot;<font color="#ff00ff">moves a monkey critter across the screen. it can spin the</font>
<font color="#ff00ff">       monkey when it is punched.</font>&quot;&quot;&quot;
    <font color="#a52a2a"><b>def</b></font> <font color="#008b8b">__init__</font>(self):
        pygame.sprite.Sprite.__init__(self) <font color="#0000ff">#call Sprite intializer</font>
        self.image, self.rect = load_image('<font color="#ff00ff">chimp.bmp</font>', -1)
        screen = pygame.display.get_surface()
        self.area = screen.get_rect()
        self.rect.topleft = 10, 10
        self.move = 9
        self.dizzy = 0

    <font color="#a52a2a"><b>def</b></font> <font color="#008b8b">update</font>(self):
        &quot;<font color="#ff00ff">walk or spin, depending on the monkeys state</font>&quot;
        <font color="#a52a2a"><b>if</b></font> self.dizzy:
            self._spin()
        <font color="#a52a2a"><b>else</b></font>:
            self._walk()

    <font color="#a52a2a"><b>def</b></font> <font color="#008b8b">_walk</font>(self):
        &quot;<font color="#ff00ff">move the monkey across the screen, and turn at the ends</font>&quot;
        newpos = self.rect.move((self.move, 0))
        <font color="#a52a2a"><b>if</b></font> self.rect.left &lt; self.area.left <font color="#a52a2a"><b>or</b></font> <font color="#6a5acd">\</font>
           self.rect.right &gt; self.area.right:
            self.move = -self.move
            newpos = self.rect.move((self.move, 0))
            self.image = pygame.transform.flip(self.image, 1, 0)
        self.rect = newpos

    <font color="#a52a2a"><b>def</b></font> <font color="#008b8b">_spin</font>(self):
        &quot;<font color="#ff00ff">spin the monkey image</font>&quot;
        center = self.rect.center
        self.dizzy = self.dizzy + 12
        <font color="#a52a2a"><b>if</b></font> self.dizzy &gt;= 360:
            self.dizzy = 0
            self.image = self.original
        <font color="#a52a2a"><b>else</b></font>:
            rotate = pygame.transform.rotate
            self.image = rotate(self.original, self.dizzy)
        self.rect = self.image.get_rect(center=center)

    <font color="#a52a2a"><b>def</b></font> <font color="#008b8b">punched</font>(self):
        &quot;<font color="#ff00ff">this will cause the monkey to start spinning</font>&quot;
        <font color="#a52a2a"><b>if</b></font> <font color="#a52a2a"><b>not</b></font> self.dizzy:
            self.dizzy = 1
            self.original = self.image


<font color="#a52a2a"><b>def</b></font> <font color="#008b8b">main</font>():
    &quot;&quot;&quot;<font color="#ff00ff">this function is called when the program starts.</font>
<font color="#ff00ff">       it initializes everything it needs, then runs in</font>
<font color="#ff00ff">       a loop until the function returns.</font>&quot;&quot;&quot;
<font color="#0000ff">#Initialize Everything</font>
    pygame.init()
    screen = pygame.display.set_mode((468, 60))
    pygame.display.set_caption('<font color="#ff00ff">Monkey Fever</font>')
    pygame.mouse.set_visible(0)

<font color="#0000ff">#Create The Backgound</font>
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250, 250, 250))

<font color="#0000ff">#Put Text On The Background, Centered</font>
    <font color="#a52a2a"><b>if</b></font> pygame.font:
        font = pygame.font.Font(None, 36)
        text = font.render(&quot;<font color="#ff00ff">Pummel The Chimp, And Win $$$</font>&quot;, 1, (10, 10, 10))
        textpos = text.get_rect(centerx=background.get_width()/2)
        background.blit(text, textpos)

<font color="#0000ff">#Display The Background</font>
    screen.blit(background, (0, 0))
    pygame.display.flip()

<font color="#0000ff">#Prepare Game Objects</font>
    clock = pygame.time.Clock()
    whiff_sound = load_sound('<font color="#ff00ff">whiff.wav</font>')
    punch_sound = load_sound('<font color="#ff00ff">punch.wav</font>')
    chimp = Chimp()
    fist = Fist()
    allsprites = pygame.sprite.RenderPlain((fist, chimp))

<font color="#0000ff">#Main Loop</font>
    <font color="#a52a2a"><b>while</b></font> 1:
        clock.tick(60)

    <font color="#0000ff">#Handle Input Events</font>
        <font color="#a52a2a"><b>for</b></font> event <font color="#a52a2a"><b>in</b></font> pygame.event.get():
            <font color="#a52a2a"><b>if</b></font> event.type <font color="#a52a2a"><b>==</b></font> QUIT:
                <font color="#a52a2a"><b>return</b></font>
            <font color="#a52a2a"><b>elif</b></font> event.type <font color="#a52a2a"><b>==</b></font> KEYDOWN <font color="#a52a2a"><b>and</b></font> event.key <font color="#a52a2a"><b>==</b></font> K_ESCAPE:
                <font color="#a52a2a"><b>return</b></font>
            <font color="#a52a2a"><b>elif</b></font> event.type <font color="#a52a2a"><b>==</b></font> MOUSEBUTTONDOWN:
                <font color="#a52a2a"><b>if</b></font> fist.punch(chimp):
                    punch_sound.play() <font color="#0000ff">#punch</font>
                    chimp.punched()
                <font color="#a52a2a"><b>else</b></font>:
                    whiff_sound.play() <font color="#0000ff">#miss</font>
            <font color="#a52a2a"><b>elif</b></font> event.type <font color="#a52a2a"><b>is</b></font> MOUSEBUTTONUP:
                fist.unpunch()

        allsprites.update()

    <font color="#0000ff">#Draw Everything</font>
        screen.blit(background, (0, 0))
        allsprites.draw(screen)
        pygame.display.flip()

<font color="#0000ff">#Game Over</font>


<font color="#0000ff">#this calls the 'main' function when this script is executed</font>
<font color="#a52a2a"><b>if</b></font> __name__ == '<font color="#ff00ff">__main__</font>': main()
</pre>
</body>
</html>