Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+11 votes

I am trying to make an effect that once the turtles reach "homex homey", they will change to their pcolor of the image, but right now it remains the random colors I have assigned each turtle. Here is the code I am trying to fix:

    turtles-own [ homex home
      to setup 
  clear-all
  reset-ticks
  import-pcolors-rgb "bear.jpg"
  ask patches [
    sprout 1 [
      set color (list (random 255) (random 255) (random 255)) ;; This is where I added the random color part
      set homex xcor
      set homey ycor
      fd 15
    ]
  ]
  ask patches [
    set pcolor black
  ]
  reset-ticks
end

to go
  ask turtles [
    facexy homex homey
    fd 0.1
    if xcor = homex [set color pcolor] ;; these two if statements are what I am tyring to use to change the colors to match
    if ycor = homey [set color pcolor];; but the statements to not do anything to influence the colors
  ]

  tick
end
asked in CSC 150 January201920 by (1 point)

3 Answers

+6 votes
 
Best answer

One problem you are running into is that your xcor and ycor are not integers -- they are numbers like 3.18293474 and 7.1323432425, so they are never EXACTLY equal to the homex and homey variables.

To fix this, turtles can access the pxcor and pycor variables of the patch that they are on top of. Your code would change to:

if pxcor = homex [set color pcolor] 
if pycor = homey [set color pcolor] 

However, in this case, you'll see that they change their color if they reach either the homex column or the homey row. It sounds like you wanted them to change ONLY when they arrive at the homex homey patch. To do this, you'd use the AND connector, so that the code only runs when BOTH conditions are true:

if pxcor = homex AND pycor = homey [ set color pcolor ]

However, right now all of your patches are black, so this will make all of your turtles look like they disappear!

You probably wanted to make it so that your turtle would change to the original pcolor of the patch (from the image), rather than the current pcolor of the patch (which is black). To do this, you'll need to add another turtles-own variable (maybe original-pcolor), and set it to the pcolor when the turtle is first sprouted, and then later you can do

set color original-pcolor

Hope this helps!

answered by (4k points)
selected by
+6 votes

I've used if statement in my project, if you want I can share it with you.

answered by (1 point)
+5

That would be great if you could share it to see an example

+5

to setup1
clear-all
import-pcolors-rgb "imagename.jpg"
ask patches [

if (pxcor < 58) [
  sprout 1 [
    set color pcolor
    set shape "x"
  ]
]

]

This is what I did

+3 votes

Yes, it is possible.
You just need to keep checking if the coordinates are homex or home y everytime the turtle moves using if statement!

answered by (1 point)
reshown by
...