SlideShare a Scribd company logo
1 of 47
Download to read offline
Game Programming
      LCA 2010
     Richard Jones
The Basics
• Displaying something
• Controlling animation
• User input
• Gameplay mechanics
• Playing sound effects and music
• Further stuff
Displaying Something

• Opening a window
• Reading images
• Displaying images
Controlling Animation

• Image position on screen
• Updates over time
• Accounting for frame rate
User Input


• Getting events
• Distinct keyboard events vs. keyboard state
Gameplay Mechanics


• Interaction of game objects
• Detecting important events (game won or
  game over)
Detecting Collisions

• Pixel-Perfect
• Axis-Aligned Bounding Box
• Circle-Circle
• Hash Map
Sound


• Reading sound files
• Playing sounds and background music
Opening a Window

 import pyglet

 w = pyglet.window.Window()

 pyglet.app.run()
Clearing the Window
  import pyglet

  w = pyglet.window.Window()

  @w.event
  def on_draw():
     w.clear()

  pyglet.app.run()
Drawing
import pyglet

w = pyglet.window.Window()

ship_image = pyglet.image.load('data/ship.png')

@w.event
def on_draw():
   w.clear()
   ship_image.blit(100, 100)

pyglet.app.run()
Better Drawing
import pyglet

w = pyglet.window.Window()

ship_image = pyglet.image.load('data/ship.png')
ship = pyglet.sprite.Sprite(ship_image)
ship.position = (100, 100)

@w.event
def on_draw():
   w.clear()
   ship_image.blit(100, 100)
   ship.draw()

pyglet.app.run()
Animation
...
   w.clear()
   ship.draw()

def update(dt):
    ship.x += 1

pyglet.clock.schedule_interval(update, 1./30)

pyglet.app.run()
Better Animation
 ...
    w.clear()
    ship.draw()

 def update(dt):
     ship.x += 1
     ship.x += 100 * dt

 pyglet.clock.schedule_interval(update, 1./30)

 pyglet.app.run()
Adding Asteroids
 import random
 import pyglet

 w = pyglet.window.Window()

 def center_anchor(img):
     img.anchor_x = img.width / 2
     img.anchor_y = img.height / 2

 ...
Adding Asteroids
...
ship_image = pyglet.image.load('data/ship.png')
center_anchor(ship_image)
ship = pyglet.sprite.Sprite(ship_image)
ship.position = (100, 100)

big_asteroid_image = pyglet.image.load('data/big_asteroid.png')
center_anchor(big_asteroid_image)
asteroids = []
for i in range(3):
    x = random.randint(0, w.width)
    y = random.randint(0, w.height)
    s = pyglet.sprite.Sprite(big_asteroid_image, x, y)
    s.dx = random.randint(-100, 100)
    s.dy = random.randint(-100, 100)
    asteroids.append(s)

@w.event
...
Adding Asteroids

   ...
   @w.event
   def on_draw():
       w.clear()
       for asteroid in asteroids:
           asteroid.draw()
       ship.draw()
   ...
Adding Asteroids

  ...
  def update(dt):
      ship.x += 100 * dt
      for asteroid in asteroids:
          asteroid.x += asteroid.dx*dt
          asteroid.y += asteroid.dy*dt
  ...
A Little Refactoring

   ...
   ship = pyglet.sprite.Sprite(ship_image)
   ship.position = (100, 100)
   ship.dx = 100
   ship.dy = 0

   ...
Screen Wrapping
  ...
  def update(dt):
      ship.x += 100 * dt
      for asteroid in asteroids:
          asteroid.x += asteroid.dx*dt
          asteroid.y += asteroid.dy*dt
      for a in asteroids + [ship]:
          a.x += a.dx*dt
          a.y += a.dy*dt
          if a.x - a.width/2 > w.width:
              a.x -= w.width + a.width
          elif a.x + a.width/2 < 0:
              a.x += w.width + a.width
          if a.y - a.height/2 > w.height:
              a.y -= w.height + a.height
          elif a.y + a.height/2 < 0:
              a.y += w.height + a.height
  ...
Control

import math
import random
import pyglet

