Introduction to Game programming with PyGame Part 1

5,048 views

Published on

A short intro to game programming at college :D

Published in: Technology

Introduction to Game programming with PyGame Part 1

  1. 1. Introduction to PyGame Abhishek Mishra hello@ideamonk.com
  2. 2. Agenda • Games - 2d games • Basics • Python • PyGame • Examples
  3. 3. Whats inside a Game? • Multidisciplinary Process • Graphics • Input Control • Game Logic / AI • Sound effects / Music • Communication • Physics, etc • Frameworks ^ Libraries ^^
  4. 4. Basics - Drawing • Drawing primitives • Pixels, Square, Rect, Ellipse, etc • Provided by development env
  5. 5. Basics - Animation • Draw things • Change their positon • Draw them again • Repeat
  6. 6. Basics - Animation • Draw things • Change their positon • Draw them again • Repeat
  7. 7. Basics - Animation • Draw things • Change their positon • Draw them again • Repeat
  8. 8. Basics - Animation • Draw things • Change their positon • Draw them again • Repeat
  9. 9. Basics - Animation • Draw things • Change their positon • Draw them again • Repeat
  10. 10. Basics - Surfaces • Drawing primitives use algorithms • Slow for repetitive work
  11. 11. Basics - Surfaces • Drawing primitives use algorithms • Slow for repetitive work
  12. 12. Basics - Surfaces • How do we save time?
  13. 13. Basics - Surfaces • How do we save time?
  14. 14. Basics - Surfaces • How do we save time? A Surface / Bitmap
  15. 15. Basics - Surfaces • How do we save time? RAM
  16. 16. Basics - Surfaces • How do we save time? RAM
  17. 17. Basics - Surfaces • Bitmaps • Rectangular • CPU Inexpensive • Can be layered
  18. 18. Basics - Surfaces • Bitmaps • Rectangular • CPU Inexpensive • Can be layered Sky layer Trees Enemies
  19. 19. Basics - Animation Again! • Monitors have refresh rate • Can’t draw so many surfaces on live screen • How do we make it smooth? • How do we sync?
  20. 20. Basics - Animation Again! • Draw on a buffer surface • Wait for vertical sync • Transfer the whole buffer to screen
  21. 21. Basics - Animation Again! • Draw on a buffer surface • Wait for vertical sync • Transfer the whole buffer to screen
  22. 22. Basics - Animation Again! • Draw on a buffer surface • Wait for vertical sync • Transfer the whole buffer to screen
  23. 23. Basics - Animation Again! • Draw on a buffer surface • Wait for vertical sync • Transfer the whole buffer to screen
  24. 24. Collision Detection
  25. 25. Collision Detection 2D Bound checks
  26. 26. Collision Detection Pixel Perfect http://wiki.allegro.cc/index.php?title=Pixel_Perfect_Collision
  27. 27. Ah! So many things to do?
  28. 28. Ah! So many things to do? Enter Frameworks / Engines/ Libraries & other angels
  29. 29. Programming • Lot of repetitive tasks • Lot of things you don’t wish to figure out • Technologies - OpenGL, DirectX, SDL • Interfacing Libraries • Generic set of solutions - frameworks • Complete solutions - Game Engines, toolsets
  30. 30. Programming • Many options to choose from - • DirectQB (old MSDOS days) • Allegro (C/C++, cross platform) • PyGame (Python, SDL) • PyGlet (Python, OpenGL) • XNA (Windows, XBox, dotNET, C#,VB) • DirectX (Windows specific,VC++, C# ...)
  31. 31. Programming • Many options to choose from - • DirectQB (old MSDOS days) • Allegro (C/C++, cross platform) • PyGame (Python, SDL) • PyGlet (Python, OpenGL) • XNA (Windows, XBox, dotNET, C#,VB) • DirectX (Windows specific,VC++, C# ...)
  32. 32. A Basic Game Loop Start While player is alive take input find collisions draw on buffer put everything on screen
  33. 33. A Basic Game Loop Start While player is alive take input find collisions draw on buffer put everything on screen
  34. 34. A Basic Game Loop Start While player is alive take input find collisions draw on buffer put everything on screen
  35. 35. A Basic Game Loop Start While player is alive take input find collisions draw on buffer put everything on screen
  36. 36. A Basic Game Loop Start While player is alive take input find collisions draw on buffer put everything on screen
  37. 37. A Basic Game Loop Start While player is alive take input find collisions draw on buffer put everything on screen
  38. 38. A Basic Game Loop Start While player is alive take input find collisions draw on buffer put everything on screen
  39. 39. What now? • An entertaining idea • A Programming Language • A Game programming framework • Some bells, whistles & decorations
  40. 40. Python • Dynamic, Interpreted, Interactive • Object Oriented • Easy to write, easy to read • Popular - education, prototyping, quick hacks, research, unlimited • Batteries included • From web to standalones
  41. 41. Python • Free • On many platforms (Unix, Linux, Windows, OS X, Symbian S60, Java, BeOS) • Lacks type declaration • Huge library of modules
  42. 42. Python • printf (“Hi %s”, name); print “Hi %s” % name • int x = 45; float y = 1.01 x = 45 y = 1.01 • int a[4] = {1,2,3,4} a = [1,2,3,4] a = [1,2,‘abhishek’, 4, 4.5]
  43. 43. Python Indentation if (name == ‘abc’): print “Yes” else: print “No”
  44. 44. Python Strings fruit = “Apple” fruit = ‘Apple’ fruit = “““ Apple and ‘apple” ””” fruit = ‘‘‘ foo bar ’’’ message = “Hello %s. Total is %d” % (name, total)
  45. 45. Python Lists l = [1,2,3, ‘foo’, 4.5] print l[3] foo l = [ [1,2,3], ‘a’, ‘b’, ‘c’ ] innerlist = l[0] print innerlist [1,2,3]
  46. 46. Python Dictionaries Associative key => value pairs d = { ‘name’ : ‘Ram’, ‘age’:45 } print d[‘name’] print d[‘age’] d[‘salary’] = 45000
  47. 47. Python Loops for (int x=0; x<10; x+=2) { // do something } for x in range(0,10,2): # do something
  48. 48. Python Loops L = [1,2,4,5,3,1] for i in L: print i 1 2 4 5 3 1
  49. 49. Python Functions def factorial( num ): if num==1: return 1 else: return num * factorial(num-1) print factorial(4) 24
  50. 50. Python Comments # single line comment “““ multi line ”””
  51. 51. Python Modules import math print math.pi
  52. 52. • Based on SDL (Simple Directmedia Layer) • Works on Windows, OSX, Linux, N900, etc • Big array of modules, does a lot to save time • http://pygame.org • $ sudo easy_install pygame
  53. 53. http://www.pygame.org/docs/ http://www.pygame.org/docs/ ref/examples.html
  54. 54. pygame.Color pygame.transform pygame.draw pygame.Rect pygame.Surface pygame.mouse pygame.image pygame.movie pygame.display pygame.camera pygame.time pygame.midi pygame.event pygame.mixer pygame.font ...
  55. 55. Code / Demo time
  56. 56. To be continued ...

×