SlideShare a Scribd company logo
Introduction to 
Presented by 
Felix Homann 
@Felix11H 
felix11h.github.io/ 
Slides 
Slideshare: 
tiny.cc/pyg-into 
Source: 
tiny.cc/pyg-into-github 
References 
- Pygame website: pygame.org 
- Richard Jones' Pygame lecture: 
- recording on Youtube 
- code samples on Bitbucket 
- List of keys: 
pygame.org/docs/ref/key.html 
This work is licensed under a Creative Commons Attribution 4.0 International License.
http://www.pygame.org 
Pygame is a Python wrapper around the SDL library (Simple 
DirectMedia Layer) with a few unique libraries with an emphasis on 
game programming, written by Pete Shinners.
From the FAQ 
Does not require OpenGL 
Uses either opengl, directx, windib, X11, linux frame buer, 
and many other dierent backends... including an ASCII art 
backend! 
Truly portable 
Supports Linux, Windows, Windows CE, BeOS, MacOS, Mac 
OS X, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, 
and QNX . . . 
Silliness built in!
A simple Pygame example 
1 import pygame 
2 
3 pygame.init() 
4 screen = pygame.display.set_mode((640, 480)) 
5 
6 color = [(0,0,0),(255,255,255)] 
7 running = True 
8 
9 while running: 
10 for event in pygame.event.get(): 
11 if event.type == pygame.QUIT: 
12 running = False 
13 if event.type == pygame.KEYDOWN: 
14 color[0], color[1] = color[1],color[0] 
15 
16 screen.fill(color[0]) 
17 pygame.display.flip() 
01-simple_pygame.py
What each element does: Importing  Initializing 
import pygame 
to import the Pygame module. 
from pygame.locals import * 
Optional. Puts limited set of constant and function in the global 
namespace. 
pygame.init() 
to initialize Pygame's modules (e.g. pygame.font). Not always 
needed, but recommended in any case.
What each element does: Setting Window  Screen 
screen = pygame.display.set_mode((640, 480)) 
initializes a window with dimensions 640  480 and returns the 
screen object. 
Everything to be displayed needs to be drawn on the screen.
Initializing a Pygame window 
Together: 
1 import pygame 
2 
3 pygame.init() 
4 screen = pygame.display.set_mode((640, 480)) 
02-window.py
Initializing a Pygame window - Extended 
1 import pygame 
2 
3 pygame.init() 
4 screen = pygame.display.set_mode((640, 480)) 
5 
6 running = True 
7 while running: 
8 for event in pygame.event.get(): 
9 if event.type == pygame.QUIT: 
10 running = False
What each element does: The Main Loop 
1 running = True 
2 while running: 
3 for event in pygame.event.get(): 
4 if event.type == pygame.QUIT: 
5 running = False 
6 if event.type == pygame.KEYDOWN: 
7 color[0], color[1] = color[1],color[0] 
8 
9 screen.fill(color[0]) 
10 pygame.display.flip() 
is the main loop of the game. 
I listen to events ! respond 
I proceed the game 
I draw on the screen 
I stop when done
A framework for your Pygames 
1 import pygame 
2 
3 pygame.init() 
4 screen = pygame.display.set_mode((640, 480)) 
5 
6 running = True 
7 while running: 
8 for event in pygame.event.get(): 
9 if event.type == pygame.QUIT: 
10 running = False 
11 if event.type == pygame.KEYDOWN: 
12 react_to_user_input() 
13 
14 do_things_the_game_does() 
15 draw_everything_on_the_screen() 
Next: 
I Drawing 
I User Input 
I Game Events
Drawing
Drawing on the screen I 
Filling the screen with a color: 
1 blue = (0,0,255) 
2 screen.fill(blue) 
3 pygame.display.flip() 
After all drawing is done, call display.flip() to update the 
display. 
Use pygame.draw to draw geometric shapes. A circle: 
1 red = (255,0,0) 
2 # position (320,240), radius = 50 
3 pygame.draw.circle(screen, red, (320,240), 50)
Drawing on the screen II 
Geometric shapes available for pygame.draw: 
circle(Surface, color, pos, radius, width=0) 
polygon(Surface, color, pointlist, width=0) 
line(Surface, color, start, end, width= 1) 
rect(Surface, color, Rect, width=0) 
ellipse(Surface, color, Rect, width=0) 
Example: 
1 red = (255,0,0) 
2 pygame.draw.line(screen, red, (10,50),(30,50),10)
Drawing on the screen - Colors 
De
ning a color 
gray = (200,200,200) 
#(red, green, blue) 
Use for example colorpicker.com:
Drawing on the screen - Positions 
De
ning a position: 
P = (11,9) 
#(x-axis, y-axis) 
To the reference coordinate system
Drawing on the screen - Rects 
pygame.Rect(left, top, width, height) 
to create a Rect. 
1 box = pygame.Rect(10, 10, 100, 40) 
2 pygame.draw.rect(screen, blue, box) 
3 #draws at (10,10) rectangle of width 100, height 40 
Rect anchors: 
top, left, bottom, right 
topleft, bottomleft, topright, bottomright 
midtop, midleft, midbottom, midright 
center, centerx, centery 
size, width, height 
w,h
A full drawing example 
1 import pygame 
2 
3 pygame.init() 
4 screen = pygame.display.set_mode((640, 480)) 
5 
6 white = (255,255,255) 
7 blue = (0,0,255) 
8 
9 running = True 
10 while running: 
11 for event in pygame.event.get(): 
12 if event.type == pygame.QUIT: 
13 running = False 
14 
15 screen.fill(white) 
16 pygame.draw.circle(screen, blue, (320,240), 100) 
17 # position (320,240), radius = 100 
18 
19 pygame.display.flip() 
04-drawing.py.
User Input
Events 
get all events in Pygame's event queue 
pygame.event.get() 
usually used as 
1 for event in pygame.event.get(): 
2 if event.type == YourEvent: 
3 react_to_your_event()
Event types 
Some of the most important event types are: 
pygame.QUIT 
pygame.KEYDOWN 
pygame.KEYUP 
pygame.USEREVENT 
With 
from pygame.locals import * 
from earlier, pre
x pygame isn't needed.
Events 
React to KEYDOWN event: 
1 while running: 
2 for event in pygame.event.get(): 
3 if event.type == KEYDOWN: 
4 react_to_key() 
Which key? ! if event type is KEYDOWN or KEYUP event has 
attribute key. 
1 for event in pygame.event.get(): 
2 if event.type == KEYDOWN: 
3 if event.key == K_ESCAPE: 
4 running = False
Pygame Keys 
Some of the most important keys are: 
K_RETURN 
K_SPACE 
K_ESCAPE 
K_UP, K_DOWN, K_LEFT, K_RIGHT 
K_a, K_b, ... 
K_0, K_1, ... 
Full list of keys: http://www.pygame.org/docs/ref/key.html
Getting continuous input 
KEYDOWN is a unique event. 
key = pygame.key.get_pressed() 
to get keys currently pressed. 
if key[pygame.K_UP]: 
move_up() 
to check and react on a speci
c key.
A user input example 
1 color = [0,0,0] 
2 
3 while running: 
4 for event in pygame.event.get(): 
5 if event.type == pygame.QUIT: 
6 running = False 
7 if event.type == KEYDOWN and event.key == K_SPACE: 
8 color = [0,0,0] 
9 
10 keys = pygame.key.get_pressed() 
11 if keys[K_UP]: 
12 color = [(rgb+1)%256 for rgb in color] 
13 
14 screen.fill(color) 
15 pygame.display.flip() 
05-user_input.py
Pygame's Clock
Limiting the frames per second 
clock = pygame.time.Clock() 
to initialize the clock. 
In your main loop call 
clock.tick(60) #limit to 60 fps
Clock example 
1 clock = pygame.time.Clock() 
2 
3 while running: 
4 for event in pygame.event.get(): 
5 if event.type == pygame.QUIT: 
6 running = False 
7 if event.type == KEYDOWN and event.key == K_SPACE: 
8 color = [0,0,0] 
9 
10 keys = pygame.key.get_pressed() 
11 if keys[K_UP]: 
12 color = [(rgb+1)%256 for rgb in color] 
13 
14 screen.fill(color) 
15 pygame.display.flip() 
16 
17 clock.tick(60) 
06-clock.py
Game Events
Sprites 
Pygame's sprite class gives convenient options for handling 
interactive graphical objects. 
If you want to draw and move (or manipulate) and object, make it 
a sprite. 
Sprites 
can be grouped together 
are easily drawn to a surface (even as a group!) 
have an update method that can be modi
ed 
have collision detection
Basic Sprite 
1 class SpriteExample(pygame.sprite.Sprite): 
2 
3 def __init__(self): 
4 pygame.sprite.Sprite.__init__(self) 
5 
6 self.image = #image 
7 self.rect = #rect 
8 
9 def update(self): 
10 pass
Sprite from a local image 
1 class SpriteExample(pygame.sprite.Sprite): 
2 
3 def __init__(self): 
4 pygame.sprite.Sprite.__init__(self) 
5 
6 self.image = pygame.image.load('local_img.png') 
7 self.rect = self.image.get_rect() 
8 self.rect.topleft = (80,120) 
9 
10 def update(self): 
11 pass
Self-drawn sprites: Rectangle 
1 class Rectangle(pygame.sprite.Sprite): 
2 
3 def __init__(self): 
4 pygame.sprite.Sprite.__init__(self) 
5 self.image = pygame.Surface([200, 50]) 
6 self.image.fill(blue) 
7 self.rect = self.image.get_rect() 
8 self.rect.top, self.rect.left = 100, 100 
9 
10 def update(self): 
11 pass
Sprites: A reminder about classes 
1 class Rectangle(pygame.sprite.Sprite): 
2 
3 def __init__(self, color): 
4 pygame.sprite.Sprite.__init__(self) 
5 self.image = pygame.Surface([200, 50]) 
6 self.image.fill(color) 
7 self.rect = self.image.get_rect() 
8 self.rect.top, self.rect.left = 100, 100 
9 
10 def update(self): 
11 pass 
1 white, blue = (255,255,255), (0,0,255) 
2 
3 white_rect = Rectangle(white) 
4 blue_rect = Rectangle(blue)
Sprite groups 
new_sprite_group = pygame.sprite.Group(sprite1) 
to create a new sprite group containing sprite sprite1. 
new_sprite_group.add(sprite2) 
to add another sprite later. 
Group updating and drawing: 
1 #inside main loop: 
2 
3 new_sprite_group.update() #game events 
4 new_sprite_group.draw() #drawing

