警告:这个程序有一个逻辑错误。牛饿了,想吃干草,但什么也没发生!

规则:事件处理程序将精灵链接到一个函数,该函数在事件被触发时被调用。

  • 点击,看看会发生什么。查看代码以查看牛应该在哪里吃干草。
  • 通过向main()函数添加事件处理程序来修复程序,以便发生碰撞事件。
  • 点击运行以测试您是否修复了程序。固定好后点击提交接下来

    To navigate the page using the TAB key, first press ESC to exit the code editor.

    def add_sprite(): """ Add cow sprite to the stage and set its speed """ cow = codesters.Sprite("cow",-400, -150) cow.set_x_speed(3) return cow def setup_stage(): """ Set up the stage with background and sprites """ stage.set_background("barn") stage.disable_right_wall() hay = codesters.Sprite("hay",-150,-150) hay = codesters.Sprite("hay",0,-150) hay = codesters.Sprite("hay",150,-150) def collision(sprite, hit_sprite): """ Collision event for the cow to eat hay """ if hit_sprite.get_image_name() == "hay": sprite.say("yum!",.2) hit_sprite.hide() def main(): """ Sets up the program and calls other functions """ setup_stage() sprite = add_sprite() main()
    • Run Code
    • 提交作品
    • 下个活动
    • Show Console
    • Reset Code Editor
    • Codesters How To (opens in a new tab)