Presentation is loading. Please wait.

Presentation is loading. Please wait.

Let's Learn Python and Pygame

Similar presentations


Presentation on theme: "Let's Learn Python and Pygame"— Presentation transcript:

1 Let's Learn Python and Pygame
Aj. Andrew Davison, CoE, PSU Hat Yai Campus 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

2 1. What is a Module? A module is a program that adds extra features to Python e.g. for 3D 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

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

4 2. Using the random Module

5 Module Documentation click on "modules"

6

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

8 3. The math Module import math or from math import * Function name
Description abs(value) absolute value ceil(value) rounds up cos(value) cosine, in radians degrees(value) convert radians to degrees floor(value) rounds down log(value, base) logarithm in any base log10(value) logarithm, base 10 max(value1, value2, ...) larger of two (or more) values min(value1, value2, ...) 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 Constant Description e pi import math or from math import *

9

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

11 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

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

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

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

15 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 left(45) forward

16 Tell the turtle to draw a square
Square.py

17 Draw a House House.py

18 A Blue Equilateral Triangle
Triangle.py

19 A Faster Turtle Square.py

20

21 Filling the Shape with Color
Square.py

22 Drawing (Bits of) Circles
Circles.py

23 Writing Text HelloWorld.py

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

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

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

27 lots more off the bottom of this picture

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

29 Info on easygui The easygui webpage: An easygui tutorial:
you can also download easygui from there, but pip is easier An easygui tutorial:

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

31 Use pip to Search PyPI

32 Use pip to Install easygui

33 More pip commands pip help pip list
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

34 9. Use easygui in Python

35 Dialog Box with Buttons
IceButtons.py

36 Text Input IceEnter.py

37 Response


Download ppt "Let's Learn Python and Pygame"

Similar presentations


Ads by Google