cp2025 計算機程式

  • Home
    • SMap
    • reveal
    • blog
  • About
    • cs101
    • Computer
      • llama
      • nginx
    • AI
      • QandA
      • Teams
    • Homework
  • Topics
    • Git
      • Git_ex1
      • Git_ex2
    • Python
      • SE
      • CL
      • Loops
      • Recursion
      • SA
      • SST
      • Maze
      • Collect
      • GT
      • Python_ex1
    • Javascript
      • HTML and CSS
    • Project
      • Waitress
      • API
  • Brython
    • Brython_ex
    • Robot_ex
  • Ref
    • Reeborg
      • ex1
      • Otto_ninja
    • tkinter
    • Pyodide
    • Pyodide_ex
    • Pyodide2
      • robot.py
      • Example2
    • Pyodide3
      • png_files
      • Harvest
robot.py << Previous Next >> Pyodide3

Example2

自行定義機器人右轉的非同步執行函式:

執行 turn_right 程式範例。

import robot
import asyncio

# 定義右轉的非同步函式
async def turn_right(bot):
    for _ in range(3):
        await bot.turn_left()

async def main():
    world, bot = await robot.init(10, 10, 1, 1)
    print("機器人開始行動")
    await bot.turn_left()
    await bot.walk(9)
    await turn_right(bot) 
    print("機器人完成行動")

# main() 讓出執行控制權,由頁面中的 even loop 決定何時執行 main()
await main()

robot.py << Previous Next >> Pyodide3

Copyright © All rights reserved | This template is made with by Colorlib