w = pyglet.window.Window()

...
Control
...
keys = pyglet.window.key.KeyStateHandler()
w.push_handlers(keys)

def update(dt):
    if keys[pyglet.window.key.LEFT]:
        ship.rotation -= 360*dt
    if keys[pyglet.window.key.RIGHT]:
        ship.rotation += 360*dt
    rotation = math.radians(ship.rotation)
    rotation_x = math.cos(-rotation)
    rotation_y = math.sin(-rotation)
    if keys[pyglet.window.key.UP]:
        ship.dx += 200 * rotation_x * dt
        ship.dy += 200 * rotation_y * dt

   for a in asteroids + [ship]:
   ...
Rotation

...
ship = pyglet.sprite.Sprite(ship_image)
ship.position = (100, 100)
ship.dx = 100
ship.dy = 0
ship.position = (w.width/2, w.height/2)
ship.dx = ship.dy = ship.dr = 0

...
Rotation

      ...
      s = pyglet.sprite.Sprite(big_asteroid_image, x, y)
      s.dx = random.randint(-100, 100)
      s.dy = random.randint(-100, 100)
      s.dr = random.randint(-100, 100)
      asteroids.append(s)

...
Rotation
...
def update(dt):
    if keys[pyglet.window.key.LEFT]:
        ship.rotation -= 360*dt
    if keys[pyglet.window.key.RIGHT]:
        ship.rotation += 360*dt
    ship.dr = (keys[pyglet.window.key.RIGHT] - keys[pyglet.window.key.LEFT]) * 360
    rotation = math.pi * ship.rotation / 180.0
    ...
    for a in asteroids + [ship]:
        a.x += a.dx*dt
        a.y += a.dy*dt
        a.rotation += a.dr * dt
        if a.x - a.width/2 > w.width:
            a.x -= w.width + a.width
Collision Detection
Collision Detection
Collision Detection
Collision Detection
Collision Detection

       distance
Collision Detection

  import   sys
  import   math
  import   random
  import   pyglet

  w = pyglet.window.Window()
  ...
Collision Detection

 ...
 w.push_handlers(keys)

 def distance(a, b):
     return math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2)

 def update(dt):
     ...
Collision Detection

       ...
       elif a.y + a.height/2 < 0:
           a.y += w.height + a.height

   for a in asteroids:
       if distance(a, ship) < (a.width/2 + ship.width/2):
           sys.exit('GAME OVER')

pyglet.clock.schedule_interval(update, 1./30)
...
Shooting

ship.position = (w.width/2, w.height/2)
ship.dx = ship.dy = ship.dr = 0
ship.gun_cooldown = 0

bullet_image = pyglet.image.load('data/bullet.png')
center_anchor(bullet_image)
bullets = []

...
...
             Shooting
    ship.dy += 200 * rotation_y * dt

if ship.gun_cooldown:
    ship.gun_cooldown = max(0, ship.gun_cooldown - dt)
elif keys[pyglet.window.key.SPACE] and len(bullets) < 2:
    b = pyglet.sprite.Sprite(bullet_image, ship.x, ship.y)
    b.dx = rotation_x * 500
    b.dy = rotation_y * 500
    b.dr = 0
    b.life = 1
    bullets.append(b)
    ship.gun_cooldown = .5

for b in list(bullets):
    b.life -= dt
    if b.life < 0:
        bullets.remove(b)

for a in asteroids + [ship]:
for a in asteroids + [ship] + bullets:
     a.x += a.dx*dt
     ...
Shooting

...
@w.event
def on_draw():
    w.clear()
    for asteroid in asteroids:
         asteroid.draw()
    for s in asteroids + bullets:
        s.draw()
    ship.draw()
...
Shooting ... and Hitting

   ...
   def distance(a, b):
       return math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2)
   def collide(a, b):
       return distance(a, b) < (a.width/2 + b.width/2)
   ...
