CHAPTER 3 (P.49-53) Importing modules + pygame intro.
-
Upload
micheal-spore -
Category
Documents
-
view
253 -
download
0
Transcript of CHAPTER 3 (P.49-53) Importing modules + pygame intro.
![Page 1: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/1.jpg)
CHAPTER 3 (P.49-53)
Importing modules + pygame intro
![Page 2: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/2.jpg)
modules
core language vs modules. millions of modules.
Networking Graphics computer vision …
To use these, you need to import them (usually at the top of your script)
import mathimport pygame
![Page 3: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/3.jpg)
modules, cont.
Sources of modules:1. shipped with python distribution2. downloaded modules3. Modules that you write yourself.
Accessing module contents:module_name.item_in_the_module.
• Example
![Page 4: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/4.jpg)
random Module
random.randint(low_num, high_num)random.uniform(low_num, high_num)random.gauss(center_num, std_dev)
![Page 5: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/5.jpg)
time module
time.sleep(num_seconds)time.time()
Example use
![Page 6: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/6.jpg)
Reference: www.pygame.org
Part I
![Page 7: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/7.jpg)
What is it?
A multimedia python library Window Management Graphics
geometric shapes bitmaps (sprites)
Input Mouse Keyboard Gamepad / Joystick
Sound SoundFX Music
![Page 8: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/8.jpg)
How to get it (at home)
www.pygame.orgMake sure you have 32-bit python installed.Download the matching pygame version
pygame-1.9.2a0.win32-py3.2.msiTry this at the prompt:
>>> import pygame If no errors, you’re all set!
![Page 9: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/9.jpg)
Overview
Pygame (sub-)module organization Connection to documentation
Pygame <= SDL <= [DirectX / OpenGL / …]
![Page 10: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/10.jpg)
Surfaces
A 2d array of (RGB) pixels An image (sprite) Rendered (at run-time) text A mini-map A HUD An interface to the window’s contents. In pygame, Surfaces are objects.
Look up the methods in the “Surfaces” section of pygame docs.
Double Buffering Monitor refreshes Life without D.B. D.B. in pygame
![Page 11: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/11.jpg)
Examples
[Basic window][Drawing]
[Look at the pygame docs]
![Page 12: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/12.jpg)
Sprites / Images
Images (in pygame) are Surface objectsFile formats loadable by pygame:
JPG, BMP, PCX, TGA, TIF, PBM, PGM, XPM GIF, PNG # Support transparency
Loading:s = pygame.image.load("img.jpg")s = pygame.image.load("sprites\img.jpg")s = pygame.image.load("..\..\media\img.jpg")s = pygame.image.load("c:\game\img.jpg")s = pygame.image.load("/usr/bin/img.jpg")import osprint(os.sep) # '\\' on Windows, '/' on *nix
![Page 13: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/13.jpg)
Sprites / Images, cont.
Converting: Color / bit encoding… Why it's good to convert… Command:s = pygame.image.load("img.jpg")s = s.convert()t = pygame.image.load("img2.png").convert_alpha()
![Page 14: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/14.jpg)
BlittingCopies contents of one (part of) a surface to
another.
screen (600x600)
ant_img (150x180)
screen.blit(ant_img, (20,100))
dest_surf.blit(src_surf, pos)
screen.blit(ant_img, (100,0), (75,0,50,100))
dest_surf.blit(src_surf, pos, src_rect)
(75,0)
50
100
(20,100)
(100,0)
![Page 15: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/15.jpg)
Transforming surfaces
pygame.transform.scale(orig_surf, (new_width, new_height))pygame.transform.rotate(orig_surf, degrees)
Returns a new surface Must be blitted to screen to be seen
Original surface is unchanged![Example]Rotating "in-place"
![Page 16: CHAPTER 3 (P.49-53) Importing modules + pygame intro.](https://reader036.fdocuments.net/reader036/viewer/2022062318/551c582c550346b1458b50d0/html5/thumbnails/16.jpg)
Fonts
Rendering text in pygame As opposed to the print statement
Two steps:1. Create a font object (just done once)
a. From a .ttf fileb. From a built-in font
2. Render text Creates a new surface …which must be blitted to the screen.
[Example]