-
Notifications
You must be signed in to change notification settings - Fork 11
/
cap1.t2t
403 lines (316 loc) · 20.2 KB
/
cap1.t2t
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
Capítulo 1
%!encoding: utf-8
==Tópicos cobertos neste capítulo==
- Baixando e instalando o interpretador Python.
- Usando o console iterativo para rodar instruções.
- Como usar este livro.
% Chapter 1
% Hello! This is a book that will teach you how to program by showing you how
% to create computer games. Once you learn how the games in this book work,
% you'll be able to create your own games. All you'll need is a computer, some
% software called the Python Interpreter, and this book. The software you'll
% need is free and you can download it from the Internet.
Oi! Este livro ira lhe ensinar como programar mostrando como criar jogos de
computador. Uma vez aprendido como os jogos neste livro funcionam, você será
capaz de criar os seus proprios games. Tudo o que você precisa é um
computador, um programa chamado interpretador Python, e este livro. O programa
que você ira precisar é livre e você pode baixa-lo da internet.
% When I was a kid, I found a book like this that taught me how to write my
% first programs and games. It was fun and easy. Now as an adult, I still have
% fun programming computers, and I get paid for it. But even if you don't
% become a computer programmer when you grow up, programming is a useful and
% fun skill to have.
Quando eu era uma criança, eu achei um livro como este que me ensinou como
escrever meus primeiros programas e jogos. Aquilo foi divertido e fácil.
Agora, como um adulto, eu ainda me divirto programando computadores, e sou
pago para fazer isso. Mas mesmo que você não venha a se tornar um programador
de computador quando crescer, a capacidade de programar um computador é uma
habilidade muito util para se ter.
% Computers are very useful machines. The good news is that learning to
% program a computer is easy. If you can read this book, you can program a
% computer. A computer <span class='term'>program</span> is just a bunch of
% instructions run by a computer, just like a storybook is just a whole bunch
% of sentences read by the reader.
Computadores são maquinas muito uteis. A boa notiica é que aprender como
programar um computador é facil. Se você ler este livro, você conseguira
programar um computador. Um programa de computador é apenas um punhado de
instruções rodadas pelo computador, da mesma forma que um livro de histórias é
apenas um punhado de sentences lidas pelo leitor.
% These instructions are like the turn-by-turn instructions you might get for
% walking to a friend's house. (Turn left at the light, walk two blocks, keep
% walking until you find the first blue house on the right.) The computer
% follows each instruction that you give it in the order that you give it.
% Video games are themselves nothing but computer programs. (And very fun
% computer programs!)
Estas instruções são como as instruções passa-a-passo que você pode ter para
ir até a cada de um amigo. (Virar a esquerda no semaforo, andar dois
quarteirões, continuar andando até você encontrar a primeira casa azul a
direita.) O computador segue estas instruções que você fornece na ordem em que
você fornece. Video games são apenas programas de computador. (E programas
muito divertidos!)
% In this book, any words you need to know will look like <span class='term'>
% this</span>. For example, the word "program" is defined in the previous
% paragraph.
% TODO: Neste livro, qualquer palavra que você precise saber vai
% In order to tell a computer what you want it to do, you write a program in a
% language that the computer understands. The programming language this book
% teaches is named Python. There are many different programming languages
% including BASIC, Java, Pascal, Haskell, and C++ (pronounced, "c plus plus").
% When I was a kid most people learned to program in BASIC as their first
% language. But new programming languages have been invented since then,
% including Python. Python is even easier to learn than BASIC and it's a
% serious programming language used by professional computer programmers.
% Many adults use Python in their work (and when programming just for fun).
Quando eu era uma criança, a maior parte das pessoas aprendiam a BASIC como a
sua primeira linguagem. Mas novas linguagens de programação foram inventadas
desde então, incluindo Python. Python é mais facil de aprender do que BASIC e
é uma linguagem de programação séria usada por programadores de computador
profissionais. Muitos adultos usam Python em seu trabalho (e quando está
programando apenas por diversão).
% The first few games we'll create together in this book will probably seem
% simple compared to the games you've played on the Xbox, Playstation, or Wii.
% They don't have fancy graphics or music but that's because they're meant to
% teach you the basics. They're purposely simple so that we can focus on
% learning to program. Games don't have to be complicated to be fun. Hangman,
% Tic Tac Toe, and making secret codes are simple to program but are also fun.
% TODO: melhor tradução para fancy
Os primeiros jogos que nós vamos criar juntos neste livro vão provavelmente
parecer simples se comparados aos que você joga no Xbox, Playstation ou Wii.
Eles não tem graficos engracadinhos ou musica mas isto se deve por que eles
tem a intenção de ensinar o basico. Eles são propositalmente simples para que
possamos focar em aprender a programar. Jogos não precisam ser complicados
para serem divertidos. Força, Jogo-da-Velha, e fazer codigos secretos são
simples de programar e igualmente divertidos.
% We'll also learn how to make the computer solve some math problems in the
% Python shell. (Don't worry if you don't know a lot of mathematics. If you
% know how to add and multiply, you know enough math to do programming.
% Programming is more about problem solving and making plans than it is about
% solving math problems.)
%TODO: muita matemática no paragrafo. Melhorar com sinonimos. :D
Nós tambem vamos aprender como cirar programas para resolver alguns problemas
matematicos no console do Python. (Não se preocupe se você não sabe muito de
matemática. Se você sabe a somar e a multiplicar, você sabe o suficiente de
matemática para programar. Programar é mais sobre resolver o problema e fazer
planos do que resolver problemas matemáticos.)
% Downloading and Installing Python
=Baixando e Instalando o Python=
% Before we can begin programming you'll need to install software called the
% Python interpreter. (You may need to ask an adult for help here.) The <span
% class='term'>interpreter</span> is a program that understands the
% instructions that you'll write in the Python language. Without the
% interpreter, your computer won't understand these instructions and your
% programs won't work. (We'll just refer to "the Python interpreter" as
% "Python" from now on.)
Antes que nos possamos começar a programar, você vai precisar instalar um
programa chamado interpretador Python. (Talvez você precise da ajuda de um
adulto aqui.) O **interpretador** é um programa que entende as instruções que
você ira escrever na linguagem Python. Sem o interpretador, seu computador não
ira entender estas instruções e seus programas não iram funcionar. (Nos vamos
nos referir ao "interpretador Python" como "Python" a partir de agora.)
% Because we'll be writing our games in the Python language we need to
% download Python first, from the official website of the Python programming
% language, <a href='http://www.python.org'>http://www.python.org</a>
%TODO como funcionam os links no txt2tags?
% I'm going to give you instructions for installing Python on Microsoft
% Windows, not because that's my favorite operating system but because chances
% are that's the operating system that your computer is running. You might
% want the help of someone else to download and install the Python software.
Eu vou dar instruções de como instalar o Python no Microsoft Windows, não por
que é mu sistema operacional favorito, mas por que é o provavel sistema
operacional que o seu computador está utililizando. Você pode precisar da
ajuda de alguem para baixar e instalar o Python no seu computador.
% When you get to python.org, you should see a list of links on the left (
% About, News, Documentation, Download, and so on.) Click on the <span class='
% menuname'>Download</span> link to go to the download page, then look for the
% file called <b>Python 3.1 Windows Installer</b> (Windows binary -- does not
% include source) and click on its link to download Python for Windows.
% TODO: ver a questão do menuname;
Quando você acessa o python.org, você eve ver uma lista de links na esquerda (
Sobre, Noticias, Documentação, Download e por aê vai.) Clique no link Download
par ir a página de download. então procure por um arquivo chamado **Python 3.1
Windows Installer** (Um binario Windows -- não inclue os fontes) e então
clique neste link para baixar o Python para windows.
% <p class='centeredImageP'><img src='images/1-1.png' alt='' class='
% centeredImage' /><br />
% Figure 1-1: Click the Windows installer link to download Python for Windows
% from <a href='http://www.python.org'>http://www.python.org</a></p>
% TODO: ver a questão das imagens.
% Double-click on the <span class='filename'>python-3.1.msi</span> file that
% you've just downloaded to start the Python installer. (If it doesn't start,
% try right-clicking the file and choosing Install.) Once the installer starts
% up, click the <span class='menuname'>Next</span> button and just accept the
% choices in the installer as you go (no need to make any changes). When the
% install is finished, click <span class='menuname'>Finish</span>.
% TODO: ver como fazer a tag filename
Efetue um duplo clique no arquivo python-3.1.msi que você acabou de baixar. (
Se a instalçao não iniciar, tente clicar com o botão direito do mouse e
escolher a opção "Instalar"). Uma vez que o instalador for iniciado, clique no
botão "Next" e apenas aceitas as opções
% <b>Important Note!</b> Be sure to install Python 3, and not Python 2. The
% programs in this book use Python 3, and you'll get errors if you try to run
% them with Python 2.
** Importante !** Tenha certeza de ter instalado o Python 3 e não o Python 2.
Os programas apresentados neste livro usam Python 3, você ira obter alguns
erros se tentar rodar eles com Python 2.
% The installation for Mac OS is similar. Instead of downloading the .msi file
% from the Python website, download the .dmg Mac Installer Disk Image file
% instead. The link to this file will look something like "Mac Installer disk
% image (3.1.1)" on the "Download Python Software" web page.
A instalaçaão para Mac OS é similar. Ao invez de baixar o arquivo
python-3.1.msi do site, baixe o arquivo python-3-1-1.dmg. O link para este
arquivo vai parecer algo como "Mac Instaler disk image (3.1.1)" na pagina de
download ("Dowload Python Software").
% If your operating system is Ubuntu, you can install Python by opening a
% terminal window (click on Applications > Accessories > Terminal) and
% entering <span class='m'>sudo apt-get install python3</span> then pressing
% Enter. You will need to enter the root password to install Python, so ask
% the person who owns the computer to type in this password.
%TODO:
Se o seu sistema operacional é um Linux Ubuntu, você pode instalar o Python
% There may be a newer version of Python available than 3.1. If so, then just
% download the latesta version. The game programs in this book will work just
% the same. If you have any problems, you can always Google for "installing
% Python on <your operating system's name>". Python is a very popular
% language, so you should have no difficulty finding help.
%TODO: será que posso adicionar a python-brasil como uma fonte de ajuda?
Pode haver disponível uma versão mais nova do que a versão 3.1 do Python. Se
isso ocorrer, apenas baixe a versão mais atual. Os programas de jogos neste
livro irão funcionar igualmente. Se você tiver qualquer problema, você pode
sempre procurar no Google por "instalar python 3 no <seu sistema operacional>
". Python é uma linguagem muito popular, logo não deve ser dificil encontrar
ajuda.
% TODO: comment -- A video tutorial of how to install Python is available from % this book's website at <a href='http://inventwithpython.com/videos/'>
% http://inventwithpython.com/videos/</a>.</p>
% Starting Python
=Começando com o Python=
% If your operating system is Windows XP, you should be able to run Python by % choosing <span class='menuname'>Start</span>, <span class='menuname'>Programs
% </span>, <span class='menuname'>Python 3.1</span>, <span class='menuname'>
% IDLE (Python GUI)</span>. When it's running it should looking something like
% Figure 1-2. (But different operating systems will look slightly different.)
% <p class='centeredImageP'><img src='images/1-2.png' alt='' class='
% centeredImage' /><br />
% Figure 1-2: The IDLE program's interactive shell on Windows.</p>
% IDLE stands for <b>I</b>nteractive <b>D</b>eve<b>L</b>opment <b>E</b>
% nvironment. The development environment is software that makes it easy to
% write Python programs. We will be using IDLE to type in our programs and run
% them.
% The window that appears when you first run IDLE is called the interactive
% shell. A shell is a program that lets you type instructions into the
% computer. The Python shell lets you type Python instructions, and the shell
% sends these instructions to software called the Python interpreter to
% perform. We can type Python instructions into the shell and, because the
% shell is interactive, the computer will read our instructions and respond in
% some way. (Ideally in a way that we expect but that will depend on whether
% we write the correct instructions.)
% How to Use This Book<
% There are a few things you should understand about this book before you get
% started. "Invent with Python" is different from other programming books
% because it focuses on the complete source code for different games. Instead
% of teaching you programming concepts and leaving it up to you to figure out
% how to make fun games with those concepts, this book shows you fun games and
% then explains how they are put together.
% The Featured Programs
% Most chapters begin with a sample run of the featured program. This sample
% run shows you what the program's output looks like, with what the user types
% in shown as <b>bold</b> print. This will give you an idea of what the
% complete game will look like when you have entered the code and run it.
% Some chapters also show the complete source code of the game, but remember:
% you don't have to enter every line of code right now. Instead, you can read
% the chapter first to understand what each line of code does and then try
% entering it later.
% You can also download the source code file from this book's website. Go to
% the URL <a href='http://inventwithpython.com/source'>
% http://inventwithpython.com/source</a> and follow the instructions to
% download the source code file.
% Line Numbers and Spaces
% When entering the source code yourself, do <b>not</b> type the line numbers
% that appear at the beginning of each line. For example, if you see this in
% the book:
% <div class='sourceblurb'>
% <ol start='9'>
% <li>number = random.randint(1, 20)</li>
% </ol>
% </div>
% You do not need to type the "9." on the left side, or the space that
% immediately follows it. Just type it like this:<
% <div class='sourceblurb'>
% number = random.randint(1, 20)
% </div>
% Those numbers are only used so that this book can refer to specific lines in
% the code. They are not a part of the actual program.
% Aside from the line numbers, be sure to enter the code exactly as it
% appears. Notice that some of the lines don't begin at the leftmost edge of
% the page, but are indented by four or eight spaces. Be sure to put in the
% correct number of spaces at the start of each line. (Since each character in
% IDLE is the same width, you can count the number of spaces by counting the
% number of characters above or below the line you're looking at.)
% For example, you can see that the second line is indented by four spaces
% because the four characters ("<span class='m'>whil</span>") on the line
% above are over the indented space. The third line is indented by another
% four spaces (the four characters, "<span class='m'>if n</span>" are above
% the third line's indented space):
% <div class='sourceblurb'>
% while guesses < 10:<br />
% if number == 42:<br />
% print('Hello')<br />
% </div>
% Text Wrapping in This Book
% Some lines of code are too long to fit on one line on the page, and the text
% of the code will wrap around to the next line. When you type these lines
% into the file editor, enter the code all on one line without pressing Enter.
% You can tell when a new line starts by looking at the line numbers on the
% left side of the code. For example, the code below has only two lines of
% code, even though the first line wraps around:
% <div class='sourceblurb'>
% <ol>
% <li>print('This is the first line! xxxxxxxxxxxxxxx<br />
% xxxxxxxxxxxx')</li>
% <li>print('This is the second line! ')</li>
% </ol>
% </div>
% <h3 id="TracingtheProgramOnline">Tracing the Program Online</h3>
% <p>You can visit <a href='http://inventwithpython.com/traces'>
% http://inventwithpython.com/traces</a> to see a trace through each of the
% programs in this book. Tracing a program means to step through the code one
% line at a time, in the same way that a computer would execute it. The traces
% web page has notes and helpful reminders at each step of the trace to
% explain what the program is doing, so it can help you better understand why
% these programs work the way they do.</p>
% <!--<p>For a demonstration video of how to use the online tracing, go to this web video: <a href='TODO'>TODO</a></p>-->
% <h3 id="CheckingYourCodeOnline">Checking Your Code Online</h3>
% <p>Some of the games in this book are a little long. Although it is very
% helpful to learn Python by typing out the source code for these games, you
% may accidentally make typos that cause your game programs to crash. It may
% not be obvious where the typo is.</p>
% <p>You can copy and paste the text of your source code to the online diff
% tool on the book's website. The diff tool will show any differences between
% the source code in the book and the source code you've typed. This is an
% easy way of finding any typos in your program.</p>
% <p>Copying and pasting text is a very useful computer skill, especially for
% computer programming. There is a video tutorial on copying and pasting at
% this book's website at <a href='http://inventwithpython.com/videos/'>
% http://inventwithpython.com/videos/</a>.
% <p>The online diff tool is at this web page: <a href='
% http://inventwithpython.com/diff'>http://inventwithpython.com/diff</a>. A
% video tutorial of how to use the diff tool is available from this book's
% website at <a href='http://inventwithpython.com/videos/'>
% http://inventwithpython.com/videos/</a>.</p>
% <!--<p>If you would like to see a video demonstration of how to use this
% book's website's online diff tool, view this YouTube video: <a href='TODO'>
% TODO</a></p>-->
% <!--<p>If you do not know how to copy and paste text, you can learn by
% watching this YouTube demonstration video: <a href='TODO'>TODO</a></p>-->
% <h2 id="Summary">Summary</h2>
% <p>This chapter has helped you get started with the Python software by
% showing you the python.org website where you can download it for free. After
% installing and starting the Python IDLE software, we will be ready to learn
% programming starting in the next chapter.</p>
% <p>This book's website at <a href='http://inventwithpython.com'>
% http://inventwithpython.com</a> has more information on each of the
% chapters, including an online tracing website that can help you understand
% what exactly each line of the programs do.</p>
% <table border='0' width='100%'><tr><td></td><td align='right'><a href='
% chapter2.html'>Go to Chapter 2 - The Interactive Shell</a></td></tr></table>
% <div style='height: 310px;'><a href='http://www.amazon.com/Invent-Your-
% Computer-Games-Python/dp/0982106017/'><img src='images/buyad.png' align='
% right'></a></div>