Skip to content

Instantly share code, notes, and snippets.

@dburles
Created March 15, 2013 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dburles/5169772 to your computer and use it in GitHub Desktop.
Save dburles/5169772 to your computer and use it in GitHub Desktop.
a little evolution experiment i made ages ago
# Plot random pixels.
require 'rubygame'
Width = 200
Height = 200
class Array
def rand
self[Kernel.rand(self.length)]
end
end
module Rubygame
class Surface
def put_pixel(point, color)
self.draw_box point, point, color
end
end
end
class Evolution
include Rubygame
def initialize
@screen = Screen.new [Width, Height]
@events = EventQueue.new
@clock = Clock.new
@clock.target_framerate = 120
@screen.fill [0, 0, 0]
@screen.update
@animals = []
@kills = 0
spawn 1, [[1,1]]
end
def spawn generation, parent = nil
#if generation > 1
large = 0
parent.each do |dna|
dna.each do |gene|
if gene > large
large = gene
end
end
end
randomx = rand(large + 2)
randomy = rand(large + 2)
#end
20.times do
animal = Animal.new
#animal.x = (150..250).to_a.rand
#animal.y = (150..250).to_a.rand
#animal.x = 200
#animal.y = 200
animal.x = rand(Width)
animal.y = rand(Height)
animal.generation = generation
animal.kills = 0
animal.age = 1
#animal.body_x.push(-(animal.gen..rand(2),rand(2))
#animal.body = parent if parent
#if generation > 1
#p parent
animal.body = parent + [[randomx,randomy]]
#end
p animal.body
@animals.push(animal)
animal = 0
end
end
def event_loop
life = 0
generation = 1
loop do
life += 1
@events.each { |event|
case event
when QuitEvent
return
end
}
if @animals.size <= 18
#if life == 100
if @kills > 0
life = 0
generation += 1
puts "GENERATION " + generation.to_s
@kills = 0
count = 0
win = ""
@animals.each do |animal|
#if animal.kills > count
# win = animal
# puts "winner " + win.body.to_s
# kill_count = animal.kills
# puts kill_count.to_s
#end
if animal.age > count
win = animal
count = win.age
end
end
puts "winner " + win.body.to_s
puts "age " + win.age.to_s
spawn(win.generation, win.body)
else
life = 0
end
end
@screen.fill [0, 0, 0]
draw
@clock.tick
@screen.update
end
end
def draw
#point = [rand(Width), rand(Height)]
#color = [rand(255), rand(255), rand(255)]
color = [255, 255, 255]
@locations = {}
@stingers = {}
@animals.each do |animal|
locations = []
stinger_locations = []
animal.age += 1
#p animal.x
#p animal.y
if rand(2) == 1
animal.x = animal.x + 1 if animal.x <= Width
else
animal.x = animal.x - 1 if animal.x >= 0
end
if rand(2) == 1
animal.y = animal.y + 1 if animal.y <= Height
else
animal.y = animal.y - 1 if animal.y >= 0
end
#puts animal.x.to_s + " " + animal.y.to_s
locations.push([animal.x, animal.y])
#@screen.put_pixel [animal.x, animal.y], color
#build the body if it has one
if animal.body
i = 0
animal.body.each do |xy|
# twice + and - for symmetry
locations.push([animal.x - xy[0] - i, animal.y + xy[1] + i])
locations.push([animal.x + xy[0] + i, animal.y - xy[1] - i])
locations.push([animal.x + xy[0] + i, animal.y + xy[1] + i])
locations.push([animal.x - xy[0] - i, animal.y - xy[1] - i])
#p locations
#exit
i += 1
end
end
=begin
animal.body_x.each do |pixel|
p pixel
locations.push([animal.x - pixel, animal.y])
#@screen.put_pixel [animal.x - pixel, animal.y], color
end
animal.body_y.each do |pixel|
locations.push([animal.x, animal.y - pixel])
#@screen.put_pixel [animal.x, animal.y - pixel], color
end
=end
#add the stinger
#@screen.put_pixel [animal.x, animal.y - animal.stinger + 1], [255, 0, 0]
#@stingers[animal] = [animal.x, animal.y - animal.stinger]
@stingers[animal] = [animal.x, animal.y - animal.generation]
@locations[animal] = locations
#p locations
#exit
end
@stingers.each do |animal, stinger|
@locations.each do |panimal, location|
location.each do |xy|
if (xy == stinger && animal != panimal)
p xy
#kill animal
#kill.push(panimal)
#print animal.to_s + " <- killed -> " + panimal.to_s
@animals.delete(panimal)
animal.kills += 1
@kills += 1
puts @kills.to_s
end
end
end
end
@locations.each do |animal, location|
location.each do |pixel|
@screen.put_pixel pixel, color
end
end
@stingers.each do |animal, stinger|
@screen.put_pixel stinger, [255, 0, 0]
end
#p @locations
#exit
end
end
=begin
+++
+0+
+++
=end
class Animal
attr_accessor :x
attr_accessor :y
attr_accessor :body
attr_accessor :generation
attr_accessor :kills
attr_accessor :age
def stinger
return body.length
end
end
Rubygame.init
Evolution.new.event_loop
Rubygame.quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment