What would i need for this code to make turtles spawn slower, but not have to hold down the mouse to kill a turtle - netlogo

Im playing whack a mole, and here is my code thus far
globals [
score
three-sec-spawn
]
turtles-own [
ttl]
extensions [bitmap]
To setup
ca
reset-ticks
import-drawing "whackamole.jpg"
import-pcolors-rgb "whackamole.jpg"
set-default-shape turtles "turtle"
ask turtles [
set size 7
set color red
set ttl 100]
reset-ticks
ask patch 18 2 [set plabel
"DIRECTIONS : To kill a bug, click on it. If you misclick, the bug stays there."]
ask patch 18 -1 [set plabel
"If there are 5 bugs on the screen, or if you dont kill it fast enough, you lose"]
set score 0
end
To play
if ticks > 65000 - (difficulty * 10000) [user-message "GAMEOVER" stop]
if (count turtles with [color = red]) = 10 [user-message "GAMEOVER" stop]
ask turtles [
set size 7
set color red]
ask patch 18 2 [set plabel ""]
ask patch 18 -1 [set plabel ""]
ask one-of patches with [pcolor = [42 13 9]] [sprout 1]
ask turtles [
set size 7
set color red
set ttl ttl - .5]
wait .5
if mouse-down? [
ask turtles with [distancexy mouse-xcor mouse-ycor < 1.5]
[set score score + 1
die]
reset-ticks
]
tick
end
because of the wait .5, turtles spawn too fast. But if i make the wait time any longer, id have to hold down the mouse button in order to "whack" the turtle

In your case I would not use the "wait." primitive at all, because it creates a gap of time where no actions (for example mouse clicks) can be tracked.
Instead I would use a time counter to delay the spawning and to create turtles on every x time units. Generally I would use the tick counter itself. For example, if you would like to spawn 1 turtle on every 100th tick:
if (ticks mod 100 = 0) [ ask one-of-patches with ... [sprout 1]]
In your example, the tick counter gets reset on mouse clicks. In that case you should define an additional time-counter for the spawning which does not get reset at anytime and gets increased by 1 on every timestep (analougous to the ticks but without resetting it back to 0 on mouse clicks):
if (your-counter mod 100 = 0) [ ask one-of-patches with ... [sprout 1]]
I hope this helps..?!?

Related

How can I properly implement a function at the end of a counter in Netlogo?

I am writing an assembly line model, and I would like to implement a counter to hold a turtle at a specific patch (in this case, patch 3 0) for 10 ticks. Once 10 ticks have passed, I would like the turtle to keep on moving at the rate of one patch per tick and for the next turtle in line to begin its own 10 tick timer once it arrives at the specified patch.
So far, I can stop the turtles at the specified patch and run a 10 tick counter; however, I cannot seem to get the turtles to keep moving continuously after the timer is completed.
Here are the relevant parts of my code so far.
to go
move-tubs
move-drums
machine-marriage
move-machines
stay
keep-going
tick
end
to move-machines
ask wmachines [
if not any? turtles-on patch-ahead 1 and xcor < 3
[ forward 1]
]
end
to stay
ask wmachines-on patch 3 0[
ifelse counter = 0 [
set counter 10
]
[set counter counter - 1
set label counter
if counter = 0
[forward 1]
]
]
end
to keep-going
ask wmachines-on patch 4 0[
if not any? turtles-on patch-ahead 1 and xcor < 12
[ forward 1]
]
end
If your problem is that turtles leave patch 3 0 but then they do not move forward continuously beyond patch 4 0, it is simply because your keep-going procedure is only addressing turtles that are exactly on patch 4 0 (and for this reason the xcor < 12 part is completely unused).
In general, it looks very complicated and unnecessary that you are using three different procedures (i.e. one before patch 3 0, one for patch 3 0, and one for patch 4 0 but which should really be beyond patch 3 0) each of which is hard-coding some location in your model.
The whole point of having a counter is that you can generalise a waiting condition across the whole model, so your go procedure can be simplified a lot by simply asking agents that have concluded their countdown to do one thing, and those who have not concluded their countdown to do another thing.
Look at this minimal reproducible example where I have an unpredictable arrangement of stopping-patches but implement the waiting condition in a very general and simple way:
turtles-own [
counter
]
to setup
clear-all
reset-ticks
resize-world 0 30 0 4
set-patch-size 25
ask patches with [pxcor = min-pxcor] [
sprout 1 [
set heading 90
set color lime
]
]
ask n-of 15 patches with [pxcor > min-pxcor] [
set pcolor brown
]
end
to go
ask turtles [
ifelse (counter = 0)
;; If the counter equals 0:
[forward 1
if (pcolor != black) [
set counter 10
]
]
;; If the counter does not equal 0:
[set counter counter - 1]
ifelse (pcolor = black)
;; If the turtle is on a black patch:
[set label ""]
;; If the turtle is not on a black patch:
[set label counter]
]
tick
end

