Lets Learn Python and Pygame Aj Andrew Davison

  • Slides: 37
Download presentation
Let's Learn Python and Pygame Aj. Andrew Davison, Co. E, PSU Hat Yai Campus

Let's Learn Python and Pygame Aj. Andrew Davison, Co. E, PSU Hat Yai Campus E-mail: ad@fivedots. coe. psu. ac. th 3. Modules Using modules to give Python even more POWER Look at built-in modules: random, math, turtle, winsound Start turtle graphics. Look at installing easygui using pip 1

1. What is a Module? §A module is a program that adds extra features

1. What is a Module? §A module is a program that adds extra features to Python § e. g. for 3 D graphics, for writing games §There are lots of modules that come with Python § called built-in or standard modules §It is easy to add more § called third-party modules 2

Python's Design A small language … … plus lots of modules turtle math winsound

Python's Design A small language … … plus lots of modules turtle math winsound 3

2. Using the random Module 4

2. Using the random Module 4

click on "modules" Module Documentation 5

click on "modules" Module Documentation 5

6

6

Information on random §Look under 'r' in the Python Module Index 7

Information on random §Look under 'r' in the Python Module Index 7

3. The math Module Function name abs(value) Description absolute value Constant e ceil(value) rounds

3. The math Module Function name abs(value) Description absolute value Constant e ceil(value) rounds up pi 3. 1415926. . . cos(value) cosine, in radians degrees(value) convert radians to degrees floor(value) rounds down log(value, base) logarithm in any base log 10(value) logarithm, base 10 max(value 1, value 2, . . . ) larger of two (or more) values min(value 1, value 2, . . . ) smaller of two (or more) values radians(value) convert degrees to radians round(value) nearest whole number sin(value) sine, in radians sqrt(value) square root tan(value) tangent 2. 7182818. . . import math or from math import * 8

9

9

Information on math §Look under 'm' in the Python Module Index 10

Information on math §Look under 'm' in the Python Module Index 10

4. The turtle Module § The turtle moves about in a drawing window with

4. The turtle Module § The turtle moves about in a drawing window with a pen for drawing lines. § The turtle can be told to turn a number of degrees, move a distance, move to a new position, and change its pen's color and line width § If the turtle’s pen is down, it draws a line; otherwise, it moves without drawing

The Turtle Drawing Window § The turtle is an icon § Initial position: (0,

The Turtle Drawing Window § The turtle is an icon § Initial position: (0, 0) § Initial direction: East (0°) § Color: black § Line width: 1 pixel § Pen: down (ready to draw) 90° x-axis (0, 0) 180° 0° y-axis 270°

Create a Turtle >>> from turtle import Turtle >>> sleepy = Turtle() Tell Python

Create a Turtle >>> from turtle import Turtle >>> sleepy = Turtle() Tell Python to add the Turtle command to its built-in commands

Move a Distance >>> from turtle import Turtle >>> sleepy = Turtle() >>> sleepy.

Move a Distance >>> from turtle import Turtle >>> sleepy = Turtle() >>> sleepy. forward(50) Move 50 pixels in the current direction. There's also a backward() command.

Turning Left or Right §A turtle can turn left or right by a number

Turning Left or Right §A turtle can turn left or right by a number of degrees § e. g. sleepy. left(45) §This angle is offset from the turtle's current forward direction. left(45) forward 15

Tell the turtle to draw a square Square. py 16

Tell the turtle to draw a square Square. py 16

Draw a House. py 17

Draw a House. py 17

A Blue Equilateral Triangle. py 18

A Blue Equilateral Triangle. py 18

A Faster Turtle Square. py 19

A Faster Turtle Square. py 19

20

20

Filling the Shape with Color Square. py 21

Filling the Shape with Color Square. py 21

Drawing (Bits of) Circles. py 22

Drawing (Bits of) Circles. py 22

Writing Text Hello. World. py 23

Writing Text Hello. World. py 23

goto(x, y) Also used in setpos(x, y) The usual turtle window has -200 ≤

goto(x, y) Also used in setpos(x, y) The usual turtle window has -200 ≤ x ≤ 200 and -150 ≤ y ≤ 150 24

Information on turtle §Look under 't' in the Python Module Index 25

Information on turtle §Look under 't' in the Python Module Index 25

6. Getting More Modules https: //pypi. python. org/pypi I'll search for "GUI" 26

6. Getting More Modules https: //pypi. python. org/pypi I'll search for "GUI" 26

lots more off the bottom of this picture 27

lots more off the bottom of this picture 27

the "easygui" looks good; click on it for lots more details 28

the "easygui" looks good; click on it for lots more details 28

Info on easygui §The easygui webpage: § https: //github. com/robertlugg/easygui § you can also

Info on easygui §The easygui webpage: § https: //github. com/robertlugg/easygui § you can also download easygui from there, but pip is easier §An easygui tutorial: § http: //easygui. sourceforge. net/tutorial. html 29

7. Installing a Py. PI Module §Use the pip program that comes as part

7. Installing a Py. PI Module §Use the pip program that comes as part of the Python download. §Use it from the Command window. 30

Use pip to Search Py. PI 31

Use pip to Search Py. PI 31

Use pip to Install easygui 32

Use pip to Install easygui 32

More pip commands §pip help § gives help on using pip §pip list §

More pip commands §pip help § gives help on using pip §pip list § lists the extra (thitd-party) modules that you have added to Python §pip install <downloaded file> § installs a module that you've downloaded yourself 33

9. Use easygui in Python 34

9. Use easygui in Python 34

Dialog Box with Buttons Ice. Buttons. py 35

Dialog Box with Buttons Ice. Buttons. py 35

Text Input Ice. Enter. py 36

Text Input Ice. Enter. py 36

Response 37

Response 37