The hundredth Post

The J-Blog has reached its hundredth post, this one. It being the hundredth post, I want to review my previous posts. But rather than going into the context of my previous 99 posts, I decided to write a program to do this for me.

The program goes thru a list fetched from my Table of contents page, looks up the html code, distills the post and counts how many characters of each type there are. It then outputs its results in a nice text-based style (can be seen below).

To further visualize the data, I wrote another program to display it graphically. The results look like this. On the left the characters are sorted alphabetically (ASCII) and on the right they are sorted by occurrence (you may click on the images to view them closely).

The characters sorted by count The characters sorted by ASCII

+------+-------+---------+
| char | count | percent |
+------+-------+---------+
| all  | 33746 | 100.0   |
| '!'  |     8 |   0.023 |
| ' '  |  5693 |  16.870 |
| '%'  |     2 |   0.005 |
| '&'  |     1 |   0.002 |
| ')'  |   109 |   0.323 |
| '('  |   109 |   0.323 |
| '-'  |    45 |   0.133 |
| ','  |   206 |   0.610 |
| '.'  |   364 |   1.078 |
| '1'  |    33 |   0.097 |
| '0'  |    88 |   0.260 |
| '3'  |    12 |   0.035 |
| '2'  |    20 |   0.059 |
| '5'  |    11 |   0.032 |
| '4'  |    21 |   0.062 |
| '7'  |     9 |   0.026 |
| '6'  |    14 |   0.041 |
| '9'  |     1 |   0.002 |
| '8'  |     4 |   0.011 |
| ';'  |     1 |   0.002 |
| ':'  |     9 |   0.026 |
| '='  |     2 |   0.005 |
| '?'  |     2 |   0.005 |
| 'A'  |    40 |   0.118 |
| 'C'  |    58 |   0.171 |
| 'B'  |    32 |   0.094 |
| 'E'  |    10 |   0.029 |
| 'D'  |    15 |   0.044 |
| 'G'  |     8 |   0.023 |
| 'F'  |    17 |   0.050 |
| 'I'  |   151 |   0.447 |
| 'H'  |    13 |   0.038 |
| 'K'  |     1 |   0.002 |
| 'J'  |     9 |   0.026 |
| 'M'  |    20 |   0.059 |
| 'L'  |    27 |   0.080 |
| 'O'  |     6 |   0.017 |
| 'N'  |     5 |   0.014 |
| 'Q'  |     1 |   0.002 |
| 'P'  |    19 |   0.056 |
| 'S'  |    48 |   0.142 |
| 'R'  |    19 |   0.056 |
| 'U'  |    33 |   0.097 |
| 'T'  |   134 |   0.397 |
| 'W'  |    19 |   0.056 |
| 'V'  |     7 |   0.020 |
| 'Y'  |    14 |   0.041 |
| 'X'  |     4 |   0.011 |
| '_'  |     2 |   0.005 |
| 'a'  |  1949 |   5.775 |
| 'c'  |  1035 |   3.067 |
| 'b'  |   287 |   0.850 |
| 'e'  |  3323 |   9.847 |
| 'd'  |   799 |   2.367 |
| 'g'  |   802 |   2.376 |
| 'f'  |   466 |   1.380 |
| 'i'  |  1912 |   5.665 |
| 'h'  |  1165 |   3.452 |
| 'k'  |   215 |   0.637 |
| 'j'  |    17 |   0.050 |
| 'm'  |   714 |   2.115 |
| 'l'  |  1126 |   3.336 |
| 'o'  |  2013 |   5.965 |
| 'n'  |  1839 |   5.449 |
| 'q'  |    24 |   0.071 |
| 'p'  |   656 |   1.943 |
| 's'  |  1782 |   5.280 |
| 'r'  |  1765 |   5.230 |
| 'u'  |   681 |   2.018 |
| 't'  |  2422 |   7.177 |
| 'w'  |   437 |   1.294 |
| 'v'  |   233 |   0.690 |
| 'y'  |   487 |   1.443 |
| 'x'  |    94 |   0.278 |
| 'z'  |    26 |   0.077 |
| '~'  |     1 |   0.002 |
+------+-------+---------+
+------+-------+---------+
| char | count | percent |
+------+-------+---------+
| all  | 33746 | 100.0   |
| ' '  |  5693 |  16.870 |
| 'e'  |  3323 |   9.847 |
| 't'  |  2422 |   7.177 |
| 'o'  |  2013 |   5.965 |
| 'a'  |  1949 |   5.775 |
| 'i'  |  1912 |   5.665 |
| 'n'  |  1839 |   5.449 |
| 's'  |  1782 |   5.280 |
| 'r'  |  1765 |   5.230 |
| 'h'  |  1165 |   3.452 |
| 'l'  |  1126 |   3.336 |
| 'c'  |  1035 |   3.067 |
| 'g'  |   802 |   2.376 |
| 'd'  |   799 |   2.367 |
| 'm'  |   714 |   2.115 |
| 'u'  |   681 |   2.018 |
| 'p'  |   656 |   1.943 |
| 'y'  |   487 |   1.443 |
| 'f'  |   466 |   1.380 |
| 'w'  |   437 |   1.294 |
| '.'  |   364 |   1.078 |
| 'b'  |   287 |   0.850 |
| 'v'  |   233 |   0.690 |
| 'k'  |   215 |   0.637 |
| ','  |   206 |   0.610 |
| 'I'  |   151 |   0.447 |
| 'T'  |   134 |   0.397 |
| '('  |   109 |   0.323 |
| ')'  |   109 |   0.323 |
| 'x'  |    94 |   0.278 |
| '0'  |    88 |   0.260 |
| 'C'  |    58 |   0.171 |
| 'S'  |    48 |   0.142 |
| '-'  |    45 |   0.133 |
| 'A'  |    40 |   0.118 |
| '1'  |    33 |   0.097 |
| 'U'  |    33 |   0.097 |
| 'B'  |    32 |   0.094 |
| 'L'  |    27 |   0.080 |
| 'z'  |    26 |   0.077 |
| 'q'  |    24 |   0.071 |
| '4'  |    21 |   0.062 |
| '2'  |    20 |   0.059 |
| 'M'  |    20 |   0.059 |
| 'P'  |    19 |   0.056 |
| 'R'  |    19 |   0.056 |
| 'W'  |    19 |   0.056 |
| 'F'  |    17 |   0.050 |
| 'j'  |    17 |   0.050 |
| 'D'  |    15 |   0.044 |
| '6'  |    14 |   0.041 |
| 'Y'  |    14 |   0.041 |
| 'H'  |    13 |   0.038 |
| '3'  |    12 |   0.035 |
| '5'  |    11 |   0.032 |
| 'E'  |    10 |   0.029 |
| ':'  |     9 |   0.026 |
| 'J'  |     9 |   0.026 |
| '7'  |     9 |   0.026 |
| '!'  |     8 |   0.023 |
| 'G'  |     8 |   0.023 |
| 'V'  |     7 |   0.020 |
| 'O'  |     6 |   0.017 |
| 'N'  |     5 |   0.014 |
| '8'  |     4 |   0.011 |
| 'X'  |     4 |   0.011 |
| '%'  |     2 |   0.005 |
| '='  |     2 |   0.005 |
| '?'  |     2 |   0.005 |
| '_'  |     2 |   0.005 |
| '&'  |     1 |   0.002 |
| '~'  |     1 |   0.002 |
| '9'  |     1 |   0.002 |
| ';'  |     1 |   0.002 |
| 'K'  |     1 |   0.002 |
| 'Q'  |     1 |   0.002 |
+------+-------+---------+

