CLASSES: This is where I start getting confused. I've re-written the maze,
per the guidance of John Fouhy (by the way, pretty cool!). Wish I had
thought of doing it that way to begin with, lol. In any case, I'm trying to
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++. None-the-less, I'm having some
issues. I've attached the changes I've made, which worked before I started
putting it in a class. I still need to work on the pathways, but figured I
would get this into a class format before I go any further.
Since I've put it in a class, my design is off because I don't know how to
'call' it, among other issues. Do the point definitions of the walls, boxes
and T's get put inside the function? They could also be a file, but I don't
think that would be necessary. Right now the function calls itself and I
don't think its the best way to do it, mostly because I don't know what to
pass it to begin with. Can someone please just stop my mind from going
different directions? Well, perhaps not, but point me in the right
direction?
Thanks
T
----- Original Message -----
From: "John Fouhy" <[EMAIL PROTECTED]>
To: "Python Tutor" <[email protected]>
Sent: Monday, May 07, 2007 3:22 PM
Subject: Re: [Tutor] canvas -> make an object 'seem' to move
On 08/05/07, Teresa Stanton <[EMAIL PROTECTED]> wrote:
I seem to be stuck again. I've attached my entire file (and the .gif I'm
using. My son made this .gif, its our lab). The problem is my xp (the
variable I use for my x coordinate) isn't updating after the .gif moves.
I
think I should probably organize the entire module better, but then
again,
I've never written anything this big in Python, and nothing like this in
Tkinter. So, perhaps someone will have a suggestion?
Some general comments:
"""
#This will build the north (top) wall of the maze
canvasOne.create_line(10, 10, 790, 10, width = 3, fill = 'blue') #west to
east
canvasOne.create_line(20, 20, 395, 20, width = 3, fill = 'blue') #2nd
line, west to middle
canvasOne.create_line(395, 20, 395, 100, width = 3, fill = 'blue')
#middle to south
canvasOne.create_line(405, 20, 405, 100, width = 3, fill = 'blue')#2nd
middle to south
canvasOne.create_line(395, 100, 405, 100, width = 3, fill = 'blue')
#short west to east
canvasOne.create_line(405, 20, 780, 20, width = 3, fill = 'blue') #2nd
line cont, middle to east
"""
This would be a good opportunity to define some functions. eg, you
could do this:
canvasOne = Canvas(width = 800, height = 700, bg = 'black')
canvasOne.pack()
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)
Then ... hmm, I see from running your code that your lines are all
effectively continuous. So you could represent each wall by a series
of points. eg:
outerWall = [(10,10), (790,10), (790, 360), ...] # etc
innerWall1 = [...]
# etc
walls = [outerWall, innerWall1, ...]
Then you could draw them like:
for wall in walls:
for i in range(len(wall-1)):
createWallall(outerWall[i], outerWall[i+1])
Second comment:
"""
xp = 100
yp = 600
# ...
#onClick Vertical moves the .gif based on where the mouse is clicked
#newY is new location for yp
def onClickVertical(newY, xp, yp):
print 'here'
for i in range(newY):
yp += .8
canvasOne.coords(ph, xp, yp)
time.sleep(.001)
canvasOne.update()
return xp, yp
"""
I notice you have a statement:
yp += .8
in this function. Are you aware that this will _not_ change the value
of yp outside the scope of the function?
If you want to change yp in the global scope, you need to tell python
to treat it as a global variable, by putting the statement 'global yp'
near the top of that function.
A better course of action (global variables are a bit ugly) might be
to encapsulate your maze into a class. eg:
class Maze(object):
def __init__(self):
# initial position of actor
self.xp = 100
self.yp = 600
# create maze, etc.
def onClickVertical(self, newY, xp, yp):
# etc
HTH!
--
John.
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor
"""
The purpose of this module is to build the maze and pathway that the '.gif's' follow.
"""
from Tkinter import *
import time
class Maze(object):
def __init__(self):
#initial position of .gif
self.xp = 100
self.yp = 600
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)
outerWall = [(450, 640), (475, 640), (475, 640), (475, 690), (475, 690),
(790, 690), (790, 690),
(790, 530), (790, 530), (660, 530), (660, 530), (660, 360),
(790, 360), (790, 10), (790, 10),
(10, 10), (10, 10), (10, 360), (10, 360), (150, 360), (150,
360), (150, 530), (150, 530),
(10, 530), (10, 530), (10, 690), (10, 690), (325, 690), (325,
690), (325, 640), (325, 640),
(350, 640), (350, 630), (350, 630), (315, 630), (315, 680),
(20, 680), (20, 680), (20, 560),
(20, 540), (160, 540), (160, 540), (160, 350), (20, 350), (20,
350), (20, 20), (20, 20),
(380, 20), (380, 20), (380, 130), (380, 130), (420, 130), (420,
130), (420, 20), (420, 20),
(780, 20), (780, 350), (780, 350), (650, 350), (650, 350), (650,
540), (650, 540), (780, 540),
(780, 540), (780, 680), (780, 680), (485, 680), (485, 630), (485,
630), (450, 630), (450, 630),
(450, 640)]
walls = [outerWall]
for wall in walls:
for i in range(len(wall)-1):
createWall(outerWall[i], outerWall[i+1])
canvasOne.create_line(350, 630, 450, 630, width = 3, fill = 'gold', tag
= 'gate')
topLeftBox = [(130, 105), (130, 125), (130, 125), (250, 125), (250,
125), (250, 105),
(130, 105), (250, 105)]
secondTopLeftBox = [(160, 215), (160, 225), (160, 215), (230, 215),
(230, 215),
(230, 225), (160, 225), (230, 225)]
topRightBox = [(545, 105), (545, 125), (545, 125), (665, 125), (665, 125), (665, 105),
(545, 105), (665, 105)]
secondTopRightBox = [(560, 215), (560, 225), (560, 215), (625, 215),
(625, 215), (625, 225),
(625, 225), (560, 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]),
createWall(topRightBox[i], topRightBox[i+1]),
createWall(secondTopRightBox[i], secondTopRightBox[i+1])
middleT = [(345, 240), (455, 240), (345, 240), (345, 240), (345, 250), (395, 250), (395, 250), (395, 335),
(395, 335), (405, 335), (405, 335), (405, 250), (405, 250), (455,
250), (455, 250), (455, 240)]
leftSidewaysT = [(275, 340), (275, 500), (275, 340), (285, 340), (285,
340), (285, 415), (285, 415), (345, 415),
(345, 415), (345, 425), (285, 425), (345, 425), (285, 425),
(285, 500), (275, 500), (285, 500)]
rightSidewaysT = [(525, 340), (525, 500), (525, 340), (515, 340), (515,
340), (515, 415), (515, 415), (455, 415),
(455, 415), (455, 425), (515, 425), (455, 425), (515, 425),
(515, 500), (515, 500), (525, 500)]
Ts = [middleT, leftSidewaysT, rightSidewaysT]
for t in Ts:
for i in range(len(t)-1):
createWall(middleT[i], middleT[i+1]),
createWall(leftSidewaysT[i], leftSidewaysT[i+1]),
createWall(rightSidewaysT[i], rightSidewaysT[i+1])
#This is the paths through the maze
#bottom left to middle
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')
#left path above left indent
x = 60
for i in range(14):
x +=20
canvasOne.create_rectangle(x, 290, x+5, 295, fill = 'white')
#left path to top
y = 315
for i in range(13):
y -= 20
canvasOne.create_rectangle(60, y, 65, y-5, fill = 'white')
#top, west to middle indent
x = 60
for i in range(12):
x += 20
canvasOne.create_rectangle(x, 50, x+5, 55, fill = 'white')
#top indent to bottom of middle T
y = 50
for i in range(11):
y += 20
canvasOne.create_rectangle(300, y, 305, y+5, fill = 'white')
#top west to east above middle T and below top indent
x = 60
for i in range(33):
x += 20
canvasOne.create_rectangle(x, 170, x+5, 175, fill = 'white')
#top north to south, right ot top indent
y = 30
for i in range(13):
y += 20
canvasOne.create_rectangle(500, y, 505, y+5, fill = 'white')
#top indent to east
x = 500
for i in range(12):
x += 20
canvasOne.create_rectangle(x, 50, x+5, 55, fill = 'white')
#top right North to south
y = 30
for i in range(13):
y +=20
canvasOne.create_rectangle(740, y, 745, y+5, fill = 'white')
#middle T towards right wall
x = 440
for i in range(14):
x += 20
canvasOne.create_rectangle(x, 290, x+5, 295, fill = 'white')
#left of T North to South
y = 290
for i in range(4):
y +=20
canvasOne.create_rectangle(340, y, 345, y+5, fill = 'white')
#right of T North to South
y = 290
for i in range(4):
y += 20
canvasOne.create_rectangle(460, y, 465, y+5, fill = 'white')
#below T west to east
x = 340
for i in range(5):
x += 20
canvasOne.create_rectangle(x, 370, x+5, 375, fill = 'white')
#below T to gate North to South
y = 370
for i in range(10):
y += 20
canvasOne.create_rectangle(400, y, 405, y+5, fill = 'white')
#from West to East above gate
x = 220
for i in range(18):
x += 20
canvasOne.create_rectangle(x, 570, x+5, 575, fill = 'white')
#right side north to south right T
y = 290
for i in range(16):
y += 20
canvasOne.create_rectangle(580, y, 585, y+5, fill = 'white')
#right side gate to right wall
x = 580
for i in range(8):
x += 20
canvasOne.create_rectangle(x, 610, x+5, 615, fill = 'white')
"""
#process image for use
image = "DustY1.gif"
photo = PhotoImage(file=image)
xp = 100
yp = 600
ph = canvasOne.create_image(xp,yp,image=photo)
ph
#direction will determine if the direction of the move is vertical or horizontal
def direction(event):
if event.x <= xp+10 and event.x >= xp-10: #tolerance for inaccurate click
newY = event.y
onClickVertical(newY, xp, yp)
if event.y <= yp+10 and event.y >= yp-10: #tolerance for inaccurate click
newX = event.x
onClickHorizontal(newX, xp, yp)
#onClick Vertical moves the .gif based on where the mouse is clicked
#newY is new location for yp
def onClickVertical(newY, xp, yp):
print 'here'
for i in range(newY):
yp += .8
canvasOne.coords(ph, xp, yp)
time.sleep(.001)
canvasOne.update()
return xp, yp
#onClick Horizontal moves the .gif based on where the mouse is clicked
#newX is new location for xp
def onClickHorizontal(newX, xp, yp):
for i in range(newX):
xp += .8
canvasOne.coords(ph, xp, yp)
time.sleep(.001)
canvasOne.update()
return xp, yp
canvasOne.bind("<Button-1>", direction)
"""
root = Tk()
root.title("Background")
canvasOne = Canvas(width = 800, height = 700, bg = 'black')
createWall(450, 640, 475, 640)
canvasOne.pack()
root.mainloop()
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor