SlideShare a Scribd company logo
1 of 47
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 ļ¬les
ā€¢ 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
Ā 
ES6(ES2015) is beautiful
ES6(ES2015) is beautifulES6(ES2015) is beautiful
ES6(ES2015) is beautifulmonikagupta18jan
Ā 
Ī» | 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

My favorite slides
My favorite slidesMy favorite slides
My favorite slidesMitchell Wand
Ā 
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
Ā 
Python From Scratch (1).pdf
Python From Scratch  (1).pdfPython From Scratch  (1).pdf
Python From Scratch (1).pdfNeerajChauhan697157
Ā 
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
Ā 
Supstat nyc subway
Supstat nyc subwaySupstat nyc subway
Supstat nyc subwayVivian S. Zhang
Ā 
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
Ā 
Message queueing
Message queueingMessage queueing
Message queueingRichard 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

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
Ā 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel AraĆŗjo
Ā 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
Ā 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
Ā 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
Ā 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
Ā 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
Ā 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
Ā 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
Ā 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
Ā 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
Ā 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
Ā 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
Ā 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
Ā 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜RTylerCroy
Ā 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
Ā 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
Ā 

Recently uploaded (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
Ā 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
Ā 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Ā 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
Ā 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
Ā 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Ā 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
Ā 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Ā 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Ā 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Ā 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Ā 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
Ā 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
Ā 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
Ā 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Ā 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜
Ā 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
Ā 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
Ā 

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 ļ¬les ā€¢ 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