Shooting ... and Hitting
        ...
        elif a.y + a.height/2 < 0:
            a.y += w.height + a.height

    for a in asteroids:
        if distance(a, ship) < (a.width/2 + ship.width/2):
            sys.exit('GAME OVER')
    for a in list(asteroids):
        if collide(a, ship):
            sys.exit('GAME OVER')
        for b in list(bullets):
            if collide(a, b):
                bullets.remove(b)
                asteroids.remove(a)

    if not asteroids:
        sys.exit('YOU WIN')

 pyglet.clock.schedule_interval(update, 1./30)
 ...
Chunks

...
center_anchor(bullet_image)
bullets = []

small_asteroid_image = pyglet.image.load('data/small_asteroid.png')
center_anchor(small_asteroid_image)
medium_asteroid_image = pyglet.image.load('data/medium_asteroid.png')
center_anchor(medium_asteroid_image)
big_asteroid_image = pyglet.image.load('data/big_asteroid.png')
center_anchor(big_asteroid_image)
...
...
                          Chunks
   for b in list(bullets):
       if collide(a, b):
           bullets.remove(b)
           asteroids.remove(a)
           if a.image is big_asteroid_image.texture:
               for i in range(2):
                   s = pyglet.sprite.Sprite(medium_asteroid_image, a.x, a.y)
                   s.dx = a.dx + random.randint(-50, 50)
                   s.dy = a.dy + random.randint(-50, 50)
                   s.dr = a.dr + random.randint(-50, 50)
                   asteroids.append(s)
           elif a.image is medium_asteroid_image.texture:
               for i in range(2):
                   s = pyglet.sprite.Sprite(small_asteroid_image, a.x, a.y)
                   s.dx = a.dx + random.randint(-50, 50)
                   s.dy = a.dy + random.randint(-50, 50)
                   s.dr = a.dr + random.randint(-50, 50)
                   asteroids.append(s)

if not asteroids:
    sys.exit('YOU WIN')
...
Sound Effects

...
      asteroids.append(s)

explosion_sound = pyglet.media.load('data/explosion.wav', streaming=False)
bullet_sound = pyglet.media.load('data/bullet.wav', streaming=False)

@w.event
def on_draw():
...
Sound Effects

     ...
     bullets.append(b)
     ship.gun_cooldown = .5
     bullet_sound.play()

  for b in list(bullets):
  ...
Sound Effects

...
bullets.remove(b)
asteroids.remove(a)
explosion_sound.play()
if a.image is big_asteroid_image.texture:
Further Stuff

• Opening screen
• Options menu
• Game won / game over screen
• Special effects
Other screen / menu


• Use libraries like Cocos2d (Scenes, Layers,
  Sprites, Animations)
Special Effects


• Simple image animations
• Use libraries like lepton
Where To From Here?

• http://pyglet.org
• http://pygame.org
• http://los-cocos.org
• http://inventwithpython.com/
• http://pyweek.org

More Related Content

What's hot

The Ring programming language version 1.2 book - Part 35 of 84
The Ring programming language version 1.2 book - Part 35 of 84The Ring programming language version 1.2 book - Part 35 of 84
The Ring programming language version 1.2 book - Part 35 of 84Mahmoud Samir Fayed
 
Mobile Game and Application with J2ME - Collision Detection
Mobile Gameand Application withJ2ME  - Collision DetectionMobile Gameand Application withJ2ME  - Collision Detection
Mobile Game and Application with J2ME - Collision DetectionJenchoke Tachagomain
 
Mobile Game and Application with J2ME
Mobile Gameand Application with J2MEMobile Gameand Application with J2ME
Mobile Game and Application with J2MEJenchoke Tachagomain
 
The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.5 book - Part 9 of 31
The Ring programming language version 1.5 book - Part 9 of 31The Ring programming language version 1.5 book - Part 9 of 31
The Ring programming language version 1.5 book - Part 9 of 31Mahmoud Samir Fayed
 
Александр Зимин – Анимация как средство самовыражения
Александр Зимин – Анимация как средство самовыраженияАлександр Зимин – Анимация как средство самовыражения
Александр Зимин – Анимация как средство самовыраженияCocoaHeads
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDXJussi Pohjolainen
 
λ | Lenses
λ | Lensesλ | Lenses
λ | LensesOpen-IT
 
