Game Utilities
(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.
Function List
add-object
constant
define-generator
define-object
get-mouse-movement
get-mouse-x
get-mouse-y
is-a?
left-clicking?
make-animation-from-files
make-world
make
me
right-clicking?
round*
say
start
Structure List
Animation
Basic
shape^
Shape
Point
Circle
Rectangle
World
top
Basic :: class
Basic is the root object of all objects in the game universe. Technically it is a class as defined by class*
in 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
Fieldsphase
- 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.
Functions
procedure: (can-collide obj) :: boolean
Returns #t if this object can collide with obj
.
procedure: (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.
procedure: (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.
procedure: (touch world obj) :: void
This object collided with obj and can now perform any side-affects.
procedure: (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.
procedure: (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 ).
procedure: (get-x) :: int
Returns the x coordinate of this object
procedure: (get-y) :: int
Returns the y coordinate of this object
top
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.
Functions
procedure: (get-width) :: number
Returns the current viewable width of the world.
procedure: (get-height) :: number
Returns the current viewable height of the world.
procedure: (get-depth) :: number
Returns the current bits per pixel used to display the world.
procedure: (get-mode) :: symbol
Returns either 'WINDOWED
or 'FULLSCREEN
representing what sort of graphics mode the world is using to be displayed.
procedure: (key keys) :: void
Let all the objects know about keys
through the key
method.
procedure: (add obj) :: void
Add an object to the internal list of objects.
procedure: (tick) :: void
Call tick
on all the objects.
procedure: (draw buffer) :: void
Call draw
on all the objects.
procedure: (remove obj) :: void
Remove obj
from the internal list of objects. All other objects in the list will receive the death
message if they have it defined.
procedure: (remove-all) :: void
Clear the list of internal objects.
procedure: (get-objects) :: list-of Basic
Returns the internal list of objects.
procedure: (get-object pred) :: Basic or #f
pred :: (lambda (obj) ..)
Return the first object that statisfies pred
.
procedure: (reset-collision) :: void
Reset the collision detection objects.
procedure: (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.
top
Animation :: class
Animation encapsulates a set of images to be displayed.
Fields
speed :: int
- The speed at which the animations change.
Functions
procedure: (add-animation image) :: void
Add an image to the list of images.
procedure: (draw buffer x y) :: void
Draw the current image onto buffer
. x
, y
specify the middle of the image, not the upper left hand corner.
procedure: (next-animation) :: void
Move the image to the next animation. This function should be called every logic cycle, not during the draw phase.
top
shape^ :: interface
shape^
is an interface that all shapes should implement. It has the following functions
procedure: (min-x) :: int
- The left most x coordinate of this shape.
procedure: (max-x) :: int
- The right most x coordinate of this shape.
procedure: (min-y) :: int
- The top most y coordinate of this shape.
procedure: (max-y) :: int
- The bottom most y coordinate of this shape.
procedure: (collide x y shape sx sy) :: boolean
- Returns #t if this shape collides with shape
. The middle coordinates of this shape are x
and y
. The middle coordinates of shape
are sx
and sy
.
procedure: (inside x y sx sy) :: boolean
- Returns #t if the coordinates sx
, sy
lies inside this shape centered at x
, y
.
top
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 object-x
+ center-x
, object-y
+ center-y
. If center-x
and center-y
are not changed the coordinates reduce to simply object-x
, object-y
.
There are 3 predefined shapes provided by game.ss: Point, Circle, and Rectangle.
top
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
top
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
top
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))
top
procedure: (round* float) :: int
Round 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
top
procedure: (calculate-angle x1 y1 x2 y2) :: float
Calculate the angle from x1
,y1
to x2
,y2
using the arc tangent. This angle is normalized so that 270 increases the y coordinate, which is farther "down" the screen.
top
procedure: (make-animation-from-files files speed) :: Animation
files :: list-of filename
speed :: int
Create an animation from the set of files
.
top
procedure: (make-world [width height] [depth] [mode]) :: World
Create a new world. You can optionally give width
, height
, depth
, and mode
.
width
- Width of the screen. Default is 640.height
- Height of the screen. Default is 480.depth
- Bits per pixel. Default is 16mode
- 'WINDOWED
or 'FULLSCREEN
. Make a window or use the entire screen. Defaults to 'WINDOWED
.
;; create a regular world (define world (make-world)) ;; use a window size of 800x600 (define world (make-world 800 600))
top
procedure: (add-object world obj) :: void
Helper function to add obj
to world
.
top
procedure: (get-mouse-x) :: int
Return the current x coordinate of the mouse.
top
procedure: (get-mouse-y) :: int
Return the current y coordinate of the mouse.
top
syntax: me
A reference to the current object, much like this
in Java/C++ or self
in Ruby/Python.
top
procedure: Cosine angle :: float
Returns the cosine of angle, specified in degrees from 0-360.
top
procedure: Sine angle :: float
Returns the sine of an angle, specified in degrees from 0-360.
top
procedure: (left-clicking?) :: boolean
Returns #t if the left mouse button is being clicked.
top
procedure: (right-clicking?) :: boolean
Returns #t if the right mouse button is being clicked.
top
procedure: (get-mouse-movement) :: (values x y)
Returns the last movement of the mouse as an x,y pair. See get-mickeys for more details.
top
syntax: (constant id expression)
Define id
to be expression
and id
cannot be mutated.
top
syntax: (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))) )
Here counter
is set to 5 when the object is created. Also you can see a usage of constant.
top
syntax: (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))
top
syntax: (say obj method args ...)
any
Ask 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)
top
procedure: (is-a? obj class) :: boolean
Returns #t if obj has a type of class
.
top
syntax: (make class args ...)
object
Create a new object whose type is class
. 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))
top
procedure: (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))))