Sprite.say( Then You Say the Name Again With an m This Time
Python
By using Python with Sprites, you may:
- Acquire Python with your previous Scratch knowledge
- Write big programs (like algorithms) easily
- Use circuitous data structures similar dictionaries to do things that are not easy in Scratch.
In mBlock 5, Python tin almost do whatever Scratch can do.
Y'all tin control the movement and appearance of your sprite. You may also draw pictures.
Using broadcasting and Sprite Variables, Python lawmaking can interact with the Python or Cake code in other sprites.
Start Using Python
Start using Python by switching the programming mode from "Blocks" to "Python"
Note: please make sure that a sprite, not a hardware device, is currently selected.
This is an case of the Python code used in a Sprite.
from mblock import * sprite.pencolor('red') sprite.pendown() for i in range(5): sprite.forward(200) sprite.right(144)
When the code is complete, click "Run" to run your Python code.
Catechumen Blocks to Python
In the Scripts area, click to covert blocks to Python. The following is an example:
Moving a Sprite
forwards(altitude,[time])
Tell the Sprite to move forward for a certain distance in the specified flow of time towards the current facing direction. You can set the time.
sprite.forward(fifty,ane)
backward(distance,[time])
Tell the Sprite to move backward for a certain distance in the specified period of fourth dimension from the current facing direction. You can set the time.
sprite.backward(50,1)
right(degrees)
Tell the sprite to turn right for certain degrees.
sprite.right(90)
left(degrees)
Tell the Sprite to plow left for sure degrees.
sprite.left(90)
management
Apply the "direction" attribute to get the current facing direction of the Sprite
sprite.direction = 90 # set the facing direction to 90 degrees sprite.direction = sprite.direction + ten # tell the sprite to rotate clockwise for 10 degrees impress(sprite.management) # now the facing direction will be 100
towards(sprite_name)
Tell the Sprite to confront some other sprite (the sprite_name is a cord)
sprite.towards("Panda") sprite.towards('mouse') # 朝向鼠标指针
bounce()
Tell the Sprite to bounce when it touches the edge of the stage.
while Truthful: sprite.forward(l) sprite.bounce()
rotation_mode(manner)
Set the rotation manner of the Sprite.
"way" is a boolean value, could be one of these:
- 'left-correct': the epitome of the Sprite only turns left or right
- 'all-around' or True: the Sprite prototype could face any direction
- 'none' or False: the Sprite paradigm volition non rotate.
sprite.rotation_mode('all-effectually')
Position and Coordinates
goto(x, y,[speed]) | goto(place,[speed])
Tell the sprite to motility to a certain position (10, y) at the specified speed.
ten and y are numbers. You may apply the following character strings to ascertain the position:
- "random": move to a random position on the phase.
- "mouse": move to the position of the mouse pointer. You tin can set the speed.
sprite.goto(xx, xl, two) sprite.goto(30, lx) sprite.10 # =30 sprite.setpos(0,0) sprite.y # =0 sprite.goto("random", 3) # Moves to a random position at the speed of three sprite.goto("random") # Moves to a random position sprite.goto("mouse") # Moves to the position of the mouse pointer
glide(x, y, duration=5) | glide(location, elapsing=5)
Tell the Sprite to slide to a specific location in a certain duration.
You may also use the post-obit cord to announce the special location:
- "random": a random location on the stage
- "mouse": the location of the mouse pointer
sprite.glide(xxx, 60, v) # Tell the sprite to move to (thirty, 60) in 5 seconds sprite.glide("random", 2) # move to a random location in 2 seconds sprite.glide("mouse", 2) # movement to the pointer in 2 seconds
coordinate of the sprite: x, y
Get the coordinate of the Sprite, or set the (10, y) coordinate of the Sprite on the stage
sprite.ten # get the x location of the Sprite sprite.y # become the y location of the Sprite sprite.ten = l # set the x location of the Sprite to l sprite.y = 60 # set the y location to threescore sprite.y = sprite.y + 10 # increase the y coordinate by 10, that means move the Sprite up for x impress(sprite.10) # print the x location of the sprite (50 in this case) print(sprite.y) # print the y location of the sprite (70 in this case)
Appearances
say(text, elapsing=0)
Tell the Sprite to "say" some text.
If the elapsing is non specified, the text box will not disappear;
otherwise, information technology will disappear afterward a certain duration.
sprite.say("nice to run across you", five) sprite.say("hello world")
call back(text, duration=0)
Tell the Sprite to "think" for a certain text.
If the duration is non specified, the text box will non disappear;
otherwise, it will disappear after a certain duration.
sprite.think("prissy to meet yous", 5) sprite.think("hello world")
hide()
Hide the sprite
sprite.hibernate()
testify()
Show the sprite
sprite.bear witness()
set_costume(costume_name)
Change the costume of the sprite. The costume_name must exist in the Sprite, otherwise nothing volition happen.
sprite.set_costume("Panda1")
next_costume()
Switch the costume of the sprite to the next costume.
sprite.next_costume()
set_backdrop(background_name)
Switch the properties of the Phase. the background_name must exist in the projects, otherwise goose egg will happen.
sprite.set_backdrop("Party")
next_backdrop()
Switch the properties to the side by side backdrop
sprite.next_costume()
set_effect(consequence, value)
Ready the effect forcefulness to a certain value. The effect could exist one of these:
- color
- fisheye
- whirl
- pixelate
- mosaic
- brightness
- ghost
sprite.set_effect("colour", 10) # Set the color value to ten
change_effect_by(outcome, value)
Increase the effect strength by a certain value. The effect could exist 1 of these:
- color
- fisheye
- whirl
- pixelate
- mosaic
- effulgence
- ghost
sprite.change_effect_by("color", 10) # Increase the "color" effect by ten
clear_effect()
Clear all the Sprite Furnishings.
sprite.clear_effect()
size
Set or get the size proportion of the Sprite
100 is the original size (100%) of the Sprite.
sprite.size = 200 # enlarge the sprite to its 200% sprite.size = sprite.size + - 50 # shrink the sprite by 50% print(sprite.size) # print the proportional size of the sprite, 150 in this instance
Play Sound
play(sound_effect_name)
Play a sound result. The audio result name must exist in the project,
otherwise null volition happen
sprite.play("meow") # play the "meow" audio
play_until_done(sound_effect_name)
Play a sound effect, and run next statement when it is done.
The sound effect name must exist in the projection, otherwise nothing will happen
sprite.play_until_done("meow") # play the "meow" sound
stop_sound()
End playing all the sound
sprite.stop_sound()
play_drum(instrument_number, beats)
Play a drum beat. Instrument number ranges from ane to 18.
The "beats" refers to the duration of the drum beat.
sprite.play_drum(ane, 0.25)
rest(beats)
Stop playing sound for a sure beat.
sprite.rest(0.5)
play_note(note_number, beats)
Play a musical notation. The note_number is an integer (MIDI number)
The greater the number, the higher the pitch.
"beats" is the elapsing of the sound.
sprite.play_note(60, 0.25) # Plays a "C" note in the third level for 0.25 beats
set_inst(instrument_number)
Prepare the current instrument. The musical instrument number ranges from 1 to 21. TODO: Tabular array of instruments
sprite.set_inst(i) # fix the musical instrument to piano
set_sfx(sound_effect_name, value)
Prepare the value of the audio effect.
sprite.set_sfx('PITCH', ten) # set the value of the sound effect as 10
change_sfx_by(sound_effect_name, value)
Modify the value of the sound effect.
sprite.change_sfx_by('PITCH', 10) # increase the value of the sound effect by 10
clear_sfx()
Articulate all sound effects.
sprite.clear_sfx() # clear all sound effects
volume
Use book to go or alter the audio book.
The book value is a number ranged from 1 to 100.
sprite.volume = l # ready the volume to l sprite.volume = sprite.book + x # change the volume by 10% impress(sprite.volume) # 60
set_tempo(beats_per_minute) | change_tempo_by(beats_per_minute)
Utilise the tempo to set beat-per-minute(bpm) of music notes and sound effects.
sprite.set_tempo(60) sprite.change_tempo_by(20)
Turtle Graphics
pendown()
Put down the pen. A trail volition be drawn by the sprite when the pen is downwards.
sprite.pendown()
penup()
Lift up the pen. No trail will left behind when the Sprite moves then.
sprite.penup()
postage()
Get out an image of the sprite at the current position, like using a stamp.
sprite.stamp()
pencolor(color) | pencolor(red, light-green, blue)
Change the color of the pen. The colour could be:
- 1 of the follow strings: "ruby-red", "orange", "yellow", "green", "cyan", "blue", "purple", "black", "white", "grey"
- Hex code start with "#", similar "#FF0000"
- A tuple of RGB value
- RGB values as parameters
sprite.pencolor("red") sprite.pencolor("#FF0000") sprite.pencolor(255, 0, 0) # 效果是相同的
pencolor_effect(effect_name, value)
set the pen effect (0-100)
the effect_name could be a cord of:
- "hue"
- "saturation"
- "brightness"
- "transparency"
sprite.pencolor_effect("hue", 60)
change_pencolor_effect_by(effect_name, value)
Change the pen effect (0-100)
the effect_name could be a string of:
- "color"
- "saturation"
- "brightness"
- "transparency"
sprite.change_pencolor_effect_by("hue", x)
change_pencolor_shade_by(value)
change the shade value of the pen color.
sprite.change_pencolor_shade_by(10)
clear()
Clear everything in the canvas drawn by the sprite.
sprite.clear()
pensize(pen_size)
Fix the size of the pen.
sprite.pensize(1)
change_pensize_by(value)
Modify the pen size past a sure value.
sprite.change_pensize_by(one) # increase the pen size past one
Employ Events
You can use events in Python to interact with the keyboard or the mouse
by adding a mark("decorator") before the function:
from mblock import * @event.greenflag # the following function will run when the green flag is clicked def on_green_flag (): # y'all may choose your own function name print("green flag clicked") # here put the code that will run after the green flag is clicked.
@greenflag
when the green flag is clicked/tapped
@event.greenflag def on_green_flag (): print("greenish flag is clicked!")
@keypressed(key_name)
When a certain key on the keyboard is pressed, fire the event. The key_name could be:
"a", "A", "1", "enter", "space", "backspace".
@consequence.keypressed("space") def on_space_key (): print("the space fundamental is pressed")
@clicked
Trigger the consequence when the Sprite is clicked.
mousedown and mouseup might be added in the time to come.
@event.clicked def on_clicked (): print("the sprite is clicked")
@backdrop_change(backdrop_name)
When the properties is fix to the specified backdrop, this event is triggered.
@event.backdrop_change("Party") def on_backdrop_change (): print("The properties is switched to Party.")
@greater_than(parameter_name, value)
when a parameter with the sure proper noun is greater than a value, this event is triggered. The parameter name could be:
- "TIMER": the internal timer of Scratch
- "VOLUME"(not implemented yet): the microphone volume.
@event.greater_than("timer", ten) def on_greater_than (): print("10 seconds has passed")
broadcast(event_name)
circulate an event to the mBlock organization.
sprite.circulate("game_start")
@received(event_name)
when an result with a certain event_name is received, trigger the event.
@event.received("game_start") def when_game_start (): print("the game has been started")
Global Variables
You tin employ global variables to communicate with other sprites' code
(whether it is in blocks or Python)
You can even utilise Python codes to communicate with robots or other devices.
set_variable(variable_name, value)
Fix the value of a Scratch block variable.
The variable must exist in the block editor;
otherwise zippo will happen (other than producing a warning in the console).
sprite.set_variable("score", l)
get_variable(variable_name)
Get the value of the variable.
If the variable name does not exist, return None.
print(sprite.get_variable("score"))
Clone Sprites and Flow Control
clone(sprite_name=None)
Clone a sprite.
Returns the cloned sprite (Sprite Object)
(Not implemented withal) If a Sprite name is non provided, clone the sprite itself.
sprite.clone() # clone myself sprite.clone("Panda") # clone a panda sprite
delete_clone()
If the sprite is a clone, delete myself.
sprite.delete_clone()
stop_all() / stop_this() / stop_other()
Stop all sprites/(not implemented yte)stop the scripts in this sprite/(not implemented yte)stop the scripts in other sprites
sprite.stop_all() # finish all sprites on the phase
Basic Inputs and Outputs
print(content)
Print some content to the console at the bottom of the Python editor. Used to debug.
impress("hello world")
input(prompt)
Prompt the user to input text at the phase.
name = sprite.input("what'south your proper noun?") sprite.say("your name is:" + name)
Sprite Properties
touching(sprite_name)
Tell if the Sprite is touching other Sprites, the mouse, or the edge of the stage.
The consequence is Truthful of Imitation
print(sprite.touching("Panda")) # True if the Sprite is touching the "Panda" sprite print(sprite.touching_mouse()) # Truthful if the Sprite is touching the mouse arrow print(sprite.touching_edge()) # True if the Sprite is touching the edge of the stagew
touching_color(colour) | touching_color(red, green, blue) | touching_color(sprite_color, screen_color)
Tell if the Sprite is touching another color. The color could be:
- Hex values start with "#", like "#FF0000"
- A Tuple of RGB values.
- RGB values in numbers
The outcome is True of False
impress(sprite.touching_color("#ff0000")) # Tell if the sprite is touching the red color print(sprite.touching_color(255, 0, 0)) # the aforementioned color = (255, 0, 0) print(sprite.touching_color(colour)) # the same print(sprite.touching_color("#ff0000", "#00ff00")) # Tell if the red office of the sprite is touching the dark-green office of the stage.
distance_to(sprite_name)
Get the distance from the Sprite to the another Sprite or the mouse
sprite.distance_to('_mouse_') # get the altitude from the Sprite to the mouse sprite.distance_to('Accordion3')
is_keypressed(key_name)
Tell if a sure key is pressed(returns True or Imitation) Examples of primal names: "a", "A", "1", "enter", "infinite", "backspace"
impress(sprite.is_keypressed("infinite")) # When the infinite primal is pressed, return True.
is_mousedown
Tell whether the mouse is downwards. Return True or Imitation.
print(sprite.is_mousedown()) # When you printing the mouse, True is returned
Sprite Backdrop
A Sprite also has the following properties:
- mousex: the mouse's x coordinate on the stage
- mousey: the mouse's y coordinate on the stage
- loudness: the loudness of the microphone
- timer: the current value of the timer
- current(unit): become the component of current fourth dimension. "unit" could be: year | month | date | day_of_week | hour | minute | 2d: the electric current time
- days_since_2000: days passed since 2000
print("%d, %d" % (sprite.mousex, sprite.mousey)) # output the mouse's coordinate print(sprite.loudness) # output the current loudness print(sprite.timer) # output the seconds from the timer print("%d-%d-%d %d:%d:%d" % (sprite.electric current('yr'), sprite.current('month'), sprite.current('date'), sprite.electric current('hour'), sprite.current('minute'), sprite.electric current('2nd'))) print("solar day of week: ", sprite.current('day_of_week')) # output the current day of the calendar week print("days since 2000", sprite.days_since_2000)
reset_timer()
reset the timer
sprite.reset_timer() print(sprite.timer) # output zip, since the timer has been reset.
- Learn Python with your previous Scratch knowledge
- Write big programs (similar algorithms) easily
- Utilise complex data structures like dictionaries to do things that are non piece of cake in Scratch.</div>
Source: https://www.mblock.cc/doc/en/textual-programming/use-python-with-sprites.html
0 Response to "Sprite.say( Then You Say the Name Again With an m This Time"
Post a Comment