The Ring programming language version 1.2 book - Part 38 of 84
The Ring programming language version 1.2 book - Part 38 of 84The Ring programming language version 1.2 book - Part 38 of 84
The Ring programming language version 1.2 book - Part 38 of 84Mahmoud Samir Fayed
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationJussi Pohjolainen
 
Expand/Collapse animation on Android
Expand/Collapse animation on AndroidExpand/Collapse animation on Android
Expand/Collapse animation on AndroidPavlo Dudka
 
The Ring programming language version 1.8 book - Part 56 of 202
The Ring programming language version 1.8 book - Part 56 of 202The Ring programming language version 1.8 book - Part 56 of 202
The Ring programming language version 1.8 book - Part 56 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
 
Snake report ROHIT MALAV
Snake report ROHIT MALAVSnake report ROHIT MALAV
Snake report ROHIT MALAVRohit malav
 

What's hot (20)

The Ring programming language version 1.2 book - Part 35 of 84
The Ring programming language version 1.2 book - Part 35 of 84The Ring programming language version 1.2 book - Part 35 of 84
The Ring programming language version 1.2 book - Part 35 of 84
 
Mobile Game and Application with J2ME - Collision Detection
Mobile Gameand Application withJ2ME  - Collision DetectionMobile Gameand Application withJ2ME  - Collision Detection
Mobile Game and Application with J2ME - Collision Detection
 
Mobile Game and Application with J2ME
Mobile Gameand Application with J2MEMobile Gameand Application with J2ME
Mobile Game and Application with J2ME
 
The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180
 
pptuni1
pptuni1pptuni1
pptuni1
 
libGDX: Tiled Maps
libGDX: Tiled MapslibGDX: Tiled Maps
libGDX: Tiled Maps
 
The Ring programming language version 1.5 book - Part 9 of 31
The Ring programming language version 1.5 book - Part 9 of 31The Ring programming language version 1.5 book - Part 9 of 31
The Ring programming language version 1.5 book - Part 9 of 31
 
Box2D and libGDX
Box2D and libGDXBox2D and libGDX
Box2D and libGDX
 
Александр Зимин – Анимация как средство самовыражения
Александр Зимин – Анимация как средство самовыраженияАлександр Зимин – Анимация как средство самовыражения
Александр Зимин – Анимация как средство самовыражения
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDX
 
ES6(ES2015) is beautiful
ES6(ES2015) is beautifulES6(ES2015) is beautiful
ES6(ES2015) is beautiful
 
λ | Lenses
λ | Lensesλ | Lenses
λ | Lenses
 
Proga 0622
Proga 0622Proga 0622
Proga 0622
 
The Ring programming language version 1.2 book - Part 38 of 84
The Ring programming language version 1.2 book - Part 38 of 84The Ring programming language version 1.2 book - Part 38 of 84
The Ring programming language version 1.2 book - Part 38 of 84
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame Animation
 
Expand/Collapse animation on Android
Expand/Collapse animation on AndroidExpand/Collapse animation on Android
Expand/Collapse animation on Android
 
Beyond Scala Lens
Beyond Scala LensBeyond Scala Lens
Beyond Scala Lens
 
The Ring programming language version 1.8 book - Part 56 of 202
The Ring programming language version 1.8 book - Part 56 of 202The Ring programming language version 1.8 book - Part 56 of 202
The Ring programming language version 1.8 book - Part 56 of 202
 
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
 
Snake report ROHIT MALAV
Snake report ROHIT MALAVSnake report ROHIT MALAV
Snake report ROHIT MALAV
 

Similar to Introduction to Game Programming Tutorial

You are task to add a yawning detection to the programme below;i.pdf
You are task to add a yawning detection to the programme below;i.pdfYou are task to add a yawning detection to the programme below;i.pdf
You are task to add a yawning detection to the programme below;i.pdfsales223546
 
imager package in R and examples..
imager package in R and examples..imager package in R and examples..
imager package in R and examples..Dr. Volkan OBAN
 
Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Jay Coskey
 
The Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 SeasonsThe Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 SeasonsBaruch Sadogursky
 
