如何在Skulpt(流星)中实施龟?

时间:2017-08-24 22:21:48

标签: python meteor

我的代码几乎与Skulpt.org上提供的内容相匹配,但我似乎无法让任何海龟出现在我的画布上。我添加了代码以确保正在加载龟库并且python正在成功运行。这两条消息都说是这种情况;一切似乎都在起作用。

以下是我的一些模板代码:

Template.pythonEnv.events({
'click #py_run': function(event, template) {
    // get content from Ace
    Tracker.autorun(function (e) {
        var editorPy = AceEditor.instance("py_inPanel", {
            theme:"dawn",
            mode:"python"
        });
        if(editorPy.loaded === true){
            e.stop();
            Session.set("pyContent", editorPy.getValue());
        }
    });
    // Skulpt
    var prog =  Session.get("pyContent");
    var mypre = document.getElementById("py_outputPanel");
    mypre.innerHTML = '';

    Sk.onAfterImport = function(library) {
        switch(library) {
            case 'turtle':
                console.log('turtle loaded');  // *** CHECK TO SEE IF TURTLE LOADED ***
                break;
        }
    }

    Sk.pre = "py_outputPanel"; // output
    Sk.configure({
        inputfun: function(prompt) {
            return window.prompt(prompt); // for raw_input()
        },
        inputfunTakesPrompt: true,
        output: outf,
        read: builtinRead
    });
    (Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'py_turtlePanel';
    var myPromise = Sk.misceval.asyncToPromise(function() {
        return Sk.importMainWithBody("<stdin>", false, prog, true);
    });
    myPromise.then(function(mod) {
        console.log('Python - success'); // *** CHECK TO SEE IF PYTHON RAN ***
    }, function(err) {
        console.log(err.toString());
    });
    // resize output panel
    template.$(".py_panel").height($(window).height() - template.$("#py_header").height() - 90);
},
...

这是我的HTML代码:

<template name="pythonEnv">
{{#with file}}
    <div id="py_container">
        <div id="py_header">
            <div id="py_buttonContainer">
                <p id="py_toggle_note">Click to toggle: &nbsp; </p>
                <div class="py_toggleButton py_active" id="py_in">Python</div>
                <div class="py_toggleButton py_active" id="py_output">Output</div>
                <div class="py_toggleButton" id="py_turtle">Turtle</div>
            </div>
            <button class="login" id="py_run">Run!</button>
        </div>
        <div id="py_bodyContainer">
            <pre id="py_inPanel" class="py_input py_panel"></pre>
            <pre id="py_outputPanel" class="py_panel"></pre>
            <canvas id="py_turtlePanel" class="py_panel py_hidden"></canvas>
        </div>
    </div>
{{/with}}
</template>

修订: 我在'click #py_run'函数下添加了此代码:

var canvas = document.getElementById("py_turtlePanel");
var context = canvas.getContext("2d");
context.fillStyle = "blue";
context.font = "bold 16px Arial";
context.fillText("Zibri", (canvas.width / 2) - 17, (canvas.height / 2) + 8);

它有效!但我的乌龟仍然没有加载。我尝试运行以下龟代码:

import turtle

t = turtle.Turtle()

for c in ['red', 'green', 'yellow', 'blue']:
    t.color(c)
    t.forward(75)
    t.left(90)

没有运气。此外,使用我的Skulpt-Python功能的所有功能似乎都能正常工作。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

事实证明,问题是我在http://www.skulpt.org/#错误地阅读了初始代码。我不应该将我的海龟代码放在 private void createVideoFromImages(final HashMap<String, String> paths) { final class ProcessTask extends AsyncTask<Void, Void, String> { @Override protected void onPreExecute() { super.onPreExecute(); cpb_ScanningMediaProgressBar.setVisibility(View.VISIBLE); rv_IndividualMediaView.setVisibility(View.GONE); } @Override protected String doInBackground(Void... params) { Calendar c = Calendar.getInstance(); int seconds = c.get(Calendar.SECOND); String out_path = Environment.getExternalStorageDirectory() + File.separator + "DCIM/" + "SM_" + seconds + ".mp4"; MySequenceEncoder se = null; DateFormat df = new SimpleDateFormat("MMMM d, yyyy HH:mm:ss aaa"); String currentDateTimeString = df.format(new Date()); ArrayList<MediaDetails> mediaDetailsList = new ArrayList<>(); ArrayList<File> files = new ArrayList<>(); File dir = new File(Environment.getExternalStorageDirectory() + "/" /*+ "Smart_Gallery/"*/); File video = null; try { video = File.createTempFile("temp", ".mp4", dir); Log.e("Test ", "File created"); se = new MySequenceEncoder(video); for (Map.Entry<String, String> stringEntry : paths.entrySet()) { File file_item = new File(stringEntry.getKey()); if (!file_item.isDirectory()) { try { if (!file_item.exists()) break; Bitmap frame = BitmapFactory.decodeFile(file_item .getAbsolutePath()); Log.e("Path ", file_item .getAbsolutePath()); se.encodeNativeFrame(fromBitmap(Bitmap.createScaledBitmap(frame, 1300, 800, false))); // se.encodeNativeFrame(fromBitmap(Bitmap.createScaledBitmap(frame, frame.getWidth(), frame.getHeight(), false))); // Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } se.finish(); if (video.exists()) { muxing(video.getAbsolutePath(), out_path); } Log.e("Test ", "Finish"); } catch (IOException e) { e.printStackTrace(); } return out_path; } protected void onPostExecute(String videopath) { cpb_ScanningMediaProgressBar.setVisibility(View.GONE); if (getActivity() != null) { ToastUtil.showToast(getActivity(), "Video saved in " + videopath); ((MainActivity) getActivity()).loadFragment(new TopTabsFragment(), "tabpager"); ((MainActivity) getActivity()).makeDefaultPreference(2); ((MainActivity) getActivity()).refreshEntireGallery(); /* if(videofile.exists()) { Calendar c = Calendar.getInstance(); int seconds = c.get(Calendar.SECOND); String out_path = Environment.getExternalStorageDirectory() + File.separator + "DCIM/" + "SM_" + seconds + ".mp4"; muxing(videofile.getAbsolutePath(), out_path); } }else { Log.e("Create Video","file doesnot exist"); }*/ } } } new ProcessTask().execute(); } 中,而应该将其放在<canvas id="mycanvas"></canvas>中。