introduction to game programming with pygame part 1

59
Introduction to PyGame Abhishek Mishra [email protected]

Upload: abhishek-mishra

Post on 08-Sep-2014

4.025 views

Category:

Technology


9 download

DESCRIPTION

A short intro to game programming at college :D

TRANSCRIPT

Page 1: Introduction to Game programming with PyGame Part 1

Introduction to PyGame

Abhishek Mishra [email protected]

Page 2: Introduction to Game programming with PyGame Part 1

Agenda

• Games - 2d games

• Basics

• Python

• PyGame

• Examples

Page 3: Introduction to Game programming with PyGame Part 1

Whats inside a Game?• Multidisciplinary Process

• Graphics

• Input Control

• Game Logic / AI

• Sound effects / Music

• Communication

• Physics, etc

• Frameworks ^ Libraries ^^

Page 4: Introduction to Game programming with PyGame Part 1

Basics - Drawing• Drawing primitives

• Pixels, Square, Rect, Ellipse, etc

• Provided by development env

Page 5: Introduction to Game programming with PyGame Part 1

Basics - Animation

• Draw things

• Change their positon

• Draw them again

• Repeat

Page 6: Introduction to Game programming with PyGame Part 1

Basics - Animation

• Draw things

• Change their positon

• Draw them again

• Repeat

Page 7: Introduction to Game programming with PyGame Part 1

Basics - Animation

• Draw things

• Change their positon

• Draw them again

• Repeat

Page 8: Introduction to Game programming with PyGame Part 1

Basics - Animation

• Draw things

• Change their positon

• Draw them again

• Repeat

Page 9: Introduction to Game programming with PyGame Part 1

Basics - Animation

• Draw things

• Change their positon

• Draw them again

• Repeat

Page 10: Introduction to Game programming with PyGame Part 1

Basics - Surfaces• Drawing primitives use algorithms

• Slow for repetitive work

Page 11: Introduction to Game programming with PyGame Part 1

Basics - Surfaces• Drawing primitives use algorithms

• Slow for repetitive work

Page 12: Introduction to Game programming with PyGame Part 1

Basics - Surfaces

• How do we save time?

Page 13: Introduction to Game programming with PyGame Part 1

Basics - Surfaces

• How do we save time?

Page 14: Introduction to Game programming with PyGame Part 1

Basics - Surfaces

• How do we save time?

A Surface / Bitmap

Page 15: Introduction to Game programming with PyGame Part 1

Basics - Surfaces

• How do we save time?

RAM

Page 16: Introduction to Game programming with PyGame Part 1

Basics - Surfaces

• How do we save time?

RAM

Page 17: Introduction to Game programming with PyGame Part 1

Basics - Surfaces

• Bitmaps

• Rectangular

• CPU Inexpensive

• Can be layered

Page 18: Introduction to Game programming with PyGame Part 1

Basics - Surfaces

• Bitmaps

• Rectangular

• CPU Inexpensive

• Can be layered

Sky layer

Trees

Enemies

Page 19: Introduction to Game programming with PyGame Part 1

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?

Page 20: Introduction to Game programming with PyGame Part 1

Basics - Animation Again!• Draw on a buffer surface

• Wait for vertical sync

• Transfer the whole buffer to screen

Page 21: Introduction to Game programming with PyGame Part 1

Basics - Animation Again!• Draw on a buffer surface

• Wait for vertical sync

• Transfer the whole buffer to screen

Page 22: Introduction to Game programming with PyGame Part 1

Basics - Animation Again!• Draw on a buffer surface

• Wait for vertical sync

• Transfer the whole buffer to screen

Page 23: Introduction to Game programming with PyGame Part 1

Basics - Animation Again!• Draw on a buffer surface

• Wait for vertical sync

• Transfer the whole buffer to screen

Page 24: Introduction to Game programming with PyGame Part 1

Collision Detection

Page 25: Introduction to Game programming with PyGame Part 1

Collision Detection

2D Bound checks

Page 26: Introduction to Game programming with PyGame Part 1

Collision Detection

Pixel Perfecthttp://wiki.allegro.cc/index.php?title=Pixel_Perfect_Collision

Page 27: Introduction to Game programming with PyGame Part 1
Page 28: Introduction to Game programming with PyGame Part 1

Ah! So many things to do?

Page 29: Introduction to Game programming with PyGame Part 1

Ah! So many things to do?

