Setup and Draw in Processing #
Processing draws a moving line. The code inside the draw() function runs continuously from top to bottom until the program is stopped. The code in setup() is run once when the program starts.
Example of Setup and Draw in Processing
int y = 180;
// The statements in the setup() block run once
// when the program begins
void setup() {
size(640, 360); // Size must be the first statement
stroke(255); // Set line drawing color to white
}
// The statements in draw() are run until the program
// is stopped. Each statement is run in sequence from top
// to bottom and after the last line is read, the
// first line is run again.
void draw() {
background(0); // Clear the screen with a black background
line(0, y, width, y);
y = y - 1;
if (y < 0) {
y = height;
}
}
Setup and Draw in Godot #
To replicate the example in Godot, set the window size to 640 by 360 pixels:
Godot > menu Project > Project Settings… > tab General > Display > Window > Width: 640, Height: 360
Set the background to black color.
Godot > menu Project > Project Settings… > tab General > Rendering > Environment > Define Clear Color
Create Node2D called Main and attach a script:
extends Node2D
# Converted from https://processing.org/examples/setupdraw.html
#
# Set the window size to 640 by 360 pixels in
# Godot > menu Project > Project Settings... > tab General >
# Display > Window > Width: 640, Height: 360
#
# Set the background to black color in
# Godot > menu Project > Project Settings... > tab General >
# Rendering > Environment > Define Clear Color
var y = 180
# Called every frame. 'delta' is the elapsed time since the previous frame.
# The update() method notifies _draw() to update this node (Main in our case).
func _process(delta):
update()
# Called automatically only once after enter scene tree.
# If re-drawing is required, call update() on this node.
func _draw():
var size = get_viewport_rect().size
var width = size.x
var height = size.y
draw_line(Vector2(0, y), Vector2(width, y), Color.white)
y = y - 1
if y < 0:
y = height - 1
You can download the whole zipped Godot project here: 03-SetupAndDraw.zip.