How would you handle interpolation in a python game?

I am currently writing a very simple game using python and pygame. It has things that move. And to make this material smooth, I organized the main game cycle, as said in Fix Your Timestep , with interpolation.

This is how I handle interpolation now.

class Interpolator(object):
    """Handles interpolation"""
    def __init__(self):
        self.ship_prev = None
        self.ship = None

        self.stars_prev = []
        self.stars = []

        self.bullets_prev = {}
        self.bullets = {}

        self.alpha = 0.5

    def add_ship(self, ship):
        self.ship_prev = self.ship
        self.ship = ship

    def add_stars(self, stars):
        self.stars_prev = self.stars
        self.stars = stars[:]

    def add_bullets(self, bullets):
        self.bullets_prev = self.bullets
        self.bullets = bullets.copy()

    def add_enemies(self, enemies):
        self.enemies_prev = self.enemies
        self.enemies = enemies  # to be continued

    def lerp_ship(self):
        if self.ship_prev is None:
            return self.ship
        return lerp_xy(self.ship_prev, self.ship, self.alpha)

    def lerp_stars(self):
        if len(self.stars_prev) == 0:
            return self.stars
        return (lerp_xy(s1, s2, self.alpha) for s1, s2 in izip(self.stars_prev, self.stars))

    def lerp_bullets(self):
        keys = list(set(self.bullets_prev.keys() + self.bullets.keys()))
        for k in keys:
            # interpolate as usual
            if k in self.bullets_prev and k in self.bullets:
                yield lerp_xy(self.bullets_prev[k], self.bullets[k], self.alpha)
            # bullet is dead
            elif k in self.bullets_prev:
                pass
            # bullet just added
            elif k in self.bullets:
                yield self.bullets[k]

Lerp_xy () function and data types

def lerp_xy(o1, o2, alpha, threshold=100):
    """Expects namedtuples with x and y parameters."""
    if sqrt((o1.x - o2.x) ** 2 + (o1.y - o2.y) ** 2) > 100:
        return o2
    return o1._replace(x=lerp(o1.x, o2.x, alpha), y=lerp(o1.y, o2.y, alpha))

Ship = namedtuple('Ship', 'x, y')
Star = namedtuple('Star', 'x, y, r')
Bullet = namedtuple('Bullet', 'x, y')

Data types are, of course, temporary, but I still expect them to have the x and y attributes in the future. update: lerper.alpha updates every frame.

- . . dict {id, Bullet}, , , , , , , - , ​​ .

. , , , , poo.

Python, , .

, OO- Python, - , 10- script.

, - . , , - , .

, , - , Python , (, , ) , Python.

, , :

lerper = Interpolator()
# game loop
while(1):
    # physics
    # physics done
    lerper.add(ship)
    lerper.add(stars)
    lerper.add(bullets)
    lerper.add(enemies) # you got the idea

    # rendering
    draw_ship(lerper.lerp('Ship'))
    # or
    draw_ship(lerper.lerp_ship())

, =)

. /? ? /dict lerper.add([ship])? , dict/whatever? , ? ?

+5
3

:

class Thing(object):
    """Generic game object with interpolation"""
    def __init__(self, x=0, y=0):
        self._x = self.x = x
        self._y = self.y = y

    def imprint(self):
        """call before changing x and y"""
        self._x = self.x
        self._y = self.y

    def __iter__(self):
        """handy to unpack like a tuple"""
        yield self.x
        yield self.y

Ship = Thing
Bullet = Thing


class Star(Thing):
    """docstring for Star"""
    def __init__(self, x, y, r):
        super(Star, self).__init__(x, y)
        self.r = r

    def __iter__(self):
        yield self.x
        yield self.y
        yield self.r


def lerp_things(things, alpha, threshold=100):
    """Expects iterables of Things"""
    for t in things:
        if sqrt((t._x - t.x) ** 2 + (t._y - t.y) ** 2) > threshold:
            yield (t.x, t.y)
        else:
            yield (lerp(t._x, t.x, alpha), lerp(t._y, t.y, alpha))
+1

, , , , , , . , Python 3.x, .

, , , . Processing.org.

+2

Breakout, ", ", , , .

, , Projectile. . , - tick.

class Projectile:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.vel_x = 0
        self.vel_y = 0
    def tick(self, dt):
        self.x += dt * self.vel_x
        self.y += dt * self.vel_y

x y - , .

, , , tick .

class Layer:
    def __init__(self):
        self.projectiles = []
    def addProjectile(self, p):
        self.projectiles.add(p)
    def tick(self, dt):
        for p in self.projectiles:
            p.tick(dt)
        #possible future expansion: put collision detection here

- , , .

#setup
l = Layer()

ship = Projectile()
#not shown: set initial position and velocity of ship
l.addProjectile(ship)

for i in range(numStars):
    star = Projectile()
    #not shown: set initial position and velocity of stars
    l.addProjectile(star)

#not shown: add bullets and enemies to l, the same way stars were

#game loop
while True:
    #get dt somehow
    dt = .42
    l.tick(dt)
    for projectile in l.projectiles:
        draw(l)

- , - - , . , , . , , , .., Projectile. . - . , ..

+1
source

All Articles