Skip to content

Commit

Permalink
Added curses interface. Removed unneded euclid libs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Chambers committed Apr 29, 2013
1 parent d97dc21 commit f871270
Show file tree
Hide file tree
Showing 8 changed files with 1,241 additions and 4,960 deletions.
10 changes: 9 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
Optimized Boids (2D)
====================

run with python optboid.py
There are two GUIs for the simulation.
One based on PyGlet which must first be installed. Run this with

python glboid.py

The second is a curses (terminal) interface. Run with

python curseboid.py


An optimized version of C. Reynolds flocking simulation which uses "boids"
with simple rules to reproduce the behaviour of flocking creatures.
Expand Down
78 changes: 78 additions & 0 deletions curseboid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/python
# -*- coding: utf8 -*-
from __future__ import (division, print_function,
absolute_import)

import curses
import simulation
import math
from time import time


class World(object):
"""
world class with draw functions for entities
"""
def __init__(self, swarm, width):
self.width = width
self.swarm = swarm
self.ents = swarm.boids # this will point to a list of boids
self.ent_char = '^'
self.num_ents = len(swarm.boids)
#self.fps = clock.ClockDisplay()
self._basescr = curses.initscr()
self.scr = curses.newwin(80, 80, 0, 0)
self.scr.border(0)
self.scr.refresh()

self._update_time = 0
self._fps = 0

def draw_entity(self, e):
""" Draws a boid """
x = int(math.floor(e.position.x / self.width))
y = int(math.floor(e.position.y / self.width))
self.scr.addstr(x, y, self.ent_char)

def draw_fps(self, dt):
self._update_time += dt
if self._update_time > 0.5:
self._fps = 1 / dt
self._update_time = 0

self.scr.addstr(3, 3, '%.1f fps' % self._fps)
self.scr.refresh()

def draw(self, dt):
self.scr.clear()
#self.draw_grid()
for ent in self.ents:
self.draw_entity(ent)
self.draw_fps(dt)
self.scr.refresh()


sim = simulation.FlockSimulation(100, 700)
world = World(sim.swarm, 10)


def run():
go = True
prev_time = time()
print('starting')
try:
while go:
now_time = time()
dt = now_time - prev_time
prev_time = now_time
sim.update(dt)
world.draw(dt)
#x = world.scr.getkey()
#if x == 'e':
# go = False
except KeyboardInterrupt:
curses.endwin()


if __name__ == '__main__':
run()
Loading

0 comments on commit f871270

Please sign in to comment.