[Edu-sig] Introducing classes
Andre Roberge
andre.roberge at gmail.com
Sat Mar 4 05:11:17 CET 2006
A long time ago, Kirby suggested to me (on this list I believe - I
can't trace the post right now) that perhaps I should use rur-ple's
code itself as an example to use. I replied that it was most likely
too complicated .... but I kept his idea at the back of my mind.
After seeing his post on the Monkey class, I thought of how I would go
about to introduce classes, to users of rur-ple that have already
written simple programs to make a robot (Reeborg) move in his world,
both using the "function" notation and the OOP notation.
Here are my thoughts at the moment. (Sorry for the amount of code; it
is designed to be all cut-and-pasted in one chunk to be run altogether
as an example. In a classroom situation it would be broken up in
various steps.) The next step would be to introduce the second
dimension (streets).
Any kind of comments would be appreciated, especially as I am thinking
of writing a rur-ple lesson using this approach :-)
André
====================
# Minimal robot that can move.
class Robot(object):
def move(self):
print "I moved."
Reeborg = Robot()
Reeborg.move()
print "----------------"
# Add real movement along a line
class Robot(object):
def __init__(self):
self.avenue = 1
def move(self):
self.avenue += 1
print "I moved to Avenue %d"%self.avenue
Reeborg = Robot()
Reeborg.move()
Reeborg.move()
print "----------------"
# Add movement in both directions along the line
class Robot(object):
def __init__(self):
self.avenue = 1
self.step = 1
def move(self):
self.avenue += self.step
print "I moved to Avenue %d"%self.avenue
def turn_around(self):
self.step = -self.step
print "I turned around."
Reeborg = Robot()
Reeborg.move()
Reeborg.move()
Reeborg.turn_around()
Reeborg.move()
print "----------------"
# Add obstacles (walls)
class Robot(object):
def __init__(self):
self.avenue = 1
self.step = 1
self.walls = [0, 5]
def move(self):
self.avenue += self.step
if self.avenue in self.walls:
print "A wall is preventing me to move to Avenue %d"%self.avenue
self.avenue -= self.step
else:
print "I moved to Avenue %d"%self.avenue
def turn_around(self):
self.step = -self.step
print "I turned around."
Reeborg = Robot()
Reeborg.move()
Reeborg.move()
Reeborg.turn_around()
Reeborg.move()
Reeborg.move()
Reeborg.move()
Reeborg.turn_around()
for i in range(5):
Reeborg.move()
print "----------------"
# Create world and robot separately; put robot in world
class World(object):
def __init__(self):
self.walls = []
def add_wall(self, avenue):
self.walls.append(avenue)
def is_clear(self, avenue):
return avenue in self.walls
class Robot(object):
def __init__(self, world):
self.avenue = 1
self.step = 1
self.world = world
def move(self):
self.avenue += self.step
if self.world.is_clear(self.avenue):
print "Something in the world is preventing me",
print "to move to Avenue %d"%self.avenue
self.avenue -= self.step
else:
print "I moved to Avenue %d"%self.avenue
def turn_around(self):
self.step = -self.step
print "I turned around."
w = World()
w.add_wall(0)
w.add_wall(5)
Reeborg = Robot(w)
Reeborg.move()
Reeborg.move()
Reeborg.move()
Reeborg.turn_around()
Reeborg.move()
Reeborg.move()
Reeborg.move()
Reeborg.move()
w.add_wall(3)
Reeborg.turn_around()
for i in range(3):
Reeborg.move()
print "----------------"
More information about the Edu-sig
mailing list