[IAEP] 7th-grade biology

forster at ozonline.com.au forster at ozonline.com.au
Wed Jul 1 04:51:13 EDT 2009


Bert
Here's the pseudocode:

Information about object: controller

Sprite: <no sprite>
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: <no parent>
Mask: <same as sprite>

Create Event:
set Alarm 0 to 7
set variable foodnumber to 0
set variable bugnumber to 0
execute code:

show_info()
set variable depth to -500
set variable writetofile to 0
set variable displayanything to 2

Alarm Event for alarm 0:
create instance of object food at position (10+ random(room_width-20),10+ random(room_height-20))
set Alarm 0 to 7

Alarm Event for alarm 1:
execute code:

//this alarm is set by the f key
file_write_string(string(foodnumber) +", " +string(bugnumber))
file_writeln() 
alarm[1]=50       //save every 50th step to file


Keyboard Event for <Escape> Key:
execute code:

// if info boxes are displayed close them, otherwise quit the simulation
numbershowing=0
with(bug)
{
    controller.numbershowing+=showstats
    showstats=0
    depth=1000
}  
if expression numbershowing=0 is true
      if displayanything is equal to 2
            set variable displayanything to 1
      else
            if the player does say yes to the question: Quit?
                  end the game

Other Event: Game End:
execute code:

file_close()

Draw Event:
if displayanything is larger than 0
      set the font for drawing text to font0 and align left
      set the drawing color to 16777215
      draw rectangle with vertices (0,0) and (displayanything*170-90,displayanything*80-50), filled
      set the drawing color to 0
      draw rectangle with vertices (0,0) and (displayanything*170-90,displayanything*80-50), outline
      at position (0,0) draw text: " food " +string(foodnumber)
      at position (0,10) draw text: " bugs " +string(bugnumber)
      if displayanything is larger than 1
            at position (0,20) draw text:  p      pause
            at position (0,30) draw text:  c      de-select all
            at position (0,40) draw text:  a      select all
            at position (0,50) draw text:  d      display detail, affects speed
            at position (0,60) draw text:  f      save population stats to comma separated file
            at position (0,70) draw text:  left click   select bug
            at position (0,80) draw text:  F1   help
            at position (0,90) draw text:  esc   return to simulation

Key Press Event for A-key Key:
execute code:

// show info boxes for all
with(bug)
{
    showstats=1
  depth=-1000
}  
displayanything=2

Key Press Event for C-key Key:
execute code:

//close all info boxes
with(bug)
{
    showstats=0
  depth=1000
}  

Key Press Event for D-key Key:
if displayanything is equal to 0
      set variable displayanything to 2
else
      if displayanything is equal to 1
            set variable displayanything to 0
      else
            if displayanything is equal to 2
            set variable displayanything to 1

Key Press Event for F-key Key:
if writetofile is equal to 0
      if the player does say yes to the question: Save population stats to smartbug.txt?
            execute code:

alarm[1]=1
writetofile=1
file_open_write("smartbug.txt")
file_write_string("foodnumber, bugnumber")
file_writeln() 
room_caption=room_caption +"  (statistics are being saved to file)"


Key Press Event for P-key Key:
execute code:

keyboard_wait();
 // waits until user presses a key
io_clear(); 
// clears all keyboard and mouse states

Information about object: bug

Sprite: sprite1
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: <no parent>
Mask: <same as sprite>

Create Event:
set variable showstats to 0
set variable controller.bugnumber relative to 1
set variable bughealth to 500
execute code:

// initialise the input, gene and output arrays

for (i=0 ;i<20; i+=1)
{
    input[i]=0
    for (j=0;j<12; j+=1)
    {
        gene[i,j]= random(100)-50
    }
}
for (j=0;j<12; j+=1)

{
    output[j]= 0
}

set variable depth to 1000

 Step Event:
set variable bughealth relative to -1
if bughealth is smaller than 0
      destroy the instance
      set variable controller.bugnumber relative to -1
execute code:

input[0]= sign(instance_position(x+15,y,food))
input[1]= sign(instance_position(x-15,y,food))
input[2]= sign(instance_position(x,y+15,food))
input[3]= sign(instance_position(x,y-15,food))
input[4]= sign(instance_position(x+25,y,food))
input[5]= sign(instance_position(x-25,y,food))
input[6]= sign(instance_position(x,y+25,food))
input[7]= sign(instance_position(x,y-25,food))
 
