[Tutor] OOP Advice
JRicker
joejava@dragoncat.net
Wed, 7 Mar 2001 19:53:46 -0500
From: "Daniel Yoo" <dyoo@hkn.eecs.berkeley.edu>
: Hello! Ok, let's do a quick scan through your program. (Some of my
code
: assumes Python 1.52, so there are probably better ways of writing the
code
: if you're using a more recent version of Python.)
I am using 2.0 but if I was to put it online at all my ISP is using
Python 1.5(1 or 2) so I try and code for that when I can.
: On Tue, 6 Mar 2001, JRicker wrote:
: > XPos = -1 #x location of unit, -1 not on game board
: > YPos = -1 #y loaction of unit, -1 not on game board
:
: In Python, you don't need to predeclare member variables. I'm
guessing
: that every Unit should their own separate XPos and YPos. What you
have
: right now defines a class variable XPos, YPos for all Units.
:
: Most likely, you don't need those two lines.
Ok I think I can understand that. What I was thinking is that an unit
that isn't on the board is stored on a ship (ie, A shuttle docked in a
shuttle bay) hence the (-1,-1). I thinking I could do this:
aship.bay[0] = Shuttle(shuttleatrributes).
and later aship.launchshuttle(shuttlenumber)
which does something like
self.bay[0].pop()
The code may not be completely correct, I don't have my python reference
in front of me and I can't yet code without glancing it at it every few
seconds to make sure I'm right but I think you can get the gist of what
I'm saying.
: > def ChangeFacing(self, d):
: > if d == -1:
: > if self.Facing == 1:
: > self.Facing = 6
: > else: self.Facing = self.Facing - 1
:
: There's a slightly nicer way of defining direction, but it involves
using
: angles and trigonometry. *shudder* Seriously though, it will help if
you
: use angles to define your facing direction. If we do so, then our
move
: function becomes a lot more direct and clean:
Shudder is right. My advanced math is kind of weak and so I wouldn't
come up with that on my own. I appreciate the tip. At the risk of
keeping this a little offtopic, I have to ask, does anyone know how do a
range function for a hex based map?
: This assumes that self.Facing now represents the angle (in radians)
where
: the ship's directed at. Turning left or right, then, represents
adding
: and subtracting the quantity pi/2.0 (90 degrees) from the Facing.
Math:
: it's actually useful! *grin*
I'm actually using a hex based map which means each direction is left to
right 60 degrees. Will this still work?
: > My question is this, Is there a way to set up the init function in
unit
: > to handle most of the initialization of the object and the rest done
by
: > the object? How do I structure things so that Unit is doing most of
the
: > work?
:
: Yes, Unit can itself have a constructor that does most of the work.
This
: way, when we make extensions to Unit, we can still initialize the
Unitness
: of an object.
:
:
: If we have something like this, we can take advantage of it in our
: inherited classes:
:
: So we can call our parent's initializer directly, passing it the
: appropriate arguments.
Nice. So basically the easiest way to plan this would be to work out
the possible different kinds of varied types of units and decide what
kinds share all the same thing, which share a few, etc and working it
down to the smallest type of object.
: Hope this helps!
It does. Thanks!
Joel