# Karel The Robot in Python ## 1.所需使用的模組 ```python # coding=UTF-8 import turtle import easygui import math import time ``` ## 2.世界邊界設定 ```python #Border position(must be integer) TOPBORDER = 200 BOTTOMBORDER = -200 LEFTBORDER = -200 RIGHTBORDER = 200 ``` ## 3.記錄檔案 ```python f = open("LOG.txt","w")#Ready to write LOG ``` ## 4.內部其他設定 ```python #Karel heading RIGHT = 0 LEFT = 180 TOP = 90 BOTTOM = 270 Karel = turtle.Turtle()#Set turtle "Karel the Robot" Candy = turtle.Turtle()#Set turtle "Candy" Choices = ["forward","Turn Left","Reset Karel the Robot"] ``` ## 5.記錄時間格式 ```python def nowTime(mode):#Show Time as YY/MM/DD hour:min:sec now = time.localtime() hour = now[3] m = now[4] sec = now[5] if now[3] <= 9: hour = "0"+str(now[3]) if now[4] <= 9: m = "0"+str(now[4]) if now[5] <= 9: sec = "0"+str(now[5]) if mode == 1: return (str(now[0])+"/"+str(now[1])+"/"+str(now[2])+" "+str(hour)+":"+str(m)+":"+str(sec)) if mode == 2: return (str(hour)+":"+str(m)+":"+str(sec)) ``` ## 6.設定Karel ```python def setup(Karel,Candy,f): #Setup Karel and Candy Karel.shape("arrow") Karel.color("blue") print("Program: Set Karel shape to arrow") f.writelines((nowTime(2)," Set Karel shape to arrow\n")) turtle.register_shape("candy.gif")#Load candy image print("Program: Load image(candy.gif)...") f.writelines((nowTime(2)," Load image(candy.gif)\n")) Candy.shape("candy.gif") print("Program: Set Candy shape to candy.gif") f.writelines((nowTime(2)," Set Candy shape to candy.gif\n")) Karel.penup() Candy.penup() print("Program: Set Karel's position...") f.writelines((nowTime(2)," Set Karel's position\n")) Karel.goto(LEFTBORDER+50,BOTTOMBORDER+50)#Karel default position print("Program: Set Candy's position...") f.writelines((nowTime(2)," Set Karel's position\n")) if ((LEFTBORDER+25)-(RIGHTBORDER-25))%100 == 50:#Candy default position Candy.goto((LEFTBORDER+RIGHTBORDER)/2,Candy.ycor()) else: Candy.goto((LEFTBORDER+RIGHTBORDER)/2-25,Candy.ycor()) if ((TOPBORDER-25)-(BOTTOMBORDER+25))%100 == 50: Candy.goto(Candy.xcor(),(TOPBORDER+BOTTOMBORDER)/2) else: Candy.goto(Candy.xcor(),(TOPBORDER+BOTTOMBORDER)/2-25) Candy.pendown() print("Program: Setup Done!") f.writelines((nowTime(2)," Setup Done!\n")) ``` ## 7.重置遊戲 ```python def reset(Karel,Candy,f): print("User: Reset Karel Robot.") f.writelines((nowTime(2)," Reset Robot\n")) #Reset Karel and Candy to default Karel.right(Karel.heading())#Karel face to 0 degree Candy.showturtle() Karel.penup() Candy.penup() print("Program: Reset Karel's position...") f.writelines((nowTime(2)," Reset Karel's position\n")) Karel.goto(LEFTBORDER+50,BOTTOMBORDER+50) print("Program: Reset Candy's position...") f.writelines((nowTime(2)," Reset Candy's position\n")) if ((LEFTBORDER+25)-(RIGHTBORDER-25))%100 == 50:#Candy default position Candy.goto((LEFTBORDER+RIGHTBORDER)/2,Candy.ycor()) else: Candy.goto((LEFTBORDER+RIGHTBORDER)/2-25,Candy.ycor()) if ((TOPBORDER-25)-(BOTTOMBORDER+25))%100 == 50: Candy.goto(Candy.xcor(),(TOPBORDER+BOTTOMBORDER)/2) else: Candy.goto(Candy.xcor(),(TOPBORDER+BOTTOMBORDER)/2-25) Candy.pendown() print("Program: Reset Done!") f.writelines((nowTime(2)," Reset Done\n")) ``` ## 8.製作障礙物 ```python def makeWall(f): print("Program: Making Wall...") f.writelines((nowTime(2)," Making Wall...\n")) #Prepare the wall wall = turtle.Turtle() wall.color("gray") wall.penup() wall.goto(LEFTBORDER+25,TOPBORDER-25)#Lower Left Hand corner is default position wall.speed(10000) wall.pensize(2) wall.pendown() LENGTH = abs(RIGHTBORDER-LEFTBORDER) WIDTH = abs(TOPBORDER-BOTTOMBORDER) for i in range(0, int(WIDTH/50)-1):#WIDTH wall.pendown() for j in range(0, int(LENGTH/50)-1):#LENGTH for k in range(0,4): #Each square 100*100pixel wall.forward(50) wall.right(90) wall.goto(wall.xcor()+50, wall.ycor()) wall.penup() wall.goto(LEFTBORDER+25,wall. ycor()-50) wall.hideturtle() print("Program: Make Wall Done!") f.writelines((nowTime(2)," Make Wall Done!\n")) ``` ## 9.偵測Karel碰撞牆壁 ```python def hitWall(Karel,f): if ((Karel.heading() == RIGHT and Karel.xcor()+50 == RIGHTBORDER) or (Karel.heading() == LEFT and Karel.xcor()-50 == LEFTBORDER) or (Karel.heading() == BOTTOM and Karel.ycor()-50 == BOTTOMBORDER) or (Karel.heading() == TOP and Karel.ycor()+50 == TOPBORDER)): easygui.msgbox("Oops! Hit the Wall!!","Karel Say:") print("Karel: Hit the Wall. Position:"+str(Karel.pos())+" Heading: "+str(Karel.heading())) f.writelines((nowTime(2)," Karel: Hit the Wall. Position:"+str(Karel.pos())+" Heading: "+str(Karel.heading())+"\n")) return True ``` ## 10.讓Karel向左轉 ```python def turnLeft(Karel,f): #Karel turn left print("Karel: Turn Left. Position:"+str(Karel.pos())) f.writelines((nowTime(2)," Karel: Turn Left. Position:"+str(Karel.pos())+"\n")) Karel.left(90) ``` ## 11.讓Karel向前走 ```python def forward(Karel,f): #Karel move forward 100 pixel, avoid Karel hit the wall if not hitWall(Karel,f): print("Karel: Forward 50 pixels from Position:"+str(Karel.pos())+" Heading: "+str(Karel.heading())) f.writelines((nowTime(2)," Karel: Forward 50 pixels from Position:"+str(Karel.pos())+" Heading: "+str(Karel.heading())+"\n")) Karel.forward(50) ``` 1~11為基本Karel的基本程式,以下為Karel進階程式: ## 12.所需使用的模組 ```python # coding=UTF-8 import OriginalKarelRobot2 as KR import easygui import time ``` ## 13.設定Karel, 糖果 ```python #Set Karel and Candy Karel = KR.Karel Candy = KR.Candy ```