More Related Content

What's hot

What is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | EdurekaWhat is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
Snake PY Game.pptx
Snake PY Game.pptxSnake PY Game.pptx
Snake PY Game.pptx
Lovely professinal university
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
Sumit Satam
 
Lab manual of Digital image processing using python by khalid Shaikh
Lab manual of Digital image processing using python by khalid ShaikhLab manual of Digital image processing using python by khalid Shaikh
Lab manual of Digital image processing using python by khalid Shaikh
khalidsheikh24
 
Gui programming
Gui programmingGui programming
Gui programming
manikanta361
 
Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019
Unity Technologies
 
Introduction to computer graphics
Introduction to computer graphicsIntroduction to computer graphics
Introduction to computer graphics
Rajamanickam Gomathijayam
 
Python Basics
Python BasicsPython Basics
Python Basics
tusharpanda88
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey
 
How to download and install Python - lesson 2
How to download and install Python - lesson 2How to download and install Python - lesson 2
How to download and install Python - lesson 2
Shohel Rana
 
Introduction to computer graphics
Introduction to computer graphicsIntroduction to computer graphics
Introduction to computer graphics
Kamal Acharya
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | Edureka
Edureka!
 
Circle drawing algo.
Circle drawing algo.Circle drawing algo.
Circle drawing algo.Mohd Arif
 
Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python Tutorial
QA TrainingHub
 
