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.
+96 −35
Diff settings

Always

Just for now

[python/turlte] Move the turtle paws to mimic crawling

  • Loading branch information...
LeaNumworks committed Dec 10, 2018
commit 3b334257ad7db900f6883f805cc06cb0dc140a6d
@@ -6,11 +6,10 @@ extern "C" {
#include "../../port.h"
#include "turtle_icon.h"

// TODO add icon in the names
static constexpr KDCoordinate k_turtleSize = 15;
static constexpr KDCoordinate k_turtleBodySize = 5;
static constexpr KDCoordinate k_turtleHeadSize = 3;
static constexpr KDCoordinate k_turtlePawSize = 2;
static constexpr KDCoordinate k_iconSize = 15;
static constexpr KDCoordinate k_iconBodySize = 5;
static constexpr KDCoordinate k_iconHeadSize = 3;
static constexpr KDCoordinate k_iconPawSize = 2;

constexpr KDColor Turtle::k_defaultColor;

@@ -170,7 +169,7 @@ bool Turtle::hasUnderneathPixelBuffer() {
if (m_underneathPixelBuffer != nullptr) {
return true;
}
m_underneathPixelBuffer = allocate<KDColor>(k_turtleSize * k_turtleSize);
m_underneathPixelBuffer = allocate<KDColor>(k_iconSize * k_iconSize);
return (m_underneathPixelBuffer != nullptr);
}

@@ -208,55 +207,77 @@ bool Turtle::hasDotBuffers() {
}

KDRect Turtle::iconRect() const {
KDPoint iconOffset = KDPoint(-k_turtleSize/2, -k_turtleSize/2);
return KDRect(position().translatedBy(iconOffset), k_turtleSize, k_turtleSize);
KDPoint iconOffset = KDPoint(-k_iconSize/2, -k_iconSize/2);
return KDRect(position().translatedBy(iconOffset), k_iconSize, k_iconSize);
}

bool Turtle::draw() {
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();

if (m_visible && hasUnderneathPixelBuffer()) {
KDContext * ctx = KDIonContext::sharedContext();

// Get the pixels underneath the turtle
ctx->getPixels(iconRect(), m_underneathPixelBuffer);

// Draw the body
KDRect drawingRect = KDRect(
position().translatedBy(KDPoint(-k_turtleBodySize/2, -k_turtleBodySize/2)),
k_turtleBodySize,
k_turtleBodySize); // TODO make special method with enum class Paw Head Body Whole
position().translatedBy(KDPoint(-k_iconBodySize/2, -k_iconBodySize/2)),
k_iconBodySize,
k_iconBodySize);
ctx->fillRect(drawingRect, m_color);

KDCoordinate membersOffsetLength = 6;

// Draw the head
KDCoordinate headOffsetX = membersOffsetLength * sin(m_heading);
KDCoordinate headOffsetY = -membersOffsetLength * cos(m_heading);
KDCoordinate headOffsetLength = 6;
KDCoordinate headOffsetX = headOffsetLength * sin(m_heading);
KDCoordinate headOffsetY = -headOffsetLength * cos(m_heading);
KDPoint headOffset(headOffsetX, headOffsetY);
drawingRect = KDRect(
position().translatedBy(headOffset).translatedBy(KDPoint(-k_turtleHeadSize/2, -k_turtleHeadSize/2)),
k_turtleHeadSize,
k_turtleHeadSize); // TODO make special method with enum class Paw Head Body Whole
position().translatedBy(headOffset).translatedBy(KDPoint(-k_iconHeadSize/2, -k_iconHeadSize/2)),
k_iconHeadSize,
k_iconHeadSize);
ctx->fillRect(drawingRect, m_color);

// Draw the paws
membersOffsetLength = 5;
constexpr int numberOfPaws = 4;
constexpr float angles[numberOfPaws] = {M_PI_2/2, M_PI_2+M_PI_2/2, M_PI+M_PI_2/2, M_PI+M_PI_2+M_PI_2/2};
constexpr float anglesEven[numberOfPaws] = {M_PI_2/2, M_PI_2+M_PI_2/2, M_PI+M_PI_2/2, M_PI+M_PI_2+M_PI_2/2};
for (int i = 0; i < numberOfPaws; i++) {
float pawX = membersOffsetLength * sin(m_heading+angles[i]);
float pawY = -membersOffsetLength * cos(m_heading+angles[i]);
KDCoordinate pawOffsetX = ((int)pawX) - (pawX < 0 ? 1 : 0);
KDCoordinate pawOffsetY = ((int)pawY) - (pawY < 0 ? 1 : 0);
KDPoint pawOffset(pawOffsetX, pawOffsetY);
drawingRect = KDRect(
position().translatedBy(pawOffset), // The paw is too small to need to offset it
k_turtlePawSize,
k_turtlePawSize); // TODO make special method with enum class Paw Head Body Whole
ctx->fillRect(drawingRect, m_color);
}

static int j = 0;
int mod = 12;
j = (j+1)%mod;

/* Our turtle walk:
*
* . . . . . . . .
* ==== ==== ==== ====
* ==== O ==== O ==== O ==== O
* ==== ==== ==== ====
* ° ° ° ° ° ° ° °
* */

if (j < mod/4) {
// First walking position
drawPaw(PawType::FrontRight, PawPosition::HalfBackwards);
drawPaw(PawType::BackRight, PawPosition::HalfBackwards);
drawPaw(PawType::FrontLeft, PawPosition::HalfForward);
drawPaw(PawType::BackLeft, PawPosition::HalfForward);
} else if (j < mod/2) {
// Second walking position
drawPaw(PawType::FrontRight, PawPosition::Forward);
drawPaw(PawType::BackRight, PawPosition::Backwards);
drawPaw(PawType::FrontLeft, PawPosition::Normal);
drawPaw(PawType::BackLeft, PawPosition::Normal);
} else if (j < 3*mod/4) {
// Third walking position
drawPaw(PawType::FrontRight, PawPosition::HalfForward);
drawPaw(PawType::BackRight, PawPosition::HalfForward);
drawPaw(PawType::FrontLeft, PawPosition::HalfBackwards);
drawPaw(PawType::BackLeft, PawPosition::HalfBackwards);
} else {
// Fourth walking position
drawPaw(PawType::FrontRight, PawPosition::Normal);
drawPaw(PawType::BackRight, PawPosition::Normal);
drawPaw(PawType::FrontLeft, PawPosition::Forward);
drawPaw(PawType::BackLeft, PawPosition::Backwards);
}
m_drawn = true;
}

