Drawing
There are three ways to draw: with the pen, which leaves a line where a sprite moves, with stamps, which copy a sprite's picture onto the stage, and with drawing sprites, which draw themselves again in every frame.
The pen
Every sprite has a pen that follows it. Put it down, and the sprite draws while it moves:
| Scratch | Java |
|---|---|
| pen down pen up | getPen().down(), getPen().up() |
| set pen color to [#ff0000] | getPen().setColor(255, 0, 0) (red, green, blue) |
| change pen (color v) by (10) | getPen().changeColor(10) |
| set pen size to (4) | getPen().setSize(4) |
| erase all | getPen().eraseAll() or eraseAll() on the stage |
The lines are drawn behind the sprites.
Stamps
stamp() leaves a copy of the sprite's picture where it is now, like Scratch's
stamp block. Click to stamp a star at the mouse:
Stamps are cheap. A wall of 100 bricks can be one sprite that stamps itself 100 times.
Sprites that draw themselves
A DrawingSprite has a draw method instead of costumes. It is called in every
frame, and draws shapes and pictures around the sprite's position:
-
rectangle(x, y, width, height, color): x and y are the middle of the rectangle. polygon(xs, ys, color)fills a shape through the corners.image(name, x, y)draws a built-in picture or a file.
The sprite's position, size, direction and layer apply to all of it. A drawing
sprite has no hitbox until you give it one with setHitbox.
Next
- Effects: colours, transparency and shaders
- Custom drawing example: a road in 3D and a pixel font
- Pen and DrawingSprite in the reference