testing system
parent
7dffa204a0
commit
d4deb8455d
102
hashengine.py
102
hashengine.py
|
@ -2,6 +2,7 @@ import tkinter as tk
|
||||||
import string
|
import string
|
||||||
import random
|
import random
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
class stdrend:
|
class stdrend:
|
||||||
def __init__(self, size, cam):
|
def __init__(self, size, cam):
|
||||||
|
@ -25,10 +26,12 @@ class stdrend:
|
||||||
out = out + i
|
out = out + i
|
||||||
return "#"+out
|
return "#"+out
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self._win.update()
|
||||||
|
|
||||||
def pix(self, x, y, text, bcolor, fcolor):
|
def pix(self, x, y, text, bcolor, fcolor):
|
||||||
if f"{x}:{y}" in self._grid:
|
if f"{x}:{y}" in self._grid:
|
||||||
self._grid[f"{x}:{y}"].config(text=text, bg=self.coltohex(bcolor), fg=self.coltohex(fcolor))
|
self._grid[f"{x}:{y}"].config(text=text, bg=self.coltohex(bcolor), fg=self.coltohex(fcolor))
|
||||||
self._win.update()
|
|
||||||
|
|
||||||
class color3:
|
class color3:
|
||||||
def __init__(self, r=0, g=0, b=0):
|
def __init__(self, r=0, g=0, b=0):
|
||||||
|
@ -85,6 +88,9 @@ class vector2:
|
||||||
self.y = y
|
self.y = y
|
||||||
self._type = "vector2"
|
self._type = "vector2"
|
||||||
|
|
||||||
|
def _magnitude(self):
|
||||||
|
return abs(self.x+self.y)
|
||||||
|
|
||||||
def __add__(self, v):
|
def __add__(self, v):
|
||||||
return vector2(self.x+v.x, self.y+v.y)
|
return vector2(self.x+v.x, self.y+v.y)
|
||||||
|
|
||||||
|
@ -123,8 +129,15 @@ class event:
|
||||||
self._attached = []
|
self._attached = []
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
|
threads = []
|
||||||
for i in self._attached:
|
for i in self._attached:
|
||||||
threading.Thread(target=i).start()
|
temp = threading.Thread(target=i)
|
||||||
|
temp.start()
|
||||||
|
threads.append(temp)
|
||||||
|
return threads
|
||||||
|
|
||||||
|
def attach(self, target):
|
||||||
|
self._attached.append(target)
|
||||||
|
|
||||||
class obj:
|
class obj:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -136,10 +149,10 @@ class obj:
|
||||||
self.velocity = vector2()
|
self.velocity = vector2()
|
||||||
self.friction = 0
|
self.friction = 0
|
||||||
self.collide = True
|
self.collide = True
|
||||||
self.touch = True
|
|
||||||
self.anchored = False
|
self.anchored = False
|
||||||
self.bcolor = color3(255, 255, 255)
|
self.bcolor = color3(255, 255, 255)
|
||||||
self.fcolor = color3()
|
self.fcolor = color3()
|
||||||
|
self._touching = event()
|
||||||
|
|
||||||
class camera(obj):
|
class camera(obj):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -161,6 +174,51 @@ class game:
|
||||||
self._objects = {}
|
self._objects = {}
|
||||||
self.camera = camera()
|
self.camera = camera()
|
||||||
self._renderer = renderer(size, self.camera)
|
self._renderer = renderer(size, self.camera)
|
||||||
|
self._threads = []
|
||||||
|
|
||||||
|
def collidingpos(self, pos, ignore):
|
||||||
|
out = []
|
||||||
|
for i in self._objects:
|
||||||
|
i = self._objects[i]
|
||||||
|
if i in ignore: continue
|
||||||
|
if (i.position-pos)._magnitude() < 1 and i.collide == True:
|
||||||
|
out.append(i)
|
||||||
|
return out
|
||||||
|
|
||||||
|
def colliding(self, target):
|
||||||
|
out = []
|
||||||
|
if target.collide == False: return []
|
||||||
|
out = self.collidingpos(target.position, [target,])
|
||||||
|
return out
|
||||||
|
|
||||||
|
def handlecollision(self, target: obj, target2: obj):
|
||||||
|
if target2.anchored == True:
|
||||||
|
target.velocity = vector2()
|
||||||
|
else:
|
||||||
|
half = vector2(target.velocity.x/2, target.velocity.y/2)
|
||||||
|
target.velocity = vector2(half.x, half.y)
|
||||||
|
target2.velocity = half
|
||||||
|
|
||||||
|
def calcphysobj(self, target: obj):
|
||||||
|
if target.anchored == True: return
|
||||||
|
colliding = self.collidingpos(target.position+target.velocity, [target,])
|
||||||
|
for i in colliding:
|
||||||
|
target._touching.execute()
|
||||||
|
i._touching.execute()
|
||||||
|
self.handlecollision(target, i)
|
||||||
|
target.position += target.velocity
|
||||||
|
target.velocity += vector2(0, target.gravity)
|
||||||
|
target.velocity += target.acceleration
|
||||||
|
temp = 2
|
||||||
|
if target.friction != 0:
|
||||||
|
temp = 2 / target.friction
|
||||||
|
x = target.velocity.x
|
||||||
|
y = target.velocity.y
|
||||||
|
if x != 0:
|
||||||
|
x = x/temp
|
||||||
|
if y != 0:
|
||||||
|
y = y/temp
|
||||||
|
target.velocity = vector2(x, y)
|
||||||
|
|
||||||
def addobj(self, obj):
|
def addobj(self, obj):
|
||||||
id = ""
|
id = ""
|
||||||
|
@ -190,23 +248,39 @@ class game:
|
||||||
for i in list(self._objects.values()):
|
for i in list(self._objects.values()):
|
||||||
pos = i.position + self.camera.position
|
pos = i.position + self.camera.position
|
||||||
if not self.between(-1, self._size[0], pos.x) or not self.between(-1, self._size[1], pos.y): continue
|
if not self.between(-1, self._size[0], pos.x) or not self.between(-1, self._size[1], pos.y): continue
|
||||||
self._renderer.pix(pos.x, pos.y, i.char, i.bcolor, i.fcolor)
|
self._renderer.pix(round(pos.x), round(pos.y), i.char, i.bcolor, i.fcolor)
|
||||||
|
self._renderer.update()
|
||||||
|
|
||||||
|
def startscript(self, target):
|
||||||
|
temp = threading.Thread(target=target)
|
||||||
|
temp.start()
|
||||||
|
self._threads.append(temp)
|
||||||
|
|
||||||
|
def stopscripts(self):
|
||||||
|
for i in self._threads:
|
||||||
|
i.join(.0)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
testgame = game()
|
testgame = game()
|
||||||
for i in range(10):
|
object = obj()
|
||||||
for f in range(10):
|
object.char = "#"
|
||||||
object = obj()
|
object.anchored = False
|
||||||
object.char = "#"
|
object.position = vector2(5, 5)
|
||||||
object.position = vector2(i, f)
|
object.gravity = 1
|
||||||
object.bcolor = color3()
|
floor = obj()
|
||||||
testgame.addobj(object)
|
floor.char = "#"
|
||||||
|
floor.anchored = True
|
||||||
|
floor.position = vector2(5, 9)
|
||||||
|
floor.gravity = 0
|
||||||
|
floor.bcolor = color3(255, 255, 255)
|
||||||
|
testgame.addobj(object)
|
||||||
|
testgame.addobj(floor)
|
||||||
testgame.render()
|
testgame.render()
|
||||||
print(object.ID)
|
print(object.ID)
|
||||||
while True:
|
while True:
|
||||||
for i in testgame._objects:
|
testgame.calcphysobj(object)
|
||||||
testgame._objects[i].fcolor += color3(1, 11, 111)
|
testgame.calcphysobj(floor)
|
||||||
testgame._objects[i].bcolor += color3(1, 11, 111)
|
|
||||||
testgame.render()
|
testgame.render()
|
||||||
|
time.sleep(0)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,4 +23,6 @@
|
||||||
"save-suc": "Gespeichert!",
|
"save-suc": "Gespeichert!",
|
||||||
"xcam": "Kamera x Position:",
|
"xcam": "Kamera x Position:",
|
||||||
"ycam": "Kamera y Position:",
|
"ycam": "Kamera y Position:",
|
||||||
|
"testing": "Testen",
|
||||||
|
"test": "Spiel ausfuehren",
|
||||||
}
|
}
|
|
@ -23,4 +23,6 @@
|
||||||
"save-suc": "Saved!",
|
"save-suc": "Saved!",
|
||||||
"xcam": "Camera x position:",
|
"xcam": "Camera x position:",
|
||||||
"ycam": "Camera y position:",
|
"ycam": "Camera y position:",
|
||||||
|
"testing": "Testing",
|
||||||
|
"test": "Execute game",
|
||||||
}
|
}
|
66
main.py
66
main.py
|
@ -5,6 +5,7 @@ import mtTkinter as tk
|
||||||
from tkinter import ttk as tkk
|
from tkinter import ttk as tkk
|
||||||
from tkinter import messagebox
|
from tkinter import messagebox
|
||||||
from tkinter import filedialog
|
from tkinter import filedialog
|
||||||
|
import copy
|
||||||
import PCPL
|
import PCPL
|
||||||
import langsys
|
import langsys
|
||||||
import ast
|
import ast
|
||||||
|
@ -15,6 +16,7 @@ import hashengine
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
import threading
|
||||||
import easygui
|
import easygui
|
||||||
|
|
||||||
global LH
|
global LH
|
||||||
|
@ -62,8 +64,7 @@ class script:
|
||||||
PCPL.resetvar()
|
PCPL.resetvar()
|
||||||
PCPL.LIS("HASHBASE")
|
PCPL.LIS("HASHBASE")
|
||||||
PCPL.run(self.code)"""
|
PCPL.run(self.code)"""
|
||||||
prep = {"HASHAPI": API}
|
exec(self.code, API)
|
||||||
exec(self.code, prep)
|
|
||||||
|
|
||||||
class previewrend:
|
class previewrend:
|
||||||
def __init__(self, size, cam, container, offset):
|
def __init__(self, size, cam, container, offset):
|
||||||
|
@ -96,12 +97,14 @@ class previewrend:
|
||||||
out = out + i
|
out = out + i
|
||||||
return "#"+out
|
return "#"+out
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self._win.update()
|
||||||
|
|
||||||
def pix(self, x, y, text, bcolor, fcolor):
|
def pix(self, x, y, text, bcolor, fcolor):
|
||||||
self._xcam.config(text=LH.string("xcam")+str(self._cam.position.x))
|
self._xcam.config(text=LH.string("xcam")+str(self._cam.position.x))
|
||||||
self._ycam.config(text=LH.string("ycam")+str(self._cam.position.y))
|
self._ycam.config(text=LH.string("ycam")+str(self._cam.position.y))
|
||||||
if f"{x}:{y}" in self._grid:
|
if f"{x}:{y}" in self._grid:
|
||||||
self._grid[f"{x}:{y}"].config(text=text, bg=self.coltohex(bcolor), fg=self.coltohex(fcolor))
|
self._grid[f"{x}:{y}"].config(text=text, bg=self.coltohex(bcolor), fg=self.coltohex(fcolor))
|
||||||
self._win.update()
|
|
||||||
|
|
||||||
def selectlang(new):
|
def selectlang(new):
|
||||||
lang = open("clang", 'w')
|
lang = open("clang", 'w')
|
||||||
|
@ -257,6 +260,58 @@ def load():
|
||||||
importobj(i)
|
importobj(i)
|
||||||
preview.render()
|
preview.render()
|
||||||
|
|
||||||
|
def log(text, end="\n", flush=False):
|
||||||
|
global logfile
|
||||||
|
file = open("logs/"+logfile+".txt", 'a')
|
||||||
|
file.write(text+end)
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
def testing():
|
||||||
|
print("preparing log file...")
|
||||||
|
global logfile
|
||||||
|
global maingame
|
||||||
|
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!")
|
||||||
|
window = tk.Tk()
|
||||||
|
maingame = hashengine.game(renderer=lambda size, cam: previewrend(size, cam, window, [0, 0]))
|
||||||
|
API["HASHGAME"] = maingame
|
||||||
|
log("main game initalised!")
|
||||||
|
objects = copy.deepcopy(preview._objects)
|
||||||
|
maingame._objects = objects
|
||||||
|
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:
|
||||||
|
maingame.startscript(lambda: i.execute(API))
|
||||||
|
log("objects transferred!")
|
||||||
|
gamescript = """
|
||||||
|
global HASHGAME
|
||||||
|
import time
|
||||||
|
while True:
|
||||||
|
for i in HASHGAME._objects:
|
||||||
|
i = HASHGAME._objects[i]
|
||||||
|
HASHGAME.calcphysobj(i)
|
||||||
|
HASHGAME.render()
|
||||||
|
time.sleep(.0)
|
||||||
|
"""
|
||||||
|
gameloopsc = script()
|
||||||
|
gameloopsc.code = gamescript
|
||||||
|
maingame.startscript(lambda: gameloopsc.execute(API))
|
||||||
|
log("game test started!!!")
|
||||||
|
log("---------------------")
|
||||||
|
|
||||||
def GUIinit():
|
def GUIinit():
|
||||||
global container
|
global container
|
||||||
global objtree
|
global objtree
|
||||||
|
@ -313,6 +368,10 @@ def GUIinit():
|
||||||
addmenu.add_command(label=LH.string("obj"), command=lambda: add("obj"))
|
addmenu.add_command(label=LH.string("obj"), command=lambda: add("obj"))
|
||||||
addmenu.add_command(label=LH.string("script"), command=lambda: add("script"))
|
addmenu.add_command(label=LH.string("script"), command=lambda: add("script"))
|
||||||
|
|
||||||
|
testmenu = tk.Menu(menu)
|
||||||
|
menu.add_cascade(label=LH.string("testing"), menu=testmenu)
|
||||||
|
testmenu.add_command(label=LH.string("test"), command=testing)
|
||||||
|
|
||||||
langmenu = tk.Menu(menu)
|
langmenu = tk.Menu(menu)
|
||||||
menu.add_cascade(label=LH.string("langs"), menu=langmenu)
|
menu.add_cascade(label=LH.string("langs"), menu=langmenu)
|
||||||
for i in LH.getlangs():
|
for i in LH.getlangs():
|
||||||
|
@ -320,6 +379,7 @@ def GUIinit():
|
||||||
|
|
||||||
container.mainloop()
|
container.mainloop()
|
||||||
|
|
||||||
|
# attribute changers
|
||||||
def ats(mode, old):
|
def ats(mode, old):
|
||||||
#mode 0 = string
|
#mode 0 = string
|
||||||
#mode 1 = single character
|
#mode 1 = single character
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
[{'id': 'obj', 'name': 'testobj', 'args': {'anchored': False, 'char': '#', 'collide': True, 'friction': 0, 'gravity': 0, 'acceleration': {'x': 0, 'y': 0, 'ARGID': 'vector2'}, 'bcolor': {'b': 0, 'g': 255, 'r': 255, 'ARGID': 'color3'}, 'fcolor': {'b': 0, 'g': 0, 'r': 255, 'ARGID': 'color3'}, 'position': {'x': 5, 'y': 5, 'ARGID': 'vector2'}, 'velocity': {'x': 0, 'y': 0, 'ARGID': 'vector2'}}}, {'id': 'script', 'name': 'skript pos test', 'args': {'code': 'import random\nimport time\ntime.sleep(3)\nobjects = list(HASHGAME._objects.values())\nobject = objects[0]\nstartpos = object.position\nwhile True:\n\tobject.position = startpos + HASHBASE.vector2(random.randint(-3, 3), random.randint(-3, 3))\n\ttime.sleep(.01)'}}]
|
Loading…
Reference in New Issue