@@ -289,6 +310,30 @@ bool Turtle::dot(mp_float_t x, mp_float_t y) {
return micropython_port_vm_hook_loop();
}

void Turtle::drawPaw(PawType type, PawPosition pos) {
assert(!m_drawn && m_underneathPixelBuffer != nullptr);

KDCoordinate pawOffset = 5;
constexpr float crawlOffset = 0.6f;
constexpr float angles[] = {M_PI_2/2, M_PI_2+M_PI_2/2, -M_PI_2-M_PI_2/2, -M_PI_2/2};

// Compute the paw offset from the turtle center
float currentAngle = angles[(int) type];
float crawlDelta = (((int)pos) - 2.0f) * crawlOffset;
float pawX = pawOffset * sin(m_heading+currentAngle) + crawlDelta * sin(m_heading);
float pawY = - pawOffset * cos(m_heading+currentAngle) - crawlDelta * cos(m_heading);
KDCoordinate pawOffsetX = ((int)pawX) - (pawX < 0 ? 1 : 0);
KDCoordinate pawOffsetY = ((int)pawY) - (pawY < 0 ? 1 : 0);
KDPoint offset(pawOffsetX, pawOffsetY);

// Draw the paw
KDRect drawingRect = KDRect(
position().translatedBy(offset), // The paw is too small to need to offset it from its center
k_iconPawSize,
k_iconPawSize);
KDIonContext::sharedContext()->fillRect(drawingRect, m_color);
}

void Turtle::erase() {
if (!m_drawn || m_underneathPixelBuffer == nullptr) {
return;
@@ -80,6 +80,21 @@ class Turtle {
static constexpr KDColor k_defaultColor = KDColorBlack;
static constexpr uint8_t k_defaultPenSize = 1;

enum class PawType : uint8_t {
FrontRight = 0,
BackRight = 1,
FrontLeft = 2,
BackLeft = 3
};

enum class PawPosition : int {
This conversation was marked as resolved by LeaNumworks

This comment has been minimized.

Copy link
@Ecco

Ecco Dec 11, 2018

Contributor

Why bother specifying int as a backing type?

This comment has been minimized.

Copy link
@LeaNumworks

LeaNumworks Dec 13, 2018

Contributor

I forgot to clean this ! Done with 91c4fea

Backwards = 0,
HalfBackwards = 1,
Normal = 2,
HalfForward = 3,
Forward = 4
};

void setHeadingPrivate(mp_float_t angle);
KDPoint position(mp_float_t x, mp_float_t y) const;
KDPoint position() const { return position(m_x, m_y); }
@@ -94,14 +109,15 @@ class Turtle {
bool draw();
bool dot(mp_float_t x, mp_float_t y);

void drawPaw(PawType type, PawPosition position);
void erase();

/* The frame's center is the center of the screen, the x axis goes to the
* right and the y axis goes upwards. */
mp_float_t m_x;
mp_float_t m_y;
/* The heading is the angle in radians between the direction of the turtle and
* the X axis, in the trigonometric direction. */
* the X axis, in the counterclockwise direction. */
mp_float_t m_heading;

KDColor m_color;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.