185 lines
4.6 KiB
Python
185 lines
4.6 KiB
Python
import hashengine
|
|
import random
|
|
import ast
|
|
import string
|
|
import threading
|
|
import mtTkinter as tk
|
|
import time
|
|
|
|
global gamedata
|
|
gamedata = {}
|
|
|
|
class rend:
|
|
def __init__(self, size, cam, container, offset):
|
|
self._size = size
|
|
self._grid = {}
|
|
self._win = container
|
|
self._frame = tk.Frame(container)
|
|
self._cam = cam
|
|
for y in range(size[1]):
|
|
for x in range(size[0]):
|
|
temp = tk.Label(self._frame, text=" ", width=3)
|
|
temp.grid(row=y+offset[1], column=x+offset[0]+1)
|
|
self._win.update()
|
|
self._grid[f"{x}:{y}"] = temp
|
|
self._frame.grid()
|
|
|
|
def coltohex(self, target):
|
|
colors = []
|
|
target = [target.r, target.g, target.b]
|
|
for i in target:
|
|
colors.append(("0"*(2-len(hex(i)[2:])))+hex(i)[2:])
|
|
out = ""
|
|
for i in colors:
|
|
out = out + i
|
|
return "#"+out
|
|
|
|
def update(self):
|
|
self._win.update()
|
|
|
|
def pix(self, x, y, text, bcolor, fcolor):
|
|
if f"{x}:{y}" in self._grid:
|
|
self._grid[f"{x}:{y}"].config(text=text, bg=self.coltohex(bcolor), fg=self.coltohex(fcolor))
|
|
|
|
class script:
|
|
def __init__(self):
|
|
self.code = ""
|
|
|
|
def execute(self, API):
|
|
#old code for PCPL code execution, replaced with python code execution
|
|
"""
|
|
PCPL.resetvar()
|
|
PCPL.LIS("HASHBASE")
|
|
PCPL.run(self.code)"""
|
|
exec(self.code, API)
|
|
|
|
def add(objtype):
|
|
obj = getattr(types, objtype)()
|
|
args = {}
|
|
for i in dir(obj):
|
|
if i.startswith("_"): continue
|
|
args[i] = getattr(obj, i)
|
|
temp = {"id": objtype, "args": args, "name": objtype}
|
|
id = ""
|
|
chars = list(string.ascii_letters)
|
|
for i in range(255):
|
|
id = id + random.choice(chars)
|
|
gamedata[id] = temp
|
|
if objtype in crucial:
|
|
game.addobj(obj)
|
|
gamedata[id]["args"]["ID"] = obj.ID
|
|
return id
|
|
def importobj(target):
|
|
oid = add(target["id"])
|
|
id = gamedata[oid]
|
|
id["name"] = target["name"]
|
|
#create arguments
|
|
outargs = {}
|
|
for argname in target["args"]:
|
|
arg = target["args"][argname]
|
|
if isinstance(arg, dict):
|
|
ID = arg.pop("ARGID")
|
|
obj = attypes[ID]()
|
|
for i in arg:
|
|
setattr(obj, i, arg[i])
|
|
arg = obj
|
|
outargs[argname] = arg
|
|
#apply arguments to obj
|
|
if target["id"] in crucial:
|
|
for i in outargs:
|
|
setattr(game.getobjbyid(gamedata[oid]["args"]["ID"]), i, outargs[i])
|
|
id["args"].update(outargs)
|
|
def load(target):
|
|
file = open(target, 'r')
|
|
target = file.read()
|
|
file.close()
|
|
target = ast.literal_eval(target)
|
|
for i in target:
|
|
importobj(i)
|
|
|
|
def log(text, end="\n", flush=False):
|
|
global logfile
|
|
file = open(logfile+".txt", 'a')
|
|
file.write(text+end)
|
|
file.close()
|
|
|
|
def NULL():
|
|
pass
|
|
|
|
def run():
|
|
print("preparing log file...")
|
|
global logfile
|
|
global win
|
|
global game
|
|
temp = time.gmtime(time.time())
|
|
logfile = ""
|
|
for i in temp:
|
|
logfile = logfile + "S" + str(i)
|
|
print("done")
|
|
log("Log file start!")
|
|
log("Preparing API...")
|
|
API = {"print": log, "HASHBASE": hashengine}
|
|
log("Done!")
|
|
API["HASHGAME"] = game
|
|
log("main game initalised!")
|
|
scripts = []
|
|
for i in gamedata:
|
|
i = gamedata[i]
|
|
if i["id"] != "script": continue
|
|
i = i["args"]["code"]
|
|
obj = script()
|
|
obj.code = i
|
|
scripts.append(obj)
|
|
for i in scripts:
|
|
game.startscript(lambda: i.execute(API))
|
|
gamescript = """
|
|
global HASHGAME
|
|
import time
|
|
while True:
|
|
for i in HASHGAME._objects:
|
|
i = HASHGAME._objects[i]
|
|
HASHGAME.calcphysobj(i)
|
|
HASHGAME.render()
|
|
"""
|
|
gameloopsc = script()
|
|
gameloopsc.code = gamescript
|
|
game.startscript(lambda: gameloopsc.execute(API))
|
|
log("game test started!!!")
|
|
log("---------------------")
|
|
|
|
def wait():
|
|
time.sleep(0.1)
|
|
load("game.HEGF")
|
|
run()
|
|
|
|
global types
|
|
global ignoreat
|
|
global valtypes
|
|
global extypes
|
|
global attypes
|
|
global crucial
|
|
global game
|
|
win = tk.Tk()
|
|
game = hashengine.game(renderer=lambda size, cam: rend(size, cam, win, [0, 0]))
|
|
crucial = ["obj",]
|
|
types = hashengine.enum({"obj": hashengine.obj, "script": script})
|
|
ignoreat = ["ID", "execute"]
|
|
"""self.position = vector2()
|
|
self.char = " "
|
|
self.ID = 0
|
|
self.gravity = 0
|
|
self.acceleration = vector2()
|
|
self.velocity = vector2()
|
|
self.friction = 0
|
|
self.collide = True
|
|
self.touch = True
|
|
self.anchored = False
|
|
self.bcolor = color3(255, 255, 255)
|
|
self.fcolor = color3()"""
|
|
attypes = {
|
|
"vector2": hashengine.vector2,
|
|
"color3": hashengine.color3,
|
|
}
|
|
threading.Thread(target=wait).start()
|
|
win.mainloop()
|