Scratch broadcast chaos? 65% reduction in broadcasting in one move!

Today we are going to briefly understand the application of the state machine.


What is a state machine

        A state machine is a model that describes the states of an object and the transitions between states. In a state machine, we need to define a set of states and transition conditions between them. For example, in a game, we can define multiple states such as "standby state", "attack state" and "defense state", and set corresponding transition conditions between states. When the game is running, the next state is determined according to the current state and input events, so as to realize the game flow control.

Implementing a state machine in Scratch

        In Scratch, we can implement state machines using variables and conditional statements. For example, we can define a statevariable to record the current state, and use several conditional statements to handle input events in different states. The specific implementation steps are as follows:

  1. First, we need to define all possible states and represent them using numbers or words. For example, we can define three states: "Standby State", "Attack State" and "Defense State", which are represented by numbers 0, 1, and 2 respectively.
  2. Then, we need to define a statevariable to record the current state. At the beginning of the program, we can stateset the initial value of s to the standby state (ie 0).
  3. Next, we need to judge what to do next based on the current state and input events (such as keystrokes, mouse clicks, etc.). Here, we can use several conditional statements to achieve state transition. For example, in the standby state, if you press the "A" key on the keyboard, it will transfer to the attack state; if you press the "D" key, it will transfer to the defense state.
  4. Finally, perform corresponding actions according to the current state, such as playing standby animation, playing attack animation, etc.

        Below, we will take a few simple programs as examples to introduce how to implement a state machine in Scratch.

Example: Role Playing Game

        In this example, we will implement a simple role-playing game, including three states: standby state, attack state and defense state. The specific implementation steps are as follows:

  1. First, create a new project in Scratch, add a character in the center of the stage, and set the size of the character's "graphics" to 150%.

Next, we need to define all possible states. In Scratch, we can define all states using a code block similar to the following:when flag clicked set [state v] to [待机状态 v] when I receive 攻击 set [state v] to [攻击状态 v] when I receive 防御 set [state v] to [防御状态 v]

        In the above code, variables are set to different states using set [state v] tostatements .state

        Next, we need to define the state transition conditions. In Scratch, we can define state transition conditions in a manner similar to the following code block:when [key space v] key pressed and <(state) = [待机状态]> then broadcast [攻击 v] when [key left arrow v] key pressed and <(state) = [待机状态]> then move (-10) stepswait until <(key left arrow) not pressed> move (10) steps when [key right arrow v] key pressed and <(state) = [待机状态]> thenmove (10) stepswait until <(key right arrow) not pressed> move (-10) steps when [key up arrow v] key pressed and <(state) = [待机状态]> thenjump (100) stepswait until <touching [edge v]> point in direction ((-1) * (direction)) when [key down arrow v] key pressed and <(state) = [待机状态]> thensay [我走累了,歇歇吧! v] for (2) secsturn cw (15) degreesturn cw (15) degrees

        In the above code, we trigger different state transitions based on the current state and input events. For example, in the standby state, if you press the space bar, you will move to the attack state; if you press the left arrow, you will move 10 pixels to the left.

        Finally, perform the corresponding action according to the current state. In Scratch, we can implement stateful behavior using code blocks similar to the following:when I receive 攻击 set [state v] to [攻击状态 v]play sound [attack v]switch costume to [attack v]wait until <not <(costume [attack v] # = (costume #))> or <not <(state) = [攻击状态]>>>switch costume to [back v]broadcast [待机 v] when [key left arrow v] key pressedif <(state) = [待机状态]> thenswitch costume to [walk left v]repeat until <(key left arrow) not pressed>move (-10) stepsend switch costume to [back v] when [key right arrow v] key pressedif <(state) = [待机状态]> thenswitch costume to [walk right v]repeat until <(key right arrow) not pressed>move (10) stepsend switch costume to [back v] when [key up arrow v] key pressedif <(state) = [待机状态]> thenjump (100) stepspoint in direction (90)end when [key down arrow v] key pressedif <(state) = [待机状态]> thensay [我走累了,歇歇吧! v] for (2) secsturn cw (15) degreesturn cw (15) degreesend

        In the above code, different actions are performed depending on the current state and input events. For example, in the attack state, play the attack animation, play the attack sound effect and wait for the attack to complete before transferring to the standby state.


in conclusion

        By using variables and conditional statements in Scratch, we can easily implement a state machine to describe the state of an object and the transition between states. In the programming learning process of Scratch, the state machine is an important concept, which is worthy of deep mastery by beginners.

Guess you like

Origin blog.csdn.net/leyang0910/article/details/132040421