[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] When to use a constant



Hey,
I had a simular problem with my pygame project - and the solution I ended up going with was using a Vector class I wrote myself (in C).
It seemed to be the only way I could use both array index's and letters (.x, .y) to access vectors, and still nicely encapsulate common operations such as vector multiplication and normalization.

I'd be happy to share the code (.c file + .py unit test) to anyone whos interested.

- Jacob

I'm working on a simple 2D physical simulation. The various simulation objects have position and velocity attributes that store the x and y components in a list. So, to get the y component of velocity for some object you do something like "sim_object.velocity[1]".

Recently I've been tempted to define constants for the index values of the lists, so I'd have X = 0, and Y = 1, and the code from above would be "sim_object.velocity[Y]". It would make reading the simulation code somewhat easier (no more converting 0 to x and y to 1 in my head) but it would add a level of indirection, which could be confusing.

So, the question is, would using constants in this way be a reasonable thing to do, or is it bad practice?