please help! I got this error msg and I don’t know what to do.
main.py:
import pygame
stmts = {}
class ScreenPlaceHolder:
def __init__(self):
pass
def fill(self, *args):
pass
screen = ScreenPlaceHolder()
in_loop = []
mouse_down = []
in_loop_stmts = ['new_object', 'new_intractable', 'fill']
mouse_down_stmts = ['mouse_detect']
class Err:
def __init__(self, msg):
self.msg = msg
class Stmt:
def __init__(self):
self.stmt = None
self.name = None
def init(self, name, exec):
debug('stmt init! stmt', str(self.name))
self.stmt = exec
self.name = name
stmts[name] = exec
def call(self, param):
if self.name == None or self.stmt == None:
err = 'Stmt name and function is None'
raise Exception(err)
else:
return self.stmt(param)
def call_stmt(name, param):
return_val = stmts[name](param)
if isinstance(return_val, Err):
raise Exception(return_val.msg)
else:
return return_val
with open('./main.gtok', encoding='utf-8') as useless:
main = useless.read()
useless = None
variables = {'max_height': '650', 'max_width': '1300', 'dc': '0', 'fc': '255'}
draw = pygame.draw
def get_tokens(line:str):
real = line
in_str = is_special = False
output = []
curr = ''
for item in variables:
real = real.replace(f'?{item}', variables[item])
for letter in real:
if letter == ',':
if in_str:
curr += ','
else:
output.append(curr)
curr = ''
elif letter == '|':
if in_str:
curr += '|'
else:
output.append(curr)
output.append('@@@@pipe')
curr = ''
elif letter == ';':
if in_str:
curr += ';'
else:
output.append(curr)
output.append('@@@@end')
curr = ''
elif letter == '\\':
if is_special:
curr += '\\'
is_special = False
else:
is_special = True
elif letter == '^':
if is_special:
curr += '^'
is_special = False
else:
in_str = not in_str
elif letter == 'n':
if is_special:
curr += '\n'
is_special = False
else:
curr += 'n'
elif is_special:
curr += f'\\{letter}'
is_special = False
else:
curr += letter
debug('token!', str(output))
return output
def execute(tokens):
if not tokens:
stmt = tokens[0]
params = tokens[1:]
if stmt in in_loop_stmts:
in_loop.append([stmt, params])
elif stmt in mouse_down_stmts:
mouse_down.append([stmt, params])
else:
debug('calling stmt', stmt)
call_stmt(stmt, params)
def debug(*args, sep=' ', end='\n'):
print(f'\t\tdebug:\n\t\t\t{sep.join(args)}{end}')
def stmt_init(args):
global screen
pygame.init()
size = int(args[0]), int(args[1])
screen = pygame.display.set_mode(size)
debug('screen init!')
Stmt().init('init', stmt_init)
def stmt_new_object(args):
mode = args[0]
x1 = int(args[1])
y1 = int(args[2])
x2 = int(args[3])
y2 = int(args[4])
color = (int(args[5]), int(args[6]), int(args[7]))
width = int(args[8])
if mode == 'rect':
draw.rect(screen, color, (x1, y1, x2 - x1, y2 - y1), width)
elif mode == 'ellipse':
draw.ellipse(screen, color, (x1, y1, x2 - x1, y2 - y1), width)
Stmt().init('new_object', stmt_new_object)
def stmt_fill(args):
screen.fill((int(args[0]), int(args[1]), int(args[2])))
Stmt().init('fill', stmt_fill)
def stmt_var(args):
if ' ' in args[0]:
return Err('Spaces (" ") can\'t be in a variable name.')
else:
if variables.get(args[0], None) == None:
variables[args[0]] = args[1]
else:
return Err(f'Variable {args[0]} is already defined.')
Stmt().init('var', stmt_var)
def stmt_vchange(args):
if ' ' in args[0]:
return Err('Spaces (" ") can\'t be in a variable name.')
else:
if variables.get(args[0], None) == None:
return Err(f'Variable {args[0]} is not defined.')
else:
variables[args[0]] = args[1]
Stmt().init('vchange', stmt_vchange)
def stmt_mouse_detect(args):
print('mouse detected!')
x1 = int(args[0])
y1 = int(args[1])
x2 = int(args[2])
y2 = int(args[3])
new_tok = get_tokens(args[4])
if x1 < pygame.mouse.get_pos()[0] < x2 and y1 < pygame.mouse.get_pos()[1] < y2:
print('in! executing token', new_tok)
execute(new_tok)
Stmt().init('mouse_detect', stmt_mouse_detect)
def stmt_echo(args):
print(' '.join(args))
Stmt().init('echo', stmt_echo)
def stmt_exec(args):
execute(get_tokens(args[0]))
Stmt().init('exec', stmt_exec)
def stmt_vchange_relative(args):
if ' ' in args[0]:
return Err('Spaces (" ") can\'t be in a variable name.')
else:
if variables.get(args[0], None) == None:
return Err(f'Variable {args[0]} is not defined.')
else:
if '.' in args[1]:
variables[args[0]] = str(float(variables[args[0]]) + float(args[1]))
else:
variables[args[0]] = str(int(variables[args[0]]) + int(args[1]))
Stmt().init('vchange_relative', stmt_vchange_relative)
def stmt_comment(args):
debug(args)
Stmt().init('comment', stmt_comment)
for each in main.split('\n'):
execute(get_tokens(each))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
for each in mouse_down:
call_stmt(each[0], each[1])
for each in in_loop:
call_stmt(each[0], each[1])
pygame.display.flip()
pygame.display.update()
console:
Traceback (most recent call last):
File "F:\data\PycharmData\GameToken\main.py", line 279, in <module>
for event in pygame.event.get():
pygame.error: video system not initialized
pygame 2.0.1 (SDL 2.0.14, Python 3.9.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
debug:
stmt init! stmt None
debug:
stmt init! stmt None
debug:
stmt init! stmt None
debug:
stmt init! stmt None
debug:
stmt init! stmt None
debug:
stmt init! stmt None
debug:
stmt init! stmt None
debug:
stmt init! stmt None
debug:
stmt init! stmt None
debug:
stmt init! stmt None
debug:
token! ['init', '1300', '650', '@@@@end']
debug:
token! ['fill', '0', '0', '0', '@@@@end']
debug:
token! ['var', 'c', '255', '@@@@end']
debug:
token! ['new_object', 'rect', '0', '0', '200', '200', '?c', '?c', '?c', '0', '@@@@end']
debug:
token! ['mouse_detect', '0', '0', '200', '200', 'new_object,^rect^,0,0,200,200,?c,?c,?c,0;vchange_relative,^c^,-10;', '@@@@end']
debug:
token! ['mouse_detect', '0', '0', '200', '200', 'vchange_relative,^c^,-10;', '@@@@end']
debug:
token! ['echo', '?c', '@@@@end']
Process finished with exit code 1
main.gtok:
init,?max_width,?max_height;
fill,?dc,?dc,?dc;
var,^c^,255;
new_object,^rect^,0,0,200,200,?c,?c,?c,0;
mouse_detect,0,0,200,200,^new_object,\^rect\^,0,0,200,200,?c,?c,?c,0;vchange_relative,\^c\^,-10;^;
mouse_detect,0,0,200,200,^vchange_relative,\^c\^,-10;^;
echo,?c;