Пример #1
0
def run_action():
    if len(run_buffer) == 0:
        print('exec finished')
        window.clearInterval(runtime)
        post_run()
        return
    a = run_buffer.pop(0)

    arg = float(a.split(' ', 1)[1])
    cmd[a.split(' ')[0]](arg)

    display.width = display.width
    imgs = ['front_r', 'front', 'back_l', 'back']

    ctx.drawImage(window.gebi(imgs[turtle.angle]), turtle.x - 50,
                  turtle.y - 100, 100, 100)

    if turtle.collision():
        window.clearInterval(runtime)
        window.gebi('error').style.width = '42vw'
        window.gebi('error').style.opacity = 1
        window.gebi('error').innerHTML = 'Rodrick can\'t move there!'
        window.hide_error()
Пример #2
0
class Cloud(object):
    x = 0
    y = 0
    speed = 0

    src = window.gebi(choice(['cloud1', 'cloud2']))
Пример #3
0
def resize_window(event):
    display.width = result.width
    display.height = result.height

    map_.width = result.width
    map_.height = result.height

    cell_w = (display.height / iso_scale) * tan(radians(62.64))
    cell_h = display.height / iso_scale

    median_cell = (display.width / 2, display.height / 2)

    ## READING MAP DATA ##

    for i, j, src, c in window.data['map']:
        map_ctx.beginPath()

        if c[0] == 'simpleFill':
            img = window.gebi(src)

            # ANTIALIAS #
            oc = document.createElement('canvas')
            oc2 = document.createElement('canvas')
            octx = oc.getContext('2d')
            octx2 = oc2.getContext('2d')

            oc.width = img.width
            oc.height = img.height

            oc2.width = img.width
            oc2.height = img.height

            octx.drawImage(img, 0, 0, oc.width, oc.height)
            octx2.drawImage(oc, 0, 0, oc.width * 0.5, oc.height * 0.5)

            oc.width = oc.width
            octx.drawImage(oc2, 0, 0, oc.width * 0.5, oc.height * 0.5)

            oc2.width = oc2.width
            octx2.drawImage(oc, 0, 0, oc.width * 0.5, oc.height * 0.5)

            map_ctx.drawImage(oc2, 0, 0, oc.width * 0.125, oc.height * 0.125,
                              (median_cell[0] + i * cell_w) - c[2][0] * cell_w,
                              (median_cell[1] + j * cell_h) - c[2][1] * cell_h,
                              c[1][0] * cell_w + c[2][0] * cell_w,
                              c[1][1] * cell_h + c[2][1] * cell_h)
            map_ctx.fill()

        elif c == 'floor':
            map_ctx.moveTo(median_cell[0] + i * cell_w - cell_w / 2,
                           median_cell[1] + j * cell_h)
            map_ctx.lineTo(median_cell[0] + i * cell_w,
                           median_cell[1] + j * cell_h - cell_h / 2)
            map_ctx.lineTo(median_cell[0] + i * cell_w + cell_w / 2,
                           median_cell[1] + j * cell_h)
            map_ctx.lineTo(median_cell[0] + i * cell_w,
                           median_cell[1] + j * cell_h + cell_h / 2)
            map_ctx.lineTo(median_cell[0] + i * cell_w - cell_w / 2,
                           median_cell[1] + j * cell_h)

            pattern = ctx.createPattern(window.gebi(src), 'repeat')

            map_ctx.fillStyle = pattern
            map_ctx.fill()

        elif c == 'wall_l':
            map_ctx.moveTo(median_cell[0] + i * cell_w - cell_w / 2,
                           median_cell[1] + j * cell_h)
            map_ctx.lineTo(median_cell[0] + i * cell_w - cell_w / 2,
                           median_cell[1] + j * cell_h - cell_h)
            map_ctx.lineTo(median_cell[0] + i * cell_w,
                           median_cell[1] + j * cell_h - cell_h / 2)
            map_ctx.lineTo(median_cell[0] + i * cell_w,
                           median_cell[1] + j * cell_h + cell_h / 2)
            map_ctx.lineTo(median_cell[0] + i * cell_w - cell_w / 2,
                           median_cell[1] + j * cell_h)

            pattern = map_ctx.createPattern(window.gebi(src), 'repeat')

            map_ctx.fillStyle = pattern
            map_ctx.fill()

            map_ctx.beginPath()

            map_ctx.globalAlpha = 0.5

            map_ctx.moveTo(median_cell[0] + i * cell_w - cell_w / 2,
                           median_cell[1] + j * cell_h)
            map_ctx.lineTo(median_cell[0] + i * cell_w - cell_w / 2,
                           median_cell[1] + j * cell_h - cell_h)
            map_ctx.lineTo(median_cell[0] + i * cell_w,
                           median_cell[1] + j * cell_h - cell_h / 2)
            map_ctx.lineTo(median_cell[0] + i * cell_w,
                           median_cell[1] + j * cell_h + cell_h / 2)
            map_ctx.lineTo(median_cell[0] + i * cell_w - cell_w / 2,
                           median_cell[1] + j * cell_h)

            pattern = map_ctx.createPattern(window.gebi(src), 'repeat')

            map_ctx.fillStyle = '#000000'
            map_ctx.fill()

            map_ctx.globalAlpha = 1

        elif c == 'wall_r':

            map_ctx.moveTo(median_cell[0] + i * cell_w,
                           median_cell[1] + j * cell_h)
            map_ctx.lineTo(median_cell[0] + i * cell_w,
                           median_cell[1] + j * cell_h - cell_h)
            map_ctx.lineTo(median_cell[0] + i * cell_w - cell_w / 2,
                           median_cell[1] + j * cell_h - cell_h / 2)
            map_ctx.lineTo(median_cell[0] + i * cell_w - cell_w / 2,
                           median_cell[1] + j * cell_h + cell_h / 2)
            map_ctx.lineTo(median_cell[0] + i * cell_w,
                           median_cell[1] + j * cell_h)

            pattern = map_ctx.createPattern(window.gebi(src), 'repeat')

            map_ctx.fillStyle = pattern
            map_ctx.fill()