Snake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malavSnake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malav
Rohit malav
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
Swapnil Yadav
 
Python, the Language of Science and Engineering for Engineers
Python, the Language of Science and Engineering for EngineersPython, the Language of Science and Engineering for Engineers
Python, the Language of Science and Engineering for Engineers
Boey Pak Cheong
 
PyCharm demo
PyCharm demoPyCharm demo
PyCharm demo
T. Kim Nguyen
 
Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cpp
Alamgir Hossain
 
Texture Mapping
Texture MappingTexture Mapping
Texture Mapping
Syed Zaid Irshad
 

What's hot (20)

What is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | EdurekaWhat is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
 
Snake PY Game.pptx
Snake PY Game.pptxSnake PY Game.pptx
Snake PY Game.pptx
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
 
Lab manual of Digital image processing using python by khalid Shaikh
Lab manual of Digital image processing using python by khalid ShaikhLab manual of Digital image processing using python by khalid Shaikh
Lab manual of Digital image processing using python by khalid Shaikh
 
Gui programming
Gui programmingGui programming
Gui programming
 
Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019
 
Introduction to computer graphics
Introduction to computer graphicsIntroduction to computer graphics
Introduction to computer graphics
 
Python Basics
Python BasicsPython Basics
Python Basics
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
 
How to download and install Python - lesson 2
How to download and install Python - lesson 2How to download and install Python - lesson 2
How to download and install Python - lesson 2
 
