[Tutor] Livewires - stuck on a class
alan.gauld at btinternet.com
Tue Aug 14 22:43:51 CEST 2007
"Tonu Mikk" <tmikk at umn.edu> wrote
> I create more robots in this way which seems to work:
> class Robot:
By using an empty class you are losing m,uch of the power of classes.
def __init__(self, x, y, shape=None):
self.x = x
self.y = y
robot.junk = 0
if shape == None:
self.shape = box(10*x, 10*y, 10*x+10, 10*y+10)
return = [Robot(random_between(0,47)-0.5,
random_between(0,63)-0.5) for x in range(numRobots)]
And instead of your random fiunction you could use the standard
> Then I was hoping to repeat the sequence for moving the robots
> placed in
> the robots list by using this code:
> for x in robots:
> # code for moving the robots
> When I run the code, only one of the robots moves on the screen. I
> not quite sure what I am doing wrong.
Without seeing the robot moving code neither are we.
But as a hiunt try putting the code for moving a robot into the Robot
Then you should be abe to do
for robot in robots:
> Incidentally, I am finding the Livewires course to be quite
> for a beginning programmer. It seems to introduce many advanced
> with little preparation. I am using other Internet based tutorials
> the side, but still having trouble. I wonder if it is me being
> hmm -
> dumb, or is the Livewires just tricky.
>From what I've seen of posts about Livewires I think its quite a
challenging course for a complete beginner. But OTOH it seems
to be the one with the most fun problems! :-)
BTW I just spotted this.... If this is the move code you
were talking about then...
> def move_robot():
> for x in robots:
> while 1:
> if robot.x + 0.5< player.x and robot.y +0.5< player.y:
You are doing *for x in robots* but then moving *robot* not x.
>From your robot placement code robot is set to the last robot
you created so it will only ever mover that robot.
To make this a method of the class you wuill need to pass
the player object that you are comparing with.
So the method will look like:
def __init__(...): as above
def move(self, player):
code as per the function but using self.x instead of robot.x
Also it would be better IMHO to use if/elif rather than all those
Author of the Learn to Program web site
More information about the Tutor