(require (planet "game.ss" ("kazzmir" "allegro.plt")))
game.ss provides some useful types and functions that remove much of the design work needed to create real-time games. Normally these games follow the same basic structure: execute a function to update the universe, draw the universe. The mechanism to do this is what game-loop gives you, but game.ss takes this a step further by providing the universe as well. All you must do is populate the universe with objects and you will have an instant game.
Of course with any framework game.ss forces you to follow one design. If you feel that this design is not what you need then roll your own. Once you understand how game.ss works its not terribly difficult to write a different version.
Look at "examples/simple.ss" in allegro.plt for a concrete example of how to use the game framework.
Basic :: class
Basic is the root object of all objects in the game universe. Technically it is a class as defined by
class.ss but for the most part you can ignore this if you are just using whats in game.ss. Basic has the following methods
phase - Affects the order of drawing. Lower numbers are drawn first and higher numbers are drawn later. This defaults to 0.
x - The x coordinate of this object. This is used for collision detection so do not provide your own x coordinate in your own objects.
y - The y coordinate, with the same restrictions as x.
(can-collide obj) :: boolean
Returns #t if this object can collide with
(shapes) :: list-of shape
Returns a list of shapes used for collision detection. An empty list means this object can't collide with anything and its
can-collide method should probably return #f for all objects.
(key world keys) :: void
This procedure is run when the user presses a key.
keys is a list of currently pressed keys and
world is the current universe.
(touch world obj) :: void
This object collided with obj and can now perform any side-affects.
(tick world) :: void
The main update procedure. This procedure is run by the universe when a logic cycle is occuring. Moving around the universe should be done here.
(draw world buffer) :: void
This procedure is run when the object is allowed to draw itself.
buffer is a a plain image( not the screen ).
(get-x) :: int
Returns the x coordinate of this object
(get-y) :: int
Returns the y coordinate of this object
World :: class
World is a special class that represents the universe. It derives from Basic. Normally you don't need to know about much of the internals of World but you would if you aren't using the predefined game-loop that is part of game.ss.
(get-width) :: number
Returns the current viewable width of the world.
(get-height) :: number
Returns the current viewable height of the world.
(get-depth) :: number
Returns the current bits per pixel used to display the world.
(get-mode) :: symbol
'FULLSCREEN representing what sort of graphics mode the world is using to be displayed.
(key keys) :: void
Let all the objects know about
keys through the
(add obj) :: void
Add an object to the internal list of objects.
(tick) :: void
tick on all the objects.
(draw buffer) :: void
draw on all the objects.
(remove obj) :: void
obj from the internal list of objects. All other objects in the list will receive the
death message if they have it defined.
(remove-all) :: void
Clear the list of internal objects.
(get-objects) :: list-of Basic
Returns the internal list of objects.
(get-object pred) :: Basic or #f
pred :: (lambda (obj) ..)
Return the first object that statisfies
(reset-collision) :: void
Reset the collision detection objects.
(collide) :: void
Tests all objects for collisions using a binary space partition. Only objects that live in the same binary space partition and both return #t from
can-collide will be tested for collisions.
Animation :: class
Animation encapsulates a set of images to be displayed.
speed :: int - The speed at which the animations change.
(add-animation image) :: void
Add an image to the list of images.
(draw buffer x y) :: void
Draw the current image onto
y specify the middle of the image, not the upper left hand corner.
(next-animation) :: void
Move the image to the next animation. This function should be called every logic cycle, not during the draw phase.
shape^ :: interface
shape^ is an interface that all shapes should implement. It has the following functions
(min-x) :: int - The left most x coordinate of this shape.
(max-x) :: int - The right most x coordinate of this shape.
(min-y) :: int - The top most y coordinate of this shape.
(max-y) :: int - The bottom most y coordinate of this shape.
(collide x y shape sx sy) :: boolean - Returns #t if this shape collides with
shape. The middle coordinates of this shape are
y. The middle coordinates of
(inside x y sx sy) :: boolean - Returns #t if the coordinates
sy lies inside this shape centered at
Shape :: class
Shape is the base type for all shapes, but it does not implement shape^. It has 2 fields that affect how it acts
center-x - The x offset used to calculate the absolute position of this shape. Defaults to 0.
center-y - The y offset used to calculate the absolute position of this shape. Defaults to 0.
A shape's absolute position is calculated by
center-y are not changed the coordinates reduce to simply
There are 3 predefined shapes provided by game.ss: Point, Circle, and Rectangle.
Point :: class
Point derives from Shape and represents a single point in space. It has no fields of its own so creation requires no extra arguments, unless you want to provide center-x and center-y.
(make Point) ;; a regular point (make Point (center-x 3) (center-y -2)) ;; a point offset by 3, -2
Circle :: class
Circle derives from Shape and represents a circular area in space. Its only field is radius.
(make Circle (radius 5)) ;; a circle with radius 5 (make Circle (radius 5) (center-x 2) (center-y -3)) ;; a circle with radius 5 offset by 2, -3
Rectangle :: class
Rectangle derives from Shape and represents a rectangular area in space. Its fields are width and height.
;; a rectangle with a width of 5 and a height of 10 (make Rectangle (width 5) (height 10)) ;; a rectangle with a width of 5 and a height of 10 offset by 2, -3 (make Rectangle (width 5) (height 10) (center-x 2) (center-y -3))
(round* float) :: int
float to an integer. This is useful to convert coordinates into values that can be passed to any drawing function.
(round* 2.3) -> 2 (round* 2.8) -> 3
(calculate-angle x1 y1 x2 y2) :: float
Calculate the angle from
y2 using the arc tangent. This angle is normalized so that 270 increases the y coordinate, which is farther "down" the screen.
(make-animation-from-files files speed) :: Animation
files :: list-of filename
speed :: int
Create an animation from the set of
(make-world [width height] [depth] [mode]) :: World
Create a new world. You can optionally give
width - Width of the screen. Default is 640.
height - Height of the screen. Default is 480.
depth - Bits per pixel. Default is 16
'FULLSCREEN. Make a window or use the entire screen. Defaults to
;; create a regular world (define world (make-world)) ;; use a window size of 800x600 (define world (make-world 800 600))
(add-object world obj) :: void
Helper function to add
(get-mouse-x) :: int
Return the current x coordinate of the mouse.
(get-mouse-y) :: int
Return the current y coordinate of the mouse.
A reference to the current object, much like
this in Java/C++ or
self in Ruby/Python.
Cosine angle :: float
Returns the cosine of angle, specified in degrees from 0-360.
Sine angle :: float
Returns the sine of an angle, specified in degrees from 0-360.
(left-clicking?) :: boolean
Returns #t if the left mouse button is being clicked.
(right-clicking?) :: boolean
Returns #t if the right mouse button is being clicked.
(get-mouse-movement) :: (values x y)
Returns the last movement of the mouse as an x,y pair. See get-mickeys for more details.
(constant id expression)
id to be
id cannot be mutated.
(define-object name (inherits ...) (vars ...) body ...)
Define a new object that derives from Basic. You can use this syntax if you do not want to create a class by hand.
(inherits ...) is a list of variables to inherit from Basic( x, y, and/or phase ).
(vars ...) is a list of variables private to this object.
body ... is any normal scheme expression.
To define methods use
define. Methods that should override methods in Basic will be handled automatically as long as they are declared in the form
(define (name ...) ...).
;; define an object that moves right as time goes on (define-object foo (x y) () (define (tick world) (set! x (add1 x))) (define (draw world buffer) (circle-fill buffer x y 4 (color 255 0 0))) )
When an object defined by
define-object is created a method
create is run immediately with no arguments. In this function you can initialize variables and do whatever else.
;; the example from above but set the radius in a variable (define-object foo (x y) (radius) (constant five 5) (define (create) (set! radius five)) (define (tick world) (set! x (add1 x))) (define (draw world buffer) (circle-fill buffer x y radius (color 255 0 0))) )
counter is set to 5 when the object is created. Also you can see a usage of constant.
(define-generator name (every expr proc))
define-generator defines an object that derives from Basic like define-object except a generator's sole purpose in life is to execute a function every time a certain amount of time has passed by. This is useful for adding objects to the universe in descrete steps.
(define-generator thing ;; add something every 10 ticks (every 10 (lambda (world) (add-object world (make something)))) ;; add something-else every 20 ticks (every 20 (lambda (world) (add-object world (make something-else)))) ;; add another at random (every (random 100) (lambda (world) (add-object world (make another)))) )
To use a generator, create one and add it to the world.
(add-object world (make thing))
(say obj method args ...)any
obj to perform a
method and pass
args to it. This works like normal method invocation except if
obj does not have a method named
method or accepts a different number of args then no function will be called. This can be a source of confusion as no warning or error will be printed, the method will just be silently ignored.
(define obj (make my-object)) ;; tell obj to say hello (say obj hello)
(is-a? obj class) :: boolean
Returns #t if obj has a type of
(make class args ...)object
Create a new object whose type is
args ... should be a list of name/value s-expressions which initialize some field of the object.
(define-object my-object (x y) (age) (void)) (make my-object (x 5) (y 10) (age 18))
(start world [before] [after]) :: void
before :: (lambda (world) ...)
after :: (lambda (world) ...)
Given a world object this method will create the graphics context and start the game. If given
before is executed immediately before the main game loop is executed and
after is executed after the game ends. These methods allow you to perform arbitrary initialization that you could not otherwise do before the
start method is called. I.e, you cannot call an image related function before
start becuase the graphics context does not exist yet.
;; define blue, but set it in the before method when it is ok to do so (define world (make-world)) (define blue #f) (start world (lambda (w) (set! blue (color 0 0 255))))