0

I'm trying to refactor some code here, to make it more manageable and reader-friendly. I'm currently stuck with this huge method which seems extremely simple to me, and I want to see if I can break it down any more:

def moveCircle(self, event):
    if event.keysym == "Up":
        self.canvas.move("circle", 0, -5)
        self.canvas.move("text", 0, -5)
        self.y1 -= 5
        self.y2 -= 5
    elif event.keysym == "Down":
        self.canvas.move("circle", 0, 5)
        self.canvas.move("text", 0, 5)
        self.y1 += 5
        self.y2 += 5
    elif event.keysym == "Left":
        self.canvas.move("circle", -5, 0)
        self.canvas.move("text", -5, 0)
        self.x1 -= 5
        self.x2 -= 5
    elif event.keysym == "Right":
        self.canvas.move("circle", 5, 0)
        self.canvas.move("text", 5, 0)
        self.x1 += 5
        self.x2 += 5
    self.canvas.update

I've got two canvas objects, "circle" and "text" which move the same value each time a user presses one of the arrow keys. The only thing that changes is the direction (duh). My instance variables for x1, x2, y1, and y2 are used in the program for purposes not important for this question (just so you know. I do need them in-, and decremented this way). What I'm hoping for is a magical self.canvas.move("circle" && "text", 0, 5) or something ridiculous like that. (Obviously, that command doesn't exist, but I'm wondering if there's a more concise way to do this, or if I'm just going crazy)

For the record, I also tried self.y1 = self.y2 -= 5 and the compiler threw a fit, so that was a bummer. Anyone know how to in- and decrement multiple values the same amount, at the same time? That'd be super cool beans.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Brad Rice
  • 1,334
  • 2
  • 17
  • 36
  • It maybe faster to replace the elif block with http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python/10272369#10272369 – Asher May 28 '13 at 03:07

2 Answers2

1

It might be a little bit more manageable if you split it up

def moveCircle(self, event):
    dif = { 
                    "Up":(0,-5),
                    "Down":(0,5),
                    "Left":(-5,0),
                    "Right":(5,0),
            }   
    self.moveCanvas(*(dif[event.keysym]))
    self.canvas.update

def moveCanvas(self, xdif, ydif):
    self.canvas.move("circle", xdif, ydif)
    self.canvas.move("text", xdif, ydif)
    self.x1 += xdif
    self.x2 += xdif
    self.y1 += ydif
    self.y2 += ydif
Curt
  • 1,394
  • 9
  • 16
  • This looks great! If you don't mind my asking, what does the asterisk in the `self.moveCanvas(*(dif[event.keysym]))` line mean? I never thought to use a dictionary, but I've never seen any notation use an asterisk like that other than bash or something like that. What does it mean? – Brad Rice Oct 22 '12 at 00:56
  • I figured it out. If anyone else was curious, look at [this article](http://stackoverflow.com/questions/4306574/python-method-function-arguments-starting-with-asterisk-and-dual-asterisk). That article explains the * passes the values given to it as a list, so the tuple that's stored as a value in the dictionary gets broken up into two variables, `xdif` and `ydif`. That is really cool! – Brad Rice Oct 22 '12 at 01:38
0

You can define a dictionary with the unit vectors for each direction. Something like:

uvs = {'right': 1,0, 'up': 0,-1...}

Then for each movement, get the current position of each element you want to move, add the unit vector (and the desired scaling), and reposition the element.

Chrismit
  • 1,488
  • 14
  • 23