Пример #4
0
def run():
    global runtime
    global run_buffer
    global used_commands

    variables = {}

    window.gebi('error').style.width = '0vw'
    window.gebi('error').style.opacity = 0

    try:
        window.clearInterval(runtime)
    except:
        pass

    display.width = display.width
    turtle.x = display.width / 2
    turtle.y = display.height / 2

    turtle.disp_x = 0
    turtle.disp_y = 0

    turtle.angle = 0

    ctx.beginPath()
    ctx.lineWidth = 3
    ctx.moveTo(turtle.x + dis_center_x, turtle.y + dis_center_y)
    ctx.stroke()

    lines = window.editor.getValue().split('\n')
    lines = list(filter(lambda x: bool(x), lines))

    used_commands = []

    run_buffer = []
    for_buffer = []
    current_buffer = run_buffer

    for line in lines:
        line = line.strip()
        if '=' in line:
            pre, post = line.split('=')

            pre = pre.strip()

            if pre[0] in '0123456789':
                window.gebi('error').style.width = '42vw'
                window.gebi('error').style.opacity = 1
                window.gebi(
                    'error'
                ).innerHTML = 'Error in line "{}": Declarations cannot begin with numericals'.format(
                    line)
                window.hide_error()
                return

            post = post.strip()

            for char in pre:
                if char not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_':
                    window.gebi('error').style.width = '42vw'
                    window.gebi('error').style.opacity = 1
                    window.gebi(
                        'error'
                    ).innerHTML = 'Error in line "{}": "{}" not allowed in variable declarations'.format(
                        line, char)
                    window.hide_error()
                    return

            variables[pre] = post

        else:
            for command, action in cmd.items():
                if line.startswith(command):
                    used_commands.append(command)

                    if command in ['FORWARD', 'BACKWARD']:
                        try:
                            arg = int(line.split(' ', 1)[1])
                        except ValueError:
                            try:
                                arg = int(variables[line.split(' ', 1)[1]])
                            except ValueError:
                                window.gebi('error').style.width = '42vw'
                                window.gebi('error').style.opacity = 1
                                window.gebi(
                                    'error'
                                ).innerHTML = 'Error in line "{}": {} only accepts integer values (whole numbers)'.format(
                                    line, command)
                                window.hide_error()
                                return

                            except KeyError:
                                window.gebi('error').style.width = '42vw'
                                window.gebi('error').style.opacity = 1
                                window.gebi(
                                    'error'
                                ).innerHTML = 'Error in line "{}": variable "{}" not defined'.format(
                                    line,
                                    line.split(' ')[1])
                                window.hide_error()
                                return

                        for i in range(5 * arg):
                            current_buffer.append(command + ' 0.2')

                    elif command in ['LEFT', 'RIGHT']:
                        current_buffer.append(command + ' 1')

                    elif command in ['FOR ']:
                        try:
                            arg = int(line.split(' ', 1)[1])
                        except ValueError:
                            window.gebi('error').style.width = '42vw'
                            window.gebi('error').style.opacity = 1
                            window.gebi(
                                'error'
                            ).innerHTML = 'Error in line "{}": {} only accepts integer values (whole numbers)'.format(
                                line, command)
                            window.hide_error()
                            return

                        if len(for_buffer) > 0:
                            for_buffer.append([arg])
                            current_buffer = for_buffer[-1]
                        else:
                            for_buffer.append(arg)
                            current_buffer = for_buffer

                    elif command in ['ENDFOR']:
                        if current_buffer == for_buffer[-1]:
                            cur = for_buffer.pop(-1)
                            counts = cur.pop(0)
                            apval = cur * counts
                            try:
                                for item in apval:
                                    for_buffer[-1].append(item)
                                current_buffer = for_buffer[-1]
                            except:
                                for item in apval:
                                    for_buffer.append(item)
                                current_buffer = for_buffer

                        elif current_buffer == for_buffer:
                            counts = for_buffer.pop(0)
                            apval = for_buffer * counts
                            for item in apval:
                                run_buffer.append(item)

                            current_buffer = run_buffer

                    break
            else:
                window.gebi('error').style.width = '42vw'
                window.gebi('error').style.opacity = 1
                window.gebi(
                    'error'
                ).innerHTML = 'Error in line "{}": Unrecognised command-word'.format(
                    line)
                window.hide_error()
                break

    runtime = window.setInterval(run_action, 35)
Пример #5
0
print('loading main.py')

from browser import document, alert, window, timer
from math import sin, cos, tan, radians
from random import choice

result = window.gebi('result')
start = window.gebi('start')

win = window.gebi('win')
challenges = window.gebi('challenges')

display = window.gebi('display')
map_ = window.gebi('map')
cloud_canvas = window.gebi('clouds_canvas')

cloud_ctx = cloud_canvas.getContext('2d')
ctx = display.getContext('2d')
map_ctx = map_.getContext('2d')


class Turtle(object):
    angles = [62.64, -62.64, -117.36, 117.36]

    def __init__(self):
        self.x = 0
        self.y = 0
        self.angle = 0

        self.disp_x = 0
        self.disp_y = 0