SlideShare a Scribd company logo
1 of 26
Download to read offline
Python in Game
Development
Ковалев Денис
Software Developer at DataArt
Используют Python
Battlefield 2 & Battlefield 2142
Eve Online
Mount & Blade
Civilization IV
Severance: Blade of Darkness
Greyhawk: Temple of Elemental Evil
Графические библиотеки
Tkinter, PyGTK, PyQt
PyGame - http://pygame.org
Pyganim - http://inventwithpython.com/pyganim
Pyglet - http://pyglet.org
Cocos2d - http://cocos2d.org
Pyggel (Python Graphical Game Engine + Libraries)
2D
3D
Panda3D - http://panda3d.org
Pygame
+ SDL (Simple Directmedia Layer) library
+ Stable and well-documented (http://www.pygame.org/docs/ref)
+ Examples (http://www.pygame.org/docs/ref/examples.html) and
tutorials (http://www.pygame.org/docs/tut/newbieguide.html)
- Not pythonic
+ Easy & powerful
Pygame Drawing Concept
Pygame в 9 строк
1. import pygame
2.
3. pygame.init()
4. screen = pygame.display.set_mode((640, 480))
5.
6. while 1:
7. for event in pygame.event.get():
8. if event.type == pygame.QUIT:
9. raise SystemExit("QUIT")
Pygame “Hello, World!”
1. import pygame
2.
3. pygame.init()
4. WHITE = pygame.Color(255, 255, 255)
5. width, height = (640, 480)
6. screen = pygame.display.set_mode((width, height))
7.
8. font = pygame.font.Font(None, 50)
9. text = font.render("Hello, World!", True, WHITE)
10.
11. # Draw text on screen
12. screen.blit(text, text.get_rect(centerx=width/2., centery=height/2.))
13. pygame.display.update()
14.
15. while 1:
16. for event in pygame.event.get():
17. if event.type == pygame.QUIT:
18. raise SystemExit("QUIT")
Pygame реакция на действия
1. def get_text(msg="Hello, World!"):
2. font = pygame.font.Font(None, 50)
3. text = font.render(msg, True, WHITE)
4. return text
5. delta_x, delta_y = (0, 0)
6. step = 20
7. while 1:
8. screen.fill(BLACK)
9. for event in pygame.event.get():
10. if event.type == pygame.QUIT:
11. raise SystemExit("QUIT")
12. elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
13. delta_y += step
14. elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
15. delta_y -= step
16. …
17. elif event.type == pygame.KEYDOWN and event.key == pygame.K_r:
18. delta_x, delta_y = (0, 0)
19. text = get_text()
20. screen.blit(text, text.get_rect(centerx=width/2. + delta_x, centery=height/2. + delta_y))
21. pygame.display.update()
Pygame surfaces
1. yellow_surface = pygame.Surface((width, height))
2. yellow_surface.fill(YELLOW)
3. green_surface = pygame.Surface((width/2, height/2))
4. green_surface.fill(GREEN)
5.
6. def draw_circle(pos, radius=width/8):
7. pygame.draw.circle(yellow_surface, GREEN, pos, radius)
8. pygame.draw.circle(green_surface, YELLOW, pos, radius)
9.
10. while 1:
11. for event in pygame.event.get():
12. if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
13. move = True
14. elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
15. move = False
16. if move:
17. yellow_surface.fill(YELLOW)
18. green_surface.fill(GREEN)
19. pos = pygame.mouse.get_pos()
20. draw_circle(pos)
21. screen.blit(yellow_surface, (0, 0))
22. screen.blit(green_surface, (0, 0))
23. pygame.display.update()
Pygame objects collision
1. static_rect = pygame.Rect((width/3, height/3), (width/3, height/3))
2. moving_rect = pygame.Rect((width*2/3, height*2/3), (width/6, height/6))
3.
4. def draw_rects(pos):
5. moving_rect.centerx, moving_rect.centery = pos
6. pygame.draw.rect(yellow_surface, GREEN, moving_rect)
7. if moving_rect.colliderect(static_rect):
8. pygame.draw.rect(yellow_surface, PURPLE, static_rect)
9. else:
10. pygame.draw.rect(yellow_surface, BLUE, static_rect)
11.
12. pos = (0, 0)
13. move = False
14. while 1:
15. for event in pygame.event.get():
16. ...
17. if move:
18. yellow_surface.fill(YELLOW)
19. pos = pygame.mouse.get_pos()
20. draw_rects(pos)
21. screen.blit(yellow_surface, (0, 0))
22. pygame.display.update()
Pygame Sprites - create
1. class Ball(pygame.sprite.Sprite):
2. radius = 25
3. groups = []
4. acceleration = 1
5. def __init__(self, pos):
6. pygame.sprite.Sprite.__init__(self, self.groups)
7. self.image = pygame.Surface((Ball.radius * 2, Ball.radius * 2))
8. self.image.fill(YELLOW)
9. self.image.convert_alpha()
10. self.rect = self.image.get_rect()
11. self.radius = Ball.radius
12. self.velocity = 0
13. pygame.draw.circle(self.image, BLUE, self.rect.center, self.radius, 0)
14. self.rect.center = pos
Pygame Sprites - move
1. class Ball(pygame.sprite.Sprite):
2. ….
3. def update(self):
4. if self.rect.top < height: # inside the screen?
5. self.rect.move_ip(0, self.velocity)
6. bricks = pygame.sprite.spritecollide(self, static, False)
7. if bricks:
8. brick = bricks[0]
9. self.rect.bottom = brick.rect.top # place the ball on top of the brick
10. self.velocity *= -0.9 # bounce with speed loss
11. if 0 > self.velocity > -0.1: # prevent infinite bounce
12. self.velocity = 0
13. else:
14. self.velocity += Ball.acceleration
Pygame Sprites
1. allsprites = pygame.sprite.Group()
2. Ball.groups = allsprites
3.
4. static = pygame.sprite.Group()
5. Brick.groups = allsprites, static
6.
7. timer = pygame.time.Clock()
8. screen.blit(yellow_surface, (0, 0))
9. while 1:
10. timer.tick(60)
11. for event in pygame.event.get():
12. if event.type == pygame.QUIT:
13. raise SystemExit("QUIT")
14. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
15. Ball(event.pos)
16. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:
17. Brick(event.pos)
18. elif event.type == pygame.KEYDOWN and event.key == pygame.K_r:
19. allsprites.empty()
20. static.empty()
21. allsprites.clear(screen, yellow_surface)
22. allsprites.update()
23. allsprites.draw(screen)
24. pygame.display.update()
Python Physics Engines
- http://www.pymunk.org
PyODE (Python bindings for The Open Dynamics Engine)
pyBox2D - http://code.google.com/p/pybox2d
Panda3D - http://www.panda3d.org
Pymunk Physics Engine
- Based on Chipmunk2D (http://chipmunk-physics.net)
- Force & impulse
- Collisions
- Constraints
- Pygame & Pyglet modules
Pymunk basics
1. import pymunk as pm
2.
3. space = pm.Space()
4. space.gravity = (0, -900.0)
5.
6. class Ball(pm.Body):
7. def __init__(self, pos, mass=10, radius=25):
8. inertia = pm.moment_for_circle(mass, 0, radius, (0,0))
9. pm.Body.__init__(self, mass, inertia)
10. self.position = pos
11. self.radius = radius
12. def get_shape(self, elasticity=0.9):
13. shape = pm.Circle(self, self.radius, (0,0))
14. shape.elasticity = elasticity
15. return shape
Pygame + Pymunk
1. while 1:
2. for event in pygame.event.get():
3. if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
4. body = Ball(to_pygame(event.pos))
5. shape = body.get_shape()
6. space.add(body, shape)
7. balls.append(shape)
8. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:
9. brick = Brick(to_pygame(event.pos))
10. space.add(brick)
11. bricks.append(brick)
12.
13. ### Draw stuff here
14.
15. ### Update physics
16. fps = 60.0
17. space.step(1./fps)
18.
19. pygame.display.flip()
20. clock.tick(fps)
Pyglet
+ No dependencies
+ Pythonic
+ Images, audio, video in any format
- Not for games
Cocos2d
+ Pyglet-based
+ For games, apps, GUI interaction
- Poor API docs
- Google does not help
+ OpenGL interface
+ Layers and scenes
Pygame vs Pyglet
1. import pygame
2.
3. pygame.init()
4. screen = pygame.display.set_mode
((640,480))
5.
6. while 1:
7. for event in pygame.event.get():
8. if event.type == pygame.QUIT:
9. raise SystemExit("QUIT")
1. import pyglet
2.
3. screen = pyglet.window.Window
(640,480)
4.
5. pyglet.app.run()
Pygame vs Pyglet
1. import pygame
2.
3. pygame.init()
4. screen = pygame.display.set_mode((640,480))
5.
6. while 1:
7. timer.tick(60)
8. for event in pygame.event.get():
9. if event.type == pygame.KEYDOWN and
event.key == pygame.K_a:
10. print 'You pressed A'
11. #update objects here
12. pygame.display.update()
1. import pyglet
2. from pyglet.window import key
3.
4. screen = pyglet.window.Window(640,480)
5.
6. @screen.event
7. def on_key_press(symbol, modifiers):
8. if symbol == key.A:
9. print 'You pressed A'
10.
11. def update(dt):
12. #update objects here
13.
14. pyglet.clock.schedule_interval(update, 1/60.0)
15. pyglet.app.run()
Редакторы уровней
Tiled - http://www.mapeditor.org
+ PyTMX
FIFE - http://www.fifengine.net
FIFE + Python = https://github.com/fifengine/python-tutorials
“Unknown Horizons”“Zero-Projekt”
Examples and games
pyvolley (cocos2d и pymunk) - https://github.
com/aikikode/pyvolley
Minecraft на Python (pyglet, OpenGL) -
https://github.com/boskee/Minecraft
“Кораблики” (PyGame + Tiled) https://github.
com/aikikode/nautili
pyhammerfight (cocos2d и pymunk) - https://github.
com/aikikode/pyhammerfight
Исходники примеров презентации - https://bitbucket.
org/aikikode/pygame_examples
Презентация - http://bit.ly/1lNp0Uk
Дополнительные источники
Игры и библиотеки - https://wiki.python.org/moin/PythonGames
Making games with Pygame - http://youtu.be/RQ-lIU0jWwg
2D game development using Pyglet with Cocos2d - http://video.kiberpipa.
org/py_2d_and_3d_game_development_using_pyglet/
Спасибо за внимание

More Related Content

What's hot

The Ring programming language version 1.2 book - Part 37 of 84
The Ring programming language version 1.2 book - Part 37 of 84The Ring programming language version 1.2 book - Part 37 of 84
The Ring programming language version 1.2 book - Part 37 of 84Mahmoud Samir Fayed
 
Tools for developing Android Games
 Tools for developing Android Games Tools for developing Android Games
Tools for developing Android GamesPlatty Soft
 
watchOS 2でゲーム作ってみた話
watchOS 2でゲーム作ってみた話watchOS 2でゲーム作ってみた話
watchOS 2でゲーム作ってみた話Kohki Miki
 
The Ring programming language version 1.10 book - Part 61 of 212
The Ring programming language version 1.10 book - Part 61 of 212The Ring programming language version 1.10 book - Part 61 of 212
The Ring programming language version 1.10 book - Part 61 of 212Mahmoud Samir Fayed
 
iOS Game Development with Cocos2D
iOS Game Development with Cocos2DiOS Game Development with Cocos2D
iOS Game Development with Cocos2DGreenwell
 
The Ring programming language version 1.5.1 book - Part 64 of 180
The Ring programming language version 1.5.1 book - Part 64 of 180The Ring programming language version 1.5.1 book - Part 64 of 180
The Ring programming language version 1.5.1 book - Part 64 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 65 of 202
The Ring programming language version 1.8 book - Part 65 of 202The Ring programming language version 1.8 book - Part 65 of 202
The Ring programming language version 1.8 book - Part 65 of 202Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 52 of 181
The Ring programming language version 1.5.2 book - Part 52 of 181The Ring programming language version 1.5.2 book - Part 52 of 181
The Ring programming language version 1.5.2 book - Part 52 of 181Mahmoud Samir Fayed
 
Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲吳錫修 (ShyiShiou Wu)
 
pygame sharing pyhug
pygame sharing pyhugpygame sharing pyhug
pygame sharing pyhugTim (文昌)
 
The Ring programming language version 1.5.3 book - Part 50 of 184
The Ring programming language version 1.5.3 book - Part 50 of 184The Ring programming language version 1.5.3 book - Part 50 of 184
The Ring programming language version 1.5.3 book - Part 50 of 184Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210The Ring programming language version 1.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 40 of 88
The Ring programming language version 1.3 book - Part 40 of 88The Ring programming language version 1.3 book - Part 40 of 88
The Ring programming language version 1.3 book - Part 40 of 88Mahmoud Samir Fayed
 
Test Driven Cocos2d
Test Driven Cocos2dTest Driven Cocos2d
Test Driven Cocos2dEric Smith
 
Test driven game development silly, stupid or inspired?
Test driven game development   silly, stupid or inspired?Test driven game development   silly, stupid or inspired?
Test driven game development silly, stupid or inspired?Eric Smith
 
The Ring programming language version 1.6 book - Part 70 of 189
The Ring programming language version 1.6 book - Part 70 of 189The Ring programming language version 1.6 book - Part 70 of 189
The Ring programming language version 1.6 book - Part 70 of 189Mahmoud Samir Fayed
 

What's hot (20)

The Ring programming language version 1.2 book - Part 37 of 84
The Ring programming language version 1.2 book - Part 37 of 84The Ring programming language version 1.2 book - Part 37 of 84
The Ring programming language version 1.2 book - Part 37 of 84
 
Tools for developing Android Games
 Tools for developing Android Games Tools for developing Android Games
Tools for developing Android Games
 
Box2D and libGDX
Box2D and libGDXBox2D and libGDX
Box2D and libGDX
 
watchOS 2でゲーム作ってみた話
watchOS 2でゲーム作ってみた話watchOS 2でゲーム作ってみた話
watchOS 2でゲーム作ってみた話
 
The Ring programming language version 1.10 book - Part 61 of 212
The Ring programming language version 1.10 book - Part 61 of 212The Ring programming language version 1.10 book - Part 61 of 212
The Ring programming language version 1.10 book - Part 61 of 212
 
iOS Game Development with Cocos2D
iOS Game Development with Cocos2DiOS Game Development with Cocos2D
iOS Game Development with Cocos2D
 
The Ring programming language version 1.5.1 book - Part 64 of 180
The Ring programming language version 1.5.1 book - Part 64 of 180The Ring programming language version 1.5.1 book - Part 64 of 180
The Ring programming language version 1.5.1 book - Part 64 of 180
 
libGDX: Tiled Maps
libGDX: Tiled MapslibGDX: Tiled Maps
libGDX: Tiled Maps
 
The Ring programming language version 1.8 book - Part 65 of 202
The Ring programming language version 1.8 book - Part 65 of 202The Ring programming language version 1.8 book - Part 65 of 202
The Ring programming language version 1.8 book - Part 65 of 202
 
Code Pad
Code PadCode Pad
Code Pad
 
The Ring programming language version 1.5.2 book - Part 52 of 181
The Ring programming language version 1.5.2 book - Part 52 of 181The Ring programming language version 1.5.2 book - Part 52 of 181
The Ring programming language version 1.5.2 book - Part 52 of 181
 
Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲
 
pygame sharing pyhug
pygame sharing pyhugpygame sharing pyhug
pygame sharing pyhug
 
The Ring programming language version 1.5.3 book - Part 50 of 184
The Ring programming language version 1.5.3 book - Part 50 of 184The Ring programming language version 1.5.3 book - Part 50 of 184
The Ring programming language version 1.5.3 book - Part 50 of 184
 
The Ring programming language version 1.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210The Ring programming language version 1.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210
 
The Ring programming language version 1.3 book - Part 40 of 88
The Ring programming language version 1.3 book - Part 40 of 88The Ring programming language version 1.3 book - Part 40 of 88
The Ring programming language version 1.3 book - Part 40 of 88
 
Test Driven Cocos2d
Test Driven Cocos2dTest Driven Cocos2d
Test Driven Cocos2d
 
Test driven game development silly, stupid or inspired?
Test driven game development   silly, stupid or inspired?Test driven game development   silly, stupid or inspired?
Test driven game development silly, stupid or inspired?
 
The Ring programming language version 1.6 book - Part 70 of 189
The Ring programming language version 1.6 book - Part 70 of 189The Ring programming language version 1.6 book - Part 70 of 189
The Ring programming language version 1.6 book - Part 70 of 189
 
Playing With Ruby
Playing With RubyPlaying With Ruby
Playing With Ruby
 

Viewers also liked

Viewers also liked (6)

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
 
Minecraft in 500 lines with Pyglet - PyCon UK
Minecraft in 500 lines with Pyglet - PyCon UKMinecraft in 500 lines with Pyglet - PyCon UK
Minecraft in 500 lines with Pyglet - PyCon UK
 
Разработка игр с помощью Cocos2d
Разработка игр с помощью Cocos2dРазработка игр с помощью Cocos2d
Разработка игр с помощью Cocos2d
 
AFFORDABLE HOUSING IN CAVITE RUSH RUSH RUSH FOR SALE
AFFORDABLE HOUSING IN CAVITE RUSH RUSH RUSH FOR SALEAFFORDABLE HOUSING IN CAVITE RUSH RUSH RUSH FOR SALE
AFFORDABLE HOUSING IN CAVITE RUSH RUSH RUSH FOR SALE
 
compendium_hiv_declarations_0
compendium_hiv_declarations_0compendium_hiv_declarations_0
compendium_hiv_declarations_0
 
MURANG BAHAY AT LUPA FOR SALE IN CAVITE
MURANG BAHAY AT LUPA FOR SALE IN CAVITEMURANG BAHAY AT LUPA FOR SALE IN CAVITE
MURANG BAHAY AT LUPA FOR SALE IN CAVITE
 

Similar to Денис Ковалев «Python в игровой индустрии»

Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...DroidConTLV
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88Mahmoud Samir Fayed
 
C++ game development with oxygine
C++ game development with oxygineC++ game development with oxygine
C++ game development with oxyginecorehard_by
 
Java term project final report
Java term project final reportJava term project final report
Java term project final reportJiwon Han
 
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLLeaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLgerbille
 
The Ring programming language version 1.8 book - Part 59 of 202
The Ring programming language version 1.8 book - Part 59 of 202The Ring programming language version 1.8 book - Part 59 of 202
The Ring programming language version 1.8 book - Part 59 of 202Mahmoud Samir Fayed
 
I wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdfI wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdffeelinggifts
 
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 moverCraig Oda
 
Non stop random2b
Non stop random2bNon stop random2b
Non stop random2bphanhung20
 
14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded GraphicsAdil Jafri
 
Testing a 2D Platformer with Spock
Testing a 2D Platformer with SpockTesting a 2D Platformer with Spock
Testing a 2D Platformer with SpockAlexander Tarlinder
 
Python과 node.js기반 데이터 분석 및 가시화
Python과 node.js기반 데이터 분석 및 가시화Python과 node.js기반 데이터 분석 및 가시화
Python과 node.js기반 데이터 분석 및 가시화Tae wook kang
 

Similar to Денис Ковалев «Python в игровой индустрии» (20)

Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...Connecting your phone and home with firebase and android things - James Cogga...
Connecting your phone and home with firebase and android things - James Cogga...
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212
 
The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189
 
The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88
 
C++ game development with oxygine
C++ game development with oxygineC++ game development with oxygine
C++ game development with oxygine
 
662305 LAB13
662305 LAB13662305 LAB13
662305 LAB13
 
Java term project final report
Java term project final reportJava term project final report
Java term project final report
 
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLLeaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGL
 
The Ring programming language version 1.8 book - Part 59 of 202
The Ring programming language version 1.8 book - Part 59 of 202The Ring programming language version 1.8 book - Part 59 of 202
The Ring programming language version 1.8 book - Part 59 of 202
 
Shootting Game
Shootting GameShootting Game
Shootting Game
 
Vanmathy python
Vanmathy python Vanmathy python
Vanmathy python
 
I wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdfI wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdf
 
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
 
Unity3 d devfest-2014
Unity3 d devfest-2014Unity3 d devfest-2014
Unity3 d devfest-2014
 
Sequence diagrams
Sequence diagramsSequence diagrams
Sequence diagrams
 
Non stop random2b
Non stop random2bNon stop random2b
Non stop random2b
 
Android Things
Android ThingsAndroid Things
Android Things
 
14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded Graphics
 
Testing a 2D Platformer with Spock
Testing a 2D Platformer with SpockTesting a 2D Platformer with Spock
Testing a 2D Platformer with Spock
 
Python과 node.js기반 데이터 분석 및 가시화
Python과 node.js기반 데이터 분석 및 가시화Python과 node.js기반 데이터 분석 및 가시화
Python과 node.js기반 데이터 분석 및 가시화
 

More from DataArt

DataArt Custom Software Engineering with a Human Approach
DataArt Custom Software Engineering with a Human ApproachDataArt Custom Software Engineering with a Human Approach
DataArt Custom Software Engineering with a Human ApproachDataArt
 
DataArt Healthcare & Life Sciences
DataArt Healthcare & Life SciencesDataArt Healthcare & Life Sciences
DataArt Healthcare & Life SciencesDataArt
 
DataArt Financial Services and Capital Markets
DataArt Financial Services and Capital MarketsDataArt Financial Services and Capital Markets
DataArt Financial Services and Capital MarketsDataArt
 
About DataArt HR Partners
About DataArt HR PartnersAbout DataArt HR Partners
About DataArt HR PartnersDataArt
 
Event management в IT
Event management в ITEvent management в IT
Event management в ITDataArt
 
Digital Marketing from inside
Digital Marketing from insideDigital Marketing from inside
Digital Marketing from insideDataArt
 
What's new in Android, Igor Malytsky ( Google Post I|O Tour)
What's new in Android, Igor Malytsky ( Google Post I|O Tour)What's new in Android, Igor Malytsky ( Google Post I|O Tour)
What's new in Android, Igor Malytsky ( Google Post I|O Tour)DataArt
 
DevOps Workshop:Что бывает, когда DevOps приходит на проект
DevOps Workshop:Что бывает, когда DevOps приходит на проектDevOps Workshop:Что бывает, когда DevOps приходит на проект
DevOps Workshop:Что бывает, когда DevOps приходит на проектDataArt
 
IT Talk Kharkiv: «‎Soft skills в IT. Польза или вред? Максим Бастион, DataArt
IT Talk Kharkiv: «‎Soft skills в IT. Польза или вред? Максим Бастион, DataArtIT Talk Kharkiv: «‎Soft skills в IT. Польза или вред? Максим Бастион, DataArt
IT Talk Kharkiv: «‎Soft skills в IT. Польза или вред? Максим Бастион, DataArtDataArt
 
«Ноль копеек. Спастись от выгорания» — Сергей Чеботарев (Head of Design, Han...
 «Ноль копеек. Спастись от выгорания» — Сергей Чеботарев (Head of Design, Han... «Ноль копеек. Спастись от выгорания» — Сергей Чеботарев (Head of Design, Han...
«Ноль копеек. Спастись от выгорания» — Сергей Чеботарев (Head of Design, Han...DataArt
 
Communication in QA's life
Communication in QA's lifeCommunication in QA's life
Communication in QA's lifeDataArt
 
Нельзя просто так взять и договориться, или как мы работали со сложными людьми
Нельзя просто так взять и договориться, или как мы работали со сложными людьмиНельзя просто так взять и договориться, или как мы работали со сложными людьми
Нельзя просто так взять и договориться, или как мы работали со сложными людьмиDataArt
 
Знакомьтесь, DevOps
Знакомьтесь, DevOpsЗнакомьтесь, DevOps
Знакомьтесь, DevOpsDataArt
 
DevOps in real life
DevOps in real lifeDevOps in real life
DevOps in real lifeDataArt
 
Codeless: автоматизация тестирования
Codeless: автоматизация тестированияCodeless: автоматизация тестирования
Codeless: автоматизация тестированияDataArt
 
Selenoid
SelenoidSelenoid
SelenoidDataArt
 
Selenide
SelenideSelenide
SelenideDataArt
 
A. Sirota "Building an Automation Solution based on Appium"
A. Sirota "Building an Automation Solution based on Appium"A. Sirota "Building an Automation Solution based on Appium"
A. Sirota "Building an Automation Solution based on Appium"DataArt
 
Эмоциональный интеллект или как не сойти с ума в условиях сложного и динамичн...
Эмоциональный интеллект или как не сойти с ума в условиях сложного и динамичн...Эмоциональный интеллект или как не сойти с ума в условиях сложного и динамичн...
Эмоциональный интеллект или как не сойти с ума в условиях сложного и динамичн...DataArt
 
IT talk: Как я перестал бояться и полюбил TestNG
IT talk: Как я перестал бояться и полюбил TestNGIT talk: Как я перестал бояться и полюбил TestNG
IT talk: Как я перестал бояться и полюбил TestNGDataArt
 

More from DataArt (20)

DataArt Custom Software Engineering with a Human Approach
DataArt Custom Software Engineering with a Human ApproachDataArt Custom Software Engineering with a Human Approach
DataArt Custom Software Engineering with a Human Approach
 
DataArt Healthcare & Life Sciences
DataArt Healthcare & Life SciencesDataArt Healthcare & Life Sciences
DataArt Healthcare & Life Sciences
 
DataArt Financial Services and Capital Markets
DataArt Financial Services and Capital MarketsDataArt Financial Services and Capital Markets
DataArt Financial Services and Capital Markets
 
About DataArt HR Partners
About DataArt HR PartnersAbout DataArt HR Partners
About DataArt HR Partners
 
Event management в IT
Event management в ITEvent management в IT
Event management в IT
 
Digital Marketing from inside
Digital Marketing from insideDigital Marketing from inside
Digital Marketing from inside
 
What's new in Android, Igor Malytsky ( Google Post I|O Tour)
What's new in Android, Igor Malytsky ( Google Post I|O Tour)What's new in Android, Igor Malytsky ( Google Post I|O Tour)
What's new in Android, Igor Malytsky ( Google Post I|O Tour)
 
DevOps Workshop:Что бывает, когда DevOps приходит на проект
DevOps Workshop:Что бывает, когда DevOps приходит на проектDevOps Workshop:Что бывает, когда DevOps приходит на проект
DevOps Workshop:Что бывает, когда DevOps приходит на проект
 
IT Talk Kharkiv: «‎Soft skills в IT. Польза или вред? Максим Бастион, DataArt
IT Talk Kharkiv: «‎Soft skills в IT. Польза или вред? Максим Бастион, DataArtIT Talk Kharkiv: «‎Soft skills в IT. Польза или вред? Максим Бастион, DataArt
IT Talk Kharkiv: «‎Soft skills в IT. Польза или вред? Максим Бастион, DataArt
 
«Ноль копеек. Спастись от выгорания» — Сергей Чеботарев (Head of Design, Han...
 «Ноль копеек. Спастись от выгорания» — Сергей Чеботарев (Head of Design, Han... «Ноль копеек. Спастись от выгорания» — Сергей Чеботарев (Head of Design, Han...
«Ноль копеек. Спастись от выгорания» — Сергей Чеботарев (Head of Design, Han...
 
Communication in QA's life
Communication in QA's lifeCommunication in QA's life
Communication in QA's life
 
Нельзя просто так взять и договориться, или как мы работали со сложными людьми
Нельзя просто так взять и договориться, или как мы работали со сложными людьмиНельзя просто так взять и договориться, или как мы работали со сложными людьми
Нельзя просто так взять и договориться, или как мы работали со сложными людьми
 
Знакомьтесь, DevOps
Знакомьтесь, DevOpsЗнакомьтесь, DevOps
Знакомьтесь, DevOps
 
DevOps in real life
DevOps in real lifeDevOps in real life
DevOps in real life
 
Codeless: автоматизация тестирования
Codeless: автоматизация тестированияCodeless: автоматизация тестирования
Codeless: автоматизация тестирования
 
Selenoid
SelenoidSelenoid
Selenoid
 
Selenide
SelenideSelenide
Selenide
 
A. Sirota "Building an Automation Solution based on Appium"
A. Sirota "Building an Automation Solution based on Appium"A. Sirota "Building an Automation Solution based on Appium"
A. Sirota "Building an Automation Solution based on Appium"
 
Эмоциональный интеллект или как не сойти с ума в условиях сложного и динамичн...
Эмоциональный интеллект или как не сойти с ума в условиях сложного и динамичн...Эмоциональный интеллект или как не сойти с ума в условиях сложного и динамичн...
Эмоциональный интеллект или как не сойти с ума в условиях сложного и динамичн...
 
IT talk: Как я перестал бояться и полюбил TestNG
IT talk: Как я перестал бояться и полюбил TestNGIT talk: Как я перестал бояться и полюбил TestNG
IT talk: Как я перестал бояться и полюбил TestNG
 

Recently uploaded

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 

Денис Ковалев «Python в игровой индустрии»

  • 1. Python in Game Development Ковалев Денис Software Developer at DataArt
  • 2. Используют Python Battlefield 2 & Battlefield 2142 Eve Online Mount & Blade Civilization IV Severance: Blade of Darkness Greyhawk: Temple of Elemental Evil
  • 3. Графические библиотеки Tkinter, PyGTK, PyQt PyGame - http://pygame.org Pyganim - http://inventwithpython.com/pyganim Pyglet - http://pyglet.org Cocos2d - http://cocos2d.org Pyggel (Python Graphical Game Engine + Libraries) 2D 3D Panda3D - http://panda3d.org
  • 4. Pygame + SDL (Simple Directmedia Layer) library + Stable and well-documented (http://www.pygame.org/docs/ref) + Examples (http://www.pygame.org/docs/ref/examples.html) and tutorials (http://www.pygame.org/docs/tut/newbieguide.html) - Not pythonic + Easy & powerful
  • 6. Pygame в 9 строк 1. import pygame 2. 3. pygame.init() 4. screen = pygame.display.set_mode((640, 480)) 5. 6. while 1: 7. for event in pygame.event.get(): 8. if event.type == pygame.QUIT: 9. raise SystemExit("QUIT")
  • 7. Pygame “Hello, World!” 1. import pygame 2. 3. pygame.init() 4. WHITE = pygame.Color(255, 255, 255) 5. width, height = (640, 480) 6. screen = pygame.display.set_mode((width, height)) 7. 8. font = pygame.font.Font(None, 50) 9. text = font.render("Hello, World!", True, WHITE) 10. 11. # Draw text on screen 12. screen.blit(text, text.get_rect(centerx=width/2., centery=height/2.)) 13. pygame.display.update() 14. 15. while 1: 16. for event in pygame.event.get(): 17. if event.type == pygame.QUIT: 18. raise SystemExit("QUIT")
  • 8. Pygame реакция на действия 1. def get_text(msg="Hello, World!"): 2. font = pygame.font.Font(None, 50) 3. text = font.render(msg, True, WHITE) 4. return text 5. delta_x, delta_y = (0, 0) 6. step = 20 7. while 1: 8. screen.fill(BLACK) 9. for event in pygame.event.get(): 10. if event.type == pygame.QUIT: 11. raise SystemExit("QUIT") 12. elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN: 13. delta_y += step 14. elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP: 15. delta_y -= step 16. … 17. elif event.type == pygame.KEYDOWN and event.key == pygame.K_r: 18. delta_x, delta_y = (0, 0) 19. text = get_text() 20. screen.blit(text, text.get_rect(centerx=width/2. + delta_x, centery=height/2. + delta_y)) 21. pygame.display.update()
  • 9. Pygame surfaces 1. yellow_surface = pygame.Surface((width, height)) 2. yellow_surface.fill(YELLOW) 3. green_surface = pygame.Surface((width/2, height/2)) 4. green_surface.fill(GREEN) 5. 6. def draw_circle(pos, radius=width/8): 7. pygame.draw.circle(yellow_surface, GREEN, pos, radius) 8. pygame.draw.circle(green_surface, YELLOW, pos, radius) 9. 10. while 1: 11. for event in pygame.event.get(): 12. if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: 13. move = True 14. elif event.type == pygame.MOUSEBUTTONUP and event.button == 1: 15. move = False 16. if move: 17. yellow_surface.fill(YELLOW) 18. green_surface.fill(GREEN) 19. pos = pygame.mouse.get_pos() 20. draw_circle(pos) 21. screen.blit(yellow_surface, (0, 0)) 22. screen.blit(green_surface, (0, 0)) 23. pygame.display.update()
  • 10. Pygame objects collision 1. static_rect = pygame.Rect((width/3, height/3), (width/3, height/3)) 2. moving_rect = pygame.Rect((width*2/3, height*2/3), (width/6, height/6)) 3. 4. def draw_rects(pos): 5. moving_rect.centerx, moving_rect.centery = pos 6. pygame.draw.rect(yellow_surface, GREEN, moving_rect) 7. if moving_rect.colliderect(static_rect): 8. pygame.draw.rect(yellow_surface, PURPLE, static_rect) 9. else: 10. pygame.draw.rect(yellow_surface, BLUE, static_rect) 11. 12. pos = (0, 0) 13. move = False 14. while 1: 15. for event in pygame.event.get(): 16. ... 17. if move: 18. yellow_surface.fill(YELLOW) 19. pos = pygame.mouse.get_pos() 20. draw_rects(pos) 21. screen.blit(yellow_surface, (0, 0)) 22. pygame.display.update()
  • 11. Pygame Sprites - create 1. class Ball(pygame.sprite.Sprite): 2. radius = 25 3. groups = [] 4. acceleration = 1 5. def __init__(self, pos): 6. pygame.sprite.Sprite.__init__(self, self.groups) 7. self.image = pygame.Surface((Ball.radius * 2, Ball.radius * 2)) 8. self.image.fill(YELLOW) 9. self.image.convert_alpha() 10. self.rect = self.image.get_rect() 11. self.radius = Ball.radius 12. self.velocity = 0 13. pygame.draw.circle(self.image, BLUE, self.rect.center, self.radius, 0) 14. self.rect.center = pos
  • 12. Pygame Sprites - move 1. class Ball(pygame.sprite.Sprite): 2. …. 3. def update(self): 4. if self.rect.top < height: # inside the screen? 5. self.rect.move_ip(0, self.velocity) 6. bricks = pygame.sprite.spritecollide(self, static, False) 7. if bricks: 8. brick = bricks[0] 9. self.rect.bottom = brick.rect.top # place the ball on top of the brick 10. self.velocity *= -0.9 # bounce with speed loss 11. if 0 > self.velocity > -0.1: # prevent infinite bounce 12. self.velocity = 0 13. else: 14. self.velocity += Ball.acceleration
  • 13. Pygame Sprites 1. allsprites = pygame.sprite.Group() 2. Ball.groups = allsprites 3. 4. static = pygame.sprite.Group() 5. Brick.groups = allsprites, static 6. 7. timer = pygame.time.Clock() 8. screen.blit(yellow_surface, (0, 0)) 9. while 1: 10. timer.tick(60) 11. for event in pygame.event.get(): 12. if event.type == pygame.QUIT: 13. raise SystemExit("QUIT") 14. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: 15. Ball(event.pos) 16. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3: 17. Brick(event.pos) 18. elif event.type == pygame.KEYDOWN and event.key == pygame.K_r: 19. allsprites.empty() 20. static.empty() 21. allsprites.clear(screen, yellow_surface) 22. allsprites.update() 23. allsprites.draw(screen) 24. pygame.display.update()
  • 14. Python Physics Engines - http://www.pymunk.org PyODE (Python bindings for The Open Dynamics Engine) pyBox2D - http://code.google.com/p/pybox2d Panda3D - http://www.panda3d.org
  • 15. Pymunk Physics Engine - Based on Chipmunk2D (http://chipmunk-physics.net) - Force & impulse - Collisions - Constraints - Pygame & Pyglet modules
  • 16. Pymunk basics 1. import pymunk as pm 2. 3. space = pm.Space() 4. space.gravity = (0, -900.0) 5. 6. class Ball(pm.Body): 7. def __init__(self, pos, mass=10, radius=25): 8. inertia = pm.moment_for_circle(mass, 0, radius, (0,0)) 9. pm.Body.__init__(self, mass, inertia) 10. self.position = pos 11. self.radius = radius 12. def get_shape(self, elasticity=0.9): 13. shape = pm.Circle(self, self.radius, (0,0)) 14. shape.elasticity = elasticity 15. return shape
  • 17. Pygame + Pymunk 1. while 1: 2. for event in pygame.event.get(): 3. if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: 4. body = Ball(to_pygame(event.pos)) 5. shape = body.get_shape() 6. space.add(body, shape) 7. balls.append(shape) 8. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3: 9. brick = Brick(to_pygame(event.pos)) 10. space.add(brick) 11. bricks.append(brick) 12. 13. ### Draw stuff here 14. 15. ### Update physics 16. fps = 60.0 17. space.step(1./fps) 18. 19. pygame.display.flip() 20. clock.tick(fps)
  • 18. Pyglet + No dependencies + Pythonic + Images, audio, video in any format - Not for games Cocos2d + Pyglet-based + For games, apps, GUI interaction - Poor API docs - Google does not help + OpenGL interface + Layers and scenes
  • 19. Pygame vs Pyglet 1. import pygame 2. 3. pygame.init() 4. screen = pygame.display.set_mode ((640,480)) 5. 6. while 1: 7. for event in pygame.event.get(): 8. if event.type == pygame.QUIT: 9. raise SystemExit("QUIT") 1. import pyglet 2. 3. screen = pyglet.window.Window (640,480) 4. 5. pyglet.app.run()
  • 20. Pygame vs Pyglet 1. import pygame 2. 3. pygame.init() 4. screen = pygame.display.set_mode((640,480)) 5. 6. while 1: 7. timer.tick(60) 8. for event in pygame.event.get(): 9. if event.type == pygame.KEYDOWN and event.key == pygame.K_a: 10. print 'You pressed A' 11. #update objects here 12. pygame.display.update() 1. import pyglet 2. from pyglet.window import key 3. 4. screen = pyglet.window.Window(640,480) 5. 6. @screen.event 7. def on_key_press(symbol, modifiers): 8. if symbol == key.A: 9. print 'You pressed A' 10. 11. def update(dt): 12. #update objects here 13. 14. pyglet.clock.schedule_interval(update, 1/60.0) 15. pyglet.app.run()
  • 23. FIFE - http://www.fifengine.net FIFE + Python = https://github.com/fifengine/python-tutorials “Unknown Horizons”“Zero-Projekt”
  • 24. Examples and games pyvolley (cocos2d и pymunk) - https://github. com/aikikode/pyvolley Minecraft на Python (pyglet, OpenGL) - https://github.com/boskee/Minecraft “Кораблики” (PyGame + Tiled) https://github. com/aikikode/nautili pyhammerfight (cocos2d и pymunk) - https://github. com/aikikode/pyhammerfight Исходники примеров презентации - https://bitbucket. org/aikikode/pygame_examples Презентация - http://bit.ly/1lNp0Uk
  • 25. Дополнительные источники Игры и библиотеки - https://wiki.python.org/moin/PythonGames Making games with Pygame - http://youtu.be/RQ-lIU0jWwg 2D game development using Pyglet with Cocos2d - http://video.kiberpipa. org/py_2d_and_3d_game_development_using_pyglet/