annotate docs/tut/MoveIt.html @ 3172:d1b367fc08bc tests-returncode

Use a return code of 1 when the tests fail.
author Thomas Kluyver <takowl@gmail.com>
date Wed, 09 Jan 2013 10:13:57 +0000
parents 9e633ef40a29
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
1 <html><head> <!--
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
2 TUTORIAL:Help! How Do I Move An Image?
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
3 -->
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
4 <title>Pygame Tutorials - Help! How Do I Move An Image?</title>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
5 </head>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
6 <body>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
7
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
8 <h1 align="center"><font size="-1">Pygame Tutorials</font><br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
9 Help! How Do I Move An Image?</h1>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
10
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
11 <h2 align="center">by Pete Shinners<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
12 <font size="-1">pete@shinners.org</font></h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
13
408
2ec6bf9b098c docs cleanup
pygame
parents: 371
diff changeset
14 <h3 align="center">Revision 1.2, August 20, 2002</h3>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
15 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
16 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
17 Many people new to programming and graphics have a hard time figuring
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
18 out how to make an image move around the screen. Without understanding
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
19 all the concepts, it can be very confusing. You're not the first person
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
20 to be stuck here, I'll do my best to take things step by step. We'll even
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
21 try to end with methods of keeping your animations efficient.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
22 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
23 Note that we won't be teaching you to program with python in this article,
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
24 just introduce you to some of the basics with pygame.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
25
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
26 <h2>Just Pixels On The Screen</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
27 Pygame has a display Surface. This is basically an image that is visible
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
28 on the screen, and the image is made up of pixels. The main way you change
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
29 these pixels is by calling the blit() function. This copies the pixels
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
30 from one image onto another.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
31 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
32 This is the first thing to understand. When you blit an image onto the
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
33 screen, you are simply changing the color of the pixels on the screen.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
34 Pixels aren't added or moved, we just change the colors of the pixels aready
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
35 on the screen. These images you blit to the screen are also Surfaces in
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
36 pygame, but they are in no way connected to the display Surface. When they
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
37 are blitted to the screen they are copied into the display, but you still
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
38 have a unique copy of the original.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
39 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
40 With this brief description. Perhaps you can already understand what
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
41 is needed to "move" an image. We don't actually move anything at all. We
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
42 simply blit the image in a new position. But before we draw the image in
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
43 the new position, we'll need to "erase" the old one. Otherwise the image
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
44 will be visible in two places on the screen. By rapidly erasing the image
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
45 and redrawing it in a new place, we achieve the "illusion" of movement.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
46 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
47 Through the rest of this tutorial we will break this process down into
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
48 simpler steps. Even explaining the best ways to have multiple images moving
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
49 around the screen. You probably already have questions. Like, how do we
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
50 "erase" the image before drawing it in a new position? Perhaps you're still
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
51 totally lost? Well hopefully the rest of this tutorial can straighten things
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
52 out for you.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
53 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
54
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
55 <h2>Let's Go Back A Step</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
56 Perhaps the concept of pixels and images is still a little foreign to
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
57 you? Well good news, for the next few sections we are going to use code that
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
58 does everything we want, it just doesn't use pixels. We're going to create
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
59 a small python list of 6 numbers, and imagine it represents some fantastic
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
60 graphics we could see on the screen. It might actually be surprising how
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
61 closely this represents exactly what we'll later be doing with real graphics.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
62 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
63 So let's begin by creating our screen list and fill it with a beautiful
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
64 landscape of 1s and 2s.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
65
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
66 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
67 <b>&gt;&gt;&gt;</b> screen = [1, 1, 2, 2, 2, 1]
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
68 <b>&gt;&gt;&gt;</b> print screen
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
69 <b>[1, 1, 2, 2, 2, 1]</b>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
70 </pre></blockquote>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
71 Now we've created our background. It's not going to be very exciting
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
72 unless we also draw a player on the screen. We'll create a mighty hero
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
73 that looks like the number 8. Let's stick him near the middle of the map
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
74 and see what it looks like.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
75
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
76 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
77 <b>&gt;&gt;&gt;</b> screen[3] = 8
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
78 <b>&gt;&gt;&gt;</b> print screen
296
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
79 <b>[1, 1, 2, 8, 2, 1]</b>
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
80 </pre></blockquote>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
81 This might have been as far as you've gotten if you jumped right in doing
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
82 some graphics programming with pygame. You've got some nice looking stuff
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
83 on the screen, but it cannot move anywhere. Perhaps now that our screen
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
84 is just a list of numbers, it's easier to see how to move him?<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
85 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
86
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
87 <h2>Making The Hero Move</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
88 Before we can start moving the character. We need to keep track of some
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
89 sort of position for him. In the last section when we drew him, we just picked
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
90 an arbitrary position. Let's do it a little more officially this time.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
91
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
92 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
93 <b>&gt;&gt;&gt;</b> playerpos = 3
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
94 <b>&gt;&gt;&gt;</b> screen[playerpos] = 8
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
95 <b>&gt;&gt;&gt;</b> print screen
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
96 <b>[1, 1, 2, 8, 2, 1]</b>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
97 </pre></blockquote>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
98 Now it is pretty easy to move him to a new position. We simple change
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
99 the value of playerpos, and draw him on the screen again.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
100
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
101 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
102 <b>&gt;&gt;&gt;</b> playerpos = playerpos - 1
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
103 <b>&gt;&gt;&gt;</b> screen[playerpos] = 8
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
104 <b>&gt;&gt;&gt;</b> print screen
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
105 <b>[1, 1, 8, 8, 2, 1]</b>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
106 </pre></blockquote>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
107 Whoops. Now we can see two heros. One in the old position, and one
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
108 in his new position. This is exactly the reason we need to "erase" the hero
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
109 in his old position before we draw him in the new position. To erase him,
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
110 we need to change that value in the list back to what it was before the hero
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
111 was there. That means we need to keep track of the values on the screen before
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
112 the hero replaced them. There's several way you could do this, but the easiest
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
113 is usually to keep a separate copy of the screen background. This means
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
114 we need to make some changes to our little game.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
115 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
116
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
117 <h2>Creating A Map</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
118 What we want to do is create a separate list we will call our background.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
119 We will create the background so it looks like our original screen did,
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
120 with 1s and 2s. Then we will copy each item from the background to the screen.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
121 After that we can finally draw our hero back onto the screen.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
122
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
123 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
124 <b>&gt;&gt;&gt;</b> background = [1, 1, 2, 2, 2, 1]
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
125 <b>&gt;&gt;&gt;</b> screen = [0]*6 <i>#a new blank screen</i>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
126 <b>&gt;&gt;&gt;</b> for i in range(6):
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
127 <b>...</b> screen[i] = background[i]
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
128 <b>&gt;&gt;&gt;</b> print screen
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
129 <b>[1, 1, 2, 2, 2, 1]</b>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
130 <b>&gt;&gt;&gt;</b> playerpos = 3
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
131 <b>&gt;&gt;&gt;</b> screen[playerpos] = 8
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
132 <b>&gt;&gt;&gt;</b> print screen
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
133 <b>[1, 1, 2, 8, 2, 1]</b>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
134 </pre></blockquote>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
135 It may seem like a lot of extra work. We're no farther off than we were
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
136 before the last time we tried to make him move. But this time we have the
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
137 extra information we need to move him properly.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
138 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
139
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
140 <h2>Making The Hero Move (Take 2)</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
141 This time it will be easy to move the hero around. First we will erase
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
142 the hero from his old position. We do this by copying the correct value
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
143 from the background onto the screen. Then we will draw the character in his
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
144 new position on the screen<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
145
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
146 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
147 <b>&gt;&gt;&gt;</b> print screen
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
148 <b>[1, 1, 2, 8, 2, 1]</b>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
149 <b>&gt;&gt;&gt;</b> screen[playerpos] = background[playerpos]
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
150 <b>&gt;&gt;&gt;</b> playerpos = playerpos - 1
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
151 <b>&gt;&gt;&gt;</b> screen[playerpos] = 8
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
152 <b>&gt;&gt;&gt;</b> print screen
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
153 <b>[1, 1, 8, 2, 2, 1]</b>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
154 </pre></blockquote>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
155 There it is. The hero has moved one space to the left. We can use this
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
156 same code to move him to the left again.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
157
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
158 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
159 <b>&gt;&gt;&gt;</b> screen[playerpos] = background[playerpos]
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
160 <b>&gt;&gt;&gt;</b> playerpos = playerpos - 1
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
161 <b>&gt;&gt;&gt;</b> screen[playerpos] = 8
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
162 <b>&gt;&gt;&gt;</b> print screen
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
163 <b>[1, 8, 2, 2, 2, 1]</b>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
164 </pre></blockquote>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
165 Excellent! This isn't exactly what you'd call smooth animation. But with
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
166 a couple small changes, we'll make this work directly with graphics on
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
167 the screen.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
168 <br>
296
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
169
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
170
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
171 <h2>Definition: "blit"</h2>
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
172 In the next sections we will transform our program from using lists to
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
173 using real graphics on the screen. When displaying the graphics we will
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
174 use the term <u>blit</u> frequently. If you are new to doing graphics
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
175 work, you are probably unframiliar with this common term.<br>
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
176 <br>
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
177 <b>BLIT:</b> Basically, blit means to copy graphics from one image
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
178 to another. A more formal definition is to copy an array of data
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
179 to a bitmapped array destination. You can think of blit as just
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
180 <i>"assigning"</i> pixels. Much like setting values in our screen-list
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
181 above, blitting assigns the color of pixels in our image.<br>
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
182 <br>
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
183 Other graphics libraries will use the word <i>bitblt</i>, or just <i>blt</i>,
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
184 but they are talking about the same thing. It is basically copying
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
185 memory from one place to another. Actually, it is a bit more advanced than
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
186 straight copying of memory, since it needs to handle things like pixel
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
187 formats, clipping, and scanline pitches. Advanced blitters can also
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
188 handle things like transparancy and other special effects.<br>
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
189 <br>
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
190
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
191
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
192 <h2>Going From The List To The Screen</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
193 To take the code we see in the above to examples and make them work with
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
194 pygame is very straightforward. We'll pretend we have loaded some pretty
296
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
195 graphics and named them "terrain1", "terrain2", and "hero". Where before
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
196 we assigned numbers to a list, we now blit graphics to the screen. Another
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
197 big change, instead of using positions as a single index (0 through 5), we
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
198 now need a two dimensional coordinate. We'll pretend each of the graphics
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
199 in our game is 10 pixels wide.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
200
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
201 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
202 <b>&gt;&gt;&gt;</b> background = [terrain1, terrain1, terrain2, terrain2, terrain2, terrain1]
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
203 <b>&gt;&gt;&gt;</b> screen = create_graphics_screen()
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
204 <b>&gt;&gt;&gt;</b> for i in range(6):
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
205 <b>...</b> screen.blit(background[i], (i*10, 0))
408
2ec6bf9b098c docs cleanup
pygame
parents: 371
diff changeset
206 <b>&gt;&gt;&gt;</b> playerpos = 3
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
207 <b>&gt;&gt;&gt;</b> screen.blit(playerimage, (playerpos*10, 0))
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
208 </pre></blockquote>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
209 Hmm, that code should seem very familiar, and hopefully more importantly;
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
210 the code above should make a little sense. Hopefully my illustration of setting
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
211 simple values in a list shows the similarity of setting pixels on the screen
296
ad426a58f19d added blit definition
pygame
parents: 283
diff changeset
212 (with blit). The only part that's really extra work is converting the player position
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
213 into coordinates on the screen. For now we just use a crude "(playerpos*10,
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
214 0)", but we can certainly do better than that. Now let's move the player
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
215 image over a space. This code should have no surprises.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
216
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
217 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
218 <b>&gt;&gt;&gt;</b> screen.blit(background[playerpos], (playerpos*10, 0))
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
219 <b>&gt;&gt;&gt;</b> playerpos = playerpos - 1
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
220 <b>&gt;&gt;&gt;</b> screen.blit(playerimage, (playerpos*10, 0))
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
221 </pre></blockquote>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
222 There you have it. With this code we've shown how to display a simple background
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
223 with a hero's image on it. Then we've properly moved that hero one space
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
224 to the left. So where do we go from here? Well for one the code is still
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
225 a little awkward. First thing we'll want to do is find a cleaner way to represent
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
226 the background and player position. Then perhaps a bit of smoother, real
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
227 animation.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
228 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
229
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
230 <h2>Screen Coordinates</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
231 To position an object on the screen, we need to tell the blit() function
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
232 where to put the image. In pygame we always pass positions as an (X,Y) coordinate.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
233 This represents the number of pixels to the right, and the number of pixels
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
234 down to place the image. The top-left corner of a Surface is coordinate (0,
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
235 0). Moving to the right a little would be (10, 0), and then moving down just
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
236 as much would be (10, 10). When blitting, the position argument represents
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
237 where the topleft corner of the source should be placed on the destination.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
238 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
239 Pygame comes with a convenient container for these coordinates, it is a
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
240 Rect. The Rect basically represents a rectangular area in these coordinates.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
241 It has topleft corner and a size. The Rect comes with a lot of convenient
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
242 methods which help you move and position them. In our next examples we will
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
243 represent the positions of our objects with the Rects.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
244 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
245 Also know that many functions in pygame expect Rect arguments. All of these
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
246 functions can also accept a simple tuple of 4 elements (left, top, width,
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
247 height). You aren't always required to use these Rect objects, but you will
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
248 mainly want to. Also, the blit() function can accept a Rect as it's position
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
249 argument, it simply uses the topleft corner of the Rect as the real position.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
250 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
251 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
252
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
253 <h2>Changing The Background</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
254 In all our previous sections, we've been storing the background as a list
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
255 of different types of ground. That is a good way to create a tile-based game,
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
256 but we want smooth scrolling. To make that a little easier, we're going to
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
257 change the background into a single image that covers the whole screen. This
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
258 way, when we want to "erase" our objects (before redrawing them) we only need
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
259 to blit the section of the erased background onto the screen.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
260 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
261 By passing an optional third Rect argument to blit, we tell blit to only
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
262 use that subsection of the source image. You'll see that in use below as we
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
263 erase the player image.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
264 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
265 Also note, now when we finish drawing to the screen, we call pygame.display.update()
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
266 which will show everything we've drawn onto the screen.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
267 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
268
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
269 <h2>Smooth Movement</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
270 To make something appear to move smoothly, we only want to move it a couple
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
271 pixels at a time. Here is the code to make an object move smoothly across
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
272 the screen. Based on what we already now know, this should look pretty simple.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
273 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
274
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
275 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
276 <b>&gt;&gt;&gt;</b> screen = create_screen()
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
277 <b>&gt;&gt;&gt;</b> player = load_player_image()
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
278 <b>&gt;&gt;&gt;</b> background = load_background_image()
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
279 <b>&gt;&gt;&gt;</b> screen.blit(background, (0, 0)) <i>#draw the background</i>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
280 <b>&gt;&gt;&gt;</b> position = player.get_rect()
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
281 <b>&gt;&gt;&gt;</b> screen.blit(player, position) <i>#draw the player</i>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
282 <b>&gt;&gt;&gt;</b> pygame.display.update() <i>#and show it all</i>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
283 <b>&gt;&gt;&gt;</b> for x in range(100): <i>#animate 100 frames</i>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
284 <b>...</b> screen.blit(background, position, position) <i>#erase</i>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
285 <b>...</b> position = position.move(2, 0) <i>#move player</i>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
286 <b>...</b> screen.blit(player, position) <i>#draw new player</i>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
287 <b>...</b> pygame.display.update() <i>#and show it all</i>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
288 <b>...</b> pygame.time.delay(100) <i>#stop the program for 1/10 second</i>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
289 </pre></blockquote>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
290 There you have it. This is all the code that is needed to smoothly animate
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
291 an object across the screen. We can even use a pretty background character.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
292 Another benefit of doing the background this way, the image for the player
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
293 can have transparancy or cutout sections and it will still draw correctly
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
294 over the background (a free bonus).<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
295 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
296 We also throw in a call to pygame.time.delay() at the end of our loop above.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
297 This slows down our program a little, otherwise it might run so fast you might
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
298 not see it.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
299 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
300
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
301 <h2>So, What Next?</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
302 Well there we have it. Hopefully this article has done everything it promised
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
303 to do. But, at this point the code really isn't ready for the next bestselling
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
304 game. How do we easily have multiple moving objects? What exactly are those
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
305 mysterious functions like load_player_image()? We also need a way to get simple
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
306 user input, and loop for more than 100 frames. We'll take the example we
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
307 have here, and turn it into an object oriented creation that would make momma
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
308 proud.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
309 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
310
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
311 <h2>First, The Mystery Functions</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
312 Full information on these types of functions can be found in other tutorials
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
313 and reference. The pygame.image module has a load() function which will do
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
314 what we want. The lines to load the images should become this.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
315
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
316 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
317 <b>&gt;&gt;&gt;</b> player = pygame.image.load('player.bmp').convert()
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
318 <b>&gt;&gt;&gt;</b> background = pygame.image.load('liquid.bmp').convert()
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
319 </pre></blockquote>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
320 We can see that's pretty simple, the load function just takes a filename
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
321 and returns a new Surface with the loaded image. After loading we make a call
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
322 to the Surface method, convert(). Convert returns us a new Surface of the
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
323 image, but now converted to the same pixel format as our display. Since the
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
324 images will be the same format at the screen, they will blit very quickly.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
325 If we did not convert, the blit() function is slower, since it has to convert
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
326 from one type of pixel to another as it goes.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
327 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
328 You may also have noticed that both the load() and convert() return new
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
329 Surfaces. This means we're really creating two Surfaces on each of these
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
330 lines. In other programming languages, this results in a memory leak (not
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
331 a good thing). Fortunately Python is smart enough to handle this, and pygame
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
332 will properly clean up the Surface we end up not using.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
333 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
334 The other mystery function we saw in the above example was create_screen().
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
335 In pygame it is simple to create a new window for graphics. The code to create
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
336 a 640x480 surface is below. By passing no other arguments, pygame will just
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
337 pick the best colordepth and pixel format for us.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
338 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
339
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
340 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
341 <b>&gt;&gt;&gt;</b> screen = pygame.display.set_mode((640, 480))
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
342 </pre></blockquote>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
343 <br>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
344 <h2>Handling Some Input</h2>
298
c67d404d3baa cleanups for moveit tutorial
pygame
parents: 296
diff changeset
345 We desparately need to change the main loop to look for any user input, (like
c67d404d3baa cleanups for moveit tutorial
pygame
parents: 296
diff changeset
346 when the user closes the window). We need to add "event handling" to our
c67d404d3baa cleanups for moveit tutorial
pygame
parents: 296
diff changeset
347 program. All graphical programs use this Event Based design. The program
c67d404d3baa cleanups for moveit tutorial
pygame
parents: 296
diff changeset
348 gets events like "keyboard pressed" or "mouse moved" from the computer. Then
c67d404d3baa cleanups for moveit tutorial
pygame
parents: 296
diff changeset
349 the program responds to the different events. Here's what the code should
c67d404d3baa cleanups for moveit tutorial
pygame
parents: 296
diff changeset
350 look like. Instead of looping for 100 frames, we'll keep looping until the
c67d404d3baa cleanups for moveit tutorial
pygame
parents: 296
diff changeset
351 user asks us to stop.<br>
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
352 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
353 <b>&gt;&gt;&gt;</b> while 1:
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
354 <b>...</b> for event in pygame.event.get():
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
355 <b>...</b> if event.type in (QUIT, KEYDOWN):
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
356 <b>...</b> sys.exit()
298
c67d404d3baa cleanups for moveit tutorial
pygame
parents: 296
diff changeset
357 <b>...</b> move_and_draw_all_game_objects()
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
358 </pre></blockquote>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
359 What this code simply does is, first loop forever, then check if there are
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
360 any events from the user. We exit the program if the user presses the keyboard
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
361 or the close button on the window. After we've checked all the events we
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
362 move and draw our game objects. (We'll also erase them before they move,
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
363 too)<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
364
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
365 <h2>Moving Multiple Images</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
366 Here's the part where we're really going to change things around. Let's
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
367 say we want 10 different images moving around on the screen. A good way to
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
368 handle this is to use python's classes. We'll create a class that represents
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
369 our game object. This object will have a function to move itself, and then
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
370 we can create as many as we like. The functions to draw and move the object
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
371 need to work in a way where they only move one frame (or one step) at a time.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
372 Here's the python code to create our class.<br>
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
373 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
374 <b>&gt;&gt;&gt;</b> class GameObject:
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
375 <b>...</b> def __init__(self, image, height, speed):
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
376 <b>...</b> self.speed = speed
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
377 <b>...</b> self.image = image
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
378 <b>...</b> self.pos = image.get_rect().move(0, height)
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
379 <b>...</b> def move(self):
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
380 <b>...</b> self.pos = self.pos.move(0, self.speed)
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
381 <b>...</b> if self.pos.right &gt; 600:
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
382 <b>...</b> self.pos.left = 0
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
383 </pre></blockquote>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
384 So we have two functions in our class. The init function constructs our object.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
385 It positions the object and sets its speed. The move method moves the object
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
386 one step. If it's gone too far, it moves the object back to the left.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
387 <br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
388 <h2>Putting It All Together</h2>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
389 Now with our new object class, we can put together the entire game. Here
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
390 is what the main function for our program will look like.<br>
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
391 <blockquote><pre>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
392 <b>&gt;&gt;&gt;</b> screen = pygame.display.set_mode((640, 480))
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
393 <b>&gt;&gt;&gt;</b> player = pygame.image.load('player.bmp').convert()
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
394 <b>&gt;&gt;&gt;</b> background = pygame.image.load('background.bmp').convert()
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
395 <b>&gt;&gt;&gt;</b> screen.blit(background, (0, 0))
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
396 <b>&gt;&gt;&gt;</b> objects = []
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
397 <b>&gt;&gt;&gt;</b> for x in range(10): <i>#create 10 objects</i>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
398 <b>...</b> o = GameObject(player, x*40, x)
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
399 <b>...</b> objects.append(o)
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
400 <b>&gt;&gt;&gt;</b> while 1:
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
401 <b>...</b> for event in pygame.event.get():
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
402 <b>...</b> if event.type in (QUIT, KEYDOWN):
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
403 <b>...</b> sys.exit()
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
404 <b>...</b> for o in objects:
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
405 <b>...</b> screen.blit(background, o.pos, o.pos)
371
866202a34ff0 indent fix for moveit tut
pygame
parents: 298
diff changeset
406 <b>...</b> for o in objects:
866202a34ff0 indent fix for moveit tut
pygame
parents: 298
diff changeset
407 <b>...</b> o.move()
866202a34ff0 indent fix for moveit tut
pygame
parents: 298
diff changeset
408 <b>...</b> screen.draw(o.image, o.pos)
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
409 <b>...</b> pygame.display.update()
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
410 <b>...</b> pygame.time.delay(100)
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
411 </pre></blockquote>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
412 And there it is. This is the code we need to animate 10 objects on the screen.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
413 The only point that might need explaining is the two loops we use to clear
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
414 all the objects and draw all the objects. In order to do things properly,
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
415 we need to erase all the objects before drawing any of them. In our sample
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
416 here it may not matter, but when objects are overlapping, using two loops
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
417 like this becomes important.<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
418 <br>
3161
9e633ef40a29 Fix spelling error in MoveIt tutorial. Thanks Samuel Villegas.
Rene Dudfield <renesd@gmail.com>
parents: 408
diff changeset
419 <h2>You Are On Your Own From Here</h2>
279
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
420 <p>So what would be next on your road to learning? Well first playing around
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
421 with this example a bit. The full running version of this example is available
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
422 in the pygame examples directory. It is the example named "moveit.py". Take
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
423 a look at the code and play with it, run it, learn it.</p>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
424 <p>Things you may want to work on is maybe having more than one type of object.
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
425 Finding a way to cleanly "delete" objects when you don't want to show them
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
426 anymore. Also updating the display.update() call to pass a list of the areas
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
427 onscreen that have changed.</p>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
428 <p>There are also other tutorials and examples in pygame that cover these
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
429 issues. So when you're ready to keep learning, keep on reading. :-)</p>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
430 <p>Lastly, you can feel free to come to the pygame mailing list or chatroom
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
431 with any questions on this stuff. There's always folks on hand who can help
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
432 you out with this sort of business.</p>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
433 <p>Lastly, have fun, that's what games are for!<br>
16c1fabe734f new moveit example and tutorial
pygame
parents:
diff changeset
434 </p>
283
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
435 </body>
7fcc16e339bd html formatting
pygame
parents: 279
diff changeset
436 </html>