Introduction to computer graphics
Introduction to computer graphicsIntroduction to computer graphics
Introduction to computer graphics
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | Edureka
 
Circle drawing algo.
Circle drawing algo.Circle drawing algo.
Circle drawing algo.
 
Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python Tutorial
 
Snake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malavSnake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malav
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
 
Python, the Language of Science and Engineering for Engineers
Python, the Language of Science and Engineering for EngineersPython, the Language of Science and Engineering for Engineers
Python, the Language of Science and Engineering for Engineers
 
PyCharm demo
PyCharm demoPyCharm demo
PyCharm demo
 
Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cpp
 
Texture Mapping
Texture MappingTexture Mapping
Texture Mapping
 

Viewers also liked

Introduction to Sumatra
Introduction to SumatraIntroduction to Sumatra
Introduction to Sumatra
Felix Z. Hoffmann
 
Minecraft in 500 lines of Python with Pyglet
Minecraft in 500 lines of Python with PygletMinecraft in 500 lines of Python with Pyglet
Minecraft in 500 lines of Python with Pyglet
Richard Donkin
 
Powerpoint Guidelines
Powerpoint GuidelinesPowerpoint Guidelines
Powerpoint Guidelines
Kathryn Harth
 
Slide Presentation Guidelines
Slide Presentation GuidelinesSlide Presentation Guidelines
Slide Presentation Guidelines
PlusOrMinusZero
 
Python Presentation
Python PresentationPython Presentation
Python Presentation
Narendra Sisodiya
 
Денис Ковалев «Python в игровой индустрии»
Денис Ковалев «Python в игровой индустрии»Денис Ковалев «Python в игровой индустрии»
Денис Ковалев «Python в игровой индустрии»
DataArt
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
Nowell Strite
 

Viewers also liked (7)

Introduction to Sumatra
Introduction to SumatraIntroduction to Sumatra
Introduction to Sumatra
 
Minecraft in 500 lines of Python with Pyglet
Minecraft in 500 lines of Python with PygletMinecraft in 500 lines of Python with Pyglet
Minecraft in 500 lines of Python with Pyglet
 
Powerpoint Guidelines
Powerpoint GuidelinesPowerpoint Guidelines
Powerpoint Guidelines
 
Slide Presentation Guidelines
Slide Presentation GuidelinesSlide Presentation Guidelines
Slide Presentation Guidelines
 
Python Presentation
Python PresentationPython Presentation
Python Presentation
 
Денис Ковалев «Python в игровой индустрии»
Денис Ковалев «Python в игровой индустрии»Денис Ковалев «Python в игровой индустрии»
Денис Ковалев «Python в игровой индустрии»
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 

Similar to Pygame presentation

Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 
The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196
Mahmoud Samir Fayed
 
