Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] Turtle module #748

Merged
merged 59 commits into from Jan 8, 2019
Merged
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
464c1a5
[python] Initial turtle module
boricj Oct 27, 2018
0a05b94
[python] Turtle module improvements
boricj Oct 28, 2018
ca589b9
[python] Fix turtle icon dependency
boricj Oct 28, 2018
3a7ae27
[python] Fix turtle icon dependency (for real)
boricj Oct 28, 2018
9e24370
[python] Clean up turtle code and samples
boricj Oct 28, 2018
0c94f44
[python] Return value for turtle.pensize()
boricj Oct 30, 2018
fcf7cc3
[python] Implement turtle.position()
boricj Oct 30, 2018
c89a369
[python] Add getters for turtle module
boricj Oct 30, 2018
34f5aba
[python] Add turtle toolbox
boricj Oct 30, 2018
efaa87d
Merge branch 'master' into python_turtle
Ecco Nov 16, 2018
82f0b71
[python/turtle] Use micropython_port_interruptible_msleep
Ecco Nov 16, 2018
22a39a2
[python/turtle] Use a dedicated folder
Ecco Nov 16, 2018
fe076d0
[python/turtle] Use mp_float_t
Ecco Nov 16, 2018
a9091c9
[python/turtle] Use a Turtle class
Ecco Nov 16, 2018
7fc34fe
[python/turtle] Use an init method to reset the turtle
Ecco Nov 16, 2018
5748521
[python/turtle] Try to play well with uPy's GC
Ecco Nov 16, 2018
467ae9c
[python/turtle] Fix small typos
Ecco Nov 16, 2018
05a2491
[python/turtle] Fix the heading
Ecco Nov 16, 2018
49e8a50
[python] Fix micropython_port_interruptible_msleep
LeaNumworks Dec 5, 2018
1e78f20
[python] Return after interruption in possibly long turtle methods
LeaNumworks Dec 6, 2018
4b3ce7a
[python/turtle] Fix Turtle::setSpeed
LeaNumworks Dec 6, 2018
e769255
[python/turtle] Default pen size is 1
LeaNumworks Dec 6, 2018
d530637
[python/turtle] Fix speed type
LeaNumworks Dec 6, 2018
432e259
[python/turtle] Speed max and min should produce the same behavior
LeaNumworks Dec 6, 2018
3ca1790
[python/turtle] The Y axis is oriented upwards
LeaNumworks Dec 6, 2018
9c9355d
[python/turtle] Use Escher::Metric values instead of copying them
LeaNumworks Dec 6, 2018
cc3beb6
[python/turtle] Turtle::reset() method
LeaNumworks Dec 6, 2018
c8a5972
[python/turtle] Turtle::circle(radius, angle)
LeaNumworks Dec 7, 2018
e836593
[kandinsky] In KDContext::getPixels: beware of rects crossing the screen
LeaNumworks Dec 7, 2018
c95e291
[python/turtle] First icon redesign
LeaNumworks Dec 7, 2018
3b33425
[python/turlte] Move the turtle paws to mimic crawling
LeaNumworks Dec 10, 2018
3249ccb
[python/turtle] "back()" shortcut for "backwards()"
LeaNumworks Dec 10, 2018
fa0e93e
[python/turtle] Fix Turtle::circle for negative angles
LeaNumworks Dec 10, 2018
7efb48a
[python/turtle] Turtle::circle's angle argument is optional
LeaNumworks Dec 10, 2018
d545c45
[python/turtle] When speed = 0, do not draw the turtle during "goTo"
LeaNumworks Dec 10, 2018
23e3038
[python/turtle] Remove waitForVBlank before drawings
LeaNumworks Dec 10, 2018
62cda01
[python/turtle] color and pencolor can take various arguments
LeaNumworks Dec 11, 2018
3bd3e64
[python/turtle] Order the qstr
LeaNumworks Dec 11, 2018
99d71bb
[python/turtle] Clean Turtle::setColor
LeaNumworks Dec 12, 2018
8ac6098
[python/turtle] Remove old turtle icon
LeaNumworks Dec 12, 2018
91c4fea
[python/turtle] Clean Turtle::PawPosition
LeaNumworks Dec 12, 2018
306869e
[python/turtle] Tune values of the turtle speed
LeaNumworks Dec 11, 2018
609740b
[apps/code] Order catalog.universal.i18n
LeaNumworks Dec 17, 2018
8a3aa1a
[apps/code] Add turtle functions in th python toolbox
LeaNumworks Dec 17, 2018
b058b02
[apps/code] Add turtle functions in the toolbox catalog
LeaNumworks Dec 17, 2018
b3c11f2
[apps/code] Put turtle module higher in Python toolbox
LeaNumworks Dec 17, 2018
62de89c
[apps/code] Fix 'blue', 'red', ... python command insertion
LeaNumworks Dec 17, 2018
101370c
[escher] Change ToolboxMessageTree constructors
LeaNumworks Dec 17, 2018
ad745f7
[python/turtle] Add speed() methods without args
LeaNumworks Dec 17, 2018
ed19d19
Merge branch 'master' into python_turtle
LeaNumworks Dec 17, 2018
4b30679
[python/turtle] Turtle::m_drawn is false if sandbox not displayed
LeaNumworks Dec 17, 2018
de6646a
[build/emscripten] Add turtle symbols to the whitelist
LeaNumworks Dec 18, 2018
5a941bc
[python/turtle] Clean circle code
LeaNumworks Dec 21, 2018
c2dbe9f
[python/turtle] Fix line drawing glitches
LeaNumworks Dec 21, 2018
df2b9c4
[escher/apps] Get children count via template in ToolboxMessageTree::…
LeaNumworks Dec 21, 2018
03f300a
[python/turtle] Add showturtle and hideturtle to the toolbox
LeaNumworks Dec 27, 2018
384fa04
[apps/code] Change the default scripts: squares instead of factorial
LeaNumworks Dec 27, 2018
9b07f19
[apps/code] Squares script template: start by resetting the turtle
LeaNumworks Jan 3, 2019
4f4ae4e
Merge branch 'master' into python_turtle
LeaNumworks Jan 8, 2019
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

