Let’s Learn 2. Installing Pygame

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

Create a Simple Game in Scratch
Week 9 Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Week 10 Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
ABC’s of PowerPoint (Office 2007) Part 1: Basic Vocabulary Part 2: Cursors Part 3: Insert Your Text Part 4: Insert Your Pictures Part 5: Basic Tools &
GameMaker.  A lot of Different Definitions  Easier to Say What is NOT a Game  Movie is Not a Game  No Active Participation  Final Outcome is Fixed.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Pygame Dick Steflik.
Guide to Programming with Python
Unit 9 pyGame Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this.
Meet Pygame Noah Kantrowitz June 8, The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,
Reference: The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)
Video Games Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
CHAPTER 3 (P.49-53) Importing modules + pygame intro.
1. Starting 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Let’s Learn 3. Modules Saenthong School, January – February 2016
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
PyGame - Unit 2 PyGame Unit – – Animation.
Graphics in Python On entry: Run Python 2.78 from N: drive/computing and ICT VLE: Computing home page - check your feedback Success criteria: ●Understands.
Intro to Pygame Lecture 05. What is Pygame? It is a set of Python modules designed for writing games. It makes writing games possible for beginners. import.
(More) Event handling. Input and Windowed-OS’s Windowed OS’s (Windows, OSX, GUI-linux’s) send messages (events) when the user does something “on” the.
11. Skier Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
9. Media (sound effects, music, video) Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
5. Animation Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Reference: What is it? A multimedia python library – Window Management – Graphics geometric shapes bitmaps (sprites) – Input Mouse Keyboard.
13. Sprites. Outline 1.Game Things in Pygame (again) 2.The Pygame sprite Module 3.The Sprite Class 4.Groups of Sprites 5.Types of Collision Detection.
Let’s Learn 12. Packaging a Game Saengthong School, June – August 2016
Sprites (Images) and Sounds
Sound and more Animations
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
A look ahead: At the end of your project, you’ll be given an opportunity to make a standalone (.exe) version of your project. For this to work, you’ll.
15. Media (sound effects, music, video)
Scratch for Interactivity
Catapult 2016.
Animations.
PYGAME.
WITH PYGAME ON THE RASPBERRY PI
8. Installing Pygame
Keyboard Input.
9. Drawing.
Game Loops + Part II Reference:
10. User Input.
11. Animation Let's Learn Python and Pygame

9. Drawing Let's Learn Python and Pygame
Catapult 2014 Session 10.
Let’s Learn 10. Invaders Saengthong School, June – August 2016
What are variables? Using input()
Let's Learn Python and Pygame
Module 1: Getting Started with Windows 95
13. Sprites Let's Learn Python and Pygame
8. Starting Pygame Let's Learn Python and Pygame
10. User Input Let's Learn Python and Pygame
14. Pong.
6. Lists Let's Learn Python and Pygame
Let's Learn Python and Pygame
WICS - Flappy Anteater Python Tutorial
16. Invaders.
BSc in Digital Media, PSUIC
Let’s Learn 7. Sprites Saengthong School, June – August 2016
14. Pong Let's Learn Python and Pygame
11. Animation.
Creating a Simple Game in Scratch
L L Line CSE 420 Computer Games Lecture #6 Game Foundations.
CoE Software Lab II , Semester 2, Pong.
CoE Software Lab II 1. Pygame Intro , Semester 2,
CoE Software Lab II , Semester 2, Sprites.
Presentation transcript:

Let’s Learn 2. Installing Pygame http://www.pygame.org/ Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th 2. Installing Pygame http://www.pygame.org/

Outline What is Pygame? Installing Pygame Run "pygameTemplate.py" pygameTemplate.py Explained

1. What is Pygame? A set of modules for writing games home page: http://pygame.org/ documentation: http://pygame.org/docs/ pyGame helps you with: 2D graphics (and 3D) images, sounds, music, (video) user input (events) from keyboard, mouse, gamepad support for game things sprites, collision detection, etc.

pyGame Modules The modules include: Search page: cdrom cursors display draw event font image joystick key mixer mouse movie music overlay rect sndarray sprite surface surfarray time transform Search page: http://www.pygame.org/docs/search.html

Game Things in Pygame sprites: moving game characters / objects collision detection: which sprites are touching? event: a user action (e.g. mouse or key press), or computer change (e.g. clock tick) game loop: read new events update sprites and game state redraw game

