OLD | NEW |
1 // Copyright (c) 2016, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dartino project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 // | 4 // |
5 // This sample was inspired by the Dart Hilbert curve sample: | 5 // This sample was inspired by the Dart Hilbert curve sample: |
6 // https://github.com/daftspaniel/dart-Hilbertcurve/blob/master/web/turtle.dart | 6 // https://github.com/daftspaniel/dart-Hilbertcurve/blob/master/web/turtle.dart |
7 | 7 |
8 library turtle; | 8 library turtle; |
9 | 9 |
10 import "dart:math"; | 10 import "dart:math"; |
11 | 11 |
12 import 'package:stm32/lcd.dart'; | 12 import 'package:stm32/lcd.dart'; |
13 | 13 |
14 class Turtle { | 14 class Turtle { |
15 FrameBuffer _display; | 15 FrameBuffer _display; |
16 int _x; | 16 int _x; |
17 int _y; | 17 int _y; |
18 int _direction = 90; | 18 int _direction = 90; |
19 Color penColor = Color.red; | 19 Color penColor = Color.red; |
20 | 20 |
21 /// Create a new Turtle, and set initial location. | 21 /// Create a new Turtle, and set initial location. |
22 Turtle(this._display, {xPosition: 0, yPosition: 0}) { | 22 Turtle(this._display, {xPosition: 0, yPosition: 0}) { |
| 23 _display.backgroundColor = Color.black; |
23 _display.clear(); | 24 _display.clear(); |
24 _x = xPosition; | 25 _x = xPosition; |
25 _y = yPosition; | 26 _y = yPosition; |
26 } | 27 } |
27 | 28 |
28 /// Move forward [distance] pixels in the current direction with the pen down. | 29 /// Move forward [distance] pixels in the current direction with the pen down. |
29 forward(int distance) { | 30 forward(int distance) { |
30 double r = _direction * (PI / 180.0); | 31 double r = _direction * (PI / 180.0); |
31 int dx = (distance * sin(r)).toInt(); | 32 int dx = (distance * sin(r)).toInt(); |
32 int dy = (distance * cos(r)).toInt(); | 33 int dy = (distance * cos(r)).toInt(); |
(...skipping 19 matching lines...) Expand all Loading... |
52 turnRight(int degrees) { | 53 turnRight(int degrees) { |
53 _turn(-degrees); | 54 _turn(-degrees); |
54 } | 55 } |
55 | 56 |
56 /// Move to [x, y] with the pen up. | 57 /// Move to [x, y] with the pen up. |
57 flyTo(int x, int y) { | 58 flyTo(int x, int y) { |
58 this._x = x; | 59 this._x = x; |
59 this._y = y; | 60 this._y = y; |
60 } | 61 } |
61 } | 62 } |
OLD | NEW |