你如何通过鼠标点击它来改变Netlogo中乌龟的形状?

时间:2014-01-12 01:35:27

标签: netlogo

我正在申请上学的大型netlogo项目,并且发现自己陷入困境。我正在做一个“记忆匹配”型游戏。

它是这样的:显示的卡面向玩3秒钟的人,然后所有卡片的形状变为蓝色矩形,或面朝下。然后,玩家应该点击一张牌,并将牌形状改变为相应的面朝上形状。如果两张面朝上的牌相同,那么他们就会死掉,然后你继续这样做,直到屏幕上没有牌。当这种情况发生时,你就赢了!

所以我的问题是,是否可以通过点击来改变乌龟的形状?我的整个项目取决于这个问题的答案。我的代码中非常感谢和承认任何帮助!

*旁注:有没有一种简单的方法可以告诉netlogo如果它们具有相同的形状会杀死两只海龟?

2 个答案:

答案 0 :(得分:1)

将此添加到您的go程序:

  if mouse-down? [
    let candidate min-one-of turtles [distancexy mouse-xcor mouse-ycor]
    if is-turtle? candidate and [distancexy mouse-xcor mouse-ycor] of candidate < 1 [
       ask candidate [ set shape "my new shape" ]
    ]
    ;; wait for the mouse button to be released
    while [mouse-down?] [ ]
  ]

一般情况下,如果您正在尝试编写使用鼠标的代码,您将需要在名为“鼠标”的模型中查看NetLogo模型库的代码示例部分:鼠标示例,鼠标拖动一个示例等等。

至于你的附注,你应该把它作为一个单独的问题。

答案 1 :(得分:1)

Seth已经为您提供了答案,但您可能会在此示例中找到一些有用的东西,我试图保持简单:

 globals [all-shapes  score item0 item1]
turtles-own [original-shape]
to setup
  clear-all
  reset-ticks
  set-patch-size 100
  resize-world 0 3 0 3
  set-globals
  set-game
  show-items
  play
end
to set-globals
  set all-shapes ["circle" "dot" "tree" "sheep" "box" "bug" "turtle" "x" "circle" "dot" "tree" "sheep" "box" "bug" "turtle" "x"]
end
to play
  set-item1
  set-item2
  if item0 != nobody
  [
    ask item0 [play-the-game]]
end
to reset-shapes
  Set shape  "square"  
  set item0 nobody
  set item1 nobody
end

to set-game
  Ask patches [sprout  1 [set shape "square"  set color blue   set original-shape ""]]
  repeat count turtles / 2[
    ask n-of 2 turtles with [original-shape = ""]
    [ 
      set original-shape item who all-shapes 
    ]
  ] 
end

to show-items
  ask turtles [
    set shape original-shape

  ]
  wait 3
  ask turtles[reset-shapes]
end




to play-the-game
  if item0 != nobody and item1 != nobody and item0 != item1

    [

      ifelse [shape] of item1 = [shape] of item0 and [shape] of item0 != "square" and [shape] of item1 != "square"
        [
          set score score + 1
          ask turtles with [ shape  != "square"] [die]
          reset-shapes
        ]

        [
          reset-shapes
          set score score - 1
        ]

    ]
end

to set-item1
  if mouse-down? and item0 = nobody [
    ask patch mouse-xcor mouse-ycor [

      set item0 min-one-of turtles-here  [ distanceXY mouse-xcor mouse-ycor]
      if item0 != nobody
      [
        ask item0
        [
          set shape original-shape 

        ]]
    ]
  ]
end

to set-item2
  wait 0.1
  if mouse-down? and item0 != nobody and [shape] of item0 != "square" [
    ask patch mouse-xcor mouse-ycor
      [
        set item1 min-one-of turtles-here  [ distanceXY mouse-xcor mouse-ycor]
        if item1 != item0
          [
            ask item1 [set shape original-shape]
          ]
      ]
  ]
end

我希望这段代码能让你开始:)

这是它的工作原理的截图:

enter image description here

等待3秒后

enter image description here

点击2只相同形状的海龟后: enter image description here