Taylor

Made for Games

Cheat Sheet

This is a lovely little overview of the basics of Taylor, if you’re looking for the full documentation head on over to here.

Index

Window

# Opens a window with width 800 pixels, height 480 pixels,
# and "Taylor Game" in the title.
Window.open(width: 800, height: 480, title: "Taylor Game")

# Wrap all your drawing methods in this block.
Window.draw do
  # Do stuff
end

# Clears the screen with black.
# defaults to a nice white if no colour is specified.
Window.clear(colour: Colour::BLACK)

# Closes the window.
Window.close

# Set the framerate we"re trying to achieve.
Window.target_frame_rate(120)

# Get the current framerate.
Window.frame_rate
# => 120

# Get the amount of time since last frame, also known as the delta.
Window.frame_time
# => 8.333333333333334

Audio

# Sets up the audio device so we can play music and sounds.
Audio.open

# Sets the master volume to 80%.
Audio.volume = 0.8

# Closes the audio device.
# No more music or sound can be played after this.
Audio.close

Keyboard Input

# Has the A key been pressed since last frame?
Key.pressed?(KEY::A)
# => true

# Has the W key been released since last frame?
Key.released?(Key::W)
# => false

# Is the space bar being pressed?
Key.down?(Key::SPACE)
# => true

# Is the enter key not being pressed?
Key.up?(Key::ENTER)
# => false

# Returns the key code since last called.
# Call more than once if there"s a queue.
Key.pressed
# => KEY_S

# Returns the character code since last called.
# Call more than once if there"s a queue.
Key.pressed_character
# => "d"

Mouse Input

# Has the left mouse button been pressed since last frame?
Mouse.pressed?(Mouse::LEFT)
# => true

# Has the right mouse button been released since last frame?
Mouse.released?(Mouse::RIGHT)
# => false

# Is the middle mouse button being pressed?
Mouse.down?(Mouse::MIDDLE)
# => true

# Is the middle mouse button not being pressed?
Mouse.up?(Mouse.MIDDLE)
# => false

# Returns a vector with the current position of the mouse cursor.
Mouse.position
# => Vector2[25, 50]

# Sets the mouse cursor position to 25, 50.
Mouse.position = Vector2[25, 50]

# Gets how much the mouse wheel has moved.
Mouse.wheel_moved
# => Vector2[-0.25, 0]

Touch Input

# Returns a vector with the touch position for that index.
Touch.position_for(0)
# => Vector2[25, 50]

Gamepad Input

# Is the gamepad at the specified index available?
Gamepad[0].available?
# => true

# Gets the name of the gamepad at the specified index.
Gamepad[0].name
# => "My Cool Controller"

# Has the top right side button (EG: Y or triangle)
# been pressed since last frame?
Gamepad[0].pressed?(Gamepad::Button::UP)
# => false

# Has the left trigger been released since last frame?
Gamepad[0]released?(Gamepad::Trigger::LEFT_1)
# => true

# Is the middle right button (EG: start) being held down?
Gamepa[0]down?(Gamepad::Button::MIDDLE_RIGHT)
# => false

# Is the left thumbstick not being pressed?
Gampead[0].up?(Gamepad::Button::LEFT_JOYSTICK)
# => true

# How many joysticks does the specified gamepad have?
Gamepad[0].axis_count
# => 2

# Get the movement from the left joystick for the specified gamepad.
Gamepad[0].axis(0)
# => Vector2[-0.5, 0.3]

Vector2

# Creates a vector with at position 20, 30
vector = Vector2[20, 30]

# Where on the x axis is the vector in pixels?
vector.x
# => 20

# Where on the y axis is the vector in pixels?
vector.y
# => 30

Rectangle

# Creates a new rectangle at position 10, 20 with width 50,
# height 80, and in green.
rectangle = Rectangle.new(
  x: 10, y: 20,
  width: 50, height: 80,
  colour: Colour::GREEN,
)

# Where on the x axis is the rectangle in pixels?
rectangle.x
# => 10

# Where on the y axis is the rectangle in pixels?
rectangle.y
# => 20

# How many pixels wide is the rectangle?
rectangle.width
# => 50

# How many pixels tall is the rectangle?
rectangle.height
# => 80

# Draw the rectangle in green.
rectangle.draw

# Draw the outline of the rectangle in red.
Rectangle.new(
  x: 10, y: 20,
  width: 50, height: 80,
  colour: Colour::GREEN,
  outline: Colour::RED, thickness: 2,
).draw

# Draw the rectangle in green with rounded corners.
Rectangle.new(
  x: 10, y: 20,
  width: 50, height: 80,
  colour: Colour::GREEN,
  roundness: 0.25
).draw

Texture2D

# Load the texture from ./assets/cool.png.
texture = Texture2D.new("./assets/cool.png")

# How many pixels wide is the texture?
texture.width
# => 32

# How many pixels tall is the texture?
texture.height
# => 64

# Draw the texture to the Rectangle's position and size.
texture.draw(
  destination: Rectangle[32, 64, 8, 8],
)

# Draw the section of texture defined by source to
# the destination's Rectangle position and size.
texture.draw(
  source: Rectangle[16, 16, 8, 8],
  destination: Rectangle[32, 64, 8, 8],
)

# Draw the texture rotated 90 degrees clockwise.
texture.draw(
  destination: Rectangle[32, 64, 8, 8],
  rotation: 90,
)

# Generates mipmaps for the texture.
texture.generate_mipmaps

# When drawing the texture, use the bilinear filter.
texture.filter = Texture::BILINEAR

# Unload the texture from memory, you will no longer be able to draw it.
texture.unload

Image

# Load the image from ./assets/cool.png.
image = Image.new("./assets/cool.png")

# Generates a 128x64 image full of white pixels.
image = Image.generate(width: 128, height: 64, colour: Colour::WHITE)

# How many pixels wide is the image?
image.width
# => 128

# How many pixels tall is the image?
image.height
# => 64

# Saves the image to output.png.
image.export("./output.png")

# Copies the specified region into a new image.
# If source is not defined it copies the whole image.
different_image = image.copy(source: Rectangle.new(0,0,10,10))

# Converts the image to a Texture2D
texture = image.to_texture

# Scales the image to be the specified size.
image.resize!(width: 256, height: 128)

# Crops the image to the specified Rectangle.
image.crop!(Rectangle.new(0,0,64,32))

# Applies the specified alpha mask to the image.
image.alpha_mask = Image.new("./alpha.png")

# Flips the image horizontally.
image.flip_horizontal!

# Flips the image vertically.
image.flip_vertical!

# Rotate clock wise
image.rotate_clockwise!

# Rotate counter clock wise
image.rotate_counter_clockwise!

# Tints the image with the specified colour.
image.tint!(Colour::BLUE)

# Inverts all the colours in the image.
image.invert!

# Changes all the colours in the image to grayscale.
image.grayscale!

# Changes the contrast in the image, must be between -100 and 100.
image.contrast!(10)

# Changes the brightness in the image, must be between -255 and 255.
image.brightness!(50)

# Changes all GREEN in the image to BLUE.
image.replace!(from: Colour::GREEN, to: Colour::BLUE)

# Unload the image from memory.
image.unload

Font

# Load the font from ./assets/neat.ttf.
font = Font.new("./assets/neat.ttf")

# When drawing the font, use this filter.
font.filter = Texture::BILINEAR

# Draws "Hello" at the vector position at size 16 and red.
font.draw("Hello", position: vector, size: 16, colour: Colour::RED)

# Returns a Vector2 with the width and height of the rendered
# text as x and y respectively.
font.measure("Hello", size: 16)
# => Vector2[80, 16]