Creating an Uber Clone - Part IV.pdf
Creating an Uber Clone - Part IV.pdfCreating an Uber Clone - Part IV.pdf
Creating an Uber Clone - Part IV.pdfShaiAlmog1
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsKandarp Tiwari
 
circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docx
circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docxcirc.db.dbcircleserver(1).py#!usrlocalbinpython3im.docx
circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docxchristinemaritza
 
Class Customization and Better Code
Class Customization and Better CodeClass Customization and Better Code
Class Customization and Better CodeStronnics
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfarihantmobileselepun
 
Creating an Uber Clone - Part XXII.pdf
Creating an Uber Clone - Part XXII.pdfCreating an Uber Clone - Part XXII.pdf
Creating an Uber Clone - Part XXII.pdfShaiAlmog1
 
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...Revolution Analytics
 
The Ring programming language version 1.10 book - Part 64 of 212
The Ring programming language version 1.10 book - Part 64 of 212The Ring programming language version 1.10 book - Part 64 of 212
The Ring programming language version 1.10 book - Part 64 of 212Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 57 of 196
The Ring programming language version 1.7 book - Part 57 of 196The Ring programming language version 1.7 book - Part 57 of 196
The Ring programming language version 1.7 book - Part 57 of 196Mahmoud Samir Fayed
 
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docxxy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docxericbrooks84875
 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions Dr. Volkan OBAN
 

Similar to Introduction to Game Programming Tutorial (20)

My favorite slides
My favorite slidesMy favorite slides
My favorite slides
 
You are task to add a yawning detection to the programme below;i.pdf
You are task to add a yawning detection to the programme below;i.pdfYou are task to add a yawning detection to the programme below;i.pdf
You are task to add a yawning detection to the programme below;i.pdf
 
imager package in R and examples..
imager package in R and examples..imager package in R and examples..
imager package in R and examples..
 
Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3
 
662305 LAB13
662305 LAB13662305 LAB13
662305 LAB13
 
The Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 SeasonsThe Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 Seasons
 
Creating an Uber Clone - Part IV.pdf
Creating an Uber Clone - Part IV.pdfCreating an Uber Clone - Part IV.pdf
Creating an Uber Clone - Part IV.pdf
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docx
circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docxcirc.db.dbcircleserver(1).py#!usrlocalbinpython3im.docx
circ.db.dbcircleserver(1).py#!usrlocalbinpython3im.docx
 
Python From Scratch (1).pdf
Python From Scratch  (1).pdfPython From Scratch  (1).pdf
Python From Scratch (1).pdf
 
Class Customization and Better Code
Class Customization and Better CodeClass Customization and Better Code
Class Customization and Better Code
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
 
Game dev 101 part 3
Game dev 101 part 3Game dev 101 part 3
Game dev 101 part 3
 
Creating an Uber Clone - Part XXII.pdf
Creating an Uber Clone - Part XXII.pdfCreating an Uber Clone - Part XXII.pdf
Creating an Uber Clone - Part XXII.pdf
 
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
 
The Ring programming language version 1.10 book - Part 64 of 212
The Ring programming language version 1.10 book - Part 64 of 212The Ring programming language version 1.10 book - Part 64 of 212
The Ring programming language version 1.10 book - Part 64 of 212
 
The Ring programming language version 1.7 book - Part 57 of 196
The Ring programming language version 1.7 book - Part 57 of 196The Ring programming language version 1.7 book - Part 57 of 196
The Ring programming language version 1.7 book - Part 57 of 196
 
Supstat nyc subway
Supstat nyc subwaySupstat nyc subway
Supstat nyc subway
 
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docxxy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
xy2.5 3.0 3.5-1.0 6 7 81.0 0 1 23.0 -6 -5 .docx
 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions
 

More from Richard Jones

Introduction to Game Programming
Introduction to Game ProgrammingIntroduction to Game Programming
Introduction to Game ProgrammingRichard Jones
 
Message Queueing - by an MQ noob
Message Queueing - by an MQ noobMessage Queueing - by an MQ noob
Message Queueing - by an MQ noobRichard Jones
 
