add files ig

main
Justus Jan Nico Wolff 2024-05-03 14:15:42 +02:00
parent a8cc019f69
commit 26e26c1627
3 changed files with 350 additions and 0 deletions

134
hashengine.py 100644
View File

@ -0,0 +1,134 @@
import tkinter as tk
import string
import random
from colorama import Fore, Back, Style
from colorama import just_fix_windows_console
just_fix_windows_console()
class stdrend:
def __init__(self, size):
self._size = size
self._grid = {}
self._win = tk.Tk()
for y in range(size[1]):
for x in range(size[0]):
temp = tk.Label(text=" ")
temp.grid(row=y, column=x)
self._win.update()
self._grid[f"{x}:{y}"] = temp
def pix(self, x, y, text):
if f"{x}:{y}" in self._grid:
self._grid[f"{x}:{y}"].config(text=text)
self._win.update()
class vector2:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __add__(self, v):
if not type(v) != vector2: raise ValueError("Must be vector2!")
return vector2(self.x+v.x, self.y+v.y)
def __sub__(self, v):
if not type(v) != vector2: raise ValueError("Must be vector2!")
return vector2(self.x-v.x, self.y-v.y)
def __mul__(self, v):
if not type(v) != vector2: raise ValueError("Must be vector2!")
return vector2(self.x*v.x, self.y*v.y)
def __iadd__(self, v):
if not type(v) != vector2: raise ValueError("Must be vector2!")
return vector2(self.x+v.x, self.y+v.y)
def __isub__(self, v):
if not type(v) != vector2: raise ValueError("Must be vector2!")
return vector2(self.x-v.x, self.y-v.y)
def __imul__(self, v):
if not type(v) != vector2: raise ValueError("Must be vector2!")
return vector2(self.x*v.x, self.y*v.y)
class NULL:
def __init__(self):
return None
class enum:
def __init__(self, sel):
self._sel = dict(sel)
for i in self._sel:
setattr(self, i, self._sel[i])
def getposssel(self):
return list(self._sel.keys())
_fcolors = {"norm": Fore.RESET, "red": Fore.RED, "green": Fore.GREEN, "blue": Fore.BLUE, "white": Fore.WHITE, "cyan": Fore.CYAN, "black": Fore.BLACK, "magenta": Fore.MAGENTA, "yellow": Fore.YELLOW}
_bcolors = {"norm": Back.RESET, "red": Back.RED, "green": Back.GREEN, "blue": Back.BLUE, "white": Back.WHITE, "cyan": Back.CYAN, "black": Back.BLACK, "magenta": Back.MAGENTA, "yellow": Back.YELLOW}
backcolors = enum(_bcolors)
forecolors = enum(_fcolors)
cammode = enum({"editable": 0, "follow": 1})
class obj:
def __init__(self):
self.position = vector2()
self.char = " "
self.ID = 0
self.parent = None
self.gravity = 0
self.acceleration = vector2()
self.velocity = vector2()
self.friction = 0
self.collide = True
self.touch = True
self.anchored = False
self.bcolor = backcolors.norm
self.fcolor = forecolors.norm
class camera(obj):
def __init__(self):
super().__init__()
self.mode = cammode.editable
self.subject = None
self.collide = False
self.touch = False
self.char = " "
def update(self):
if self.mode == cammode.follow and self.subject:
self.position = self.subject.position
class game:
def __init__(self, size=[20, 20], renderer=stdrend):
if renderer == None: raise TypeError("Renderer class needed!")
self._size = size
self._renderer = renderer(size)
self._objects = {}
def addobj(self, obj):
id = ""
for i in range(256):
id = id + random.choice(list(string.ascii_letters))
obj.ID = id
self._objects[id] = obj
def removeobj(self, obj):
self._objects.pop(obj.ID)
obj.ID = NULL()
def removeobjbyid(self, id):
self._objects.pop(id).ID = NULL()
def render(self):
for i in self._objects:
if __name__ == "__main__":
testgame = game()
object = obj()
testgame.addobj(object)
print(object.ID)

41
main.py 100644
View File

@ -0,0 +1,41 @@
import mtTkinter as tk
import PCPL
import langsys
import time
import subprocess
import sys
global LH
LH = langsys.langhandler()
lang = open("clang", 'r')
lang = lang.read()
LH.setlang(lang)
# LH.string("")
def selectlang(new):
lang = open("clang", 'w')
lang.write(new)
lang.close()
container.quit()
subprocess.Popen([sys.executable, __file__])
def GUIinit():
global container
container = tk.Tk()
menu = tk.Menu(container)
container.config(menu=menu)
filemenu = tk.Menu(menu)
menu.add_cascade(label=LH.string("file"), menu=filemenu)
filemenu.add_command(label=LH.string("new"))
filemenu.add_command(label=LH.string("open"))
filemenu.add_separator()
filemenu.add_command(label=LH.string("exit"), command=container.quit)
langmenu = tk.Menu(menu)
menu.add_cascade(label=LH.string("langs"), menu=langmenu)
for i in LH.getlangs():
langmenu.add_command(label=i, command=lambda i=i: selectlang(i))
container.mainloop()
GUIinit()

175
mtTkinter.py 100644
View File

@ -0,0 +1,175 @@
import sys
import threading
if sys.version_info[0] == 2:
# Python 2
from Tkinter import *
import Queue as queue
else:
# Python 3
from tkinter import *
import queue
class _Tk(object):
"""Wrapper for underlying attribute tk of class Tk"""
def __init__(self, tk, mt_debug=0, mt_check_period=10):
"""
:param tk: Tkinter.Tk.tk Tk interpreter object
:param mt_debug: Determines amount of debug output.
0 = No debug output (default)
1 = Minimal debug output
...
9 = Full debug output
:param mt_check_period: Amount of time in milliseconds (default
10) between checks for out-of-thread events when things are
otherwise idle. Decreasing this value can improve GUI
responsiveness, but at the expense of consuming more CPU
cycles.
# TODO: Replace custom logging functionality with standard
# TODO: logging.Logger for easier access and standardization
"""
self._tk = tk
# Create the incoming event queue
self._event_queue = queue.Queue(1)
# Identify the thread from which this object is being created
# so we can tell later whether an event is coming from another
# thread.
self._creation_thread = threading.current_thread()
# Create attributes for kwargs
self._debug = mt_debug
self._check_period = mt_check_period
# Destroying flag to be set by the .destroy() hook
self._destroying = False
def __getattr__(self, name):
"""
Diverts attribute accesses to a wrapper around the underlying tk
object.
"""
return _TkAttr(self, getattr(self._tk, name))
class _TkAttr(object):
"""Thread-safe callable attribute wrapper"""
def __init__(self, tk, attr):
self._tk = tk
self._attr = attr
def __call__(self, *args, **kwargs):
"""
Thread-safe method invocation. Diverts out-of-thread calls
through the event queue. Forwards all other method calls to the
underlying tk object directly.
"""
# Check if we're in the creation thread
if threading.current_thread() == self._tk._creation_thread:
# We're in the creation thread; just call the event directly
if self._tk._debug >= 8 or \
self._tk._debug >= 3 and self._attr.__name__ == 'call' and \
len(args) >= 1 and args[0] == 'after':
print('Calling event directly:', self._attr.__name__, args, kwargs)
return self._attr(*args, **kwargs)
else:
if not self._tk._destroying:
# We're in a different thread than the creation thread;
# enqueue the event, and then wait for the response.
response_queue = queue.Queue(1)
if self._tk._debug >= 1:
print('Marshalling event:', self._attr.__name__, args, kwargs)
self._tk._event_queue.put((self._attr, args, kwargs, response_queue), True, 1)
is_exception, response = response_queue.get(True, None)
# Handle the response, whether it's a normal return value or
# an exception.
if is_exception:
ex_type, ex_value, ex_tb = response
raise ex_type(ex_value, ex_tb)
return response
def _Tk__init__(self, *args, **kwargs):
"""
Hook for Tkinter.Tk.__init__ method
:param self: Tk instance
:param args, kwargs: Arguments for Tk initializer
"""
# We support some new keyword arguments that the original __init__ method
# doesn't expect, so separate those out before doing anything else.
new_kwnames = ('mt_check_period', 'mt_debug')
new_kwargs = {
kw_name: kwargs.pop(kw_name) for kw_name in new_kwnames
if kwargs.get(kw_name, None) is not None
}
# Call the original __init__ method, creating the internal tk member.
self.__original__init__mtTkinter(*args, **kwargs)
# Replace the internal tk member with a wrapper that handles calls from
# other threads.
self.tk = _Tk(self.tk, **new_kwargs)
# Set up the first event to check for out-of-thread events.
self.after_idle(_check_events, self)
# Define a hook for class Tk's destroy method.
def _Tk_destroy(self):
self.tk._destroying = True
self.__original__destroy()
def _check_events(tk):
"""Checks events in the queue on a given Tk instance"""
used = False
try:
# Process all enqueued events, then exit.
while True:
try:
# Get an event request from the queue.
method, args, kwargs, response_queue = tk.tk._event_queue.get_nowait()
except queue.Empty:
# No more events to process.
break
else:
# Call the event with the given arguments, and then return
# the result back to the caller via the response queue.
used = True
if tk.tk._debug >= 2:
print('Calling event from main thread:', method.__name__, args, kwargs)
try:
response_queue.put((False, method(*args, **kwargs)))
except SystemExit:
raise # Raises original SystemExit
except Exception:
# Calling the event caused an exception; return the
# exception back to the caller so that it can be raised
# in the caller's thread.
from sys import exc_info # Python 2 requirement
ex_type, ex_value, ex_tb = exc_info()
response_queue.put((True, (ex_type, ex_value, ex_tb)))
finally:
# Schedule to check again. If we just processed an event, check
# immediately; if we didn't, check later.
if used:
tk.after_idle(_check_events, tk)
else:
tk.after(tk.tk._check_period, _check_events, tk)
"""Perform in-memory modification of Tkinter module"""
# Replace Tk's original __init__ with the hook.
Tk.__original__init__mtTkinter = Tk.__init__
Tk.__init__ = _Tk__init__
# Replace Tk's original destroy with the hook.
Tk.__original__destroy = Tk.destroy
Tk.destroy = _Tk_destroy