Download presentation
Presentation is loading. Please wait.
Published byNatalie Grace Wilcox Modified over 8 years ago
1
PyGame - Unit 2 PyGame Unit 2 2-1 – 2-4 2.1 – Animation
2
PyGame - Unit 2 PyGame 2-1 Animation
3
PyGame - Unit 2 Objectives By the end of this unit you will be able to: –Define animation. –Create simple animation.
4
Animation Can you give me? –Examples of animation outside of video games??? PyGame - Unit 2
5
Drawing Images pygame.image.load() Sprite – An image loaded on to the Surface object. –Sprites can be PNG, JPG, GIF, or BMP image files. What are the tradeoffs of these file types??? Images must be loaded before they can be used: catImage = pygame.image.load(‘cat.png') PyGame - Unit 2
6
Loading Images Loading an Image Using.convert() backgroundImg = "images/background.jpg" background = pygame.image.load(backgroundImg).convert() Loading an Image Using.convert_alpha() –Image Transparency (imageFileName.png) pizzaImg = "images/pizza_cursor.png" mouseCursor = pygame.image.load(pizzaImg).convert_alpha() PyGame - Unit 2
7
.png File Types (Transparent Backgrounds) pointerImg = pygame.image.load('point.png').convert_alpha() DISPLAYSURF.blit(pointerImg, (100, 100)) point.png is a fancy pointer with a transparent background. We must do the.convert_alpha() Once drawn on the Surface object, it can be: –Blitted Copied to DISPLAYSURF so it will appear on the screen. PyGame - Unit 2
8
Demo (Transparency) transparent_vs_non-transparent.py PyGame - Unit 1
9
Blitting an Image Blitting - drawing the contents of one Surface onto another. Loading cat.png on a surface object named catImg. catImg = pygame.image.load('cat.png‘) We must blit (copy) the image’s Surface object to the display Surface object. DISPLAYSURF.blit(catImg, (catx, caty)) – (catx, caty) is a two integer tuple that determines where to position the image on DISPLAYSURF PyGame - Unit 2
10
Moving the Image (Inside of Game Loop) #Blit the Image to DISPLAYSURF DISPLAYSURF.blit(mouseCursor, (x, 160)) # Move the image (changing the x value) x += 1 # Updating the display. Pygame.display.update() PyGame - Unit 2
11
basic_animation.py Review basic_animation.pybasic_animation.py PyGame - Unit 2
12
Reading / Exercises Read the “Making Games with Python and PyGame” textbook PP. 24-28 pg2-1-1.py
13
PyGame - Unit 2 PyGame 2-2 Timing (Clock Object)
14
PyGame - Unit 2 Objectives By the end of this unit you will be able to: –Work with the pygame.time.Clock() Control the speed of our animation regardless of computing power.
15
The Clock Object If we created a game that ran great on an old computer, how would it run on the an extremely fast computer? fpsClock = pygame.time.Clock() Puts in small pauses on each iteration of the game loop. –Without pauses, our game program would run as fast as the computer could run it. PyGame - Unit 1
16
The Clock Objects tick() Method The Clock object’s tick() method should be called at the very end of the game loop, after the call to pygame.display.update(). FPS = 26... pygame.display.update() fpsClock.tick(FPS) The length of the pause is calculated based on how long it has been since the previous call to tick() PyGame - Unit 1
17
cat_animation.py Review cat_animation.pycat_animation.py Review diagonal_animation.pydiagonal_animation.py PyGame - Unit 1
18
PyGame - Unit 2 Reading / Exercises Read the “Making Games with Python and PyGame” textbook PP. ?? – ?? pg2-1-1.py pg2-1-2.py ???
19
PyGame - Unit 2 PyGame 2-3 Displaying Text pygame.font.Font() Object
20
PyGame - Unit 2 Objectives By the end of this unit you will be able to: –Display text on the screen using the: pygame.font.Font() function to define font style and size to define a font object. Rendering the font and text into a surface object. Blitting it on to the screen.
21
Definition(s) Render - the process of generating an image on a computer screen. Anti-aliasing - a graphics technique for making text and shapes look less blocky by adding a little bit of blur to their edges. –It takes a little more processing time to draw with anti-aliasing, so: The graphics may look better, but Your program may run slower (but only just a little). PyGame - Unit 2
22
Anti-Aliasing (Continued) Left line is aliased –Blockier when zoomed out The line on the right is ant-aliased –Smoother when zoomed out. PyGame - Unit 2
23
Six steps to displaying text in PyGame. 1.Create a pygame.font.Font object. 2.Rendering the font object, text definitions, colors, size etc. onto a surface object. 3.Create a Rect object from the surface object by calling the surface object’s get_rect() method. 4. Set the position of the Rect object. 5.Blit the Surface object onto the desired surface object (typically DISPLAYSURF) 6.Call pygame.display.update() PyGame - Unit 2
24
Text code Sample fontObj = pygame.font.SysFont("comicsans", 32) textSurfaceObj = fontObj.render('Hello world!', True, GREEN, BLUE) textRectObj = textSurfaceObj.get_rect() textRectObj.center = (200, 150) while True: # main game loop DISPLAYSURF.fill(WHITE) DISPLAYSURF.blit(textSurfaceObj, textRectObj) PyGame - Unit 2
25
Creating a pygame.font.Font object (Step 1) fontObj = pygame.font.SysFont("comicsans", 32) Type face = comicsans Type Size = 32 pts PyGame - Unit 2
26
Font Objects – Render Method (Step 2) textSurfaceObj = fontObj.render('Hello world!', True, GREEN, BLUE) Font.render(‘text’, True, color, backcolor) Our text is ‘hello world!’ The True parameter means we want to anti- alias the text (more on this in the next slide) GREEN is the text color. BLUE is the background color PyGame - Unit 2
27
The Surface object’s get_rect() method (Step 3) Us this to create a Rect object from the Surface object by calling the Surface object's get_rect() method: textRectObj = textSurfaceObj.get_rect() You cannot position your text on the screen unless it is associated with a rect() object. PyGame - Unit 2
28
Set the Position of the Rect object (Step 4) textRectObj.center = (200, 150) PyGame - Unit 2
29
Blit the Text Object to the Surface Object (Step 5) We are attaching the text to DISPLAYSURF DISPLAYSURF.blit(textSurfaceObj, textRectObj) Blit the following to the primary Display Surface ( DISPLAYSURF ) –textSurfaceObj - Defines the text –textRectObj - Helps place on DISPLAYSURF PyGame - Unit 2
30
pygame.display.update() (Step 6) Draws the DISPLAYSURF on screen. Discuss Performance pygame.display.update() –Vs. pygame.display.flip() PyGame - Unit 2
31
Reading / Exercises Read the “Making Games with Python and PyGame” textbook PP. 28 – 30 pg2-3-1.py pg2-1-2.py???
32
PyGame - Unit 2 PyGame 2-4 Sounds pygame.mixer.Sound() Object
33
PyGame - Unit 2 Objectives By the end of this lesson you will be able to: –Play sound effects in PyGame. –Play background music in PyGame.
34
pygame.mixer.Sound() Object soundObj = pygame.mixer.Sound('beeps.wav') In this example, beeps.wav must be in the same folder as the module. Plays sound effects. PyGame - Unit 2
35
Play() and stop() Methods soundObj = pygame.mixer.Sound('beeps.wav‘) Plays the entire sound (unless stopped) –soundObj.play() Immediately stops the sound from playing –soundObj.stop() PyGame - Unit 2
36
Sound Effect Example import time soundObj = pygame.mixer.Sound('beeps.wav') soundObj.play() time.sleep(1) # wait for 1 second soundObj.stop() PyGame - Unit 2
37
Background Music Example pygame.mixer.music.load(‘song.wav') pygame.mixer.music.play(-1, 0.0) –See next slide for parameter descriptions. #...some more of your code goes here... pygame.mixer.music.stop() PyGame - Unit 2
38
Parameters for pygame.mixer.music.play() pygame.mixer.music.load(‘song.wav') pygame.mixer.music.play(-1, 0.0) pygame.mixer.music.play( NbrPlayTimes, StartPoint) NbrPlayTimes – The number of times you want the music to play It will play the number of times indicated +1 -1 means loop endlessly. StartPoint – The number of seconds into the music track to begin playing. -1 means loop endlessly PyGame - Unit 2
39
Reading / Exercise(s) Read the Making Games with Python and PyGame textbook PP. 31 – 32 (Sounds) Exercises 2-4-1 - ???? PyGame - Unit 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.