Walking duck

Post Reply
Melle
Posts: 6
Joined: Fri Dec 28, 2012 9:23 pm

Walking duck

Post by Melle »

Hi all,

Here a program of a walking duck on a 50x50 grid.

Rules are:
When cell has no color:
Duck will color cell yellow and move right.
When cell has yellow:
Duck will color red and move left.
When color has red:
Duck will color yellow and move right.

It turns out the grid is coloured quite frantic.
image.jpg
image.jpg (504.74 KiB) Viewed 4858 times
image.jpg
image.jpg (530.34 KiB) Viewed 4858 times
image.jpg
image.jpg (592.23 KiB) Viewed 4858 times

Code: Select all



graphics
maxx = screen_width()
maxy = screen_height()
circlex = maxx / 50
circley = maxy /50

dim field(51,51)
gosub fieldclear

gosub drawfield

duckx = 24
ducky = 24

xr =1
yr=0
padr=1

field(duckx, ducky) = 1
gosub drawfield

for walk = 1 to 10000
  
  gosub drawcell
  gosub move
  if field(duckx, ducky)= 0 then
    vulkleur = 1
  end if
  if field(duckx, ducky)= 1 then
    vulkleur=2
  end if
   if field(duckx, ducky)= 2 then
    vulkleur=1
  end if
  field(duckx, ducky) = vulkleur
  gosub drawcell
  gosub newr
 pause 0.02
next walk

gosub drawfield
end

drawcell:
   if field(duckx, ducky) = 0 then
     fill color 0.1, 0.1, 0.1
   end if
   if field(duckx, ducky) = 1 then
     fill color 0.7, 0.7, 0
    end if
   if field(duckx, ducky) = 2 then
     fill color 0.8, 0.2, 0.2
    end if

   
   posx = circlex * duckx -6
   posy = circley * ducky -6
   
    fill circle posx, posy size 6
return


drawfield:
for i=1 to 50
  for j=1 to 50
   if field(i,j) = 0 then
     fill color 0.1, 0.1, 0.1
   end if
   if field(i,j) = 1 then
     fill color 0.7, 0.4, 0
    end if
if field(i,j) = 2 then
     fill color 0.8, 0.2, 0.5
    end if

   
   posx = circlex * i -6
   posy = circley * j -6
   
    fill circle posx, posy size 6
   
  next j
next i
return

fieldclear:
for i=1 to 50
  for j=1 to 50
   field(i,j)=0
  next j
next i
return


move:
duckx = duckx + xr
ducky = ducky + yr
return

newr:
r = field(duckx,ducky)
if r=1 then
gosub left
else
gosub right
end if
return

left:
padr=0
if xr=1 then
  xr=0
  yr=-1
  return
end if
if xr=-1 then
  xr=0
  yr=1
  return
end if
if yr=-1 then
  yr=0
  xr=-1
  return
end if
if yr=1 then
  yr=0
  xr=1
  return
end if


right:
padr=1
if xr=1 then
  xr=0
  yr=1
  return
end if
if xr = -1 then
let xr=0
yr=-1
return
end if
if yr =-1 then
  yr=0
  xr=1
  return
end if
if yr=1 then
  yr=0
  xr=-1
  return
end if
return

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Walking duck

Post by Henko »

Quit amazing that the pattern shows randomness without the random function beiing used.
There are no fences at the borders of the field. The duck wants to leave the field and causes an "out of bound" error. :D

Melle
Posts: 6
Joined: Fri Dec 28, 2012 9:23 pm

Re: Walking duck

Post by Melle »

Yes indeed very random! I was hoping to see some symmetry.

I now have a slightly improved version, because it shows where the action is.
Colouring the actual position of the duck white makes it more clear.

Also changed the rules which leads to a duck that seem to want to 'grow' its playground!

I would call this one the yellow fence builder :-)

Code: Select all

graphics
maxx = screen_width()
maxy = screen_height()
circlex = maxx / 50
circley = maxy /50

dim field(51,51)
gosub fieldclear

gosub drawfield

duckx = 24
ducky = 24

xr =1
yr=0
padr=1

field(duckx, ducky) = 0
gosub drawfield

for walk = 1 to 10000
  
  
  
  
  gosub whitecircle
  pause .1
  if field(duckx, ducky)= 0 then
    vulkleur = 1
  end if
  if field(duckx, ducky)= 1 then
    vulkleur=2
  end if
   if field(duckx, ducky)= 2 then
    vulkleur=2
  end if
  
  field(duckx, ducky) = vulkleur
gosub drawcell
  gosub removecircle
  gosub move
gosub newr
  
  
  
  
 
next walk

gosub drawfield
end

drawcell:
   if field(duckx, ducky) = 0 then
     fill color 0.1, 0.1, 0.1
   end if
   if field(duckx, ducky) = 1 then
     fill color 0.7, 0.7, 0
    end if
   if field(duckx, ducky) = 2 then
     fill color 0.8, 0.2, 0.2
    end if

   
   posx = circlex * duckx -6
   posy = circley * ducky -6
   
    fill circle posx, posy size 6
return

removecircle:
gosub determinexy
draw color 0.1, 0.1, 0.1
draw circle posx, posy size 6
return

whitecircle:
gosub determinexy
draw color 1, 1, 1
draw circle posx, posy size 6
return


determinexy:
posx = circlex * duckx -6
   posy = circley * ducky -6
return

drawfield:
for i=1 to 50
  for j=1 to 50
   if field(i,j) = 0 then
     fill color 0.1, 0.1, 0.1
   end if
   if field(i,j) = 1 then
     fill color 0.7, 0.4, 0
    end if
if field(i,j) = 2 then
     fill color 0.8, 0.2, 0.5
    end if

   
   posx = circlex * i -6
   posy = circley * j -6
   
    fill circle posx, posy size 6
   
  next j
next i
return

fieldclear:
for i=1 to 50
  for j=1 to 50
   field(i,j)=0
  next j
next i
return


move:
duckx = duckx + xr
ducky = ducky + yr
return

newr:
r = field(duckx,ducky)
if r=0 then
gosub left
end if
if r=1 then
gosub right
end if
return

right:
padr=0
if xr=1 then
  xr=0
  yr=-1
  return
end if
if xr=-1 then
  xr=0
  yr=1
  return
end if
if yr=-1 then
  yr=0
  xr=-1
  return
end if
if yr=1 then
  yr=0
  xr=1
  return
end if


left:
padr=1
if xr=1 then
  xr=0
  yr=1
  return
end if
if xr = -1 then
let xr=0
yr=-1
return
end if
if yr =-1 then
  yr=0
  xr=1
  return
end if
if yr=1 then
  yr=0
  xr=-1
  return
end if
return




User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Walking duck

Post by Dutchman »

I extended the previous version with some randomness and a finish function.

Code: Select all

/*
Walking duck 2
by Melle » Sat Nov 14, 2015
Hi all,
Here a program of a walking duck on a 50x50 grid.
Rules are:
When cell has no color:
Duck will color cell yellow and move right.
When cell has yellow:
Duck will color red and move left.
When color has red:
Duck will color yellow and move right.
It turns out the grid is coloured quite frantic.
Version 2:
Extended by Dutchman with randomness and finish-function
*/
noise=0.05 'determines randomness
break=10 ' determines interval in milliseconds
graphics
maxx = screen_width()
maxy = screen_height()
circlex = maxx / 50
circley = maxy /50

dim field(51,51) ' initialises with zeros

gosub drawfield

duckx = 24
ducky = 24

xr =1
yr=0
padr=1

field(duckx, ducky) = 1
gosub drawfield

DO
  gosub drawcell
  gosub move
  if field(duckx, ducky)= 0 then
    vulkleur = 1
  end if
  if field(duckx, ducky)= 1 then
    vulkleur=2
  end if
   if field(duckx, ducky)= 2 then
    vulkleur=1
  end if
  IF RND(1)<noise THEN vulkleur=RND(3)
  field(duckx, ducky) = vulkleur
  gosub drawcell
  gosub newr
 pause break/1000
UNTIL forever
end

drawcell:
   if field(duckx, ducky) = 0 then
     fill color 0.1, 0.1, 0.1
   end if
   if field(duckx, ducky) = 1 then
     fill color 0.7, 0.7, 0
    end if
   if field(duckx, ducky) = 2 then
     fill color 0.8, 0.2, 0.2
    end if

   
   posx = circlex * duckx -6
   posy = circley * ducky -6
   
    fill circle posx, posy size 6
return


drawfield:
for i=1 to 50
  for j=1 to 50
   if field(i,j) = 0 then
     fill color 0.1, 0.1, 0.1
   end if
   if field(i,j) = 1 then
     fill color 0.7, 0.4, 0
    end if
if field(i,j) = 2 then
     fill color 0.8, 0.2, 0.5
    end if

   
   posx = circlex * i -6
   posy = circley * j -6
   
    fill circle posx, posy size 6
   
  next j
next i
return

move:
duckx = duckx + xr
ducky = ducky + yr
IF MIN(duckx,ducky)=0 OR MAX(duckx,ducky)=51 THEN Finish
return

newr:
r = field(duckx,ducky)
if r=1 then
gosub left
else
gosub right
end if
return

left:
padr=0
if xr=1 then
  xr=0
  yr=-1
  return
end if
if xr=-1 then
  xr=0
  yr=1
  return
end if
if yr=-1 then
  yr=0
  xr=-1
  return
end if
if yr=1 then
  yr=0
  xr=1
  return
end if


right:
padr=1
if xr=1 then
  xr=0
  yr=1
  return
end if
if xr = -1 then
let xr=0
yr=-1
return
end if
if yr =-1 then
  yr=0
  xr=1
  return
end if
if yr=1 then
  yr=0
  xr=-1
  return
end if
return

DEF Finish
T$="Duck is gone!"
width=TEXT_WIDTH(T$)
fontsize=.maxx/2/width*FONT_SIZE()
DRAW FONT SIZE fontsize
width=TEXT_WIDTH(T$)
height=TEXT_HEIGHT(T$)
DRAW COLOR 0.3,1,1
DRAW TEXT T$ AT (.maxx-width)/2,(.maxy-height)/2
STOP
END DEF
finished duck
finished duck
duck.png (458.14 KiB) Viewed 4783 times

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Walking duck

Post by Henko »

And with some other minor modifications...

Code: Select all

/*
Walking duck 2
by Melle » Sat Nov 14, 2015
Hi all,
Here a program of a walking duck on a 50x50 grid.
Rules are:
When cell has no color:
Duck will color cell yellow and move right.
When cell has yellow:
Duck will color red and move left.
When color has red:
Duck will color yellow and move right.
It turns out the grid is coloured quite frantic.
Version 2:
Extended by Dutchman with randomness and finish-function
*/
noise=0.05 'determines randomness
break=10 ' determines interval in milliseconds
graphics
maxx = screen_width()
maxy = screen_height()
circlex = maxx / 50
circley = maxy /64

dim field(51,65) ' initialises with zeros

' gosub drawfield

duckx = 24
ducky = 24

xr =1
yr=0
padr=1

field(duckx, ducky) = 1
' gosub drawfield

DO
  gosub drawcell
  gosub move
  if field(duckx, ducky)= 0 then
    vulkleur = 1
  end if
  if field(duckx, ducky)= 1 then
    vulkleur=2
  end if
   if field(duckx, ducky)= 2 then
    vulkleur=1
  end if
  IF RND(1)<noise THEN vulkleur=RND(3)
  field(duckx, ducky) = vulkleur
  gosub drawcell
  gosub newr
'  pause break/1000
UNTIL forever
end

drawcell:
   if field(duckx, ducky) = 0 then
     fill color 0.1, 0.1, 0.1
   end if
   if field(duckx, ducky) = 1 then
     fill color 0.7, 0.7, 0
    end if
   if field(duckx, ducky) = 2 then
     fill color 0.8, 0.2, 0.2
    end if

   
   posx = circlex * duckx -6
   posy = circley * ducky -6
   
    fill rect posx, posy size 7
return


drawfield:
for i=1 to 50
  for j=1 to 50
   if field(i,j) = 0 then
     fill color 0.1, 0.1, 0.1
   end if
   if field(i,j) = 1 then
     fill color 0.7, 0.4, 0
    end if
if field(i,j) = 2 then
     fill color 0.8, 0.2, 0.5
    end if

   
   posx = circlex * i -6
   posy = circley * j -6
   
    fill circle posx, posy size 6
   
  next j
next i
return

move:
duckx = duckx + xr
ducky = ducky + yr
IF MIN(duckx,ducky)=0 OR duckx=51 or ducky=65 THEN Finish
return

newr:
r = field(duckx,ducky)
if r=1 then
gosub left
else
gosub right
end if
return

left:
padr=0
if xr=1 then
  xr=0
  yr=-1
  return
end if
if xr=-1 then
  xr=0
  yr=1
  return
end if
if yr=-1 then
  yr=0
  xr=-1
  return
end if
if yr=1 then
  yr=0
  xr=1
  return
end if


right:
padr=1
if xr=1 then
  xr=0
  yr=1
  return
end if
if xr = -1 then
let xr=0
yr=-1
return
end if
if yr =-1 then
  yr=0
  xr=1
  return
