Introduction to Python

Functions to Control Sprites

Lesson 2: Animations

Controlling Sprites with Python

In PictoBlox Python mode, every sprite is an object with a set of built-in functions you can call in your code. These functions let you move, rotate, resize, show, hide, and animate your sprites — all through Python!

Diagram of the Tobi sprite with labeled arrows pointing to different properties — position (x, y), direction, size, and visibility

Motion Functions

Sprite Motion Functions

FunctionWhat it DoesExample
sprite.move(steps)Moves the sprite forward by a number of stepstobi.move(10)
sprite.go_to(x, y)Moves the sprite to a specific positiontobi.go_to(0, -100)
sprite.set_x(value)Sets the sprite's X positiontobi.set_x(50)
sprite.set_y(value)Sets the sprite's Y positiontobi.set_y(-50)
sprite.change_x(value)Changes the sprite's X position by a valuetobi.change_x(10)
sprite.change_y(value)Changes the sprite's Y position by a valuetobi.change_y(10)
sprite.point_in_direction(angle)Points the sprite in a directiontobi.point_in_direction(90)
sprite.turn_right(degrees)Rotates the sprite clockwisetobi.turn_right(15)
sprite.turn_left(degrees)Rotates the sprite counter-clockwisetobi.turn_left(15)
PictoBlox Stage coordinate grid showing x-axis from -240 to 240 and y-axis from -180 to 180 with the center marked as 0,0 and Tobi placed at various positions

Appearance Functions

Sprite Appearance Functions

FunctionWhat it DoesExample
sprite.show()Makes the sprite visibletobi.show()
sprite.hide()Hides the spritetobi.hide()
sprite.set_size(percent)Sets the sprite's size as a percentagetobi.set_size(50)
sprite.change_size(value)Changes the sprite's size by a valuetobi.change_size(10)
sprite.say(message, duration)Shows a speech bubble for a durationtobi.say("Hello!", 2)
Three versions of the Tobi sprite side by side at 30%, 60%, and 100% size to illustrate the set_size function

Timing Functions

To create smooth animations and delays between actions, Python provides the time module with a sleep() function.

import time tobi.move(10) time.sleep(0.5) # Wait 0.5 seconds tobi.move(10)

Always import the time module at the top of your script before using time.sleep(). Forgetting to import it will cause an error!

Subscribe to our newsletter.

Get updates to news and events.