[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] When to use a constant
Jon Doda wrote:
Using a class is an interesting idea, which I may look into further.
Right now, however, my code involves a number of for-loops and list
comprehensions which can act on any set of components (which is the
only reason I'm using a list) and I'm not sure how I could replace
them without introducing a lot of duplicate code.
As an example, I have a lot of constructs similar to this:
def apply_force(self, force, interval):
"""Return the change in velocity caused by a force.
"""
return [(force[n] / self.mass) * interval for n in range(2)]
And while there's plenty of ways to do the same thing, I can't think
of any that are nearly as simple and clean. Maybe I just like list
comprehensions too much :).
I've used complex numbers before. For the above, you have:
return force / self.mass * interval
...which is very clean. You can add two complex numbers, multiply a
complex by a scalar, and take the abs() of a complex, and get the
results you expect for a vector (abs corresponds to the magnitude).
Using .real for X and .imag for Y isn't all that pretty, getattr and
setattr can save you from that.