Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
(The little gamer)
Christian “C-Keen” Kellermann
2012-08-26 @ FrOSCon’s quasiconf 2012
(the little gamer) C-Keen @ FrOSCon 2012 1/37
Why program games?
I Definition of game:“game noun: an amusement or pastime:children’s games.” – webster’s dictionary
I Games are known to ease learning
I So why not learn a language and have fun at the sametime?
(the little gamer) C-Keen @ FrOSCon 2012 2/37
First steps
I ASCIII Most programming introductions will tell you how to
write text-based gamesI They can get pretty fancyI Unless you are a roguelike fan (like I am) or fond of text
adventures, this will not hold attention spanI older machines, like the C64 did better
I I want picturesI So why don’t you get graphics in these books?I Graphical IO (i.e. modern HW access) is complicated!
(the little gamer) C-Keen @ FrOSCon 2012 3/37
SDL / GL / Allegro / WTF / BBQ
I Game “engines” and “frameworks” come by the dozen
I Different entry levels
I Many programming languages available
I Too much detailI Advanced language features
(the little gamer) C-Keen @ FrOSCon 2012 4/37
Available frameworks (a small selection)
I SDLI Allegro
I Processing
I LÖVEI How to design worlds (TeachScheme now!)
I (Your favourite one is probably missing. . . )
(the little gamer) C-Keen @ FrOSCon 2012 5/37
SDL
I This has been used by many many (more or less) populargames
I Fast, reliable, etc.I Very low level
I Game loop needs to be implemented
(the little gamer) C-Keen @ FrOSCon 2012 6/37
Allegro
I Looks bigger than SDL
I Featurer complete, huge
I Supports Joysticks, Gamepads, Storage, Audio
I Game loop needs to be implemented
(the little gamer) C-Keen @ FrOSCon 2012 7/37
Processing
I Not really for games but visualisations
I Pseudo C like language + IDE
I Distribution is easy: Javascript, Java applet
I Lots of things have to be written from scratch
(the little gamer) C-Keen @ FrOSCon 2012 8/37
LÖVE
I Written in LUAI Moderately big communityI Lots of new gamesI Easy to get startedI Easy to distribute games
(the little gamer) C-Keen @ FrOSCon 2012 9/37
How to design worlds
I Not really a game framework but a way of teachingprogramming
I Uses Racket as baseI Very nice emphasis on graphics
I Get the book for free (CC-BY-NC-SA 3.0) athttp://world.cs.brown.edu/1/htdw-v1.pdf
I Usage outside of classes unknown to me
I The exercises are fun to reprogram if you are bored.(“Why did the chicken cross the road”. . . )
(the little gamer) C-Keen @ FrOSCon 2012 10/37
My favourite: CHICKEN Scheme
I A practical and portable Scheme system:http://call-cc.org
I Mailing lists: chicken-users, chicken-hackers
I Friendly helpers online: #chicken on freenode
I Comes with hundreds (> 500) extensions (“eggs”)
I Easy FFI to C/C++
(the little gamer) C-Keen @ FrOSCon 2012 11/37
Your options with CHICKEN Scheme
I GraphicsI SDL, OpenGL, GLUTI Cairo GraphicsI AllegroI simple-graphicsI doodle
I SoundI OpenALI libsdlmixer
I Physics enginesI Box2DI Chipmunk
(the little gamer) C-Keen @ FrOSCon 2012 12/37
Doodle
I My attempt to “just open a window for graphics”
I Goal: Find a convenient API / language / method forwriting games
I 2D only
I Geared at the curious beginner and casual gameprogrammer (like I am)
I Usable for games and visualisations, graphs, L-Systems,Turtlegraphics. . .
I A work in progress, still looking for the golden path. . .
(the little gamer) C-Keen @ FrOSCon 2012 13/37
Example L-Systems(define-l-system grass "F" ( "F" -> "F[+F]F[-F]F"))(draw-system grass 6 x: center-y y: h angle: -90 angle-step: 25.7 step-width: 2)(save-screenshot "grass.png")
(the little gamer) C-Keen @ FrOSCon 2012 14/37
Example Mouse events
(the little gamer) C-Keen @ FrOSCon 2012 15/37
Example Tile set
(the little gamer) C-Keen @ FrOSCon 2012 16/37
SpritesI Not there yet, I am not sure of the API, demo:
(the little gamer) C-Keen @ FrOSCon 2012 17/37
Basic structure
(use doodle)(new-doodle) ;; opens a window(define red ’(1 0 0 0.3)) ;; some nice transparent red(filled-circle (/ doodle-width 2)
(/ doodle-height 2))(show!);; If you want to save stuff(save-screenshot "circle.png")
The easy way for visualisations:I open a window
I draw things
I show windowI For games with interaction we have a game loop. . .
(the little gamer) C-Keen @ FrOSCon 2012 18/37
Open the window
(new-doodle #!key (width 680) (height 460)(title "Doodle") (background solid-black)(fullscreen #f))
I Adjust to your own needs
I If background is a string, the file by that name will getloaded
(the little gamer) C-Keen @ FrOSCon 2012 19/37
“The world” – Game loop
(new-doodle)(world-inits (lambda () ...))(world-changes (lambda (events dt exit-continuation) ...)(world-ends (lambda () ...)(run-event-loop)
I Modelled after LÖVE and Processing, thanks!
I The “world” has three stages world-inits, world-changes,world-exits
I You can implement the ones you need
I Start the game loop with run-event-loop
I Those are (scheme) parameters, so you can reset themdynamically in the interpreter
(the little gamer) C-Keen @ FrOSCon 2012 20/37
Drawing primitives
I LinesI CirclesI Rectangles
I Text (True Type Fonts)
I Image blitting
(the little gamer) C-Keen @ FrOSCon 2012 21/37
An interactive example
(use matchable doodle)(define *paint* #f)(define red ’(1 0 0 0.3))
(world-changes(lambda (events dt exit)
(for-each(lambda (e)
(match e((’mouse ’pressed x y 1)(set! *paint* #t)(filled-circle x y 10 red))
((’mouse ’released x y 1)(set! *paint* #f))
((’mouse ’moved x y)(when *paint*
(filled-circle x y 10 red)))((’key ’pressed #\esc)(exit #t))
(else (void))))events)))
(new-doodle title: "Doodle paint" background: solid-white)(run-event-loop)
(the little gamer) C-Keen @ FrOSCon 2012 22/37
Beesteroids
I Heavily influenced by Andy Diver’s CL version:https://github.com/andyhd/asteroids
I Ships, Rockets, Space!
I Gets unnecessary cruel later in the game
I Was done in 333 lines of code (sloccount)
I A tiny portion of the code has to do with actual graphicsor sound!
(the little gamer) C-Keen @ FrOSCon 2012 23/37
Beesteroids Screenshot
(the little gamer) C-Keen @ FrOSCon 2012 24/37
(Poor) Design Choices
I No object system is used but regular scheme records
I All drawable objects are things
I All things have a velocity and a direction
I Vectors and points are just lists
I Things are drawn as polygons, which points are storedand transformed
I Objects should wrap around the screen when they leave,but only whole objects are drawn
(the little gamer) C-Keen @ FrOSCon 2012 25/37
The things: Like. . . a ship
(define (make-ship #!key (angle (random 360))heading(pos center)(radius 30)velocity(speed 0)(thrust 0))
(let* ((h (or heading angle))(nose (radial-point-from pos radius h))(left (radial-point-from pos radius (- h 130)))(right (radial-point-from pos radius (+ h 130)))(tail (radial-point-from pos (round (* radius 0.5)) (+ h 180)))(velocity (or velocity
(map - (radial-point-from pos speed angle) pos))))(make-thing ’ship pos velocity h thrust radius (list nose left tail right))))
(the little gamer) C-Keen @ FrOSCon 2012 26/37
Drawing things. . .
;; Draws a polygon from a list of points(define (draw-poly points)
(unless (null? points)(let loop ((ps (cdr points))
(last (car points)))(cond ((null? ps) (line last (car points)))
(else (line last (car ps))(loop (cdr ps) (car ps)))))))
;; This draws the polygon of a thing(define (draw-thing thing)
(draw-poly (thing-edges thing))(when *debugging*
(let ((pos (thing-pos thing)))(circle (first pos) (second pos) (* 2 (thing-bounding-radius thing)) ’(1 0 0 1))(line pos (map + (map (cut * 100 <>) (thing-velocity thing)) pos)))))
;; Draw all things(define (draw-things . list-of-things)
(for-each (lambda (things)(for-each draw-thing things))
list-of-things))
(the little gamer) C-Keen @ FrOSCon 2012 27/37
Dodging Bullets: Movement
;; Move one thing around(define (move-thing dt t)
(let* ((pos (thing-pos t))(thrust (* dt (thing-thrust t)))(heading (thing-heading t))(velocity (thing-velocity t))(thrust-vec (map - (radial-point-from pos thrust heading) pos))(scaled-vel (map (cut * 100 <> dt) (map + velocity thrust-vec)))(new-vel (map + velocity thrust-vec)))
(thing-pos-set! t (map + pos scaled-vel))(thing-edges-set! t (map (lambda (e)
(map + e scaled-vel))(thing-edges t)))
(thing-velocity-set! t new-vel)))
;; Move objects around(define (move-things time-delta . list-of-things)
(for-each(lambda (things)
(for-each(lambda (t)
(move-thing time-delta t))things))
list-of-things))
(the little gamer) C-Keen @ FrOSCon 2012 28/37
All the things belong somewhere. . .
The world object holds asteroids, bullets, our ship andeverything the game rules need.(define-record world
paused?levellivesscorebullets-leftshieldbulletsasteroidsship)
(the little gamer) C-Keen @ FrOSCon 2012 29/37
Hits and misses: Collision detection
Works on the world, as it influences world state such as lives,score etc.(define (check-collisions world)
(let ((bullets (world-bullets world))(ship (world-ship world))(asteroids (world-asteroids world)))
(cond ((find (cut collides? ship <>) asteroids) =>(lambda (a)
(if (zero? (world-shield world))(ship-wrecked)(begin (world-asteroids-set! world (delete a asteroids))
(asteroid-destroyed a))))))(for-each(lambda (b)
(cond ((find (cut collides? b <>) asteroids) =>(lambda (a)
(world-asteroids-set! world (delete a asteroids))(world-bullets-set! world (delete b bullets))(world-bullets-left-set! *w*
(add1 (world-bullets-left *w*)))(asteroid-destroyed a)))))
bullets)))
(the little gamer) C-Keen @ FrOSCon 2012 30/37
Putting it together
DEMO!
(the little gamer) C-Keen @ FrOSCon 2012 31/37
Need it simpler?
I The simple-graphics extension by Alaric Snell-Pym givesyou turtle graphics
I In a REPL!I Easy to save pictures
I Approved by real children
(the little gamer) C-Keen @ FrOSCon 2012 32/37
Adding sound
I OpenAL bindins and SDL mixer bindings available
I Beesteroids uses sdl-mixerI The sdl-mixer extension is an attempt to make the API
more scheme likeI There is one piece of music and there are samples, you
can play them all at the same time(open-audio)(set! *thrust-sound* (load-sample "assets/thrusters.wav"))(set! *firing-sound* (load-sample "assets/firing-rocket.wav"))(set! *explosion-sound* (load-sample "assets/explosion.wav"))(set! *background-music* (load-music "assets/MyVeryOwnDeadShip.ogg"))(play-music *background-music*)(music-finished (lambda () (play-music *background-music*))); meanwhile somewhere else...(when (< 0 bullets-left)
(play-sample *firing-sound* channel: 2 duration: 1000)); much later...(close-audio)
(the little gamer) C-Keen @ FrOSCon 2012 33/37
Lessons learned so far during game sprints
I I always come back to a OO like model of the world nomatter how hard I resist
I UI handling is not hard but cumbersome
I I am not there yet
(the little gamer) C-Keen @ FrOSCon 2012 34/37
Things I would like to work on in the future
I Better collision detection support in doodle
I Direct access to the graphics engine w/o cairo
I (Optional) integration of sound
(the little gamer) C-Keen @ FrOSCon 2012 35/37
What to take home
I Game programming is easy if your language helps you
I Programming games in a lisp is fun
I There are options for evey skill level in CHICKEN Scheme
(the little gamer) C-Keen @ FrOSCon 2012 36/37
Where to go from here?
I Find these slides on the froscon pages or at:http://pestilenz.org/~ckeen/little-gamer.pdf
I More doodle examples on my blog:http://pestilenz.org/~ckeen/blog/
I Grab beesteroids and enhance it, play it, make it yourown:$ git clone [email protected]:ckeen/beesteroids.git
I Flamewars criticisms, thoughts, questions can be directedto me also via
I C-Keen on IRC (drop by in #chicken on freenode)I @MinorGC on twitterI [email protected]
(the little gamer) C-Keen @ FrOSCon 2012 37/37