コード例 #1
0
 def ifelse_fn(g: Plotter):
     car = g.entity(
         components=[
             Position,
             Speed,
             Velocity,
         ]
     )
     env = g.entity(components=[Clock])
     if env[Clock].tick_ms > 2000:
         car[Position].x += g.min(car[Speed].max_x, 2 * car[Velocity].x)
         car[Position].y = car[Position].x + 2
     elif env[Clock].tick_ms > 5000:
         car[Position].x += g.min(car[Speed].max_x, 5 * car[Velocity].x)
         car[Position].y = car[Position].x + 10
     else:
         car[Position].x = g.min(car[Speed].max_x, 1 * car[Velocity].x)
         car[Position].y = car[Position].x - 5
コード例 #2
0
    def control_sys(g: Plotter):
        controls = g.entity(components=[Keyboard])
        car = g.entity(components=[Movement, Velocity, Position, Meta])
        acceleration, max_speed, steer_rate = 5.0, 18.0, 10.0

        # steer car
        if controls[Keyboard].left:
            car[Movement].rotation -= steer_rate
            controls[Keyboard].left = False
        elif controls[Keyboard].right:
            car[Movement].rotation += steer_rate
            controls[Keyboard].right = False

        # accelerate/slow down car
        if controls[Keyboard].up:
            car[Movement].speed = g.min(car[Movement].speed + acceleration,
                                        max_speed)
            controls[Keyboard].up = False
        elif controls[Keyboard].down:
            car[Movement].speed = g.max(car[Movement].speed - acceleration,
                                        0.0)
            controls[Keyboard].down = False