end if
if yr=1 then
  yr=0
  xr=-1
  return
end if
return

DEF Finish
T$="Duck is gone!"
width=TEXT_WIDTH(T$)
fontsize=.maxx/2/width*FONT_SIZE()
DRAW FONT SIZE fontsize
width=TEXT_WIDTH(T$)
height=TEXT_HEIGHT(T$)
DRAW COLOR 0.3,1,1
DRAW TEXT T$ AT (.maxx-width)/2,(.maxy-height)/2
STOP
END DEF

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Walking duck

Post by Dutchman »

These 'minor modifications' are for portrait mode only :?:

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Walking duck

Post by Henko »

Portrait mode? May be the case, i didn't care. I'm a sloppy programmer.
Here is another modification: a high voltage fence is added, throwing the duck back into the field if it tries to escape.

Code: Select all

' Walking duck 3
' by Melle (org), Dutchman, and Henko » Sat Nov 14, 2015
'
noise=0.05 'determines randomness
break=10 ' determines interval in milliseconds
graphics
maxx = screen_width() ! maxy = screen_height()
circlex = maxx / 50 ! circley = maxy /64

dim field(51,65) ' initialises with zeros
duckx = 24 ! ducky = 24 ! xr =1 ! yr=0
field(duckx, ducky) = 1
DO
  gosub drawcell ! gosub move
  if field(duckx, ducky)%2= 0 then vulkleur = 1
  if field(duckx, ducky)= 1 then vulkleur=2
  IF RND(1)<noise THEN vulkleur=RND(3)
  field(duckx, ducky) = vulkleur
  gosub drawcell
  if field(duckx,ducky)=1 then gosub left else gosub right
UNTIL forever
end

drawcell:
if field(duckx,ducky)=0 then fill color 0.1, 0.1, 0.1
if field(duckx,ducky)=1 then fill color 0.7, 0.7, 0
if field(duckx,ducky)=2 then fill color 0.8, 0.2, 0.2
posx=circlex*duckx-6 ! posy=circley*ducky-6
fill rect posx, posy size 7
return

move:
duckx=duckx+xr ! ducky=ducky+yr
IF duckx=0 OR duckx=51 then duckx=1+rnd(50)
IF ducky=0 OR ducky=65 then ducky=1+rnd(64)
return

left:
if xr=1 then  ! xr=0 ! yr=-1 ! return ! end if
if xr=-1 then ! xr=0 ! yr=1  ! return ! end if
if yr=-1 then ! yr=0 ! xr=-1 ! return ! end if
if yr=1 then  ! yr=0 ! xr=1  ! return ! end if

right:
if xr=1 then  ! xr=0 ! yr=1  ! return ! end if
if xr=-1 then ! xr=0 ! yr=-1 ! return ! end if
if yr=-1 then ! yr=0 ! xr=1  ! return ! end if
if yr=1 then  ! yr=0 ! xr=-1 ! return ! end if

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Walking duck

Post by rbytes »

Here is my contribution. I kept the enhancements by Dutchman, but restored the white circle (now filled) to show exactly where the duck Is moving. And now the duck has a quack, which fades away once he escapes. I really feel sorry for this little duck, as he seems to be in a real panic. Sometimes I feel like cheering when he escapes!

You will need the duck.mp3 attachment to make it quack. :P

Code: Select all

/*
Walking duck 2.1
by Melle » Sat Nov 14, 2015
Hi all,
Here a program of a walking duck on a 50x50 grid.
Rules are:
When cell has no color:
Duck will color cell yellow and move right.
When cell has yellow:
Duck will color red and move left.
When color has red:
Duck will color yellow and move right.
It turns out the grid is coloured quite frantic.
Version 2.1:
Modified by ricardobytes to restore white circle and add sound
Version 2:
Extended by Dutchman with randomness and finish-function
*/
f$="duck.mp3"
c$="player"
MUSIC c$ LOAD f$
noise=0.05 'determines randomness
BREAK=10 ' determines interval in milliseconds
GRAPHICS
maxx = SCREEN_WIDTH()
maxy = SCREEN_HEIGHT()
circlex = maxx / 50
circley = maxy /50

DIM FIELD(51,51) ' initialises with zeros

GOSUB drawfield

duckx = 24
ducky = 24

xr =1
yr=0
padr=1

FIELD(duckx, ducky) = 1
GOSUB drawfield

