James Posted February 1, 2009 Share Posted February 1, 2009 (edited) Hey, This is just a start with writing a LOGO interpreter. Commands, FORWARD, CLEAR, COLOR, SIZE, TURN, WAIT, PENDOWN and PENUP. So code: expandcollapse popup#include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Global $arrDic[9] = ["FORWARD", "COLOR", "CLEAR", "WAIT", "DO", "SIZE", "TURN", "PENUP", "PENDOWN"] Global $arrCols[4][2] = [["BLACK", "0x000000"],["RED", "0xFF0000"],["BLUE", "0x0000FF"],["GREEN", "0x00FF00"]] Global $turtle = @ScriptDir & '\turtle.gif', $Count = 0, $picDraw Global Const $Pi = 3.14159265358979323846264338328 Dim $oldX = 228, $oldY = 300, $oldA = 90, $penColor = 0x000000, $penSize = 1, $penDown = 0 ; 0 is down and 1 is up Global Const $degToRad = $Pi / 180 $guiMain = GUICreate("A3 LOGO Interpreter", 474, 445) GUICtrlCreateGroup("", 8, 336, 457, 105) $strCmd = GUICtrlCreateEdit("", 16, 352, 377, 81, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL)) $btnRun = GUICtrlCreateButton("&Run", 400, 352, 57, 81, 0) GUICtrlCreateGroup("", -99, -99, 1, 1) $picDraw = GUICtrlCreateGraphic(8, 8, 457, 330) GUICtrlSetBkColor(-1, 0xFFFFFF) GUICtrlSetGraphic(-1, $GUI_GR_MOVE, $oldX, $oldY) $picTurt = GUICtrlCreatePic($turtle, 228, 300, 20, 24) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $btnRun Command() EndSwitch WEnd Func Turtle($x, $y, $color = $penColor, $size = $penSize, $isDown = $penDown) If $isDown = 0 Then GUICtrlSetPos($picTurt, $x, $y) GUICtrlSetGraphic($picDraw, $GUI_GR_MOVE, $oldX, $oldY) GUICtrlSetGraphic($picDraw, $GUI_GR_PENSIZE, $size) GUICtrlSetGraphic($picDraw, $GUI_GR_COLOR, $color) GUICtrlSetGraphic($picDraw, $GUI_GR_LINE, $x, $y) GUICtrlSetGraphic($picDraw, $GUI_GR_REFRESH) $oldY = $y $oldX = $x Else GUICtrlSetPos($picTurt, $x, $y) $oldY = $y $oldX = $x EndIf EndFunc ;==>Turtle Func Command() $Line = StringSplit(StringStripCR(GUICtrlRead($strCmd)), @LF) For $i = 1 To $Line[0] If $Line[$i] = "" Then ContinueLoop EndIf If StringInStr($Line[$i], " ") Then $ThisThing = StringLeft($Line[$i], StringInStr($Line[$i], " ") - 1) Else $ThisThing = $Line[$i] EndIf Switch StringUpper($ThisThing) Case $arrDic[0] ; FORWARD $ThisY = StringSplit($Line[$i], " ") If IsArray($ThisY) Then Turtle($oldX + ( Cos($oldA * $degToRad) * $ThisY[2]) , $oldY - ( Sin($oldA * $degToRad) * $ThisY[2])) Case $arrDic[1] ; COLOR $getPen = StringSplit($Line[$i], " ") If IsArray($getPen) Then $penColor = $getPen[2] Case $arrDic[2] ; CLEAR ClearGraphic() Case $arrDic[3] ; WAIT $Time = StringSplit($Line[$i], " ") If IsArray($Time) Then Sleep($Time[2]) Case $arrDic[4] ; DO (Loop) $doCount = 0 $ThisLoop = StringSplit($Line[$i], " ") If Not IsNumber($ThisLoop[2]) Then MsgBox(0, "LOGO", "Error: DO loop requires a number.") Else $RegExp = StringRegExp($Line[$i], "\[.*?\]", 3) If @error Then MsgBox(0, "LOGO", "Error: Malformed DO loop.") Else For $j = 0 To $RegExp[0] ConsoleWrite("~> DEBUG: " & $RegExp[$j] & @CRLF) Next Do ConsoleWrite("Count: #" & $doCount & @CRLF) ; DEBUG $doCount = $doCount + 1 ; Increment the DD count Until $doCount = $ThisLoop[2] EndIf EndIf Case $arrDic[5] ; SIZE $getSize = StringSplit($Line[$i], " ") If IsArray($getSize) Then $penSize = $getSize[2] Case $arrDic[6] ; TURN $ThisTurn = StringSplit($Line[$i], " ") If Not IsArray($ThisTurn) Then MsgBox(0, "LOGO", "Error: TURN requires a degree.") Else $oldA += Number($ThisTurn[2]) While ($oldA > 360) $oldA -= 360 WEnd While ($oldA < 0) $oldA += 360 WEnd ;Turtle($oldX - $ThisTurn[2], $oldY) EndIf Case $arrDic[7] ; PENUP $penDown = 1 Case $arrDic[8] ; PENDOWN $penDown = 0 Case Else MsgBox(0, "LOGO", "Error: Unexpected command: " & $ThisThing) EndSwitch Next EndFunc ;==>Command Func ClearGraphic() For $INDEX = 0 To $Count GUICtrlDelete($picDraw) GUICtrlCreateGraphic(8, 8, 457, 330) GUICtrlSetBkColor(-1, 0xFFFFFF) GUICtrlSetGraphic(-1, $GUI_GR_MOVE, $oldX, $oldY) $oldX = 228 $oldY = 300 $oldA = 90 Turtle(228, 300) Next $Count = 0 EndFunc ;==>ClearGraphic And turtle: This code also contains my start at the DO loop. Have fun and improvements welcome James Edit: What the hell happened to boxes? Edit 2: Fixed the problem with not drawing after second command. Thanks Andreik. Edit 3: Added COLOR command Edit 4: Added WAIT command, Andreik. Edited February 2, 2009 by JamesBrooks Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Andreik Posted February 1, 2009 Share Posted February 1, 2009 I didn't saw anything when I press RUN button. Use GUICtrlSetGraphic($picDraw,$GUI_GR_REFRESH) to refresh your graphic after draw. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Author Share Posted February 1, 2009 I didn't saw anything when I press RUN button.Use GUICtrlSetGraphic($picDraw,$GUI_GR_REFRESH) to refresh your graphic after draw.Run the command again. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Author Share Posted February 1, 2009 Fixed the re-draw problem. Thanks Andreik. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Andreik Posted February 1, 2009 Share Posted February 1, 2009 A suggestion: before clear the graphic maybe you want to wait a little time Global $arrDic[5] = ["FORWARD", "RIGHT", "REPEAT", "CLEAR","WAIT"] Case $arrDic[4] $TIME = StringSplit($Line[$i]," ") If IsArray($TIME) Then Sleep($TIME[2]) EndIf When the words fail... music speaks. Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Author Share Posted February 1, 2009 A suggestion: before clear the graphic maybe you want to wait a little time Global $arrDic[5] = ["FORWARD", "RIGHT", "REPEAT", "CLEAR","WAIT"] Case $arrDic[4] $TIME = StringSplit($Line[$i]," ") If IsArray($TIME) Then Sleep($TIME[2]) EndIfUpdated, first post :0 Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Andreik Posted February 1, 2009 Share Posted February 1, 2009 Updated, first post :0$arrDic[4] When the words fail... music speaks. Link to comment Share on other sites More sharing options...
FireFox Posted February 1, 2009 Share Posted February 1, 2009 @JamesBrooks When I try to enter anyvalid commands I have array error like this one : Array variable has incorrect number of subscripts or subscript dimension range exceeded.: Turtle($oldX, $oldY - $ThisY[2]) Turtle($oldX, $oldY - ^ ERROR Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Author Share Posted February 1, 2009 $arrDic[4] Oops my bad @JamesBrooks When I try to enter anyvalid commands I have array error like this one : Array variable has incorrect number of subscripts or subscript dimension range exceeded.: Turtle($oldX, $oldY - $ThisY[2]) Turtle($oldX, $oldY - ^ ERRORAre you using the latest? Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
FireFox Posted February 1, 2009 Share Posted February 1, 2009 @JamesBrooks tried now the lastest one and again error.... Cheers, FireFox. Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Author Share Posted February 1, 2009 @JamesBrooks tried now the lastest one and again error.... Cheers, FireFox.Can I ask what code you are putting into the text box? Try this: COLOR 0xff9900 FORWARD 100 WAIT 5000 COLOR 0xcc3300 FORWARD 100 Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
FireFox Posted February 1, 2009 Share Posted February 1, 2009 @JamesBrooks Working now... I've forgotten to put space between commands Cheers, FireFox. Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Author Share Posted February 1, 2009 @JamesBrooksWorking now... I've forgotten to put space between commands Cheers, FireFox.Heh no problemo Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Achilles Posted February 1, 2009 Share Posted February 1, 2009 Wow, I used a program like this way back in the time... That was probably when I first time realized I enjoy computers a lot. Good start on what you have now. My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list] Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Author Share Posted February 1, 2009 Yay, I have a somewhat DO loop working. I just need to figure out processing the commands as it's being called inside the switch statement itself. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Author Share Posted February 1, 2009 Ichigo, thanks Woo, pen size is done (not available yet). A DO loop is in the works Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
martin Posted February 1, 2009 Share Posted February 1, 2009 Should FORWARD allow X and Y so this Case would be Case $arrDic[0] $ThisXY = StringSplit($Line[$i], " ") Turtle($oldX + $ThisXY[2], $oldY - $ThisXY[3]) and maybe you need a bit more checking for the correct number of parameters. What is the problem with Code boxes? Would be nice if you could have options like clear to start a new drawing because if you want to build up a complicated drawing you would want to try a bit then add a bit and try that, so you need to be able to clear everything. Also an option to save the instructions, and allow FORWARD x,y as well as FORWARD x y maybe? Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Author Share Posted February 1, 2009 Should FORWARD allow X and Y so this Case would be Case $arrDic[0] $ThisXY = StringSplit($Line[$i], " ") Turtle($oldX + $ThisXY[2], $oldY - $ThisXY[3]) and maybe you need a bit more checking for the correct number of parameters. What is the problem with Code boxes? Would be nice if you could have options like clear to start a new drawing because if you want to build up a complicated drawing you would want to try a bit then add a bit and try that, so you need to be able to clear everything. Also an option to save the instructions, and allow FORWARD x,y as well as FORWARD x y maybe?FORWARD only accepts a Y parameter as later TURN will be the X Checking is not done yet, I want to make sure everything works first. There is a CLEAR command which will redraw the "canvas". Save instructions... Like a history type thing? Sounds good, will come when commands are done. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
martin Posted February 1, 2009 Share Posted February 1, 2009 FORWARD only accepts a Y parameter as later TURN will be the X Checking is not done yet, I want to make sure everything works first.There is a CLEAR command which will redraw the "canvas". Save instructions... Like a history type thing? Sounds good, will come when commands are done.Ah thanks I somehow missed the CLEAR. Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Author Share Posted February 1, 2009 Ah thanks I somehow missed the CLEAR.Heh no problem TURN is done (Thanks Manadar ) Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now