Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#MonthOfCode day 11 - ascii
import os
from time import sleep
import random
DISP_WIDTH = 96
DISP_HEIGHT = 32
NB_MAX_CHAR_GEN=64
class MovingChar():
def __init__(self , display):
self.content = chr(random.choice( range(65,122) ))
self.x = float( DISP_WIDTH/2)
self.y = float( DISP_HEIGHT/2 )
self.x_speed = random.choice( (-1,1) ) * 5 * random.random()
self.y_speed = -random.random()*6.0
self.disp_x, self.disp_y = int(self.x), int(self.y)
display[self.disp_y][self.disp_x] = self.content
def isOut( self):
#we calculate the next pos
#is it out of bounds?
return( self.disp_y>=DISP_HEIGHT
or self.disp_x>=DISP_WIDTH
or self.disp_x<0
or self.disp_y<0 )
def markForDisplay(self, to_display):
if( self.disp_x in to_display[self.disp_y].keys() ):
del to_display[self.disp_y][self.disp_x]
self.x += self.x_speed
self.y_speed += 1.1 # simulates gravity
self.y += self.y_speed
self.disp_x, self.disp_y = int(round(self.x)), int(round(self.y))
if( not self.isOut() ):
to_display[self.disp_y][self.disp_x] = self.content
def disp_ascii_picture():
os.system('clear') # replace by os.system('cls') on windows systems
line_to_disp = ''.join( [ '-' for x in xrange(DISP_WIDTH) ] )
print line_to_disp
for line_dict in chars_to_disp:
line_to_disp = ''
for x in xrange(DISP_WIDTH):
if( not x in line_dict.keys()):
line_to_disp+=' '
else:
line_to_disp+=line_dict[x]
print line_to_disp
line_to_disp = ''.join( [ '-' for x in xrange(DISP_WIDTH) ] )
print line_to_disp
# MAIN PART
chars_to_disp = list()
for i in xrange(DISP_HEIGHT):
chars_to_disp.append( dict() ) # [y][x] -> char
l_letters = list()
for i in xrange(16):
l_letters.append( MovingChar(chars_to_disp) )
remaining_l = list()
generated = 0
while( len(l_letters)>0 ):
disp_ascii_picture()
for mc in l_letters:
mc.markForDisplay( chars_to_disp )
if(mc.isOut() ):
if( generated< NB_MAX_CHAR_GEN):
remaining_l.append( MovingChar(chars_to_disp) )
generated+=1
continue
remaining_l.append( mc)
l_letters = remaining_l
del remaining_l
remaining_l = list()
sleep(0.1)
disp_ascii_picture()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment