Need help creating poles for Flappy Bird - netlogo

I've been using NetLogo Programming for a few weeks now, but I'm still a little confused with the syntax. I'm trying to create a NetLogo version of the Flappy Bird game, but I can't seem to make the poles work.
I tried using patches, but the poles don't move very smoothly. When I use turtles, I can't make it stop without stopping the whole program. This is part of my code:
to top-pole
crt 1
[set size 1500
set color pink + 2.8
set shape (one-of ["pole 7" "pole 8" "pole 9" "pole 10" "pole 11"])]
ask turtles with [shape = "pole 7"]
[setxy 75 0]
ask turtles with [shape = "pole 8"]
[setxy -1 0]
ask turtles with [shape = "pole 9"]
[setxy 149 0]
ask turtles with [shape = "pole 10"]
[setxy 149 0]
ask turtles with [shape = "pole 11"]
[setxy -1 0]
; facexy 0 1]
end
to move-top
every 10
[top-pole]
every 0.05
[ask turtles with [who > 0]
[setxy (xcor + x-vel) (ycor + y-vel)
set x-vel (x-vel - .01)]]
every 3
[ask turtles with [(shape = "pole 7") and ((round xcor) >= 155) and ((round xcor) <= 145) ] ;150
[die]
ask turtles with [(shape = "pole 8") and ((round xcor) >= 65) and ((round xcor) <= 80)] ;74
[die]
ask turtles with [(shape = "pole 9") and ((round xcor) >= 220) and ((round xcor) <= 230)] ; 225
[die]
ask turtles with [(shape = "pole 10") and ((round xcor) >= 220) and ((round xcor) >= 230)] ; 225
[die]
ask turtles with [(shape = "pole 11") and ((round xcor) >= 70) and ((round xcor) <= 80)] ; 75
[die]]
end
Btw, the "pole 7", "pole 8", etc. are just turtles I created:
enter image description here
Also, my world dimensions are currently 300 by 200 and the patch size is 1.

Related

Hatch not changing the Breed Netlogo

im trying to change the breed of my turtles at hatch but its just giving the mothers breed.
if more code is need please let me know. Thanks!
So, the edit is a more complete code and can be copy pasted. The idea is that if males (machos) find a females (hembra) then they should reproduce, and the new hatch needs to be either male or female. However this is not happenign, theres only females, i chagned the variables in such way that they reproduce alot, meaning that i should see at least one male spawn, but it doesnt.
turtles-own[energia fecundidad vejez]
breed[machos macho]
breed[hembras hembra]
patches-own[alimento]
to setup
resize-world 0 90 0 60
clear-all
ask patches with [
pxcor <= 30 and
pxcor >= min-pxcor and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 65
set alimento random 100
]
set-default-shape turtles "cow"
create-machos 3
[ set size 3 ;; easier to see
set color gray
setxy random xcor random ycor
move-to one-of patches with [pcolor = 65]
set heading random 45 + 45
set energia random 500
set fecundidad 0
set vejez 0
]
set-default-shape turtles "cow"
create-hembras 3
[ set size 3 ;; easier to see
set color black
set xcor random 30
set ycor random 60
move-to one-of patches with [pcolor = 65]
set heading random 45 + 45
set energia random 500
set fecundidad 0
set vejez 0
]
reset-ticks
end
to go
ask machos [
if energia >= 60 [reproducirse]
set heading random 360
fd 1
set energia energia - 1
]
ask machos [set fecundidad fecundidad + 1]
ask machos [set vejez vejez + 1]
ask hembras [
if energia >= 50 [reproduccion_hembra]
set heading random 360
fd 1
set energia energia - 1
]
ask hembras [set fecundidad fecundidad + 1]
ask hembras [set vejez vejez + 1]
tick
end
to reproducirse
ask machos[if any? hembras with [fecundidad >= 6][ set heading towards one-of hembras with [fecundidad >= 6]]
]
end
to reproduccion_hembra
ask hembras [ if energia >= 50 [if any? machos in-radius 4 [ set energia energia - 60
hatch 2 [set breed one-of (list machos hembras) rt random-float 360 fd 15
set energia (100 + random 400)
set vejez 0]]]]
end

