[Tutor] canvas -> CLASSES
Teresa Stanton
tms43 at clearwire.net
Thu May 10 18:59:19 CEST 2007
OK....
Would it be ... er... uh... 'proper'... to put the maze in a module, then
make the actual movement of the .gif's in a class in a seperate file
(module)? I find the code required to build the maze to be quite large,
even with the refinenements. For now, at least, I can't figure out how to
do the maze in a class, and I want to get the .gif moving in a class because
that makes sense. But I'm not sure how to put the .gif ON the maze if they
are seperate files.
I will, of course import the maze, but then, how do I put the gif on the
maze from the class? Is it even possible? am I asking the right question?
T
----- Original Message -----
From: "Alan Gauld" <alan.gauld at btinternet.com>
To: <tutor at python.org>
Sent: Wednesday, May 09, 2007 1:08 PM
Subject: Re: [Tutor] canvas -> CLASSES
>
> "Teresa Stanton" <tms43 at clearwire.net> wrote
>> CLASSES: This is where I start getting confused.
>
> No kidding! :-)
>
>> put it into a class, and for some reason it all starts to get
>> jumbled here,
>> which it shouldn't because I write in C++.
>
> C++ OOP and Python are significantly different and some of
> those differences may be at work here.
>
>> Since I've put it in a class, my design is off because I don't know
>> how to
>> 'call' it, among other issues.
>
> You need an instance which you don;t have currently.
>
>> Do the point definitions of the walls, boxes and T's get
>> put inside the function?
>
> Probably not, probably etter to puut them in a list - as you do - and
> pass the list into the method.
>
>> Right now the function calls itself and I don't think its the best
>> way to do it,
>
> Nor do I, but I'mnot clear exactly what you are doing since
> the data is all hard coded inside the function so it looks to me
> like you will you will get into a loop...
>
> Some snippets and comments follow:
>
>> class Maze(object):
>> def __init__(self):
>> #initial position of .gif
>> self.xp = 100
>> self.yp = 600
>
> You don't seem to use selfd.xp/yp anywhere...
> In fact since you don't initialize any objects this never even
> gets called!
>
>> def createWall((x0, y0), (x1, y1), colour = 'blue', width = 3):
>> """ Create a wall from (x0, y0) to (x1, y1). """
>> canvasOne.create_line(x0, y0, x1, y1, width = width, fill =
>> colour)
>
> You don't have a self parameter in the method. Recall that
> self is equivalent to this in C++ but needs to be explicitly
> declared in Python methods.
>
> Also you are declaring parameters to be tuples which I don't
> think you can do. You need to name the tuples, something like:
>
> def createWall(self, pt1,pt2, canvas = None, colour='blue', width=3)
>
> Also you use the canvasOne global value here which would
> be better passed in as am parameter as shown above.
>
>> outerWall = [(450, 640), (475, 640), (475, 640),
>> walls = [outerWall]
>
> Not sure why the second line is there.
> The first is static data which means the method never changes
> what it does. You never use the x0,y0, params passed in after
> the create line above...
>
>> for wall in walls:
>> for i in range(len(wall)-1):
>> createWall(outerWall[i], outerWall[i+1])
>
> For each entry draw a kine then come back into this loop
> using the same data so we very quickly hit the Python
> recursion limit I suspect.
>
>> topLeftBox = [(130, 105), (130, 125), ...
>> secondTopLeftBox = [(160, 215), (160, 225), ...
>> ...
>> boxes = [topLeftBox, secondTopLeftBox, topRightBox,
>> secondTopRightBox]
>>
>> for box in boxes:
>> for i in range(len(box)-1):
>> createWall(topLeftBox[i], topLeftBox[i+1]),
>> createWall(secondTopLeftBox[i],
>> secondTopLeftBox[i+1]),
>
> Sorry, I have no idea how this is supposed to work. I just got
> confused about the mix of topLefts and box versus boxes etc.
>
> But remember that each call to createWall only uses the arguments
> to draw a single line, then the rest uses the same data over again.
>
> Also to acces createWall you should really be using self.createWall
> since createWall is a method of your Maze class.
>
>> x = 40
>> for i in range(9):
>> x += 20
>> canvasOne.create_rectangle(x, 610, x+5, 615, fill =
>> 'white')
>>
>> #left path built north to south
>> y = 290
>> for i in range(15):
>> y += 20
>> canvasOne.create_rectangle(220, y, 225, y+5, fill =
>> 'white')
>
>
> All these bits could be put into a function with the values as
> parameters, something like::
>
> def buildPath(val1,val2, number, increment, deltax,deltay,orientation,
> fill='white')
> for i in range(number):
> val1 += increment
> if orientation = 0: x,y = val1,val2
> else: x,y = val2,val1
> canvas.create_rectangle(x,y,x+deltax,y+deltay,fill)
>
>
>> root = Tk()
>> root.title("Background")
>>
>> canvasOne = Canvas(width = 800, height = 700, bg = 'black')
>> createWall(450, 640, 475, 640)
>> canvasOne.pack()
>>
>> root.mainloop()
>
>
> Here yuou call createWall which is a method of Maze but you
> don't instantiate Maze. I'd expect so9mething like:
>
> maze = Maze(x,y)
> maze.createWall(....)
>
> But there's a lot of logic missing between there ared here.
> Alsoi if thats all you are doing there is no point of having
> a maze class, just write a createWall function. You need
> to figure out what a Maze object is for. What are its
> responsibilities in the program? What data does it manage?
> What actions do you want it top perform?
>
> Unfortunately from your code I can't work out what you were
> expecting of your Maze instances.
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list