for (i=8; i<20; i+=1)
{
    input[i] = sign(output[i-8])
}


for (j=0 ;j<12; j+=1)
{
    output[j]=0
    for (i=0;i<20; i+=1)
    {
        output[j]= output[j] + gene[i,j]* input[i]
    }
}
vspeed=0
hspeed=0
if (output[0]>0)hspeed-=1
if (output[1]>0)hspeed+=1
if (output[2]>0)vspeed-=1
if (output[3]>0)vspeed+=1




Collision Event with object food:
set variable bughealth relative to 150
for other object: destroy the instance
if bughealth is larger than 1000
      execute code:

bughealth=500
newid=instance_create(x+20,y+20,bug)
//copy over the parent genes
for (i=0 ;i<20; i+=1)
{
    for (j=0;j<12; j+=1)
    {
        newid.gene[i,j]= gene[i,j]
    }
}
// mutate one gene
newid.gene[random(20),random(12)]=random(100)-50

Mouse Event for Left Button:
if showstats is equal to 0
      set variable showstats to 1
      set variable depth to -1000
      set variable controller.displayanything to 2
else
      set variable showstats to 0
      set variable depth to 1000

Other Event: Outside Room:
if x is larger than room_width
      set variable x to 0
if x is smaller than 0
      set variable x to room_width
if y is larger than room_height
      set variable y to 0
if y is smaller than 0
      set variable y to room_height

Draw Event:
at relative position (0,0) draw image -1 of sprite sprite1
if controller.displayanything is larger than 0
      at relative position (0,10) draw text: "" +string(bughealth)
      if showstats is not equal to 0
            execute code:

draw_line(x,y,x+30,y+30)
draw_set_color(c_white)
draw_rectangle(x+30,y+30,x+420,y+200,false)
draw_set_color(c_black)
draw_rectangle(x+30,y+30,x+420,y+200,true)
displaystring= "input "
for (i=0; i<20; i+=1) displaystring=displaystring + string(sign(input[i])) + " "
draw_text(x+35,y+30,displaystring)
draw_text(x+35,y+40,"gene")
for (j=0; j<12; j+=1)
{
    displaystring= ""
    for (i=0; i<20; i+=1) 
    {
        displaystring=displaystring + string (round(gene[i,j])) + " "
    }
    draw_text(x+35,y+50+10*j,displaystring)    
}
displaystring= "output "
for (j=0; j<12; j+=1) displaystring=displaystring + string(sign(output[j])) + " "
draw_text(x+35,y+170,displaystring)

//draw food sensors, red if food sensed
if(instance_position(x+15,y,food)= noone) 
    draw_sprite(sprite2,-1,x+15,y)
else draw_sprite(sprite3,-1,x+15,y)
if(instance_position(x-15,y,food)= noone) 
    draw_sprite(sprite2,-1,x-15,y)
else draw_sprite(sprite3,-1,x-15,y)
if(instance_position(x+25,y,food)= noone) 
    draw_sprite(sprite2,-1,x+25,y)
else draw_sprite(sprite3,-1,x+25,y)
if(instance_position(x-25,y,food)= noone) 
    draw_sprite(sprite2,-1,x-25,y)
else draw_sprite(sprite3,-1,x-25,y)
if(instance_position(x,y+15,food)= noone) 
    draw_sprite(sprite2,-1,x,y+15)
else draw_sprite(sprite3,-1,x,y+15)
if(instance_position(x,y-15,food)= noone) 
    draw_sprite(sprite2,-1,x,y-15)
else draw_sprite(sprite3,-1,x,y-15)
if(instance_position(x,y+25,food)= noone) 
    draw_sprite(sprite2,-1,x,y+25)
else draw_sprite(sprite3,-1,x,y+25)
if(instance_position(x,y-25,food)= noone) 
    draw_sprite(sprite2,-1,x,y-25)
else draw_sprite(sprite3,-1,x,y-25)


Information about object: food

Sprite: sprite0
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: <no parent>
Mask: <same as sprite>

Create Event:
set variable controller.foodnumber relative to 1
set variable depth to 1000

Destroy Event:
set variable controller.foodnumber relative to -1



More information about the IAEP mailing list