expected input to be a number but got the TRUE/FALSE true instead

I want to write an evacuation with leader code in net-logo but i have this error (AND expected input to be a TRUE/FALSE but got the number 0 instead.)the error came from function (follow_leader). my project is that i evacuate an environment and evacuate it with the nearest leader from the crowd . the code is not perfect that because iam knew to netlogo .
this is my code
globals[ goal-x goal-y n number leader
percent_of_leader wall
window
door largecircle]
patches-own [path?
obstacle
goal]
turtles-own [direction
fast?
fear?
leader?
is-leader?
follower
other-nearby]
to setup
clear-all
set-default-shape turtles "person"
;create-turtles 50 [ set color yellow ]
;ask turtles [fd random 13 ]
drwa-walls
ask n-of population patches with [ pcolor = black]
[sprout 1
[ set color white
set size 0.9
set shape "person"
set leader? false
;set follower self
if xcor < 9 and ycor > 9 [set heading 0]
if xcor >= 9 and ycor >= -10[set heading -90]
if xcor > -12 and ycor < -10 [set heading 90]
if xcor <= -8 and ycor > -10 and ycor < 0 [set heading 180]
]]
choose-leaders
end
to choose-leaders
ask turtle 6
[
set leader? true
set color yellow
set size 1
set shape "default"
set leader self
set xcor 3
set ycor 10
set heading 0
]
end
to go
move-for
ask turtle 6[move-to-exit1
fd 1 ]
ask turtles with [shape = "person"][
ifelse (xcor < 9 and ycor > 9 ) [follow_leader] [move-to-exit1]
if xcor >= 9 and ycor >= -10 [move-to-exit2]
if xcor > -12 and ycor < -10 [move-to-exit3]
if xcor < -12 and ycor < -10 [move-to-exit3]
if xcor <= -8 and ycor > -10 and ycor < 0 [move-to-exit4]
;avoid obstcao
; avoid_obstacles
;ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
;[ fd 1 ] ;; Otherwise, it is safe to move forward.
; ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;blue or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;blue or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;blue
;[lt random-float 360 ]
;[fd 1]
; while [patch-ahead 1 = nobody]
;[
;lt random-float 360
;]
;ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
;[ fd 1 ]
; ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;white or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;white or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;white
; [lt random-float 360 ]
; [fd 1]
;ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;green or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;green or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;green
;[lt random-float 360]
; [fd 1]
]
reset-ticks
tick
end
to rt-random
while [patch-ahead 3 = nobody]
[
rt random 360
]
end
to avoide
ask turtles [
if [pcolor] of patch-ahead 1 = green
[lt random 360 fd 1]
]
end
to avoid_obstacles ;; all obstacles set as green patches
let i 1
while [[pcolor] of patch-ahead i != blue ]
[set i (i + 1) ]
if ([pcolor] of patch-ahead i = blue)
[
ifelse [pcolor] of patch-at-heading-and-distance (heading - 20) i + 1 = blue
[
ifelse [pcolor] of patch-at-heading-and-distance (heading + 20) i + 1 = blue
[
ifelse random 1 = 0
[ rt 360 ]
[ lt 360]
]
[ rt 360 ]
]
[lt 360]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;
to bounce
if [pcolor] of patch-at dx 8 = blue [
set heading (90)
]
if [pcolor] of patch-at 0 dy = blue [
set heading (-90)
]
end
;;;;;;;;;;;;;;;;;;;;;;;
to move-for
ask turtles with [shape = "person"][fd 1]
end
to drwa-walls
draw-exit1
draw-exit2
draw-exit3
draw-exit4
;rows x
ask patches with [pxcor <= 30 and pxcor >= 0 and pycor = -10]
[set pcolor blue]
ask patches with [pxcor >= -30 and pxcor <= 0 and pycor = 9]
[set pcolor blue]
;coilmn
;ask patches with [pycor <= 9 and pycor >= -10 and pxcor = 0]
;[set pcolor green]
;ask patches with [pycor <= 4 and pycor >= 0 and pxcor = 0]
;[set pcolor green]
;circle shape
ask patch -14 -9 [
set largecircle patches in-radius (2)
]
;set color of largecircle patches green
ask largecircle [
set pcolor green
]
ask patch 16 16 [
set largecircle patches in-radius (2)
]
;set color of largecircle patches green
ask largecircle [
set pcolor white
]
end
to draw-exit1
;exits at top of concourse area, where turtles will leave
set goal-x -1
set goal-y -30
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit2
set goal-x -1
set goal-y 30
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit3
set goal-x -30
set goal-y -6
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit4
set goal-x 30
set goal-y 4
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
;;;;;;;;;;;;;;;;;;;;;;exit goals;;
to move-to-exit1
facexy -1 30
end
to move-to-exit2
facexy 30 4
end
to move-to-exit3
facexy -1 -30
end
to move-to-exit4
facexy -30 -6
end
;;;;;;;;;;;;;;;;;;;;;;;;;;
;to follow-leader
;if not leader? ;; we only want to ask non-leaders
;[let nearby-leaders turtles with [leader? and distance myself < 3] ;; find nearby leaders
;if any? nearby-leaders ;; to avoid 'nobody'-error, check if there are any first
; [ set heading (towards min-one-of nearby-leaders [distance myself]) ]] ;; then face the one closest to myself
; end
;;;;;;;;;;;;;;;;;;;;;;;;;;
to follow_leader
let nearby-leaders turtles with [is-leader? and distance myself < 10] ;; nearby leaders
if any? nearby-leaders[
face min-one-of nearby-leaders [distance myself]
fd 0.5]
end
In the future, please only provide relevant code. For NetLogo, that is usually the procedure that throws the error (or doesn't work correctly etc) and whichever procedure calls it.
In your case, this is the procedure you mentioned:
to follow_leader
let nearby-leaders turtles with [is-leader? and distance myself < 10]
if any? nearby-leaders[
face min-one-of nearby-leaders [distance myself]
fd 0.5]
end
So the and that is throwing the error must be [is-leader? and distance myself < 10] (also, please state which line if you know it). My guess would be that you haven't initialised the variable is-leader? so it is 0 (the default value) instead of either true or false.
If this is the problem, wherever you creates turtles, initialise with set is-leader? false

How to move turtles on a game board simulation?

I'm creating a board game simulation similar to monopoly, where pieces are moving around the board. My turtle acts as the user's game piece and moves around the border of the world. The border is set up in alternating colors. I have a button that generates the number of steps the turtle moves. Those steps are stored as dice-num, a global variable. However, when the turtle lands on the patches before the patch (16 -16) (the bottom right-hand corner of the board) and the number of steps it needs to move exceeds 1, I can't turn the turtle's heading halfway to move up the board. As a result it only moves back to the beginning of the board.
I've tried to treat each case separately:
;if the turtle lands on the patch before the corner
ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1
[setxy max-pxcor min-pycor
set dice-num dice-num - 1
show dice-num
]
]
;dice-num refers to number of steps the turtle moves
Heres my code so far:
to setup
board
end
to go
dice-roll
end
to board
ask patches [if pxcor = max-pxcor or pycor = max-pycor or pxcor = min-
pxcor or pycor = min-pycor
[set pcolor blue]]
ask patches [if pycor = max-pycor or pycor = min-pycor
[if pxcor mod 2 = 0
[set pcolor orange]]]
ask patches [if pxcor = max-pxcor or pxcor = min-pxcor
[if pycor mod 2 = 0
[set pcolor orange]]]
ask patch min-pxcor min-pycor [set pcolor green]
end
to dice-roll
set dice [1 2 3 4 5 6]
set dice-num one-of dice
user-message (word "You rolled: " dice-num)
ask turtle 0 [
fd dice-num
]
;allows the turtle to turn if it lands on a corner
ask turtle 0 [if ycor = min-pycor and xcor = max-pxcor [set heading 0]
if xcor = max-pxcor and ycor = max-pycor [set heading 270]
if xcor = min-pxcor and ycor = max-pycor [set heading 180]
;add a statement to end game once player rereaches the green patch
]
;if the turtle lands on the patch before the corner
ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1
[setxy max-pxcor min-pycor
set dice-num dice-num - 1
show dice-num
]
]
end
I expect the output to be the turtle to start moving up the right hand side of the board once it lands on the patch (15 -16) and recieves a dice-num greater than 1. However, when the turtle does land on the patch (15 -16) and the dice-num is greater than 1 it simply moves back to the beginning of the board.
Okay, this was simply too long for comments so I had to make some guesses. I expect the problem was that you were forwarding the full amount of the die roll after checking location. This would mean you have to test all the potential patches that could reach the corner. Instead, it is easier to move one patch and test location before moving the next patch. This is a cleaned up version of your code that implements this move and check process.
to setup
clear-all
board
ask one-of patches with [pcolor = green]
[ sprout 1
[ set color white
set heading 90
forward 1
]
]
end
to go
let die-num dice-roll
ask one-of turtles [ move die-num ]
end
to board
ask patches with [ pxcor = max-pxcor or pxcor = min-pxcor ]
[ set pcolor ifelse-value (pycor mod 2 = 0) [orange][blue] ]
ask patches with [ pycor = max-pycor or pycor = min-pycor ]
[ set pcolor ifelse-value (pxcor mod 2 = 0) [orange][blue] ]
ask patch min-pxcor min-pycor [set pcolor green]
end
to-report dice-roll
let dice-num one-of [1 2 3 4 5 6]
user-message (word "You rolled: " dice-num)
report dice-num
end
to move [#roll]
while [ #roll > 0 ]
[ if pxcor = max-pxcor and pycor = max-pycor [set heading one-of [180 270] ]
if pxcor = max-pxcor and pycor = min-pycor [set heading one-of [0 270] ]
if pxcor = min-pxcor and pycor = max-pycor [set heading one-of [180 90] ]
if pxcor = min-pxcor and pycor = min-pycor [ stop ]
fd 1
set #roll #roll - 1
]
end
The alternative is simply to add the die roll to the current location and see if it goes past. If it does, calculate where you want to get to.

writing a driving simulation and i want to use three commands at once to keep cars single file in a three patch road

This is for a car to not pass a car in front or in front to the left and to the right. how can i use all commands, how do i link them?
to place-cars-level0 ;creating basic real life driver
;make a random placement of cars
ask n-of (num-of-cars-level0 / 3) patches with [meaning = "road-up"] [
if not any? cars-on patch pxcor (pycor + 1) and not any? cars-here and not any? cars-on patch pxcor (pycor - 1) and not any? patches with [meaning = "crossing"] in-radius 2 [
sprout-cars 1 [
set size 3
set will-turn? "maybe"
set will-stop? "maybe"
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 0
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
] ; set the number of cars for the level, size, politness,speed
ask n-of (num-of-cars-level0 / 3) patches with [meaning = "road-down" and count turtles-on neighbors = 0] [
if not any? cars-on patch pxcor (pycor + 1) and not any? cars-here and not any? cars-on patch pxcor (pycor - 1) and not any? patches with [meaning = "crossing"] in-radius 2 [
sprout-cars 1 [
set size 3
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 180
set will-turn? "maybe"
set will-stop? "maybe"
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
]
ask n-of (num-of-cars-level0 / 3) patches with [meaning = "road-left" and
count turtles-on neighbors = 0] [
if not any? cars-on patch (pxcor + 1) pycor and not any? cars-here and not
any? cars-on patch (pxcor - 1) pycor and not any? patches with [meaning =
"crossing"] in-radius 2 [
sprout-cars 1 [
set will-turn? "maybe"
set will-stop? "maybe"
set size 3
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 270
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
]
while [count cars < num-of-cars-level0] [
ask one-of patches with [meaning = "road-right"] [
if not any? cars-on patch (pxcor + 1) pycor and not any? cars-here and not
any? cars-on patch (pxcor - 1) pycor and not any? patches with [meaning =
"crossing"] in-radius 2 [
sprout-cars 1 [
set will-turn? "maybe"
set will-stop? "maybe"
set size 3
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 90
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
]
]
end
in the section above is the code for writing the car placement and below is the speed and control
to control-speed
let car-ahead one-of cars-on patch-ahead 4
ifelse car-ahead = nobody [
ifelse speed < maxSpeed [set speed speed + acceleration] [set speed speed
- deceleration]
]
[
ifelse [speed] of car-ahead = 0 [set speed 0] [
ifelse [speed] of car-ahead >= maxSpeed [
set speed maxSpeed
set speed speed - deceleration
] [
;try to overtake
;ifelse [meaning] of patch-left-and-ahead 90 1 = meaning and not any? turtles-on patch-left-and-ahead 90 1 and [meaning] of patch-left-and-ahead 90 1 != "crossroad"
;and meaning != "crossing" and [meaning] of patch-left-and-ahead 180 1.3 != "crossing" and not any? turtles-on patch-left-and-ahead 169 3
;and not any? turtles-on patch-left-and-ahead 45 1 and not any? turtles-on patch-left-and-ahead 135 1 and not any? turtles-on patch-left-and-ahead 23 2
; and not any? turtles-on patch-left-and-ahead 157 2 and not any? turtles-on patch-left-and-ahead 12 3 and [meaning] of patch-ahead 1 != "crossing" [move-to patch-left-and-ahead 90 1] [
; ifelse [meaning] of patch-right-and-ahead 90 1 = meaning and not any? turtles-on patch-right-and-ahead 90 14 and [meaning] of patch-right-and-ahead 90 1 != "crossroad"
;and meaning != "crossing" and [meaning] of patch-right-and-ahead 180 1.3 != "crossing" and not any? turtles-on patch-right-and-ahead 12 3
;and not any? turtles-on patch-right-and-ahead 45 1 and not any? turtles-on patch-right-and-ahead 135 1 and not any? turtles-on patch-right-and-ahead 23 2
;and not any? turtles-on patch-right-and-ahead 157 2 and not any? turtles-on patch-right-and-ahead 169 3 and [meaning] of patch-ahead 1 != "crossing"[move-to patch-right-and-ahead 90 1] [
;set speed [speed] of car-ahead
;set speed speed - deceleration]
]
]
]
i hope this is more usefulthe image shows the overall simulation
if you can see the image, my aim is to keep one car in each lane rather then three side by side per lane, this is to simulate semi autonomous vehicle lane centering
Okay, instead of doing
let car-ahead one-of cars-on patch-ahead 4
what you probably want to do is create a patch-set of all the three patches that a potential car could be occupying. That's a little difficult without knowing how you have created the lanes, and you need to be careful because the car doing the asking could be in any of three patches too. After creating that patchset, you can simply do what you did before but using the patch-set.
let in-lane (patch-set patch-ahead 4 <other patches you need go here>)
let car-ahead one-of cars-on in-lane

Make Bases (shape squares) - Multiple sizes - Diagonal placement

I want to make diagonal bases (Perfet square), but it only work until size 2 more then 2 i start to lose the squares.
My code is:
to setup-patches
set-patch-size 15
ask patches[
if (distancexy 20 20) < base-size [set pcolor yellow]
if (distancexy -20 -20) < base-size [set pcolor yellow]
if (distancexy -20 20) < base-size [set pcolor pink]
if (distancexy 20 -20) < base-size [set pcolor pink]
]
end
Please check the picture to see what i am talking about.
Look at two examples in the Models Library: Moore and Von Neumann, and Neighborhoods Example. You want the box (Moore) neighborhood, and you should use at-points to get it. However, sticking close to your coded setup, you can also try this:
to-report linf [#p1 #p2]
let _xdist abs ([pxcor] of #p1 - [pxcor] of #p2)
let _ydist abs ([pycor] of #p1 - [pycor] of #p2)
report max (list _xdist _ydist)
end
to setup-patches [base-size]
clear-all
set-patch-size 15
let diag1 (patch-set patch 20 20 patch -20 -20)
ask diag1 [ask patches with [linf self myself < base-size] [set pcolor yellow]]
let diag2 (patch-set patch -20 20 patch 20 -20)
ask diag2 [ask patches with [linf self myself < base-size] [set pcolor pink]]
end