# myEtchASketch application from Coding Club: Python Basics from tkinter import * import math ##### Set variables: canvas_height = 400 canvas_width = 600 canvas_colour = "black" p1_x=canvas_width/2 p1_y=canvas_height p1_colour="green" line_width=5 line_length=5 ##### Logo mode variables: heading=0 x2=p1_x y2=p1_y ##### Functions: # player controls def p1_move_N(self): global p1_y canvas.create_line(p1_x, p1_y, p1_x, (p1_y-line_length), width=line_width, fill=p1_colour) p1_y = p1_y - line_length def p1_move_S(self): global p1_y canvas.create_line(p1_x, p1_y, p1_x, p1_y+line_length, width=line_width, fill=p1_colour) p1_y = p1_y + line_length def p1_move_E(self): global p1_x canvas.create_line(p1_x, p1_y, p1_x + line_length, p1_y, width=line_width, fill=p1_colour) p1_x = p1_x + line_length def p1_move_W(self): global p1_x canvas.create_line(p1_x, p1_y, p1_x - line_length, p1_y, width=line_width, fill=p1_colour) p1_x = p1_x - line_length def erase_all(self): canvas.delete(ALL) # Logo mode functions: def logo_command(self): global heading global x2 global y2 heading = int(input("What heading would you like to set? ")) distance = int(input("How far - how many pixels? ")) total_distance = distance * line_length if heading < 91: heading_rads = heading / 57.32 x2 = p1_x + math.sin(heading_rads) * distance y2 = p1_y - math.cos(heading_rads) * distance elif heading >90 and heading <181: heading = heading - 90 heading_rads = heading / 57.32 x2 = p1_x + math.cos(heading_rads) * distance y2 = p1_y + math.sin(heading_rads) * distance elif heading >180 and heading <271: heading = heading - 180 heading_rads = heading / 57.32 x2 = p1_x - math.sin(heading_rads) * distance y2 = p1_y + math.cos(heading_rads) * distance elif heading >270: heading = heading - 270 heading_rads = heading / 57.32 x2 = p1_x - math.cos(heading_rads) * distance y2 = p1_y - math.sin(heading_rads) * distance print (math.sin(heading_rads)) print (math.cos(heading_rads)) print ("x-move = ", x2-p1_x) print ("y-move = ", y2-p1_y) def draw_line(self): global p1_x global p1_y canvas.create_line(p1_x, p1_y, x2, y2, width=line_width, fill=p1_colour) p1_x = x2 p1_y = y2 ##### main: window = Tk() window.title("MyEtchaSketch - MiniLogo - Press l for Logo...") canvas = Canvas(bg=canvas_colour, height=canvas_height, width=canvas_width, highlightthickness=0) canvas.pack() # bind movement to key presses window.bind("", p1_move_N) window.bind("", p1_move_S) window.bind("", p1_move_W) window.bind("", p1_move_E) window.bind("u", erase_all) # Logo mode: window.bind("l", logo_command) window.bind("d", draw_line) window.mainloop()