Web micro-framework BATTLE!
Web micro-framework BATTLE!Web micro-framework BATTLE!
Web micro-framework BATTLE!Richard Jones
 
State of Python (2010)
State of Python (2010)State of Python (2010)
State of Python (2010)Richard Jones
 
What's New In Python 2.6
What's New In Python 2.6What's New In Python 2.6
What's New In Python 2.6Richard Jones
 
What's New In Python 2.5
What's New In Python 2.5What's New In Python 2.5
What's New In Python 2.5Richard Jones
 
What's New In Python 2.4
What's New In Python 2.4What's New In Python 2.4
What's New In Python 2.4Richard Jones
 
Tkinter Does Not Suck
Tkinter Does Not SuckTkinter Does Not Suck
Tkinter Does Not SuckRichard Jones
 

More from Richard Jones (11)

Angboard
AngboardAngboard
Angboard
 
Don't do this
Don't do thisDon't do this
Don't do this
 
Introduction to Game Programming
Introduction to Game ProgrammingIntroduction to Game Programming
Introduction to Game Programming
 
Message Queueing - by an MQ noob
Message Queueing - by an MQ noobMessage Queueing - by an MQ noob
Message Queueing - by an MQ noob
 
Message queueing
Message queueingMessage queueing
Message queueing
 
Web micro-framework BATTLE!
Web micro-framework BATTLE!Web micro-framework BATTLE!
Web micro-framework BATTLE!
 
State of Python (2010)
State of Python (2010)State of Python (2010)
State of Python (2010)
 
What's New In Python 2.6
What's New In Python 2.6What's New In Python 2.6
What's New In Python 2.6
 
What's New In Python 2.5
What's New In Python 2.5What's New In Python 2.5
What's New In Python 2.5
 
What's New In Python 2.4
What's New In Python 2.4What's New In Python 2.4
What's New In Python 2.4
 
Tkinter Does Not Suck
Tkinter Does Not SuckTkinter Does Not Suck
Tkinter Does Not Suck
 

Recently uploaded

Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfAarwolf Industries LLC
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 

Recently uploaded (20)

Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdf
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 

Introduction to Game Programming Tutorial

  • 1. Game Programming LCA 2010 Richard Jones
  • 2. The Basics • Displaying something • Controlling animation • User input • Gameplay mechanics • Playing sound effects and music • Further stuff
  • 3. Displaying Something • Opening a window • Reading images • Displaying images
  • 4. Controlling Animation • Image position on screen • Updates over time • Accounting for frame rate
  • 5. User Input • Getting events • Distinct keyboard events vs. keyboard state
  • 6. Gameplay Mechanics • Interaction of game objects • Detecting important events (game won or game over)
  • 7. Detecting Collisions • Pixel-Perfect • Axis-Aligned Bounding Box • Circle-Circle • Hash Map
  • 8. Sound • Reading sound files • Playing sounds and background music
  • 9. Opening a Window import pyglet w = pyglet.window.Window() pyglet.app.run()
  • 10. Clearing the Window import pyglet w = pyglet.window.Window() @w.event def on_draw(): w.clear() pyglet.app.run()
  • 11. Drawing import pyglet w = pyglet.window.Window() ship_image = pyglet.image.load('data/ship.png') @w.event def on_draw(): w.clear() ship_image.blit(100, 100) pyglet.app.run()
  • 12. Better Drawing import pyglet w = pyglet.window.Window() ship_image = pyglet.image.load('data/ship.png') ship = pyglet.sprite.Sprite(ship_image) ship.position = (100, 100) @w.event def on_draw(): w.clear() ship_image.blit(100, 100) ship.draw() pyglet.app.run()
  • 13. Animation ... w.clear() ship.draw() def update(dt): ship.x += 1 pyglet.clock.schedule_interval(update, 1./30) pyglet.app.run()
  • 14. Better Animation ... w.clear() ship.draw() def update(dt): ship.x += 1 ship.x += 100 * dt pyglet.clock.schedule_interval(update, 1./30) pyglet.app.run()
  • 15. Adding Asteroids import random import pyglet w = pyglet.window.Window() def center_anchor(img): img.anchor_x = img.width / 2 img.anchor_y = img.height / 2 ...
  • 16. Adding Asteroids ... ship_image = pyglet.image.load('data/ship.png') center_anchor(ship_image) ship = pyglet.sprite.Sprite(ship_image) ship.position = (100, 100) big_asteroid_image = pyglet.image.load('data/big_asteroid.png') center_anchor(big_asteroid_image) asteroids = [] for i in range(3): x = random.randint(0, w.width) y = random.randint(0, w.height) s = pyglet.sprite.Sprite(big_asteroid_image, x, y) s.dx = random.randint(-100, 100) s.dy = random.randint(-100, 100) asteroids.append(s) @w.event ...
  • 17. Adding Asteroids ... @w.event def on_draw(): w.clear() for asteroid in asteroids: asteroid.draw() ship.draw() ...
  • 18. Adding Asteroids ... def update(dt): ship.x += 100 * dt for asteroid in asteroids: asteroid.x += asteroid.dx*dt asteroid.y += asteroid.dy*dt ...
  • 19. A Little Refactoring ... ship = pyglet.sprite.Sprite(ship_image) ship.position = (100, 100) ship.dx = 100 ship.dy = 0 ...
  • 20. Screen Wrapping ... def update(dt): ship.x += 100 * dt for asteroid in asteroids: asteroid.x += asteroid.dx*dt asteroid.y += asteroid.dy*dt for a in asteroids + [ship]: a.x += a.dx*dt a.y += a.dy*dt if a.x - a.width/2 > w.width: a.x -= w.width + a.width elif a.x + a.width/2 < 0: a.x += w.width + a.width if a.y - a.height/2 > w.height: a.y -= w.height + a.height elif a.y + a.height/2 < 0: a.y += w.height + a.height ...
  • 21. Control import math import random import pyglet w = pyglet.window.Window() ...
  • 22. Control ... keys = pyglet.window.key.KeyStateHandler() w.push_handlers(keys) def update(dt): if keys[pyglet.window.key.LEFT]: ship.rotation -= 360*dt if keys[pyglet.window.key.RIGHT]: ship.rotation += 360*dt rotation = math.radians(ship.rotation) rotation_x = math.cos(-rotation) rotation_y = math.sin(-rotation) if keys[pyglet.window.key.UP]: ship.dx += 200 * rotation_x * dt ship.dy += 200 * rotation_y * dt for a in asteroids + [ship]: ...
  • 23. Rotation ... ship = pyglet.sprite.Sprite(ship_image) ship.position = (100, 100) ship.dx = 100 ship.dy = 0 ship.position = (w.width/2, w.height/2) ship.dx = ship.dy = ship.dr = 0 ...
  • 24. Rotation ... s = pyglet.sprite.Sprite(big_asteroid_image, x, y) s.dx = random.randint(-100, 100) s.dy = random.randint(-100, 100) s.dr = random.randint(-100, 100) asteroids.append(s) ...
  • 25. Rotation ... def update(dt): if keys[pyglet.window.key.LEFT]: ship.rotation -= 360*dt if keys[pyglet.window.key.RIGHT]: ship.rotation += 360*dt ship.dr = (keys[pyglet.window.key.RIGHT] - keys[pyglet.window.key.LEFT]) * 360 rotation = math.pi * ship.rotation / 180.0 ... for a in asteroids + [ship]: a.x += a.dx*dt a.y += a.dy*dt a.rotation += a.dr * dt if a.x - a.width/2 > w.width: a.x -= w.width + a.width
  • 31. Collision Detection import sys import math import random import pyglet w = pyglet.window.Window() ...
  • 32. Collision Detection ... w.push_handlers(keys) def distance(a, b): return math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2) def update(dt): ...
  • 33. Collision Detection ... elif a.y + a.height/2 < 0: a.y += w.height + a.height for a in asteroids: if distance(a, ship) < (a.width/2 + ship.width/2): sys.exit('GAME OVER') pyglet.clock.schedule_interval(update, 1./30) ...
  • 34. Shooting ship.position = (w.width/2, w.height/2) ship.dx = ship.dy = ship.dr = 0 ship.gun_cooldown = 0 bullet_image = pyglet.image.load('data/bullet.png') center_anchor(bullet_image) bullets = [] ...
  • 35. ... Shooting ship.dy += 200 * rotation_y * dt if ship.gun_cooldown: ship.gun_cooldown = max(0, ship.gun_cooldown - dt) elif keys[pyglet.window.key.SPACE] and len(bullets) < 2: b = pyglet.sprite.Sprite(bullet_image, ship.x, ship.y) b.dx = rotation_x * 500 b.dy = rotation_y * 500 b.dr = 0 b.life = 1 bullets.append(b) ship.gun_cooldown = .5 for b in list(bullets): b.life -= dt if b.life < 0: bullets.remove(b) for a in asteroids + [ship]: for a in asteroids + [ship] + bullets: a.x += a.dx*dt ...
  • 36. Shooting ... @w.event def on_draw(): w.clear() for asteroid in asteroids: asteroid.draw() for s in asteroids + bullets: s.draw() ship.draw() ...
  • 37. Shooting ... and Hitting ... def distance(a, b): return math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2) def collide(a, b): return distance(a, b) < (a.width/2 + b.width/2) ...
  • 38. Shooting ... and Hitting ... elif a.y + a.height/2 < 0: a.y += w.height + a.height for a in asteroids: if distance(a, ship) < (a.width/2 + ship.width/2): sys.exit('GAME OVER') for a in list(asteroids): if collide(a, ship): sys.exit('GAME OVER') for b in list(bullets): if collide(a, b): bullets.remove(b) asteroids.remove(a) if not asteroids: sys.exit('YOU WIN') pyglet.clock.schedule_interval(update, 1./30) ...
  • 39. Chunks ... center_anchor(bullet_image) bullets = [] small_asteroid_image = pyglet.image.load('data/small_asteroid.png') center_anchor(small_asteroid_image) medium_asteroid_image = pyglet.image.load('data/medium_asteroid.png') center_anchor(medium_asteroid_image) big_asteroid_image = pyglet.image.load('data/big_asteroid.png') center_anchor(big_asteroid_image) ...
  • 40. ... Chunks for b in list(bullets): if collide(a, b): bullets.remove(b) asteroids.remove(a) if a.image is big_asteroid_image.texture: for i in range(2): s = pyglet.sprite.Sprite(medium_asteroid_image, a.x, a.y) s.dx = a.dx + random.randint(-50, 50) s.dy = a.dy + random.randint(-50, 50) s.dr = a.dr + random.randint(-50, 50) asteroids.append(s) elif a.image is medium_asteroid_image.texture: for i in range(2): s = pyglet.sprite.Sprite(small_asteroid_image, a.x, a.y) s.dx = a.dx + random.randint(-50, 50) s.dy = a.dy + random.randint(-50, 50) s.dr = a.dr + random.randint(-50, 50) asteroids.append(s) if not asteroids: sys.exit('YOU WIN') ...
  • 41. Sound Effects ... asteroids.append(s) explosion_sound = pyglet.media.load('data/explosion.wav', streaming=False) bullet_sound = pyglet.media.load('data/bullet.wav', streaming=False) @w.event def on_draw(): ...
  • 42. Sound Effects ... bullets.append(b) ship.gun_cooldown = .5 bullet_sound.play() for b in list(bullets): ...
  • 44. Further Stuff • Opening screen • Options menu • Game won / game over screen • Special effects
  • 45. Other screen / menu • Use libraries like Cocos2d (Scenes, Layers, Sprites, Animations)
  • 46. Special Effects • Simple image animations • Use libraries like lepton
  • 47. Where To From Here? • http://pyglet.org • http://pygame.org • http://los-cocos.org • http://inventwithpython.com/ • http://pyweek.org

Editor's Notes

  1. r1
  2. r2
  3. r3
  4. r4
  5. r5
  6. r6
  7. r7
  8. r8
  9. r9
  10. r10
  11. r11
  12. r12
  13. r13
  14. r15
  15. asteroids-cocos.py