XIX PUG-PE - Pygame game development
XIX PUG-PE - Pygame game developmentXIX PUG-PE - Pygame game development
XIX PUG-PE - Pygame game development
matheuscmpm
 
14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded GraphicsAdil Jafri
 
The Ring programming language version 1.7 book - Part 50 of 196
The Ring programming language version 1.7 book - Part 50 of 196The Ring programming language version 1.7 book - Part 50 of 196
The Ring programming language version 1.7 book - Part 50 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 56 of 210
The Ring programming language version 1.9 book - Part 56 of 210The Ring programming language version 1.9 book - Part 56 of 210
The Ring programming language version 1.9 book - Part 56 of 210
Mahmoud Samir Fayed
 
Vanmathy python
Vanmathy python Vanmathy python
Vanmathy python
PriyadharshiniVS
 
The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202
Mahmoud Samir Fayed
 
Teaching Python to 9 Year Old Girl - map mover
Teaching Python to 9 Year Old Girl -  map moverTeaching Python to 9 Year Old Girl -  map mover
Teaching Python to 9 Year Old Girl - map mover
Craig Oda
 
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84
Mahmoud Samir Fayed
 
C++ game development with oxygine
C++ game development with oxygineC++ game development with oxygine
C++ game development with oxygine
corehard_by
 
The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 47 of 185
The Ring programming language version 1.5.4 book - Part 47 of 185The Ring programming language version 1.5.4 book - Part 47 of 185
The Ring programming language version 1.5.4 book - Part 47 of 185
Mahmoud Samir Fayed
 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconftutorialsruby
 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconftutorialsruby
 
[EN] Ada Lovelace Day 2014 - Tampon run
[EN] Ada Lovelace Day 2014  - Tampon run[EN] Ada Lovelace Day 2014  - Tampon run
[EN] Ada Lovelace Day 2014 - Tampon run
Maja Kraljič
 
JavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your codeJavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your code
Laurence Svekis ✔
 
CE344L-200365-Lab5.pdf
CE344L-200365-Lab5.pdfCE344L-200365-Lab5.pdf
CE344L-200365-Lab5.pdf
UmarMustafa13
 
The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212
Mahmoud Samir Fayed
 

Similar to Pygame presentation (20)

Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 
The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185
 
The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196
 
XIX PUG-PE - Pygame game development
XIX PUG-PE - Pygame game developmentXIX PUG-PE - Pygame game development
XIX PUG-PE - Pygame game development
 
14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded Graphics
 
The Ring programming language version 1.7 book - Part 50 of 196
The Ring programming language version 1.7 book - Part 50 of 196The Ring programming language version 1.7 book - Part 50 of 196
The Ring programming language version 1.7 book - Part 50 of 196
 
The Ring programming language version 1.9 book - Part 56 of 210
The Ring programming language version 1.9 book - Part 56 of 210The Ring programming language version 1.9 book - Part 56 of 210
The Ring programming language version 1.9 book - Part 56 of 210
 
Vanmathy python
Vanmathy python Vanmathy python
Vanmathy python
 
The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202
 
Teaching Python to 9 Year Old Girl - map mover
Teaching Python to 9 Year Old Girl -  map moverTeaching Python to 9 Year Old Girl -  map mover
Teaching Python to 9 Year Old Girl - map mover
 
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84
 
C++ game development with oxygine
C++ game development with oxygineC++ game development with oxygine
C++ game development with oxygine
 
The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30
 
The Ring programming language version 1.5.4 book - Part 47 of 185
The Ring programming language version 1.5.4 book - Part 47 of 185The Ring programming language version 1.5.4 book - Part 47 of 185
The Ring programming language version 1.5.4 book - Part 47 of 185
 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
 
[EN] Ada Lovelace Day 2014 - Tampon run
[EN] Ada Lovelace Day 2014  - Tampon run[EN] Ada Lovelace Day 2014  - Tampon run
[EN] Ada Lovelace Day 2014 - Tampon run
 
JavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your codeJavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your code
 
CE344L-200365-Lab5.pdf
CE344L-200365-Lab5.pdfCE344L-200365-Lab5.pdf
CE344L-200365-Lab5.pdf
 
