Skip to content

Instantly share code, notes, and snippets.

@ColtonPhillips
Created June 4, 2016 07:31
Show Gist options
  • Save ColtonPhillips/380ca6ee3b0c63270b003bb15951b4bc to your computer and use it in GitHub Desktop.
Save ColtonPhillips/380ca6ee3b0c63270b003bb15951b4bc to your computer and use it in GitHub Desktop.
Precompiler for the Pets Game (seek metadata and output objects as image strip)
colors = ["black","white","grey","darkgrey", "lightgrey","gray","darkgray","lightgray","red","darkred","lightred","brown","darkbrown","lightbrown","orange","yellow","green","darkgreen","lightgreen","blue","lightblue","darkblue","purple","pink","transparent"]
numerics = ["1","2","3","4","5","6","7","8","9","0"]
def get_me_my_game_string_now():
import urllib2
game_url = "https://gist.githubusercontent.com/ColtonPhillips/3d66191462a417f45419/raw/6512cd41f0bae75333f7a3b2befd22e4b8fae7ad/Let's%2520Save%2520Everybody's%2520Pets.txt"
response = urllib2.urlopen(game_url)
html = response.read()
return html
def PASS1(code):
line_count = sum(1 for line in code.splitlines())
line_tokens = code.splitlines()
while (line_tokens != []):
line = line_tokens.pop(0)
if "OBJECTS" in line:
break
object_count = 0
previous_line = ""
objects = []
while (line_tokens != []):
line = line_tokens.pop(0)
if line.lower() in colors or "#" in line:
objects.append(previous_line)
object_count+=1
if "LEGEND" in line:
break
previous_line = line
classes_count = 0
legend_count = 0
while (line_tokens != []):
line = line_tokens.pop(0)
if "=" in line and "or" in line:
classes_count+=1
elif "=" in line:
legend_count+=1
if "SOUND" in line:
legend_count-=2 #because reasons
break
#bug picks up commented ones
sfx_count = 0
while (line_tokens != []):
line = line_tokens.pop(0)
if any(number in line for number in numerics):
sfx_count+=1
if "COLLISIONLAYERS" in line:
break
layer_count = 0
while (line_tokens != []):
line = line_tokens.pop(0)
if any(object in line for object in objects):
layer_count+=1
if "RULES" in line:
break
rule_count = 0
while (line_tokens != []):
line = line_tokens.pop(0)
if "->" in line:
rule_count+=1
if "WINCONDITIONS" in line:
break
return {'objects':objects,
'line_count':line_count,'object_count':object_count,
'classes_count':classes_count,'legend_count':legend_count,'sfx_count':sfx_count,
'layer_count':layer_count,'rule_count':rule_count}
#http://stackoverflow.com/questions/214359/converting-hex-color-to-rgb-and-vice-versa
def hex_to_rgba(value):
value = value.lstrip('#')
lv = len(value)
t = list(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
t.append(255)
return tuple(t)
def PASS2(code,code_dict):
line_tokens = code.splitlines()
while (line_tokens != []):
line = line_tokens.pop(0)
if "OBJECTS" in line:
break
objects_data_list = [] #png data PIL Image
while (line_tokens!= []):
line = line_tokens.pop(0)
if any(object in line for object in code_dict['objects']):
color_line = line_tokens.pop(0)
if any(color in color_line.lower() for color in colors): #skip the boring ones
# throw away 5 lines
for _ in range(5):
__ = line_tokens.pop(0)
else:
my_colors_hex = color_line.strip().split(" ")
my_colors_rgba = [hex_to_rgba(x) for x in my_colors_hex]
new_data = []
for _ in range(5):
color_line = line_tokens.pop(0).strip()
for ch in color_line:
if ch == ".":
new_data.append((0,0,0,0))
else:
new_data.append(my_colors_rgba[int(ch)])
objects_data_list.append(new_data)
if "LEGEND" in line:
break
return objects_data_list
def save_object_data(object_data_list):
from PIL import Image
from random import randint
im = Image.new("RGBA",(len(object_data_list)*5,5),None)
im.putdata(object_data_list[23],2,0)
k = 0
for o in range(len(object_data_list)-1):
c = 0
for j in range (5):
for i in range (5):
im.putpixel((i+k,j),object_data_list[o][c])
c+=1
k+=5
im.save('mess.png')
def deploy():
code = get_me_my_game_string_now()
pass1_out = PASS1(code)
print(pass1_out)
pass2_out = PASS2(code,pass1_out)
print(pass2_out)
save_object_data(pass2_out)
if __name__ == "__main__":
deploy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment