-
Notifications
You must be signed in to change notification settings - Fork 10
Syntax
Zajal strives to have as consistent a syntax as possible. The few conventions mentioned on this page generally apply across the language as a whole.
Comments begin with a single #
and continue to the end of the line. There are no multiline comments.
# I am a comment
circle 10, 20, 5 # This is also a comment
In Zajal, you never have to declare a variable before you use it. Assigning a value to a variable creates it.
x = 20 # the variable x is created and it has the value of 20
A variable declared inside a block is not visible outside that block.
setup do
word = "hello there!"
word_x = 0
end
update do
word_x += 1
end
draw do
text word, word_x, height/2 # this will result in an "undefined local variable" error
end
To make variables visible to all blocks, create them outside of any block. This corrects the above sketch:
word = "hello there!"
word_x = 0
update do
word_x += 1
end
draw do
text word, word_x, height/2 # this will result in an "undefined local variable" error
end
A variable declared with a capital first letter is a constant and its value is not expected to change. Zajal will issue a warning if you try and change a constant.
ShouldNotChange = 93
ShouldNotChange += 5 # will work, but will generate a warning. don't do this.
Methods in Zajal are always lowercase and underscore separated such rectangle
, circle
and mouse_x
. They take a comma separated list of paramaters like other languages, but parentheses are optional. For example, setting the window size to 300x400 is as simple as
size 300, 400
Isn't that pretty?
Zajal has a bunch of settings that control the way the sketch works and your code is interpreted. Reading and writing these settings is an important part of programming, and has been given a uniform interface.
Calling one of these methods, such as size
, smoothing
or alpha_blending
with parameters sets the respective setting.
size 200, 500 # the window size is now 200px by 500px
smoothing true # smoothing is now enabled
alpha_blending false # alpha blending is now disabled
Calling one of these methods without parameters gets the respective setting, which you can store or otherwise react to. Again, since parameters are optional this results in a clean, readable syntax.
text size # will display [500, 500]
k = smoothing # the variable k now holds true if smoothing is on, false otherwise
last_x = mouse_x # stores the last x position of the mouse in last_x
Some setting can only be read from, like mouse_x
and mouse_y
. Everything that can be written to can be read from.
Zajal builds upon the pioneering work of the Processing project and features the familiar setup
-update
-events
-draw
loop that many creative coders are accustomed to. This is what they look like:
setup do
# your set up code here, runs once at startup
end
update do
# your update code here, runs once per frame
end
draw do
# your draw code here, runs once per frame, draws to the sketch window
end
Some blocks take parameters.
mouse_pressed do |x, y, button|
# your mouse pressed code, called each time the mouse is pressed
# gives you x, y and the button pressed to deal with
end