mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[python/turtle] Turtle::reset() method
This commit is contained in:
@@ -27,6 +27,9 @@ Q(set_pixel)
|
||||
// Turtle QSTRs
|
||||
|
||||
Q(turtle)
|
||||
|
||||
Q(reset)
|
||||
|
||||
Q(forward)
|
||||
Q(fd)
|
||||
Q(backward)
|
||||
@@ -63,7 +66,6 @@ Q(ht)
|
||||
Q(isvisible)
|
||||
|
||||
Q(pencolor)
|
||||
Q(reset)
|
||||
|
||||
// utime QSTRs
|
||||
Q(time)
|
||||
|
||||
@@ -19,6 +19,11 @@ mp_obj_t modturtle___init__() {
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t modturtle_reset() {
|
||||
sTurtle.reset();
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t modturtle_forward(mp_obj_t px) {
|
||||
sTurtle.forward(mp_obj_get_float(px));
|
||||
return mp_const_none;
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
mp_obj_t modturtle___init__();
|
||||
void modturtle_gc_collect();
|
||||
|
||||
mp_obj_t modturtle_reset();
|
||||
|
||||
mp_obj_t modturtle_forward(mp_obj_t px);
|
||||
mp_obj_t modturtle_backward(mp_obj_t px);
|
||||
mp_obj_t modturtle_right(mp_obj_t deg);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "modturtle.h"
|
||||
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_reset_obj, modturtle_reset);
|
||||
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(modturtle_forward_obj, modturtle_forward);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(modturtle_backward_obj, modturtle_backward);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(modturtle_right_obj, modturtle_right);
|
||||
@@ -27,6 +29,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle___init___obj, modturtle___init__);
|
||||
STATIC const mp_rom_map_elem_t modturtle_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_turtle) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___init__), (mp_obj_t)&modturtle___init___obj },
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_reset), (mp_obj_t)&modturtle_reset_obj },
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_forward), (mp_obj_t)&modturtle_forward_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_fd), (mp_obj_t)&modturtle_forward_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_backward), (mp_obj_t)&modturtle_backward_obj },
|
||||
|
||||
@@ -7,6 +7,7 @@ extern "C" {
|
||||
#include "turtle_icon.h"
|
||||
|
||||
static constexpr KDSize k_iconSize = KDSize(9, 9);
|
||||
constexpr KDColor Turtle::k_defaultColor;
|
||||
|
||||
template <typename T> static inline T * allocate(size_t count) {
|
||||
/* We forward dynamic allocations to the Python GC so we don't have to bother
|
||||
@@ -16,6 +17,25 @@ template <typename T> static inline T * allocate(size_t count) {
|
||||
return static_cast<T*>(m_malloc(sizeof(T) * count));
|
||||
}
|
||||
|
||||
void Turtle::reset() {
|
||||
// Erase the drawing
|
||||
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->resetSandbox();
|
||||
|
||||
// Reset turtle values
|
||||
m_x = 0;
|
||||
m_y = 0;
|
||||
m_heading = k_headingOffset;
|
||||
m_color = k_defaultColor;
|
||||
m_penDown = true;
|
||||
m_visible = true;
|
||||
m_speed = k_defaultSpeed;
|
||||
m_penSize = k_defaultPenSize;
|
||||
m_mileage = 0;
|
||||
|
||||
// Draw the turtle
|
||||
draw();
|
||||
}
|
||||
|
||||
void Turtle::forward(mp_float_t length) {
|
||||
goTo(
|
||||
m_x + length * sin(m_heading),
|
||||
|
||||
@@ -25,11 +25,11 @@ public:
|
||||
m_x(0),
|
||||
m_y(0),
|
||||
m_heading(k_headingOffset),
|
||||
m_color(KDColorBlack),
|
||||
m_color(k_defaultColor),
|
||||
m_penDown(true),
|
||||
m_visible(true),
|
||||
m_speed(k_defaultSpeed),
|
||||
m_penSize(1),
|
||||
m_penSize(k_defaultPenSize),
|
||||
m_mileage(0),
|
||||
m_drawn(false),
|
||||
m_underneathPixelBuffer(nullptr),
|
||||
@@ -39,6 +39,8 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
void reset();
|
||||
|
||||
void forward(mp_float_t length);
|
||||
void backward(mp_float_t length) { forward(-length); }
|
||||
void right(mp_float_t angle) { left(-angle); }
|
||||
@@ -76,6 +78,9 @@ private:
|
||||
static constexpr int k_numberOfIcons = 8;
|
||||
static constexpr uint8_t k_defaultSpeed = 3;
|
||||
static constexpr uint8_t k_maxSpeed = 10;
|
||||
static constexpr KDColor k_defaultColor = KDColorBlack;
|
||||
static constexpr uint8_t k_defaultPenSize = 1;
|
||||
|
||||
|
||||
KDPoint position(mp_float_t x, mp_float_t y) const;
|
||||
KDPoint position() const { return position(m_x, m_y); }
|
||||
|
||||
@@ -17,13 +17,10 @@ public:
|
||||
ExecutionEnvironment();
|
||||
static ExecutionEnvironment * currentExecutionEnvironment();
|
||||
void runCode(const char * );
|
||||
virtual const char * inputText(const char * prompt) {
|
||||
return nullptr;
|
||||
}
|
||||
virtual void displaySandbox() {
|
||||
}
|
||||
virtual void printText(const char * text, size_t length) {
|
||||
}
|
||||
virtual const char * inputText(const char * prompt) { return nullptr; }
|
||||
virtual void displaySandbox() {}
|
||||
virtual void resetSandbox() {}
|
||||
virtual void printText(const char * text, size_t length) {}
|
||||
void interrupt();
|
||||
protected:
|
||||
bool m_sandboxIsDisplayed;
|
||||
|
||||
Reference in New Issue
Block a user