turtle
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
turtle [2024/01/12 03:17] – appledog | turtle [2024/01/12 03:37] (current) – appledog | ||
---|---|---|---|
Line 205: | Line 205: | ||
This class clearly and succinctly defines the //turtle world// and the position and velocity of the //turtle//. | This class clearly and succinctly defines the //turtle world// and the position and velocity of the //turtle//. | ||
+ | == TurtleProgram.py | ||
+ | The turtle program is a way to store and deal with turtle programs (i.e. '.t files' | ||
+ | |||
+ | < | ||
+ | class TurtleProgram: | ||
+ | def __init__(self, | ||
+ | self.turtle = turtle | ||
+ | self.program = [] | ||
+ | self.pc = 0 | ||
+ | |||
+ | # Erase the program | ||
+ | def reset(self): | ||
+ | self.program = [] | ||
+ | self.pc = 0 | ||
+ | |||
+ | def add(self, s): | ||
+ | self.program.append(s) | ||
+ | |||
+ | def run(self): | ||
+ | self.pc = 0 | ||
+ | while self.pc < len(self.program): | ||
+ | self.do(self.program[self.pc]) | ||
+ | self.pc += 1 | ||
+ | </ | ||
+ | |||
+ | In this beginning part of the program we define what a program is (an array of lines) and we allow for lines to be added and cleared from the program. There is also the concept of running the program. The addition of the run() command as a stub leads us to part II, the do command. Think of this like an instruction fetch cycle for a CPU, with the do() method being the actual lookup and execution of the command. | ||
+ | |||
+ | === opcode execution: do() | ||
+ | < | ||
+ | def do(self, statement): | ||
+ | # Create cmd and para based on statement. | ||
+ | if ' ' in statement: | ||
+ | cmd, para = statement.split(' | ||
+ | try: | ||
+ | para = float(para) | ||
+ | except: | ||
+ | para = 0 | ||
+ | else: | ||
+ | cmd, para = statement, 0 # one space | ||
+ | |||
+ | #print(cmd, para) # for debugging | ||
+ | |||
+ | # Comment lines -- do nothing. | ||
+ | if cmd == "#": | ||
+ | pass | ||
+ | return | ||
+ | |||
+ | # RESET | ||
+ | if cmd == " | ||
+ | self.turtle.reset() | ||
+ | return | ||
+ | |||
+ | # GO | ||
+ | if cmd == " | ||
+ | self.turtle.forward(para) | ||
+ | return | ||
+ | |||
+ | # LEFT | ||
+ | if cmd == " | ||
+ | self.turtle.turnLeft(para) | ||
+ | return | ||
+ | |||
+ | # RIGHT | ||
+ | if cmd == " | ||
+ | self.turtle.turnRight(para) | ||
+ | return | ||
+ | |||
+ | # PEN UP | ||
+ | if cmd == " | ||
+ | self.turtle.penUp() | ||
+ | return | ||
+ | |||
+ | # PEN UP | ||
+ | if cmd == " | ||
+ | self.turtle.penDown() | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | The purpose of do() is to control the turtle. It creates the idea of an automatic turtle which works on the statements of a turtle program. This program could be added manually with .add() method calls or by loading a .t file as shown below: | ||
+ | |||
+ | === Loading a .t file | ||
+ | This is the third part of Class TurtleProgram. | ||
+ | |||
+ | < | ||
+ | def load_file(self, | ||
+ | file = open(filename, | ||
+ | program = file.readlines() | ||
+ | file.close() | ||
+ | |||
+ | # Clean up the file. | ||
+ | self.program = [] | ||
+ | for line in program: | ||
+ | s = line.strip() | ||
+ | if len(s) > 0: | ||
+ | self.program.append(s) | ||
+ | |||
+ | </ | ||
+ | |||
+ | Originally I didn't have the code which cleaned up the program and it wouldn' | ||
+ | |||
+ | == Future Development | ||
+ | === 1. Refactor Turtle.py | ||
+ | We encourage refactoring Turtle.py into two separate classes. | ||
+ | |||
+ | Since we are dealing with a conceptual turtle, there is no reason no to force it into a separate class. | ||
+ | |||
+ | Conceptually, | ||
+ | |||
+ | On that note, it is easy to see a bit of a conflict in the idea of a GameWorld (in Game.py). TurtleWorld (in a theoretical TurtleWorld.py) is itself a discrete thing, which is not necessarily a game. So a good argument for leaving Game.py alone and making a TurtleWorld.py is that TurtleWorld is just data and Game.py is the projector of that world onto the screen. However in this theory Game.py would only contain calls to classes during it's lifecycle maintenance (checkEvents(), | ||
+ | |||
+ | === 2. Editing | ||
+ | We discourage in-suite editing and saving of .t files. | ||
+ | |||
+ | There is no need to do that since they are just text files and text files are a well-established idiom on desktop PCs. | ||
+ | |||
+ | === 3. Expanding | ||
+ | Expanding the commands of the turtle could be interesting. | ||
+ | As a next-step project, depending on the student, we could add commands for color (and maybe alpha), then we could add commands for line width and maybe line patterns. We could also add commands for polygons, arcs, and circles. | ||
+ | |||
+ | Beyond those basic drawing ideas the fun gets in when you add line numbers and GOTOs, and then flow control |
turtle.1705029476.txt.gz · Last modified: 2024/01/12 03:17 by appledog