File: chimp.py.html

package info (click to toggle)
pygame 1.9.1release%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 7,280 kB
  • ctags: 6,685
  • sloc: ansic: 41,205; python: 21,987; cpp: 537; objc: 196; php: 92; sh: 77; makefile: 41
file content (196 lines) | stat: -rw-r--r-- 10,691 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<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>