You could define your own "bounded" numeric type. For example if paddle1_pos[1]
was an integer value you could create a class like the following and use it instead
class BoundedInt(int):
def __new__(cls, *args, **kwargs):
lower, upper = bounds = kwargs.pop('bounds')
val = int.__new__(cls, *args, **kwargs) # supports all int() args
val = lower if val < lower else upper if val > upper else val
val = super(BoundedInt, cls).__new__(cls, val)
val._bounds = bounds
return val
def __add__(self, other):
return BoundedInt(int(self)+other, bounds=self._bounds)
__iadd__ = __add__
def __sub__(self, other):
return BoundedInt(int(self)-other, bounds=self._bounds)
__isub__ = __sub__
def __mul__(self, other):
return BoundedInt(int(self)*other, bounds=self._bounds)
__imul__ = __mul__
# etc, etc...
if __name__ == '__main__':
v = BoundedInt(100, bounds=(0, 100))
print type(v), v
v += 10
print type(v), v
w = v + 10
print type(w), w
x = v - 110
print type(x), x
Output:
<class '__main__.BoundedInt'> 100
<class '__main__.BoundedInt'> 100
<class '__main__.BoundedInt'> 100
<class '__main__.BoundedInt'> 0