[python] Turtle module improvements

  • Loading branch information...
boricj committed Oct 28, 2018
commit 0a05b94b4d9d1c7a3ef54d13a2536185b1aa3a1d
@@ -62,8 +62,17 @@ def roots(a,b,c):

constexpr ScriptTemplate spiralScriptTemplate("spiral.py", "\x00" R"(from turtle import *
for i in range(255):
# Change pen width
s=int(1+i/16)
if s > 10:
s = 10
pensize(s)
# Change pen color
gray=255-i
color(gray,int(gray*0.75),int(gray*0.25))
pencolor(gray,int(gray*0.75),int(gray*0.25))
# Draw a segment of the spiral
forward(i*0.1)
left(10))");

@@ -28,17 +28,33 @@ Q(set_pixel)

Q(turtle)
Q(forward)
Q(fd)
Q(backward)
Q(bk)
Q(right)
Q(rt)
Q(left)
Q(lt)
Q(goto)
Q(setheading)
Q(seth)
Q(speed)

Q(pendown)
Q(pd)
Q(down)
Q(penup)
Q(pu)
Q(up)
Q(pensize)
Q(width)

Q(color)
Q(showturtle)
Q(st)
Q(hideturtle)
Q(hd)

Q(pencolor)
Q(reset)

// MicroPython QSTRs
@@ -12,25 +12,46 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(turtle_speed_obj, turtle_speed);

STATIC MP_DEFINE_CONST_FUN_OBJ_0(turtle_pendown_obj, turtle_pendown);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(turtle_penup_obj, turtle_penup);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(turtle_pensize_obj, turtle_pensize);

STATIC MP_DEFINE_CONST_FUN_OBJ_3(turtle_color_obj, turtle_color);

STATIC MP_DEFINE_CONST_FUN_OBJ_0(turtle_showturtle_obj, turtle_showturtle);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(turtle_hideturtle_obj, turtle_hideturtle);

STATIC MP_DEFINE_CONST_FUN_OBJ_0(turtle___init___obj, turtle___init__);

STATIC const mp_rom_map_elem_t turtle_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_turtle) },
{ MP_ROM_QSTR(MP_QSTR___init__), (mp_obj_t)&turtle___init___obj },
{ MP_ROM_QSTR(MP_QSTR_forward), (mp_obj_t)&turtle_forward_obj },
{ MP_ROM_QSTR(MP_QSTR_fd), (mp_obj_t)&turtle_forward_obj },
{ MP_ROM_QSTR(MP_QSTR_backward), (mp_obj_t)&turtle_backward_obj },
{ MP_ROM_QSTR(MP_QSTR_bk), (mp_obj_t)&turtle_backward_obj },
{ MP_ROM_QSTR(MP_QSTR_right), (mp_obj_t)&turtle_right_obj },
{ MP_ROM_QSTR(MP_QSTR_rt), (mp_obj_t)&turtle_right_obj },
{ MP_ROM_QSTR(MP_QSTR_left), (mp_obj_t)&turtle_left_obj },
{ MP_ROM_QSTR(MP_QSTR_lt), (mp_obj_t)&turtle_left_obj },
{ MP_ROM_QSTR(MP_QSTR_goto), (mp_obj_t)&turtle_goto_obj },
{ MP_ROM_QSTR(MP_QSTR_setheading), (mp_obj_t)&turtle_setheading_obj },
{ MP_ROM_QSTR(MP_QSTR_seth), (mp_obj_t)&turtle_setheading_obj },
{ MP_ROM_QSTR(MP_QSTR_speed), (mp_obj_t)&turtle_speed_obj },

{ MP_ROM_QSTR(MP_QSTR_pendown), (mp_obj_t)&turtle_pendown_obj },
{ MP_ROM_QSTR(MP_QSTR_pd), (mp_obj_t)&turtle_pendown_obj },
{ MP_ROM_QSTR(MP_QSTR_down), (mp_obj_t)&turtle_pendown_obj },
{ MP_ROM_QSTR(MP_QSTR_penup), (mp_obj_t)&turtle_penup_obj },
{ MP_ROM_QSTR(MP_QSTR_pu), (mp_obj_t)&turtle_penup_obj },
{ MP_ROM_QSTR(MP_QSTR_up), (mp_obj_t)&turtle_penup_obj },
{ MP_ROM_QSTR(MP_QSTR_pensize), (mp_obj_t)&turtle_pensize_obj },
{ MP_ROM_QSTR(MP_QSTR_width), (mp_obj_t)&turtle_pensize_obj },

{ MP_ROM_QSTR(MP_QSTR_pencolor), (mp_obj_t)&turtle_color_obj },

{ MP_ROM_QSTR(MP_QSTR_color), (mp_obj_t)&turtle_color_obj },
{ MP_ROM_QSTR(MP_QSTR_showturtle), (mp_obj_t)&turtle_showturtle_obj },
{ MP_ROM_QSTR(MP_QSTR_st), (mp_obj_t)&turtle_showturtle_obj },
{ MP_ROM_QSTR(MP_QSTR_hideturtle), (mp_obj_t)&turtle_hideturtle_obj },
{ MP_ROM_QSTR(MP_QSTR_hd), (mp_obj_t)&turtle_hideturtle_obj },
};

STATIC MP_DEFINE_CONST_DICT(turtle_module_globals, turtle_module_globals_table);
@@ -10,8 +10,12 @@ mp_obj_t turtle_speed(mp_obj_t speed);

mp_obj_t turtle_pendown();
mp_obj_t turtle_penup();
mp_obj_t turtle_pensize(mp_obj_t size);

mp_obj_t turtle_color(mp_obj_t r, mp_obj_t g, mp_obj_t b);

mp_obj_t turtle_showturtle();
mp_obj_t turtle_hideturtle();

mp_obj_t turtle___init__();
void turtle_deinit();
@@ -13,49 +13,53 @@ constexpr int t_x_offset = Ion::Display::Width / 2;
constexpr int t_y_offset = (Ion::Display::Height-18) / 2;
constexpr int t_size = 9;
constexpr int t_icons = 8;
constexpr KDPoint t_icon_offset(-t_size/2, -t_size/2);
constexpr KDPoint t_icon_offset(-t_size/2 + 1, -t_size/2 + 1);
constexpr KDSize t_icon_size(t_size, t_size);

static KDColor t_color;
static float t_heading;
static float t_x, t_y;
static bool t_penup;
static bool t_hidden;
static int t_speed;
static int t_dotsize;

static int t_mileage;
static bool t_drawn;

static KDColor *t_underneath;
static KDColor *t_icon;

constexpr KDCoordinate dotDiameter = 5;
const uint8_t dotMask[dotDiameter][dotDiameter] = {
{0xE1, 0x45, 0x0C, 0x45, 0xE1},
{0x45, 0x00, 0x00, 0x00, 0x45},
{0x00, 0x00, 0x00, 0x00, 0x00},
{0x45, 0x00, 0x00, 0x00, 0x45},
{0xE1, 0x45, 0x0C, 0x45, 0xE1},
};
static uint8_t *t_dot;