The main program (shell-based):

# Python 2.7.7 Code
# Jonathan Frech 15th of January, 2016

The visualization program:

# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 15th of January, 2016


The main program (shell-based):

# import needeed modules
import urllib, os, re, threading

DATASORTTYPE = ["highest_count", "alphabetically"][1]

# links the program checks
links = [
	"https://jonathanfrech.wordpress.com/2015/03/28/1/",
	"https://jonathanfrech.wordpress.com/2015/03/29/%cf%80-generator/",
	"https://jonathanfrech.wordpress.com/2015/03/29/rectangles/",
	"https://jonathanfrech.wordpress.com/2015/03/30/circle-walk/",
	"https://jonathanfrech.wordpress.com/2015/03/30/moving/",
	"https://jonathanfrech.wordpress.com/2015/03/31/polygons/",
	"https://jonathanfrech.wordpress.com/2015/04/01/clean-up-your-mac/",
	"https://jonathanfrech.wordpress.com/2015/04/02/circle-walk-ii/",
	"https://jonathanfrech.wordpress.com/2015/04/03/colors-v/",
	"https://jonathanfrech.wordpress.com/2015/04/04/colors-v-other-results/",
	"https://jonathanfrech.wordpress.com/2015/04/05/happy-eastern/",
	"https://jonathanfrech.wordpress.com/2015/04/06/colors-v-other-results-2/",
	"https://jonathanfrech.wordpress.com/2015/04/07/colors-v-reupload/",
	"https://jonathanfrech.wordpress.com/2015/04/08/bobbles/",
	"https://jonathanfrech.wordpress.com/2015/04/09/%cf%86-generator/",
	"https://jonathanfrech.wordpress.com/2015/04/10/hangman/",
	"https://jonathanfrech.wordpress.com/2015/04/11/rand-pix/",
	"https://jonathanfrech.wordpress.com/2015/04/12/fs-letters/",
	"https://jonathanfrech.wordpress.com/2015/04/18/plant-mimic/",
	"https://jonathanfrech.wordpress.com/2015/04/19/circle-crawler/",
	"https://jonathanfrech.wordpress.com/2015/04/25/bouncing/",
	"https://jonathanfrech.wordpress.com/2015/04/26/worldwide-pinhole-day/",
	"https://jonathanfrech.wordpress.com/2015/05/01/star/",
	"https://jonathanfrech.wordpress.com/2015/05/02/rain/",
	"https://jonathanfrech.wordpress.com/2015/05/03/circle-splatter/",
	"https://jonathanfrech.wordpress.com/2015/05/09/the-firefox-unicorn/",
	"https://jonathanfrech.wordpress.com/2015/05/10/shaper/",
	"https://jonathanfrech.wordpress.com/2015/05/16/shadow-v-1-1/",
	"https://jonathanfrech.wordpress.com/2015/05/17/graph-sim/",
	"https://jonathanfrech.wordpress.com/2015/05/23/colors-vi/",
	"https://jonathanfrech.wordpress.com/2015/05/24/jclock-ii/",
	"https://jonathanfrech.wordpress.com/2015/05/25/pentecost/",
	"https://jonathanfrech.wordpress.com/2015/05/30/prime-spiral/",
	"https://jonathanfrech.wordpress.com/2015/05/31/boxes/",
	"https://jonathanfrech.wordpress.com/2015/06/06/grow/",
	"https://jonathanfrech.wordpress.com/2015/06/07/jetris-v-1-2/",
	"https://jonathanfrech.wordpress.com/2015/06/13/jong/",
	"https://jonathanfrech.wordpress.com/2015/06/14/dig/",
	"https://jonathanfrech.wordpress.com/2015/06/20/jake/",
	"https://jonathanfrech.wordpress.com/2015/06/21/fs-letters-ii/",
	"https://jonathanfrech.wordpress.com/2015/06/27/bubbletree/",
	"https://jonathanfrech.wordpress.com/2015/06/28/primes/",
	"https://jonathanfrech.wordpress.com/2015/07/04/circle-mover/",
	"https://jonathanfrech.wordpress.com/2015/07/05/gradient-drawer/",
	"https://jonathanfrech.wordpress.com/2015/07/11/jclock-iii/",
	"https://jonathanfrech.wordpress.com/2015/07/12/jic-jac-joe/",
	"https://jonathanfrech.wordpress.com/2015/07/18/sierpinski-triangle/",
	"https://jonathanfrech.wordpress.com/2015/07/19/prime-spiral-ii/",
	"https://jonathanfrech.wordpress.com/2015/07/25/menger-sponge/",
	"https://jonathanfrech.wordpress.com/2015/07/26/bobble-throw/",
	"https://jonathanfrech.wordpress.com/2015/08/01/jeakout/",
	"https://jonathanfrech.wordpress.com/2015/08/02/pattern/",
	"https://jonathanfrech.wordpress.com/2015/08/08/jclock-iv/",
	"https://jonathanfrech.wordpress.com/2015/08/09/caesar-cipher/",
	"https://jonathanfrech.wordpress.com/2015/08/15/jonnect-jour/",
	"https://jonathanfrech.wordpress.com/2015/08/16/jasteroids/",
	"https://jonathanfrech.wordpress.com/2015/08/22/random-resource-locator/",
	"https://jonathanfrech.wordpress.com/2015/08/23/stacking-stones/",
	"https://jonathanfrech.wordpress.com/2015/08/29/trippy/",
	"https://jonathanfrech.wordpress.com/2015/08/30/r-lines/",
	"https://jonathanfrech.wordpress.com/2015/09/05/j-filters/",
	"https://jonathanfrech.wordpress.com/2015/09/06/space-adventures/",
	"https://jonathanfrech.wordpress.com/2015/09/12/primes-ii/",
	"https://jonathanfrech.wordpress.com/2015/09/13/j-filters-ii/",
	"https://jonathanfrech.wordpress.com/2015/09/19/text-scrambler/",
	"https://jonathanfrech.wordpress.com/2015/09/20/jclock-v/",
	"https://jonathanfrech.wordpress.com/2015/09/26/pastel/",
	"https://jonathanfrech.wordpress.com/2015/09/27/pygame-gfxdraw-aacircle-bug/",
	"https://jonathanfrech.wordpress.com/2015/10/03/j-filters-iii-2/",
	"https://jonathanfrech.wordpress.com/2015/10/04/sand/",
	"https://jonathanfrech.wordpress.com/2015/10/10/jdrawer/",
	"https://jonathanfrech.wordpress.com/2015/10/11/gradient-triangles/",
	"https://jonathanfrech.wordpress.com/2015/10/17/flare/",
	"https://jonathanfrech.wordpress.com/2015/10/18/ct-racetrack/",
	"https://jonathanfrech.wordpress.com/2015/10/24/e-generator/",
	"https://jonathanfrech.wordpress.com/2015/10/25/spiral/",
	"https://jonathanfrech.wordpress.com/2015/10/31/halloween/",
	"https://jonathanfrech.wordpress.com/2015/11/01/langtons-ant/",
	"https://jonathanfrech.wordpress.com/2015/11/07/prime-remainders/",
	"https://jonathanfrech.wordpress.com/2015/11/08/jclock-vi/",
	"https://jonathanfrech.wordpress.com/2015/11/14/jclock-vii/",
	"https://jonathanfrech.wordpress.com/2015/11/15/sailing/",
	"https://jonathanfrech.wordpress.com/2015/11/21/text-spinner/",
	"https://jonathanfrech.wordpress.com/2015/11/22/bubbles/",
	"https://jonathanfrech.wordpress.com/2015/11/28/sleeper/",
	"https://jonathanfrech.wordpress.com/2015/11/29/first-sunday-in-advent/",
	"https://jonathanfrech.wordpress.com/2015/12/05/ct-racetrack-ii/",
	"https://jonathanfrech.wordpress.com/2015/12/06/second-sunday-in-advent/",
	"https://jonathanfrech.wordpress.com/2015/12/12/mandelbrot-set/",
	"https://jonathanfrech.wordpress.com/2015/12/13/third-sunday-in-advent/",
	"https://jonathanfrech.wordpress.com/2015/12/19/%cf%80-approximation/",
	"https://jonathanfrech.wordpress.com/2015/12/20/fourth-sunday-in-advent/",
	"https://jonathanfrech.wordpress.com/2015/12/24/merry-christmas/",
	"https://jonathanfrech.wordpress.com/2015/12/26/spinning-shapes/",
	"https://jonathanfrech.wordpress.com/2015/12/27/prime-circle/",
	"https://jonathanfrech.wordpress.com/2015/12/31/happy-new-year/",
	"https://jonathanfrech.wordpress.com/2016/01/02/white-fireworks/",
	"https://jonathanfrech.wordpress.com/2016/01/03/rotating-squares/",
	"https://jonathanfrech.wordpress.com/2016/01/09/j-filters-iv/"
]

# load the link and get html string
def load(link):
	page = urllib.urlopen(link).read()
	return page

# strip off WordPress-code, leaving my post in raw text
def strip(page):
	page = re.sub("\n", "", page)
	page = re.sub('.*<div class="entry-content">\t\t<p>', '', page)
	page = re.sub('<p><span id="more-.*', '', page)
	return page

# strips off my code section from the post (the stuff that comes after the <hr />-line)
def stripcode(page):
	page = re.sub("<hr />.*", "", page)
	# not every post has a <hr \>-line...
	page = re.sub("\t*\(function\(g\).*", "", page)
	return page

# strips off the html <pre>...</pre> tag
def stripcode2(page):
	page = re.sub("<pre>.*</pre>", "", page)
	return page

# strips off every html <...> string bit
def striphtml(page):
	page = re.sub("<.*?>", "", page)
	return page

# strips off &...;-characters
def stripspecial(page):
	page = re.sub("\&\#\d*\;", "", page)
	return page

# strips off characters, which are not ascii and the tab
def stripnonascii(page):
	page = re.sub("[^\x00-\x7f]", "", page)
	page = re.sub("\x0D", "", page)
	page = re.sub("\t", "", page)
	return page

# strips off \n characters
def striplinebreaks(page):
	page = re.sub("\n", "", page)
	return page

# strips off everything from a page (using above functions)
def stripall(page):
	return striplinebreaks( stripnonascii( stripspecial( striphtml( stripcode2( stripcode( strip( page ) ) ) ) ) ) )

# makes sure given string has a specific length (adds spaces left or right)
def lengthen(s, length, spaceadd = "left"):
	s = str(s)
	if spaceadd == "left":
		while len(s) < length:
			s = " " + s
	elif spaceadd == "right":
		while len(s) < length:
			s = s + " "
	if len(s) > length:
		s = s[0:length]

	return s

# makes a float fitting for the screen
def fit(n):
	split = re.split("\.", str(n))
	integer, decimal = split[0], split[1]
	return lengthen(integer, 3) + "." + lengthen(decimal, 3, "right")

# generates a table entry with given character and count
def gettableentry(char, count):
	if char == "all":
		pass
	elif char == "\n":
		char = "\\n "
	else:
		char = "'" + char + "'"

	data.append([char, count])
	
	return "\n| " + char + "  | " + lengthen(str(count), 5) + " | " + fit(count / totalcharacters * 100) + " |"

# check every link entry
n = .0
string = ""
for link in links:
	page = stripall(load(link))
	string += page 
	
	# status report
	n += 1
	print "Loaded " + str(int(n / len(links) * 100)) + "% of the posts..."

# sort the string with
string = list(string)
string.sort()

# get the number of every character in the string
characters = {" ":0}
for char in string:
	if char not in characters:
		characters[char] = 1
	else:
		characters[char] += 1

# get the total number of characters
totalcharacters = float(len(string))

# initialize the data list (character, count)
data = []

# start the table
table =	"+------+-------+---------+"
table += "\n| char | count | percent |"
table += "\n+------+-------+---------+"
table += gettableentry("all", int(totalcharacters))

# sort characters and create table string
if DATASORTTYPE == "highest_count":
	characters2 = characters.copy()
	for char in characters:
		c = None
		for _char in characters2:
			if not c or characters[_char] > characters[c]:
				c = _char
		
		if c:
			#print c, characters[c]
			table += gettableentry(c, characters[c])
			characters2.pop(c)

# keep the sorted string and create table string
elif DATASORTTYPE == "alphabetically":
	for char in characters:
		table += gettableentry(char, characters[char])

# finish table
table += "\n+------+-------+---------+"

# print data sort type
print "DATASORTTYPE = " + DATASORTTYPE

# print data
print "DATA = " + str(data)

# print calculated table
print table

The visualization program:

# insert data here
# sorted alphabetically
DATA = [['all', 33746], ["'!'", 8], ["' '", 5693], ["'%'", 2], ["'&'", 1], ["')'", 109], ["'('", 109], ["'-'", 45], ["','", 206], ["'.'", 364], ["'1'", 33], ["'0'", 88], ["'3'", 12], ["'2'", 20], ["'5'", 11], ["'4'", 21], ["'7'", 9], ["'6'", 14], ["'9'", 1], ["'8'", 4], ["';'", 1], ["':'", 9], ["'='", 2], ["'?'", 2], ["'A'", 40], ["'C'", 58], ["'B'", 32], ["'E'", 10], ["'D'", 15], ["'G'", 8], ["'F'", 17], ["'I'", 151], ["'H'", 13], ["'K'", 1], ["'J'", 9], ["'M'", 20], ["'L'", 27], ["'O'", 6], ["'N'", 5], ["'Q'", 1], ["'P'", 19], ["'S'", 48], ["'R'", 19], ["'U'", 33], ["'T'", 134], ["'W'", 19], ["'V'", 7], ["'Y'", 14], ["'X'", 4], ["'_'", 2], ["'a'", 1949], ["'c'", 1035], ["'b'", 287], ["'e'", 3323], ["'d'", 799], ["'g'", 802], ["'f'", 466], ["'i'", 1912], ["'h'", 1165], ["'k'", 215], ["'j'", 17], ["'m'", 714], ["'l'", 1126], ["'o'", 2013], ["'n'", 1839], ["'q'", 24], ["'p'", 656], ["'s'", 1782], ["'r'", 1765], ["'u'", 681], ["'t'", 2422], ["'w'", 437], ["'v'", 233], ["'y'", 487], ["'x'", 94], ["'z'", 26], ["'~'", 1]]
# sorted highest_count
#DATA = [['all', 33746], ["' '", 5693], ["'e'", 3323], ["'t'", 2422], ["'o'", 2013], ["'a'", 1949], ["'i'", 1912], ["'n'", 1839], ["'s'", 1782], ["'r'", 1765], ["'h'", 1165], ["'l'", 1126], ["'c'", 1035], ["'g'", 802], ["'d'", 799], ["'m'", 714], ["'u'", 681], ["'p'", 656], ["'y'", 487], ["'f'", 466], ["'w'", 437], ["'.'", 364], ["'b'", 287], ["'v'", 233], ["'k'", 215], ["','", 206], ["'I'", 151], ["'T'", 134], ["'('", 109], ["')'", 109], ["'x'", 94], ["'0'", 88], ["'C'", 58], ["'S'", 48], ["'-'", 45], ["'A'", 40], ["'1'", 33], ["'U'", 33], ["'B'", 32], ["'L'", 27], ["'z'", 26], ["'q'", 24], ["'4'", 21], ["'2'", 20], ["'M'", 20], ["'P'", 19], ["'R'", 19], ["'W'", 19], ["'F'", 17], ["'j'", 17], ["'D'", 15], ["'6'", 14], ["'Y'", 14], ["'H'", 13], ["'3'", 12], ["'5'", 11], ["'E'", 10], ["':'", 9], ["'J'", 9], ["'7'", 9], ["'!'", 8], ["'G'", 8], ["'V'", 7], ["'O'", 6], ["'N'", 5], ["'8'", 4], ["'X'", 4], ["'%'", 2], ["'='", 2], ["'?'", 2], ["'_'", 2], ["'&'", 1], ["'~'", 1], ["'9'", 1], ["';'", 1], ["'K'", 1], ["'Q'", 1]]

# fix data
#TOTALCOUNT = DATA[0][1]
DATA.pop(0)

#"""
# remove spaces
for data in DATA:
	if data[0] == "' '":
		DATA.remove(data)
#"""

"""
string = ""
for data in DATA:
	string += data[0] + ", "
string = string[0:len(string)-2]
print string
#"""

MAX = 0
for data in DATA:
	if data[1] > MAX:
		MAX = data[1]

# importing needed modules
import pygame, sys, time, math, os, random
pygame.font.init()

""" GAME """
# game class
class GAME():
	# initialize program
	def __init__(self):
		self.WIDTH, self.HEIGHT = 1080, 720
		self.SIZE = [self.WIDTH, self.HEIGHT]
		self.SURF = pygame.Surface(self.SIZE)
		self.SCREEN = pygame.display.set_mode(self.SIZE)

		self.TICKS = 0
		self.RUNNING = True

		self.FONT = pygame.font.SysFont(None, 20)

		# functions
		pygame.display.set_caption("-")
	
	# tick function
	def tick(self):
		# handle events
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				self.quit()
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_SPACE:
					self.screenshot()

	# render function
	def render(self):
		# fill
		self.SURF.fill([255, 255, 255])

		"""
		lastpos = None
		w = -1
		for data in DATA:
			w += 1
			pos = [int( (1. * self.WIDTH / len(DATA) * w) + (self.WIDTH / len(DATA) / 2) ), int( self.HEIGHT - (1. * self.HEIGHT / MAX * data[1]) )]
			if lastpos:
				pygame.draw.line(self.SURF, [0, 0, 0], pos, lastpos)
			lastpos = pos
		"""

		# render graph
		xoff = 0#self.WIDTH / len(DATA) / 2
		w = -1
		for data in DATA:
			w += 1
			
			# calculate position
			pos = [xoff, int( self.HEIGHT - (1. * self.HEIGHT / MAX * data[1]) )]
			width = self.WIDTH / len(DATA)
			xoff += width

			# make the string to fit on screen
			txt = data[0][1:2]
			if txt == " ":
				txt = "' '"

			# generate text surface
			txt = self.FONT.render(txt, 1, [255, 0, 0])
			rct = txt.get_rect()
			pos = [pos[0], pos[1] - rct.bottom]
			if pos[1] < 0:
				pos[1] = 0
			
			# blit
			pygame.draw.rect(self.SURF, [0, 0, 0], [pos[0], pos[1], width, self.HEIGHT])
			self.SURF.blit(txt, pos)

		# blit and flip
		self.SCREEN.blit(self.SURF, [0, 0])
		pygame.display.flip()
	
	# quits
	def quit(self):
		self.RUNNING = False

	# takes a screenshot
	def screenshot(self):
		path = os.getcwd() + "/out/"
		
		try:
			if not os.path.isdir(path):
				os.mkdir(path)
			name = path + "img" + str(len(os.listdir(path))+1) + ".png"
			print name
			pygame.image.save(self.SURF, name)
		except:
			pass

	# run function (uses tick() and render())
	def run(self):
		ticksPerSecond = 60
		lastTime = time.time() * 1000000000
		nsPerTick =  1000000000.0 / float(ticksPerSecond)
	
		ticks = 0
		frames = 0
	
		lastTimer = time.time() * 1000
		delta = 0.0
		
		while self.RUNNING:
			now = time.time() * 1000000000
			delta += float(now - lastTime) / float(nsPerTick)
			lastTime = now
			shouldRender = False
					
			while delta >= 1:
				ticks += 1
				self.TICKS += 1
				self.tick()
				delta -= 1
				shouldRender = True
			
			if shouldRender:
				frames += 1
				self.render()
			
			if time.time() * 1000 - lastTimer >= 1000:
				lastTimer += 1000
				
				# debug
				# print("Frames: " + str(frames) + ", ticks: " + str(ticks))
			
				frames = 0
				ticks = 0

# start game
GAME().run()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s