Mar-29-2025, 09:54 PM
i made a module for the first time but its glitchy
i keep getting this error:
i keep getting this error:
Error:Exception has occurred: UnboundLocalError
cannot access local variable 'foreverfuncs' where it is not associated with a value
File "C:\Users\Azdaghost\Documents\Coding Projects\Python\ghostmansion.py", line 111, in forever
foreverfuncs += func
^^^^^^^^^^^^
File "C:\Users\Azdaghost\Documents\Coding Projects\Python\catchgame.py", line 23, in <module>
script.forever(forever)
~~~~~~~~~~~~~~^^^^^^^^^
UnboundLocalError: cannot access local variable 'foreverfuncs' where it is not associated with a valuecode:import tkinter as tk
import time
import math
window = tk.Tk()
window.title('ghostmansion window')
window.geometry('100x100')
foreverfuncs = []
age = -1
mouse_x = 0
mouse_y = 0
def forever():
for element in foreverfuncs:
element
window.after(0, forever)
def forever_module():
global age
age += 1
window.after(1, forever_module)
def find_mouse_pos(event):
global mouse_x, mouse_y
mouse_x, mouse_y = event.x, event.y
window.bind('<Motion>', find_mouse_pos)
class Project():
def __init__(self, size):
self.size = size
self.name = 'ghostmansion window'
def rename(self, name):
self.name = name
def resize(self, size):
self.size = [size[0], size[1]]
window.geometry(f'{self.size[0]}x{self.size[1]}')
def finish(self):
window.mainloop()
class Painting():
def __init__(self, pos, size, dir, name):
self.pos = pos
self.size = size
self.dir = dir
self.inst = tk.Frame(width=size[0], height=size[1])
# MOTION
def get_data(self, type):
if type == 'x':
return self.pos[0]
if type == 'y':
return self.pos[1]
if type == 'xs':
return self.size[0]
if type == 'ys':
return self.size[1]
if type == 'dir':
return self.dir
def print(self):
self.inst.place(x=self.pos[0], y=self.pos[1])
def move_steps(self, steps):
self.pos[0] += steps * math.sin(self.dir)
self.pos[1] += steps * math.cos(self.dir)
def turn(self, angle):
self.dir += angle
def set_pos(self, x, y):
self.pos[0] = x
self.pos[1] = y
def point_in_dir(self, dir):
self.dir = dir
def change_pos(self, x, y):
self.pos[0] += x
self.pos[1] += y
# LOOKS
def resize_pxl(self, xs, ys):
self.size[0] = xs
self.size[1] = ys
def resize_per(self, perc):
self.size[0] *= perc / 100
class Sensing():
def __init__(self, obj):
self.obj = obj
def touching(frame1, frame2):
x1, y1 = frame1.winfo_x(), frame1.winfo_y()
w1, h1 = frame1.winfo_width(), frame1.winfo_height()
x2, y2 = frame2.winfo_x(), frame2.winfo_y()
w2, h2 = frame2.winfo_width(), frame2.winfo_height()
if (x1 < x2 + w2 and x1 + w1 > x2 and y1 < y2 + h2 and y1 + h1 > y2):
return True
return False
def distance_from(self, frame):
s1 = self.obj.pos[0] - frame.pos[0]
s2 = self.obj.pos[1] - frame.pos[1]
return abs(s1 + s2)
def get_mouse_pos(self):
return [mouse_x, mouse_y]
def get_time(self):
return age
class Script():
def __init__(self):
pass
# CONTROL
def wait(self, milsecs):
time.sleep(milsecs / 100)
def forever(self, func):
foreverfuncs += func
# OPERATORS
def contains(self, text, context):
save = ''
reset = True
for i in range(len(text)):
if text[i] == context[i]:
save += text[i]
elif reset:
save = ''
return save == text
def mod(self, n1, n2):
n1v, n2v = n1, n2
while n1 < n2:
n2 -= n1
return n2
# CONSOLE
def log(self, text):
print(text)
def warn(self, text):
print('WARING:', text)
def error(self, text, end):
print('Error:', text)
if end:
exit()
class Sensing():
def __init__(self):
pass
def touching(self, frame1, frame2):
x1, y1 = frame1.winfo_x(), frame1.winfo_y()
w1, h1 = frame1.winfo_width(), frame1.winfo_height()
x2, y2 = frame2.winfo_x(), frame2.winfo_y()
w2, h2 = frame2.winfo_width(), frame2.winfo_height()
if (x1 < x2 + w2 and x1 + w1 > x2 and y1 < y2 + h2 and y1 + h1 > y2):
return True
return False
def distance_from(self, frame):
s1 = self.obj.pos[0] - frame.pos[0]
s2 = self.obj.pos[1] - frame.pos[1]
return abs(s1 + s2)
def get_mouse_pos(self, dim):
if dim == 'x':
return mouse_x
elif dim == 'y':
return mouse_y
else:
return (mouse_x, mouse_y)
def get_time(self):
return age