Enter Frameworks / Engines/ Libraries

& other angels

Page 30: Introduction to Game programming with PyGame Part 1

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

Page 31: Introduction to Game programming with PyGame Part 1

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# ...)

Page 32: Introduction to Game programming with PyGame Part 1

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# ...)

Page 33: Introduction to Game programming with PyGame Part 1

A Basic Game LoopStart

take input

find collisions

While player is alive

draw on buffer

put everything on screen

Page 34: Introduction to Game programming with PyGame Part 1

A Basic Game LoopStart

take input

find collisions

While player is alive

draw on buffer

put everything on screen

Page 35: Introduction to Game programming with PyGame Part 1

A Basic Game LoopStart

take input

find collisions

While player is alive

draw on buffer

put everything on screen

Page 36: Introduction to Game programming with PyGame Part 1

A Basic Game LoopStart

take input

find collisions

While player is alive

draw on buffer

put everything on screen

Page 37: Introduction to Game programming with PyGame Part 1

A Basic Game LoopStart

take input

find collisions

While player is alive

draw on buffer

put everything on screen

Page 38: Introduction to Game programming with PyGame Part 1

A Basic Game LoopStart

take input

find collisions

While player is alive

draw on buffer

put everything on screen

Page 39: Introduction to Game programming with PyGame Part 1

A Basic Game LoopStart

take input

find collisions

While player is alive

draw on buffer

put everything on screen

Page 40: Introduction to Game programming with PyGame Part 1

What now?

• An entertaining idea

• A Programming Language

• A Game programming framework

• Some bells, whistles & decorations

Page 41: Introduction to Game programming with PyGame Part 1

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

Page 42: Introduction to Game programming with PyGame Part 1

Python

• Free

• On many platforms (Unix, Linux, Windows, OS X, Symbian S60, Java, BeOS)

• Lacks type declaration

• Huge library of modules

Page 43: Introduction to Game programming with PyGame Part 1

Python

• printf (“Hi %s”, name);print “Hi %s” % name

• int x = 45; float y = 1.01x = 45 y = 1.01

• int a[4] = {1,2,3,4}a = [1,2,3,4]a = [1,2,‘abhishek’, 4, 4.5]

Page 44: Introduction to Game programming with PyGame Part 1

Python

Indentation

if (name == ‘abc’): print “Yes”else: print “No”

Page 45: Introduction to Game programming with PyGame Part 1

PythonStrings

fruit = “Apple”

fruit = ‘Apple’

fruit = “““ Apple and ‘apple” ”””

fruit = ‘‘‘ foo bar ’’’

message = “Hello %s. Total is %d” % (name, total)

Page 46: Introduction to Game programming with PyGame Part 1

PythonLists

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]

Page 47: Introduction to Game programming with PyGame Part 1

PythonDictionaries

Associative key => value pairs

d = { ‘name’ : ‘Ram’, ‘age’:45 }

print d[‘name’]print d[‘age’]

d[‘salary’] = 45000

Page 48: Introduction to Game programming with PyGame Part 1

Python

Loops

for (int x=0; x<10; x+=2) { // do something }

for x in range(0,10,2): # do something

Page 49: Introduction to Game programming with PyGame Part 1

PythonLoops

L = [1,2,4,5,3,1]

for i in L: print i124531

Page 50: Introduction to Game programming with PyGame Part 1

PythonFunctions

def factorial( num ): if num==1: return 1 else: return num * factorial(num-1)

print factorial(4)

24

Page 51: Introduction to Game programming with PyGame Part 1

Python

Comments

# single line comment

“““ multi line ”””

Page 52: Introduction to Game programming with PyGame Part 1

Python

Modules

import mathprint math.pi

Page 53: Introduction to Game programming with PyGame Part 1

• 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

Page 54: Introduction to Game programming with PyGame Part 1
Page 56: Introduction to Game programming with PyGame Part 1
Page 57: Introduction to Game programming with PyGame Part 1

pygame.Color

pygame.Rect pygame.Surface

pygame.camera

pygame.draw

pygame.display

pygame.font

pygame.image

pygame.event

pygame.midi

pygame.mixer

pygame.mouse

pygame.moviepygame.time

pygame.transform

...

Page 58: Introduction to Game programming with PyGame Part 1

Code / Demo time

Page 59: Introduction to Game programming with PyGame Part 1

To be continued ...