DO
  IF NOT MUSIC_PLAYING(c$) THEN
    MUSIC c$ PLAY
  ENDIF
  GOSUB drawcell
  GOSUB removecircle
  GOSUB move
  IF FIELD(duckx, ducky)= 0 THEN
    vulkleur = 1
  END IF
  IF FIELD(duckx, ducky)= 1 THEN
    vulkleur=2
  END IF
   IF FIELD(duckx, ducky)= 2 THEN
    vulkleur=1
  END IF
  IF RND(1)<noise THEN vulkleur=RND(3)
  FIELD(duckx, ducky) = vulkleur
  GOSUB drawcell
  GOSUB whitecircle
  GOSUB newr
 PAUSE BREAK/1000
UNTIL forever
END

drawcell:
   IF FIELD(duckx, ducky) = 0 THEN
     FILL COLOR 0.1, 0.1, 0.1
   END IF
   IF FIELD(duckx, ducky) = 1 THEN
     FILL COLOR 0.7, 0.7, 0
    END IF
   IF FIELD(duckx, ducky) = 2 THEN
     FILL COLOR 0.8, 0.2, 0.2
    END IF

   
   posx = circlex * duckx -6
   posy = circley * ducky -6
   
    FILL CIRCLE posx, posy SIZE 6
RETURN

removecircle:
GOSUB determinexy
DRAW COLOR 0.1, 0.1, 0.1
DRAW CIRCLE posx, posy SIZE 6
RETURN

whitecircle:
GOSUB determinexy
FILL COLOR 1, 1, 1
FILL CIRCLE posx, posy SIZE 6
RETURN

determinexy:
posx = circlex * duckx -6
   posy = circley * ducky -6
RETURN

drawfield:
FOR i=1 TO 50
  FOR j=1 TO 50
   IF FIELD(i,j) = 0 THEN
     FILL COLOR 0.1, 0.1, 0.1
   END IF
   IF FIELD(i,j) = 1 THEN
     FILL COLOR 0.7, 0.4, 0
    END IF
IF FIELD(i,j) = 2 THEN
     FILL COLOR 0.8, 0.2, 0.5
    END IF

   
   posx = circlex * i -6
   posy = circley * j -6
   
    FILL CIRCLE posx, posy SIZE 6
   
  NEXT j
NEXT i
RETURN

move:
duckx = duckx + xr
ducky = ducky + yr
IF MIN(duckx,ducky)=0 OR MAX(duckx,ducky)=51 THEN Finish
RETURN

newr:
r = FIELD(duckx,ducky)
IF r=1 THEN
GOSUB LEFT
ELSE
GOSUB RIGHT
END IF
RETURN

LEFT:
padr=0
IF xr=1 THEN
  xr=0
  yr=-1
  RETURN
END IF
IF xr=-1 THEN
  xr=0
  yr=1
  RETURN
END IF
IF yr=-1 THEN
  yr=0
  xr=-1
  RETURN
END IF
IF yr=1 THEN
  yr=0
  xr=1
  RETURN
END IF


RIGHT:
padr=1
IF xr=1 THEN
  xr=0
  yr=1
  RETURN
END IF
IF xr = -1 THEN
LET xr=0
yr=-1
RETURN
END IF
IF yr =-1 THEN
  yr=0
  xr=1
  RETURN
END IF
IF yr=1 THEN
  yr=0
  xr=-1
  RETURN
END IF
RETURN

DEF Finish
T$="Duck is gone!"
width=TEXT_WIDTH(T$)
fontsize=.maxx/2/width*FONT_SIZE()
DRAW FONT SIZE fontsize
width=TEXT_WIDTH(T$)
height=TEXT_HEIGHT(T$)
DRAW COLOR 0.3,1,1
DRAW TEXT T$ AT (.maxx-width)/2,(.maxy-height)/2
FOR n=1 TO 100
  IF NOT MUSIC_PLAYING(.c$) THEN
    MUSIC .c$ PLAY
  ENDIF
  MUSIC .c$ VOLUME (100-n)/100
  PAUSE .06
NEXT n
STOP
END DEF
Attachments
duck.mp3
(40 KiB) Downloaded 321 times
The only thing that gets me down is gravity...

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Walking duck

Post by Dutchman »

@Henko: maybe sloppy, but efficient

@Ricardobytes, a programmer with a heart for animals. :D :lol:
Thanks to bring the duck to life. And indeed, I feel like cheering too when he escapes!
(sob and sigh)

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Walking duck

Post by Henko »

When turning this into a game, name it "The lost duckdom"

Post Reply