Skip to content

Commit

Permalink
now running the reading in a separate thread, still sometimes updates…
Browse files Browse the repository at this point in the history
… seems irreguar, its better but still wrong
  • Loading branch information
pedersen committed Nov 15, 2012
1 parent 58a2ae3 commit dfbb23a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
11 changes: 6 additions & 5 deletions life.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def show_state():
print s


fixed_cmd_cnt = 1000
fixed_cmd_cnt = 10000

for i in range(generations):

Expand All @@ -83,11 +83,12 @@ def show_state():
print cmd

sys.stdout.flush()
#busy loop
for b in range(cnt):
print "#", i, ": busy ", (fixed_cmd_cnt - b)

update_cells()
time.sleep(.76)

# #busy loop
# for b in range(cnt):
# print "#", i, ": busy ", (fixed_cmd_cnt - b)



Expand Down
2 changes: 1 addition & 1 deletion rand.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
H = 20


for i in range(1000):
for i in range(100000):
x = random.randint(0, W-1);
y = random.randint(0, H-1);
cmd = "{} {}".format(x,y)
Expand Down
60 changes: 29 additions & 31 deletions sss.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import sys, random, time
import math
import threading


W = 120
H = 20
Expand All @@ -13,37 +15,21 @@

#COLORS
black = [0,0,0]
red = [255,0,0]
green = [0,255,0]
blue = [0,0,255]
white = [255,255,255]
yellow = [255,165, 0]
purple = [102, 0, 172]
grey = [120,120,120]


# pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag
# #modified from http://www.freesound.org/people/Koops/sounds/20258/
# flip_snd = pygame.mixer.Sound("flip.ogg")
# pygame.mixer.set_num_channels(W*H)
# print "ther are ", pygame.mixer.get_num_channels()

screen = pygame.display.set_mode((W*cell_size,H*cell_size), pygame.NOFRAME)
clock = pygame.time.Clock()
screenCenter = screen.get_rect().center
changes = pygame.Surface((W*cell_size,H*cell_size))

def idx(x,y):
return W*y + x

def draw_cell(screen, x,y):

def draw_cell(surface, x,y):
r = pygame.Rect((x * cell_size, y*cell_size), (cell_size, cell_size))
pygame.draw.rect(screen, black, r)
pygame.draw.rect(surface, black, r)
width = 0 if cells[idx(x,y)] else 1
pygame.draw.circle(screen, white, r.center, (cell_size / 2) - 1, width)
# chn = pygame.mixer.find_channel()
# if chn:
# chn.play(flip_snd)
pygame.draw.circle(surface, white, r.center, (cell_size / 2) - 1, width)


#init
Expand All @@ -54,21 +40,33 @@ def draw_cell(screen, x,y):
cells.append(False)
draw_cell(screen, x, y)

def read_cmds():
for line in sys.stdin:
# print line
if line.startswith("#"):
continue
coords = line.split()
x = int(coords[0])
y = int(coords[1])
cells[idx(x,y)] = not cells[idx(x,y)]
draw_cell(screen, x,y)
time.sleep(.00005); # the flip time aproximation

t = threading.Thread(target=read_cmds)
t.start()


done = False
while not done:
clock.tick_busy_loop(1500);
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
done = True

for line in sys.stdin:
clock.tick_busy_loop(1000)
# print line
if line.startswith("#"):
continue
coords = line.split()
x = int(coords[0])
y = int(coords[1])
cells[idx(x,y)] = not cells[idx(x,y)]
draw_cell(screen, x,y)

pygame.display.update()
pygame.display.flip()


while (pygame.event.wait().type != pygame.KEYDOWN): pass
pygame.quit()

0 comments on commit dfbb23a

Please sign in to comment.