# Returns an image of "G'day" in size 16 font and violet.
image = font.to_image("G'day", size: 16, colour: Colour::VIOLET)

# Unloads the font from memory. It can no longer be used to draw text.
font.unload

Music

# Load the music from ./assets/cool_song.mp3.
music = Music.open("./assets/cool_song.mp3")

# Starts the playback of the music.
music.play

# Updates the music stream, this must be called every game loop.
music.update

# Stops the music playing, you need to call music.play to start it again.
music.stop

# Pauses the music from playing.
# You need to call music.resume to start it again.
music.pause

# Resumes paused music.
music.resume

# Is the music currently playing?
music.playing?
# => true

# How long is the music?
music.length
# => 120.0

# How long has the music been playing?
music.played
# => 30.5

# Sets the volume of the music, must be between 0 and 1 inclusive.
music.volume = 0.8

# Sets the pitch of the music.
music.pitch = 1.2

# Unloads the music from memory. It can no longer be played.
music.unload

Sound

# Load the sound from ./assets/jump.mp3.
sound = Sound.new("./assets/jump.mp3")

# Starts the playback of the sound.
sound.play

# Sets the volume of the sound, must be between 0 and 1 inclusive.
sound.volume = 0.8

# Sets the pitch of the sound.
sound.pitch = 1.2

# Stops all currently playing sounds.
Sound.stop

# How many sounds are currently playing?
Sound.playing
# => 3

# Unloads the sound from memory. It can no longer be played.
sound.unload

Camera2D

# Creates a new camera object with no rotation and no zoom.
camera = Camera2D.new(
  target: Vector2[0, 0],
  offset: Vector2[0, 0],
  rotation: 0,
  zoom: 1,
)

# Sets the camera target to be 160 pixels along the x axis.
camera.target.x = 160

# Sets the camera target to be 120 pixels along the y axis.
camera.target.y = 120

# Sets the camera offset to be 30 pixels along the x axis.
camera.offset.x = 30

# Sets the camera offset to be 40 pixels along the y axis.
camera.offset.y = 40

# Wrap all your drawing methods in this block and it will be rendered as though viewed through the camera.
camera.drawing do
  # Do stuff
end

# Takes a vector with world coordinates and translates it to what it would be if viewed through the camera.
camera.as_in_viewport(Vector2[0])
# => Vector2[-130, -80]

# Takes a vector with camera coordinates and translates it to what it would be in the world.
camera.as_in_world(Vector2[0])
# => Vector2[130, 80]

Platform

# Is this a released version of the game?
Taylor::Platform.released?
# => true

# Are we running in the browser?
Taylor::Platform.browser?
# => false

# Are we running on a Linux computer?
Taylor::Platform.linux?
# => true

# Are we running on a Windows computer?
Taylor::Platform.windows?
# => false

# Are we running on an OSX computer?
Taylor::Platform.osx?
# => true

Web

These methods are only available when running a web exported game.


# For web exports we use this method to set the main loop since
# infinite loops would run forever and never render due to the
# single threaded nature of browsers.
Browser.main_loop = "main"

# Sets the key "my_key" in LocalStorage in the browser to "value".
LocalStorage.set_item("my_key", "value")

# Gets the key "my_key" from LocalStorage in the browser.
LocalStorage.get_item("my_key")
# => "value"

Shader

# Load the fragment shader from "./assets/shaders/fragment_330.fs" on
# desktop and "./assets/shaders/fragment_100.fs" in the browser.
shader = Shader.new(
  fragment: "./assets/shaders/fragment_#{Taylor::Platform::GLSL_VERSION}.fs"
)

# Is the shader ready to be used yet?
shader.ready?
# => true

# Sets the variable "red" inside the shader program to be 0.75.
shader.set_value(value: 0.75, variable: "red")

# Renders everything inside with the shader applied.
shader.draw do
  # Do stuff
end

# Unloads the shader from memory.
shader.unload