This document provides information about the Sprite
and Pen
classes which are designed to mimic the block-based programming logic of Scratch in Python. These classes allow for controlling sprites and pen functions programmatically in a Scratch-like environment.
Sprite Class
The Sprite
class is the core component for handling sprite operations in the programming environment. This class provides a comprehensive set of methods to perform various sprite-related actions similar to Scratch.
Class Constants
RANDOM
: Represents a random target ("_random_"
).MOUSE
: Target the mouse pointer ("_mouse_"
).EDGE
: Represents the edge of the stage ("_edge_"
).SPACE
: Represents the space key ("space"
).STAGE
: Represents the stage itself ("_stage_"
).
Initialization
Sprite(name="")
Creates a new sprite instance. Optionally, you can provide a name of the sprite, to override which sprite is controlled by the class object.
Methods
Movement
move_steps(steps=10)
: Moves the sprite a specified number of steps forward.turn_right(degrees=15)
: Turns the sprite to the right by the specified degrees.turn_left(degrees=15)
: Turns the sprite to the left by the specified degrees.goto(to="_random_")
: Moves the sprite to a specified position or to a random position if_random_
is used. You can also use constants like: Sprite.RANDOM as an argument.goto_xy(x=0, y=0)
: Moves the sprite to specific (x, y) coordinates.change_x(DX=10)
: Changes the sprite’s x position by the specified amount.change_y(DY=10)
: Changes the sprite’s y position by the specified amount.set_x(X=-240)
: Sets the sprite’s x position to a specific value.set_y(Y=180)
: Sets the sprite’s y position to a specific value.
Position and Direction
x()
: Returns the current x position of the sprite.y()
: Returns the current y position of the sprite.direction()
: Returns the current direction of the sprite.set_direction(DIRECTION=15)
: Sets the sprite’s direction.point_towards()
: Points the sprite towards the mouse pointer.
Appearance and Looks
say(MESSAGE="Hello!", SECS=0)
: Displays a message for a specified duration.think(MESSAGE="Hmm...", SECS=0)
: Thinks a message for a specified duration.switch_costume(COSTUME="costume2")
: Switches to a specific costume.next_costume()
: Switches to the next costume.change_size(CHANGE=10)
: Changes the sprite’s size by the specified amount.set_size(SIZE=100)
: Sets the sprite’s size to a specific value.show()
: Shows the sprite on the stage.hide()
: Hides the sprite from the stage.
Sound Control
play(SOUND_MENU="Meow")
: Plays a sound.stop_all_sounds()
: Stops all sounds currently playing.change_volume(VOLUME=-10)
: Changes the sprite’s volume by the specified amount.set_volume_to(VOLUME=100)
: Sets the sprite’s volume to a specific level.
Events and Sensors
is_touching(TOUCHINGOBJECTMENU="_edge_")
: Checks if the sprite is touching a specified object.distance_to(DISTANCETOMENU="_mouse_")
: Calculates the distance to another object.ask(QUESTION="")
: Asks a question and waits for an answer. This will also returns the user provided answer.answer()
: Returns the answer to the last question asked.
Cloning
create_clone(CLONE_OPTION=None)
: Creates a clone of the sprite.
Control Broadcasts
broadcast(BROADCAST_INPUT="message1")
: Broadcasts a message that other sprites can listen for.broadcast_and_wait(BROADCAST_INPUT="message1")
: Broadcasts a message and waits for all recipients to finish handling it.
Pen Class
The Pen
class provides methods for drawing on the stage using a virtual pen, allowing for creative graphical representations.
Initialization
Pen(name="")
Creates a new pen instance. Optionally, a sprite name for the pen can be specified, and the pen object will be associated with the corresponding sprite.
Methods
down()
: Lowers the pen to start drawing.up()
: Raises the pen to stop drawing.clear()
: Clears everything drawn by the pen.stamp()
: Stamps the sprite as a picture on the current pen position.set_color(COLOR=[0,0,0])
: Sets the color of the pen. Optionally, you can also pass color as a string like: ‘#FF00FF‘change_color(COLOR_PARAM="color", VALUE=10)
: Changes a specified color parameter of the pen.set_size(SIZE=1)
: Sets the pen size to a specific value.change_size(SIZE=1)
: Changes the pen size by the specified amount.
These classes offer a powerful set of methods to control and manipulate sprites and drawings significantly, emulating Scratch’s logic in Python. This documentation can help you effectively use the Sprite
and Pen
classes when developing your Scratch-like applications.
NOTE: These functions are for the Scratch Sprite object, if the containing object is an Stage, only stage related functions like: switch_backdrop, etc. will work for an stage Sprite class object.
NOTE: Also if its the first time you have loaded CodeSkool website, it might not work. In that case, just close the tab, and reopen the CodeSkool website once again, in another tab.