A maior rede de estudos do Brasil

Grátis
433 pág.
Apostila PyOpenGL

Pré-visualização | Página 5 de 50

latest Python OpenGL modules here. 
 
5. Visit http://nehe.gamedev.net for an interesting website containing information 
and tutorials on programming OpenGL using various languages, including 
Python. The orientation is mostly C/C++, but the Python OpenGL syntax 
(language grammar) is nearly the same. 
 
6. Visit http://vpython.org for a very nice module for Python. VPython was designed 
for math and physics instruction and can be used to program the 3D Geowall. 
Highly recommended! 
 
7. Visit www.pygame.org to see how Python may be used to program games. 
 
8. Finally, use Google or your favorite search engine and look for “OpenGL 
RedBook”. The RedBook is a standard reference for OpenGL and older versions 
of this fine text can be found online. You can usually download a copy and have 
it available on your computer desktop for easy referral. I have not provided a link 
for the RedBook because the links have a tendency to change over time. If you 
can also find a copy of the OpenGL BlueBook, so much the better. The “Blue 
Book” is a reference for specific OpenGL commands and it may be possible to 
find an older version online. 
 
Note: Throughout this text you will be asked to type in programs from the chapter 
sections as well as create some programs of your own. A few comments are in order. 
First, all program code listings in this text will be written using the Courier New 
(bold) font. Second, some program lines may be placed in quotes such as “for t 
in arange(0.0, 3.14, 0.01)” Do NOT type the quotes in the program unless 
specifically requested to do so. Quotes are usually used only in print statements. 
Finally, I suggest that you type the programs in each section of text exactly as written as 
a starting point. Once the program is running properly and it is saved for future 
reference, then you should feel free to experiment with the code according to the 
demands of the exercises at the end of each section or according to your personal 
tastes and creativity. Make certain that you save any modifications to the original code 
under a NEW name so that you can preserve the original for use in future exercises. 
 
Enjoy! 
Chapter 3 Your First Python Program 
 
Section 3.1 Super-3 Numbers 
 
 We are going to start programming by using a numerical example. This is the 
only program in this text that will not generate graphics, so please don't panic. I’m going 
to use this program to illustrate some basic programming concepts and hopefully to 
show you how powerful computers can be. 
 
 Super-3 numbers are numbers that when first cubed and then multiplied by 3, 
result in a value that contains three “3’s” in succession.1 For example, the first Super-3 
number is 261, because when we multiply the cube of 261 by 3, we get 53338743. How 
many other Super-3 numbers are there less than 1000? How about less than 10000? 
Do you want to try to find these numbers by hand? I didn’t think so. Let’s see if Python 
can help. 
 
 First, start DrPython (or your programming editor). You should see a screen 
similar to Figure 3.1 below. 
 
 
 Figure 3.1 
 
 
1 See Pickover, C. A. (1995) "Keys to Infinity", New York: Wiley p. 7 
 14
 If a blinking cursor isn't visible in the white workspace area in next to the "1", 
simply click in the workspace area and the cursor should then appear. 
 
 Now type the following program exactly as it is listed below.2 Make certain that 
you indent each line as shown in the program listing! 
 
# Super-3 Numbers 
 
import string 
 
i = input(“Please enter the upper bound: “) 
for n in range(i): 
 x = 3*n**3 
 if string.find(str(x), “333”) <> -1: 
 print n, x 
 
# End of program 
 
Notice that DrPython automatically numbers lines for us. This feature is very 
handy when we are trying to trace program errors. 
 
Your screen should look similar to Figure 3.2: 
 
 
 Figure 3.2 
 
 
2 Program listings will use the Courier New font throughout the text. 
 15
 Once you have typed in this short program and have checked it twice3 for 
accuracy, save it to your program folder by clicking on “File” and “Save As”. In the 
dialog box that appears, type “super3.py”4 for the name of the program (do not use 
quotes) as shown in Figure 3.3. Click “Ok” when you have finished. Note that your 
directory and the saved programs that appear will be different from mine. 
 
 
 Figure 3.3 
 
 When the program has been saved, let’s run it and see what happens! Click on 
the white “Play” triangle in the menu area. You should see something similar to Figure 
3.4 if your program contains no errors. Notice that there is a new area below your 
program listing. This is the console area and all error messages and results of program 
calculations will be found here. If everything is OK and you have a blinking cursor in the 
console, you should be able to type in a value, press enter, and the program will run. 
Type in 1000 and press enter. You should see the Super-3 numbers less than 1000 
displayed in the console as shown in Figure 3.5. 
 
 
3 OK, three times. I'm not kidding. 
4 The ".py" ending (suffix) tells the Python interpreter that this program is a Python program and 
should be treated as such. Failure to add the ".py" suffix will probably cause either unpredictable 
results or the program will not run at all. 
 16
 
 Figure 3.4 
 
 
 Figure 3.5 
 17
 Try running the program again. This time enter 10000 or 100000. What do you 
see? If you want to stop a calculation at any time, press the white square “Stop” button 
to the right of the green Python icon.5 
 
If an error occurs, carefully read the message(s) displayed in the console. The 
message or messages will usually give you some indication of the problem.6 Look at 
Figure 3.6 for an example of an error message. Notice that Python is telling us that in 
line 6, the name “rnge” is not defined. Try it! Change the spelling of range in line 6 to 
rnge, save, and then run the program again. What happens? If you change the 
spelling back to range, the program should work properly once more. Many program 
errors are simple spelling errors and are easily remedied. We call such spelling errors 
syntax errors. Computer languages such as Python require a very precise usage of 
commands. Make certain that you type all commands correctly! However, don't feel too 
badly when errors occur. Even the best programmers make mistakes. Just be prepared 
to hunt down programming errors (bugs) and fix them! 
 
 
 Figure 3.6 
 
5 The white (or red in newer versions) stop button can be used to halt a program while it is 
running. If DrPython doesn’t behave as it should or a program fails to run, check to see if this 
button is “available” (white). If so, press it to stop whatever is in progress and hopefully return 
everything back to normal. 
6 NOTE**** Sometimes an error message will be generated and the line that is "flagged" appears 
to be OK. If this is the case, look at the first unremarked line ABOVE the indicated line and see if 
there is an error with that line. You may have forgotten to close a parenthesis? Also, indentation 
is extremely important in Python as we will soon see. If you do NOT indent properly, your 
program will either not run at all or will run in unexpected ways. 
 18
Let’s go over our program word for word and see if we can understand it. I’ll type 
the program lines in bold and in Courier New font for emphasis. Here's the first line: 
 
# Super-3 Numbers 
 
 Any line beginning with a “#” sign is a remark statement. Remarks are ignored 
by Python, but

Crie agora seu perfil grátis para visualizar sem restrições.