Note that there are some explanatory texts on larger screens.

plurals
  1. POBugs drawing with turtle(python) using onkey() and dictionaries
    primarykey
    data
    text
    <p>I decided redo this question as per advice I received, This is an assignment question I have been given for 1st year uni, python coding. I have bugs in my code and Can't figure out where to fix them. BUG 1 The turtle starts drawing when program is ran even though pen is up. BUG 2 undefined key such as 's, 7, tab' trigger space_bar function</p> <p>COLOURING BOOK</p> <p>In this task you create a children's colouring game in which given pictures can be coloured by tracing around a shape and then filling it. The controls are as follows.</p> <p>ARROW KEYS - Move the "brush" (turtle cursor) left, right, up or down by a fixed small amount.</p> <p>'z' - Undo the last step.</p> <p>'r', 'g', 'b' - Change the brush colour to red, green or blue, respectively. (You can define more colours if you like, but we expect at least these three.)</p> <p>SPACE BAR - Toggle the painting mode. In "move" mode, which is the initial mode, the "brush" (turtle) moves around the screen without drawing. In "paint" mode the brush leaves a coloured line as it moves. Most importantly, when the mode is changed from "paint" to "move", the area traced out by the brush is filled with colour.</p> <pre><code>from turtle import * from functools import partial bgpic("Colour_A_Turkey.gif") # change this to change the picture #control the accuracy/speed of the drawing step_size =8 pensize(4) penup() # whenever spacebar is pressed the current state and next state switch values current_state = penup next_state = pendown def space_bar(): global current_state, next_state next_state() current_state, next_state = next_state, current_state #if the current stat is penup fill in with current colour if current_state == penup: end_fill() else: begin_fill() onkey(space_bar, " ") # undo do a mistake function def mistake(): undo() onkey(mistake, "z") #using partial function to store the following functions #so they can be called as arguments from a dictionary #movement strait = partial(fd, step_size) reverse = partial(bk, step_size) turn_rt = partial(rt, step_size) turn_lf = partial(lt, step_size) #colour brow = partial(color, "brown") gree = partial(color, "green") yell = partial(color, "yellow") oran = partial(color, "orange") purp = partial(color, "purple") red = partial(color, "red") blue = partial(color, "blue") #create a dictionary to store all the keys and there abilities key_action = {"b" : blue, "r" : red, "p" : purp, "o" : oran,\ "y" : yell, "g" : gree, "w" : brow, "Right" : turn_rt , "Up" : strait,\ "Down" : reverse, "Left" : turn_lf, "z" : undo()} #when a key in then above dictionary #is pressed it's function is activated for pressed_key, activated_key in key_action.items(): onkey(activated_key, pressed_key) #make turtle look for key strokes with predefined function listen() #finish done() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload