testing system

main
Justus Jan Nico Wolff 2024-05-13 15:00:36 +02:00
parent 7dffa204a0
commit d4deb8455d
7 changed files with 156 additions and 17 deletions

View File

@ -2,6 +2,7 @@ import tkinter as tk
import string
import random
import threading
import time
class stdrend:
def __init__(self, size, cam):
@ -25,10 +26,12 @@ class stdrend:
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))
self._win.update()
class color3:
def __init__(self, r=0, g=0, b=0):
@ -85,6 +88,9 @@ class vector2:
self.y = y
self._type = "vector2"
def _magnitude(self):
return abs(self.x+self.y)
def __add__(self, v):
return vector2(self.x+v.x, self.y+v.y)
@ -123,8 +129,15 @@ class event:
self._attached = []
def execute(self):
threads = []
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:
def __init__(self):
@ -136,10 +149,10 @@ class obj:
self.velocity = vector2()
self.friction = 0
self.collide = True
self.touch = True
self.anchored = False
self.bcolor = color3(255, 255, 255)
self.fcolor = color3()
self._touching = event()
class camera(obj):
def __init__(self):
@ -161,6 +174,51 @@ class game:
self._objects = {}
self.camera = 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):
id = ""
@ -190,23 +248,39 @@ class game:
for i in list(self._objects.values()):
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
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__":
testgame = game()
for i in range(10):
for f in range(10):
object = obj()
object.char = "#"
object.position = vector2(i, f)
object.bcolor = color3()
testgame.addobj(object)
object = obj()
object.char = "#"
object.anchored = False
object.position = vector2(5, 5)
object.gravity = 1
floor = obj()
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()
print(object.ID)
while True:
for i in testgame._objects:
testgame._objects[i].fcolor += color3(1, 11, 111)
testgame._objects[i].bcolor += color3(1, 11, 111)
testgame.calcphysobj(object)
testgame.calcphysobj(floor)
testgame.render()
time.sleep(0)

View File

@ -23,4 +23,6 @@
"save-suc": "Gespeichert!",
"xcam": "Kamera x Position:",
"ycam": "Kamera y Position:",
"testing": "Testen",
"test": "Spiel ausfuehren",
}

View File

@ -23,4 +23,6 @@
"save-suc": "Saved!",
"xcam": "Camera x position:",
"ycam": "Camera y position:",
"testing": "Testing",
"test": "Execute game",
}

66
main.py
View File

@ -5,6 +5,7 @@ import mtTkinter as tk
from tkinter import ttk as tkk
from tkinter import messagebox
from tkinter import filedialog
import copy
import PCPL
import langsys
import ast
@ -15,6 +16,7 @@ import hashengine
import os
import random
import string
import threading
import easygui
global LH
@ -62,8 +64,7 @@ class script:
PCPL.resetvar()
PCPL.LIS("HASHBASE")
PCPL.run(self.code)"""
prep = {"HASHAPI": API}
exec(self.code, prep)
exec(self.code, API)
class previewrend:
def __init__(self, size, cam, container, offset):
@ -96,12 +97,14 @@ class previewrend:
out = out + i
return "#"+out
def update(self):
self._win.update()
def pix(self, x, y, text, bcolor, fcolor):
self._xcam.config(text=LH.string("xcam")+str(self._cam.position.x))
self._ycam.config(text=LH.string("ycam")+str(self._cam.position.y))
if f"{x}:{y}" in self._grid:
self._grid[f"{x}:{y}"].config(text=text, bg=self.coltohex(bcolor), fg=self.coltohex(fcolor))
self._win.update()
def selectlang(new):
lang = open("clang", 'w')
@ -257,6 +260,58 @@ def load():
importobj(i)
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():
global container
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("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)
menu.add_cascade(label=LH.string("langs"), menu=langmenu)
for i in LH.getlangs():
@ -320,6 +379,7 @@ def GUIinit():
container.mainloop()
# attribute changers
def ats(mode, old):
#mode 0 = string
#mode 1 = single character

View File

@ -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)'}}]