commit 265940e025297fe3c0d2da1213974957c1185e6b Author: Pavel Date: Fri Sep 25 22:25:38 2020 +0200 initial commit diff --git a/README b/README new file mode 100644 index 0000000..49cca8b --- /dev/null +++ b/README @@ -0,0 +1,12 @@ +This is a modified version of the Python script for the TI-Planet and +Planete Casio back to school 2020 contest. + +This modified version works with Python 3 under GNU Linux and MS Windows. + +The original code can be found at: + +https://tiplanet.org/forum/viewtopic.php?f=49&t=24276 + +This script can be executed with the following command: + + python3 labygui.py diff --git a/labygui.png b/labygui.png new file mode 100644 index 0000000..7ae05a6 Binary files /dev/null and b/labygui.png differ diff --git a/labygui.py b/labygui.py new file mode 100644 index 0000000..be9f400 --- /dev/null +++ b/labygui.py @@ -0,0 +1,184 @@ +from math import sin, cos, pi, sqrt, asin +from tkinter import * + +laby_w, laby_h = 20, 12 + +wl = [676499, 819210, 704161, 828804, 911396, 1010837, 772540, 600182, 526017, 803480, 756064, 701196, 1756736, 1376344, 1158834, 1315990, 1102792, 1323847, 1497194, 1810708, 1327018, 1094903, 1349813] + +zint = 2 +screen_w, screen_h = laby_w * 50 + 2, laby_h * 50 + 2 +zx, zy = (screen_w - zint) / laby_w, (screen_h - zint) / laby_h + +master = Tk() +master.resizable(0, 0) + +index = 0 +path = [(0.0, 0.5)] + +def escape(event): + global master + master.quit() + +def add(event): + global path, index + if len(path) < 28: + index += 1 + path.append((0.0, 0.5)) + aller_selon(path) + +def remove(event): + global path, index + if len(path) > 1: + index -= 1 + path = path[:-1] + aller_selon(path) + +def next(event): + global path, index + if index < len(path) - 1: + index += 1 + aller_selon(path) + +def previous(event): + global path, index + if index > 0: + index -= 1 + aller_selon(path) + +def left(event): + global path, index + path[index] = (round(path[index][0] + 0.1, 1), path[index][1]) + aller_selon(path) + +def right(event): + global path, index + path[index] = (round(path[index][0] - 0.1, 1), path[index][1]) + aller_selon(path) + +def up(event): + global path, index + path[index] = (path[index][0], round(path[index][1] + 0.1, 1)) + aller_selon(path) + +def down(event): + global path, index + if path[index][1] > 0: + path[index] = (path[index][0], round(path[index][1] - 0.1, 1)) + aller_selon(path) + +def print_code(event): + global path + print('') + print('path = %s' % path) + print('') + print('def chemin():') + for i in range(len(path)): + print(' a_gauche(%s)' % path[i][0]) + print(' avancer(%s)' % path[i][1]) + print('') + +master.bind('', escape) +master.bind('', add) +master.bind('', remove) +master.bind('', previous) +master.bind('', next) +master.bind('', left) +master.bind('', right) +master.bind('', up) +master.bind('', down) +master.bind('p', print_code) +master.bind('P', print_code) + +canvas = Canvas(master, width = screen_w, height = screen_h + 68, bg = 'white') +canvas.pack() + +def clean_screen(): + canvas.delete('all') + +def draw_rect(x, y, w, h, color): + x += 1 + y += 1 + canvas.create_rectangle(x, y, x + w, y + h, fill = color, outline = '') + +def draw_line(x1, y1, x2, y2, color): + canvas.create_line(x1 + 1, y1 + 1, x2 + 1, y2 + 1, fill = color, width = 2.0) + +def draw_help(): + help = 'change angle: Left/Right, change length: Up/Down, add/remove element: Enter/Delete, previous/next element: PgDn/PgUp, print code: P, exit: Esc' + canvas.create_text(4, screen_h + 36, anchor = NW, text = help, fill = 'black') + +def draw_path(): + global path + canvas.create_text(4, screen_h + 4, anchor = NW, text = '%s' % path[0:14], fill = 'black') + canvas.create_text(4, screen_h + 20, anchor = NW, text = '%s' % path[14:28], fill = 'black') + +def draw_score(score): + canvas.create_text(4, screen_h + 52, anchor = NW, text = 'score: %f' % score, fill = 'black') + +def fix_angle(a): + return a * 2 * asin(1) / pi + +def cout(x): + return len(str(round(abs(x)/1.,5))) + +def tourner(a): + global state + state[7] += a + state[5] += 5 + cout(a) + state[2] -= a + +def avancer(l, color): + global state + t = fix_angle(state[2]) + dx, dy = cos(t), sin(t) + state[7] += l + state[5] += 8 + cout(l) + while(l > 0): + state[3:5] = state[0:2] + x, y = state[0] + dx/4, state[1] + dy/4 + ix, iy = int(x) - (x < 0), int(y) - (y < 0) + drx, dry = ix - int(state[3]), iy - int(state[4]) + vw = lambda y, x: wl[y] & 2**x + hw = lambda y, x: wl[y + laby_h] & 2**x + tx = drx and (ix < 0 or ix >= laby_w or vw(iy - dry, laby_w - 2 - min(ix, ix - drx))) + ty = dry and (iy < 0 or iy >= laby_h or hw(min(iy, iy - dry), laby_w - 1 - (ix - drx))) + t = dx <= 0 or int(x) < laby_w - 1 or int(y) < laby_h - 1 + if t and tx or ty or (drx and dry and (t and tx or ty)) or (drx and dry and (t and vw(iy, laby_w - 2 - min(ix, ix - drx)) or hw(min(iy, iy - dry), laby_w - 1 - ix))): + state[5] += 15 + return + l -= .25 + state[6] += (state[6] < 200) + state[0:2] = (x, y) + draw_line(state[3] * zx, state[4] * zy, state[0] * zx, state[1] * zy, color) + +def aller_selon(path): + global state + state = [0, .5, 0, 0, .5, 0, 0, 0] + clean_screen() + for i in range(2): + draw_rect(0, i * laby_h * zy, laby_w * zx + zint, zint, 'black') + draw_rect(i * laby_w * zx, (not i) * zy, zint, (laby_h - 1) * zy, 'black') + for y in range(2 * laby_h - 1): + for z in range(laby_w - (y < laby_h)): + if wl[y] & 2**z: + x = laby_w - 1 - z + if y < laby_h: + draw_rect(x * zx, y * zy, zint, zy + zint, 'black') + else: + pass + draw_rect(x * zx, (y - laby_h + 1) * zy, zx + zint, zint, 'black') + for i in range(len(path)): + tourner(path[i][0]) + if i == index: + avancer(path[i][1], 'red') + else: + avancer(path[i][1], 'blue') + state[5] += sin(fix_angle(state[7])) - state[6] // 2 + draw_help() + draw_path() + draw_score(state[5]) + +aller_selon(path) + +if __name__== "__main__": + master.mainloop()