I am trying to create a game and what I want to do is make the patch behind the turtle to change its color as soon as the turtle eats a fruit. So the turtle moves and eats fruits. Upon eating one fruit, the patch behind the one it is on will change color but this color will move with the turtle so as to create the effect of an increasing length/size of the turtle
Now, I have tried working with the code:
ask turtles
[ if score = 10
[ ask patch-ahead -1
[ set pcolor yellow
]
]
]
The problem with this is that as the turtle keeps on moving, the whole path gets coloured yellow instead of the patch that is right behind its current location. Is there any code to get around this problem? Also, as it eats another fruit, I want the two patches behind it with the colour yellow. So how do I code this?
So are you after a game like Snake? You could give the patches a memory
variable so they can track how long it has been since the turtle passed over them. If this memory value is modified by the current 'size' of the snake body (based on how many fruit it has consumed) you can have the patches stay colored for longer. Below is a simple version (without controls, the snake just has random movement):
patches-own [ mem ]
breed [ snakes snake ]
breed [ fruits fruit ]
snakes-own [ tail-len ]
to setup
ca
create-snakes 1 [
set color white
set tail-len 1
face one-of neighbors4
ask patch-here [
set pcolor [pcolor] of myself
]
]
create-fruits 30 [
move-to one-of patches
set shape "flower"
]
reset-ticks
end
to go
ask snakes [
if random-float 1 < 0.05 [
rt one-of [ 90 -90 ]
]
if [ pcolor ] of patch-ahead 1 = white [
stamp
die
]
move-to patch-ahead 1
ask patch-ahead -1 [
set pcolor [color] of myself
set mem [tail-len] of myself + 1
]
if any? fruits-here [
ask fruits-here [ die ]
set tail-len tail-len + 1
]
]
if not any? snakes [
print "The snake tried to eat itself."
stop
]
ask patches with [ mem > 0 ] [
set mem mem - 1
if mem = 0 [
set pcolor black
]
]
tick
end