Limiting Population Size while Hatching Additional Turtles

I have a slider that controls population, set to the max value of 100, which creates 100 turtles in the nest. However, the number of turtles within a breed is independent of the total population, so instead of having 100 total turtles, I get 100 + #breed1 + #breed2. Additionally, I am hatching new turtles of breed [followers] and [foragers] during the course of the model. How do I get the turtles to die each time a new breed member is hatched?
I know this is not a code problem, but ideally, I would like the new foragers to be the turtles that are in the nest, not just new turtles.
to setup
clear-all
set-default-shape turtles "bug"
create-turtles population
create-foragers 10
[set color yellow]
end
to go ;; forever button
ask leaders
[wiggle
fd 1
return-to-nest]
ask followers
[if any? leaders
[uphill-chemical
fd 3
pickup-food]
uphill-food
fd 1
if distancexy nest-x nest-y < 3 and color = violet
[hatch-foragers 1
[set color yellow
uphill-chemical]]
tick
end
to return-to-nest ;; turtle procedure
ifelse nest?
[if count followers < 5
[hatch-followers 1 [set color brown - 1]]
facexy food-x food-y ;; drop food and head out again
move-to patch food-x food-y]
[else commands]
end
````````
The easiest way to maintain a population is simply to ask a newly born turtle to kill a randomly selected turtle. So instead of:
[ if count followers < 5
[ hatch-followers 1
[ set color brown - 1
]
]
you could have:
[ if count followers < 5
[ hatch-followers 1
[ set color brown - 1
ask one-of turtles [die]
]
]

Netlogo ticks as a counter for a race

I'm writing code to make a race between 5 turtles. I have to show who wins the race through the use of ticks. I think I can use the ticks to count how much time it takes each turtle to move then compare them. However, I can't figure out where to put the "tick" in my code. Here's my code:
to finish
ask patches
;sets finish line pattern
[ifelse (pxcor + pycor) mod 2 = 0
;if true do this
[set pcolor pink]
;if false do this
[set pcolor yellow]
]
ask patches
;sets background black other than the finish line
[if pxcor < 12 [set pcolor black]
]
end
to lanes
ask patches
;sets the lanes
[ if pycor = 3 and pxcor < 12 [set pcolor white]
if pycor = 9 and pxcor < 12 [set pcolor white]
if pycor = -3 and pxcor < 12 [set pcolor white]
if pycor = -9 and pxcor < 12 [set pcolor white] ]
;setup for the turtle positions
cro 5
ask turtle 0 [setxy -15 0]
ask turtle 1 [setxy -15 6]
ask turtle 2 [setxy -15 12]
ask turtle 3 [setxy -15 -6]
ask turtle 4 [setxy -15 -12]
ask turtles [set heading 90] ;set heading 90 means moving the head of the turtle right 90 degrees
reset-ticks
end
to setup
finish
lanes
end
to movecars
every .1
[fd random 10 / 10]
end
to endrace
movecars
if xcor >= 12 [die]
end
to go
endrace
end
Almost always, tick goes as the last command in the go procedure. Certainly that's what you should be doing while you are new to NetLogo. Having said that, it won't make your code work.
Think of ticks as a time step counter. Each loop through the go procedure should do all the actions that need one time step to do and also advances the time step counter. So you don't need the every command, you just have the movecars procedure called by the go procedure and have ask turtles [forward random 10 / 10] in the movecars procedure.
This is a fairly fundamental conceptual gap and I suggest you look through some of the models in the NetLogo models library, focusing on the link between the go procedure and the moving procedure and the passage of time. Or perhaps do the tutorials again. Also, start your model more simply. Just create one car and get it to move, then worry about multiple cars, colours and seeing who wins. Add a little piece of your model and make it work before adding the next piece.
Try this:
to go
movecars
endrace
tick
end
to movecars
ask turtles [ fd random 10 / 10]
end
to endrace
ask turtles [ if xcor >= 12 [die] ]
end

NetLogo: creation of lattice/grid resources world without using turtles?

I would like to create a "gridded" world of resources, in specific distance from the central patch and keep distances equal between these patches. Due to calculation demand, I prefer not to use turtles to create this patchy world. I expect to create something like this:
Equally, I would like to define distance between patches as a slider tool. I was wandering to use turtle lattice walk and then turn patches to different color, but is there any way how to do that without turtles ? Thanks for any suggestions !
My not totally working exemple:
to setup
clear-all
ask patches [set pcolor green]
foreach [5 10 15] [
repeat 9 [
make-red-patch ?
]
]
reset-ticks
end
to make-red-patch [dist]
crt 1 [
fd dist
rt 90
while [pcolor = red] [
bk dist
rt 90
fd 2 * dist
]
set pcolor red
die
]
end
I am not exactly sure what you need, first you mentioned you don't want to use turtles and in your own answer you have problem with the patch without a turtle.
There might be another way to approach this question:
to setup
clear-all
ask patches with [pxcor mod Grid = 0 and pycor mod Grid = 0] [set pcolor red]
end
And these are examples with different Grid size:
After more detailed search I found my answer here: http://netlogo-users.18673.x6.nabble.com/Setting-up-agents-in-a-grid-formation-td4864083.html
They consider to distribute turtles, not patches and then attribute patches turtles' qualities.
Here is the code:
to setup
clear-all
create-turtles 1
[ let $n 0 ; actual number of turtles on this patch
let $s 0 ; current number of turtles on a side of square pattern - 1
set heading 0
ask patch-here [set pcolor red]
repeat 16 ; number of needed turtles
[ hatch 1 jump Grid ; make one turtle and move
set $n $n + 1 ; increment count of curent side
ask patch-here [set pcolor red]
if $n > $s ; if side finished...
[
right 90 set $n 0 ; turn and reset count
ask patch-here [set pcolor red]
; if headed up or down, increment side count
if heading mod 180 = 0 [ set $s $s + 1
]
]
]
die
]
end
which produce:
I still don't know how to deal with 1 patch without turtle (bottom right corner), but this exemple helped me a lot ! :)

How show few turtles at a time?

I have this basic code for setup and moving turtle.
By this time I want only few turtles to appear during setup and then when they move. Other turtles will show or will become visible.
to setup
crt 100
setxy random 19 random 80
end
to go
fd 1
end
I tried this. But I got error
to setup
clear-all
create-turtles random 10
reset-ticks
end
to go
fd 1
if count turtles < 100 [
create-turtles min list (random 10) (100 - count turtles)
]
tick
end
Your question is not that clear, if you want to be able to set visibility of turtles you should use hidden? Primitive to set visibility of turtles,
Following example shows how turtles appear when their who id is smaller than ticks, in tick 101 all turtles will be visible.
to setup
clear-all
reset-ticks
crt 100 [
set hidden? true
setxy random 19 random 80
]
end
to go
ask turtles
[
if who < ticks
[
set hidden? false
fd 1
]
]
ask patch 0 0 [set plabel ticks] ; just for your info
ask patch 1 1 [set plabel "Ticks"] ; just for your info
tick
end
After 1 tick only one turtle is visible:
And now 40 turtles are visible :
Update:
In this example, you can have a list of numbers that you want to ask turtles to set their visibility to true:
globals [ number-to-set-visible]
to setup
clear-all
reset-ticks
set number-to-set-visible [ 5 5 7 8 2 ]
crt 100 [
set hidden? true
setxy random 19 random 80
]
end
to go
if visibility-condition and length number-to-set-visible > 0
[
ask n-of item 0 number-to-set-visible turtles [
set hidden? false
]
set number-to-set-visible remove-item 0 number-to-set-visible
]
ask turtles with [not hidden? ]
[
fd 1
]
tick
end
to-report visibility-condition
report ticks mod 100 = 0 and ticks > 0
end
Marzy's answer covers how to create invisible turtles during setup, then gradually make them visible during go.
It's not clear to me if you actually need this visible/invisible distinction. Do you actually need all the turtles to exist from the beginning? Might it be OK to just create a few more turtles each tick? If so, try code like this:
to setup
clear-all
create-turtles random 10
reset-ticks
end
to go
if count turtles < 100 [
create-turtles min list (random 10) (100 - count turtles)
]
tick
end