static KDPoint pos_turtle(float x, float y) {
return KDPoint(x + t_x_offset, y + t_y_offset);
return KDPoint(round(x + t_x_offset), round(y + t_y_offset));
}

void draw_turtle() {
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();

int offset = int((t_heading / (2*M_PI)) * t_icons + 0.5) % t_icons * (t_size*t_size);
int frame = (int)((t_heading / (2*M_PI)) * t_icons + 0.5);
if (frame < 0) {
frame = t_icons - ((-frame) % t_icons) - 1;
}
else {
frame = frame % t_icons;
}

KDIonContext::sharedContext()->fillRectWithPixels(KDRect(pos_turtle(t_x, t_y).translatedBy(t_icon_offset), t_icon_size), &t_icon[offset], nullptr, t_drawn ? nullptr : t_underneath);
int offset = frame * (t_size*t_size);

if (t_mileage > 1000) {
if (t_speed > 0) {
Ion::msleep(8 * (8 - t_speed));
if (!t_hidden) {
KDIonContext::sharedContext()->fillRectWithPixels(KDRect(pos_turtle(t_x, t_y).translatedBy(t_icon_offset), t_icon_size), &t_icon[offset], nullptr, t_drawn ? nullptr : t_underneath);

if (t_mileage > 1000) {
if (t_speed > 0) {
Ion::msleep(8 * (8 - t_speed));
}
t_mileage -= 1000;
}
t_mileage -= 1000;
}

t_drawn = true;
t_drawn = true;
}
}

void erase_turtle() {
@@ -69,9 +73,9 @@ void dot_turtle(float x, float y) {
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();

if (!t_penup) {
KDColor colors[dotDiameter*dotDiameter];
KDRect rect(pos_turtle(x, y), KDSize(dotDiameter, dotDiameter));
KDIonContext::sharedContext()->blendRectWithMask(rect, t_color, &dotMask[0][0], colors);
KDColor colors[t_dotsize*t_dotsize];
KDRect rect(pos_turtle(x, y).translatedBy(KDPoint(-t_dotsize/2, -t_dotsize/2)), KDSize(t_dotsize, t_dotsize));
KDIonContext::sharedContext()->blendRectWithMask(rect, t_color, t_dot, colors);
}

if (t_speed > 0) {
@@ -175,11 +179,56 @@ mp_obj_t turtle_penup() {
return mp_const_none;
}

mp_obj_t turtle_pensize(mp_obj_t size) {
int s = mp_obj_get_int(size);
if (s < 1) {
s = 1;
}
else if (s > 10) {
s = 10;
}

if (t_dot) {
delete[] t_dot;
}
t_dot = new uint8_t[s*s];
t_dotsize = s;

float middle = s / 2;
for (int j = 0; j < s; j++) {
for (int i = 0; i < s; i++) {
float distance = sqrt((j - middle)*(j - middle) + (i - middle)*(i - middle)) / middle;
int value = distance * distance * 255;
if (value < 0) {
value = 0;
}
else if (value > 255) {
value = 255;
}
t_dot[j*s + i] = value;
}
}

return mp_const_none;
}

mp_obj_t turtle_color(mp_obj_t r, mp_obj_t g, mp_obj_t b) {
t_color = KDColor::RGB888(mp_obj_get_int(r), mp_obj_get_int(g), mp_obj_get_int(b));
return mp_const_none;
}

mp_obj_t turtle_showturtle() {
t_hidden = false;
draw_turtle();
return mp_const_none;
}

mp_obj_t turtle_hideturtle() {
t_hidden = true;
erase_turtle();
return mp_const_none;
}

mp_obj_t turtle___init__() {
if (!t_underneath) {
t_underneath = new KDColor[t_size * t_size];
@@ -201,6 +250,9 @@ mp_obj_t turtle___init__() {
t_penup = false;
t_speed = 6;
t_mileage = 0;
t_hidden = false;

turtle_pensize(MP_OBJ_NEW_SMALL_INT(5));

return mp_const_none;
}
@@ -210,4 +262,6 @@ void turtle_deinit() {
t_underneath = nullptr;
delete[] t_icon;
t_icon = nullptr;
delete[] t_dot;
t_dot = nullptr;
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.