2. Installing Pygame Install python first! make sure you can call python 3 and pip from a command window I'm using 32-bit Python 3.5.1

What's a Command Window? Also called a command prompt or shell. Look in the "Accessories" menu or perhaps on the taskbar

Install Pygame for Python 3.5 Download 32-bit or 64-bit WHL installer from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame Get "cp35" version, either 32 or 64

Is pygame installed? no Is pygame WHL file here? yes

Install WHL file using pip Check pip list

3. Run "pygameTemplate.py" python pygameTemplate.py A pygame game window

Using IDLE A pygame game window

4. pygameTemplate.py Explained import pygame from pygame.locals import * pygame.init() screenSize = (640, 480) screen = pygame.display.set_mode(screenSize) screen.fill((255,255,255)) # white background pygame.display.set_caption("Hello, World!") # set title bar clock = pygame.time.Clock() :

running = True while running: # game loop clock.tick(30) # set loop speed to 30 FPS # handle events for event in pygame.event.get(): if event.type == QUIT: running = False # update game state (nothing yet) # redraw game pygame.display.update() pygame.quit()

4.1. Creating a Pygame Window screen = pygame.display.set_mode((640,480)) set_mode() can take three arguments: (width,height), flag(s), bit-depth flags let the window become full-screen and resizeable bit-depth sets the number of colors that can be used

4.2. Pygame Colors screen.fill((255,255,255)) # white background A color is made from three integers (0-255) for the amount of red, green, and blue (RGB): 0 means "no color" 255 means "maximum color" e.g. (0,0,0) means "black"

Some Common Colors

Built-in Color Names The pygame.color module as a large dictionary of predefined color names, called THECOLORS Import it to use color names instead of (R,G,B) tuples: from pygame.color import THECOLORS # 600 color names : screen.fill(THECOLORS['white'])

What names, what colors? Add the for-loop: for key in sorted(THECOLORS): print(key, THECOLORS[key]) # sorted print Or have a look at a color table online: https://sites.google.com/site/meticulosslacker/pygame-thecolors

4.3. Frames per Second (FPS) clock.tick(30) sets pygame's loop to run at a speed of about 30 frames/sec (FPS) A frame = one game loop: handle events, update game state, redraw game 30 FPS = 30 frames(loops) in 1 second so 1 frame (loop) = 1/30 sec = 1000/30 millisecs (ms) ≈ 33 ms

Why about 30 FPS? If the work inside the loop is big, then the loop can take longer than 33 ms. If the work is small, and takes less than 33 ms, then Pygame will wait until 33 ms has passed before repeating the loop.

Why Use FPS? Games are easier to program if we know that one loop takes a fixed amount of time in our case, 1 loop = 33 ms e.g. a game object that should be on-screen for 5 seconds will need to be drawn in 150 (30*5) loops 1 sec == 30 FPS 5 secs == 30*5 == 150

Checking the FPS Modify pygameTemplate.py to print the actual time used for 1 loop. Inside the game loop: time_passed = clock.tick(30) # set loop speed to 30 FPS print(time_passed, "ms")

Or you can print the average FPS using clock.get_fps(): print("FPS", round(clock.get_fps(),1)) Game loop slowed down for a while (lots of work)

4.4. Events An event is a user action (e.g. mouse or key press), or a computer change (e.g. clock tick). a bit like "messages" sent to Pygame from the user and computer handle events update game state redraw game

The "quit" event for event in pygame.event.get(): if event.type == QUIT: # user clicks close box running = False When running is false, the game loop ends, and Pygame quits.

Quit by Also Typing <Esc> for event in pygame.event.get(): if event.type == QUIT: # user clicks close box running = False if (event.type == KEYUP and event.key == K_ESCAPE): Or combine into a single expression: if (event.type == QUIT) or \ (event.type == KEYUP and event.key == K_ESCAPE):

Keyboard Events KEYDOWN is sent when a key is pressed KEYUP is sent when a key is released Each key has a constant that begins with K_: alphabet keys are K_a through K_z Others: K_SPACE, K_RETURN, K_ESCAPE, etc. For a complete list see http://www.pygame.org/docs/ref/key.html

Other Events Add print(event) to for-loop to see many events arriving at game loop.