The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212
 

Recently uploaded

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 

Pygame presentation

  • 1. Introduction to Presented by Felix Homann @Felix11H felix11h.github.io/ Slides Slideshare: tiny.cc/pyg-into Source: tiny.cc/pyg-into-github References - Pygame website: pygame.org - Richard Jones' Pygame lecture: - recording on Youtube - code samples on Bitbucket - List of keys: pygame.org/docs/ref/key.html This work is licensed under a Creative Commons Attribution 4.0 International License.
  • 2. http://www.pygame.org Pygame is a Python wrapper around the SDL library (Simple DirectMedia Layer) with a few unique libraries with an emphasis on game programming, written by Pete Shinners.
  • 3. From the FAQ Does not require OpenGL Uses either opengl, directx, windib, X11, linux frame buer, and many other dierent backends... including an ASCII art backend! Truly portable Supports Linux, Windows, Windows CE, BeOS, MacOS, Mac OS X, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, and QNX . . . Silliness built in!
  • 4. A simple Pygame example 1 import pygame 2 3 pygame.init() 4 screen = pygame.display.set_mode((640, 480)) 5 6 color = [(0,0,0),(255,255,255)] 7 running = True 8 9 while running: 10 for event in pygame.event.get(): 11 if event.type == pygame.QUIT: 12 running = False 13 if event.type == pygame.KEYDOWN: 14 color[0], color[1] = color[1],color[0] 15 16 screen.fill(color[0]) 17 pygame.display.flip() 01-simple_pygame.py
  • 5. What each element does: Importing Initializing import pygame to import the Pygame module. from pygame.locals import * Optional. Puts limited set of constant and function in the global namespace. pygame.init() to initialize Pygame's modules (e.g. pygame.font). Not always needed, but recommended in any case.
  • 6. What each element does: Setting Window Screen screen = pygame.display.set_mode((640, 480)) initializes a window with dimensions 640 480 and returns the screen object. Everything to be displayed needs to be drawn on the screen.
  • 7. Initializing a Pygame window Together: 1 import pygame 2 3 pygame.init() 4 screen = pygame.display.set_mode((640, 480)) 02-window.py
  • 8. Initializing a Pygame window - Extended 1 import pygame 2 3 pygame.init() 4 screen = pygame.display.set_mode((640, 480)) 5 6 running = True 7 while running: 8 for event in pygame.event.get(): 9 if event.type == pygame.QUIT: 10 running = False
  • 9. What each element does: The Main Loop 1 running = True 2 while running: 3 for event in pygame.event.get(): 4 if event.type == pygame.QUIT: 5 running = False 6 if event.type == pygame.KEYDOWN: 7 color[0], color[1] = color[1],color[0] 8 9 screen.fill(color[0]) 10 pygame.display.flip() is the main loop of the game. I listen to events ! respond I proceed the game I draw on the screen I stop when done
  • 10. A framework for your Pygames 1 import pygame 2 3 pygame.init() 4 screen = pygame.display.set_mode((640, 480)) 5 6 running = True 7 while running: 8 for event in pygame.event.get(): 9 if event.type == pygame.QUIT: 10 running = False 11 if event.type == pygame.KEYDOWN: 12 react_to_user_input() 13 14 do_things_the_game_does() 15 draw_everything_on_the_screen() Next: I Drawing I User Input I Game Events
  • 12. Drawing on the screen I Filling the screen with a color: 1 blue = (0,0,255) 2 screen.fill(blue) 3 pygame.display.flip() After all drawing is done, call display.flip() to update the display. Use pygame.draw to draw geometric shapes. A circle: 1 red = (255,0,0) 2 # position (320,240), radius = 50 3 pygame.draw.circle(screen, red, (320,240), 50)
  • 13. Drawing on the screen II Geometric shapes available for pygame.draw: circle(Surface, color, pos, radius, width=0) polygon(Surface, color, pointlist, width=0) line(Surface, color, start, end, width= 1) rect(Surface, color, Rect, width=0) ellipse(Surface, color, Rect, width=0) Example: 1 red = (255,0,0) 2 pygame.draw.line(screen, red, (10,50),(30,50),10)
  • 14. Drawing on the screen - Colors De
  • 15. ning a color gray = (200,200,200) #(red, green, blue) Use for example colorpicker.com:
  • 16. Drawing on the screen - Positions De
  • 17. ning a position: P = (11,9) #(x-axis, y-axis) To the reference coordinate system
  • 18. Drawing on the screen - Rects pygame.Rect(left, top, width, height) to create a Rect. 1 box = pygame.Rect(10, 10, 100, 40) 2 pygame.draw.rect(screen, blue, box) 3 #draws at (10,10) rectangle of width 100, height 40 Rect anchors: top, left, bottom, right topleft, bottomleft, topright, bottomright midtop, midleft, midbottom, midright center, centerx, centery size, width, height w,h
  • 19. A full drawing example 1 import pygame 2 3 pygame.init() 4 screen = pygame.display.set_mode((640, 480)) 5 6 white = (255,255,255) 7 blue = (0,0,255) 8 9 running = True 10 while running: 11 for event in pygame.event.get(): 12 if event.type == pygame.QUIT: 13 running = False 14 15 screen.fill(white) 16 pygame.draw.circle(screen, blue, (320,240), 100) 17 # position (320,240), radius = 100 18 19 pygame.display.flip() 04-drawing.py.
  • 21. Events get all events in Pygame's event queue pygame.event.get() usually used as 1 for event in pygame.event.get(): 2 if event.type == YourEvent: 3 react_to_your_event()
  • 22. Event types Some of the most important event types are: pygame.QUIT pygame.KEYDOWN pygame.KEYUP pygame.USEREVENT With from pygame.locals import * from earlier, pre
  • 23. x pygame isn't needed.
  • 24. Events React to KEYDOWN event: 1 while running: 2 for event in pygame.event.get(): 3 if event.type == KEYDOWN: 4 react_to_key() Which key? ! if event type is KEYDOWN or KEYUP event has attribute key. 1 for event in pygame.event.get(): 2 if event.type == KEYDOWN: 3 if event.key == K_ESCAPE: 4 running = False
  • 25. Pygame Keys Some of the most important keys are: K_RETURN K_SPACE K_ESCAPE K_UP, K_DOWN, K_LEFT, K_RIGHT K_a, K_b, ... K_0, K_1, ... Full list of keys: http://www.pygame.org/docs/ref/key.html
  • 26. Getting continuous input KEYDOWN is a unique event. key = pygame.key.get_pressed() to get keys currently pressed. if key[pygame.K_UP]: move_up() to check and react on a speci
  • 28. A user input example 1 color = [0,0,0] 2 3 while running: 4 for event in pygame.event.get(): 5 if event.type == pygame.QUIT: 6 running = False 7 if event.type == KEYDOWN and event.key == K_SPACE: 8 color = [0,0,0] 9 10 keys = pygame.key.get_pressed() 11 if keys[K_UP]: 12 color = [(rgb+1)%256 for rgb in color] 13 14 screen.fill(color) 15 pygame.display.flip() 05-user_input.py
  • 30. Limiting the frames per second clock = pygame.time.Clock() to initialize the clock. In your main loop call clock.tick(60) #limit to 60 fps
  • 31. Clock example 1 clock = pygame.time.Clock() 2 3 while running: 4 for event in pygame.event.get(): 5 if event.type == pygame.QUIT: 6 running = False 7 if event.type == KEYDOWN and event.key == K_SPACE: 8 color = [0,0,0] 9 10 keys = pygame.key.get_pressed() 11 if keys[K_UP]: 12 color = [(rgb+1)%256 for rgb in color] 13 14 screen.fill(color) 15 pygame.display.flip() 16 17 clock.tick(60) 06-clock.py
  • 33. Sprites Pygame's sprite class gives convenient options for handling interactive graphical objects. If you want to draw and move (or manipulate) and object, make it a sprite. Sprites can be grouped together are easily drawn to a surface (even as a group!) have an update method that can be modi
  • 34. ed have collision detection
  • 35. Basic Sprite 1 class SpriteExample(pygame.sprite.Sprite): 2 3 def __init__(self): 4 pygame.sprite.Sprite.__init__(self) 5 6 self.image = #image 7 self.rect = #rect 8 9 def update(self): 10 pass
  • 36. Sprite from a local image 1 class SpriteExample(pygame.sprite.Sprite): 2 3 def __init__(self): 4 pygame.sprite.Sprite.__init__(self) 5 6 self.image = pygame.image.load('local_img.png') 7 self.rect = self.image.get_rect() 8 self.rect.topleft = (80,120) 9 10 def update(self): 11 pass
  • 37. Self-drawn sprites: Rectangle 1 class Rectangle(pygame.sprite.Sprite): 2 3 def __init__(self): 4 pygame.sprite.Sprite.__init__(self) 5 self.image = pygame.Surface([200, 50]) 6 self.image.fill(blue) 7 self.rect = self.image.get_rect() 8 self.rect.top, self.rect.left = 100, 100 9 10 def update(self): 11 pass
  • 38. Sprites: A reminder about classes 1 class Rectangle(pygame.sprite.Sprite): 2 3 def __init__(self, color): 4 pygame.sprite.Sprite.__init__(self) 5 self.image = pygame.Surface([200, 50]) 6 self.image.fill(color) 7 self.rect = self.image.get_rect() 8 self.rect.top, self.rect.left = 100, 100 9 10 def update(self): 11 pass 1 white, blue = (255,255,255), (0,0,255) 2 3 white_rect = Rectangle(white) 4 blue_rect = Rectangle(blue)
  • 39. Sprite groups new_sprite_group = pygame.sprite.Group(sprite1) to create a new sprite group containing sprite sprite1. new_sprite_group.add(sprite2) to add another sprite later. Group updating and drawing: 1 #inside main loop: 2 3 new_sprite_group.update() #game events 4 new_sprite_group.draw() #drawing
  • 40. Framework for working with sprite groups 1 class NewSprite(pygame.sp... 2 #defining a new sprite 3 4 newsprite = NewSprite() 5 sprites = pygame.sprite.Group() 6 sprites.add(newsprite) 7 8 while running: 9 for event in ... 10 11 sprites.update() #game events 12 sprites.draw() 13 pygame.display.flip()
  • 41. Sprite groups: working example 1 class Circle(pygame.sprite.Sprite): 2 def __init__(self): 3 pygame.sprite.Sprite.__init__(self) 4 self.image = pygame.Surface([100, 100]) 5 pygame.draw.circle(self.image, blue, (50, 50), 50) 6 self.rect = self.image.get_rect() 7 self.rect.center = [320,240] 8 9 def draw(sprites): 10 screen.fill(white) 11 sprites.draw(screen) 12 pygame.display.flip() 13 14 circle = Circle() 15 sprites = pygame.sprite.Group(circle) 16 17 while running: 18 sprites.update() 19 draw(sprites) 07-sprite-circle.py
  • 42. Working example: Transparency By default pygame.Surface is black. For transparency: 1 class Circle(pygame.sprite.Sprite): 2 def __init__(self): 3 pygame.sprite.Sprite.__init__(self) 4 self.image = pygame.Surface([100, 100], pygame.SRCALPHA, 32) 5 pygame.draw.circle(self.image, blue, (50, 50), 50) 6 self.image = self.image.convert_alpha() 08-circle.py
  • 43. Sprite Collision Detection pygame.sprite.collide_rect(left, right) to detect collision between two sprites pygame.sprite.spritecollideany(sprite, group) to test if the given sprite intersects with any sprites in a Group
  • 44. Have fun with ! Presented by Felix Homann @Felix11H felix11h.github.io/ Slides Slideshare: tiny.cc/pyg-into Source: tiny.cc/pyg-into-github References - Pygame website: pygame.org - Richard Jones' Pygame lecture: - recording on Youtube - code samples on Bitbucket - List of keys: pygame.org/docs/ref/key.html This work is licensed under a Creative Commons Attribution 4.0 International License.