Ejemplo n.º 1
0
from math import hypot

import pygame
import rabbyt

rabbyt.init_display((640, 480))
rabbyt.set_viewport((640, 480), projection=(0, 0, 640, 480))

control_points = [
    rabbyt.Sprite(xy=xy)
    for xy in [(100, 100), (200, 50), (300, 150), (400, 100)]
]
grabbed_point = None

path_followers = []


def generate_followers():
    global path_followers
    p = [c.xy for c in control_points]
    path_followers = [
        rabbyt.Sprite(xy=rabbyt.bezier3(
            p[0], p[1], p[2], p[3], i * 200, i * 200 + 2000, extend="repeat"))
        for i in range(10)
    ]


generate_followers()

print "Click and drag the control points."
Ejemplo n.º 2
0
import rabbyt
import pygame

pygame.init()
pygame.display.set_mode((640, 480), pygame.OPENGL | pygame.DOUBLEBUF)
rabbyt.set_viewport((640, 480))
rabbyt.set_default_attribs()

sprites = [rabbyt.Sprite(x=x) for x in range(-100, 100, 50)]

# Constant is the default extend mode.  It will not go beyond start or end.
sprites[0].y = rabbyt.lerp(-100, 100, 1, 3, extend="constant")

# With extrapolate, it just keeps going.
sprites[1].y = rabbyt.lerp(-100, 100, 1, 3, extend="extrapolate")

# With repeat, it starts at start again after reaching end.
sprites[2].y = rabbyt.lerp(-100, 100, 1, 3, extend="repeat")

# Reverse is like repeat, only every other time it moves from end to start.
sprites[3].y = rabbyt.lerp(-100, 100, 1, 3, extend="reverse")

while not pygame.event.get(pygame.QUIT):
    rabbyt.clear()
    rabbyt.set_time(pygame.time.get_ticks() / 1000.0)

    rabbyt.render_unsorted(sprites)

    pygame.display.flip()
Ejemplo n.º 3
0
 def add_sprite(self, img, offset):
     s = rabbyt.Sprite(img,
                       xy=(self.pos.x + offset[0], self.pos.y + offset[1]))
     s.offset_x, s.offset_y = offset
     self.sprites.append(s)
Ejemplo n.º 4
0
    def run(self):
        # Attempt to set up the display with 0x AA.
        display = pyglet.window.get_platform().get_default_display()
        screen = display.get_default_screen()
        glconfig = pyglet.gl.Config(
            double_buffer=True,
            sample_buffers=True,
            samples=0,
        )
        try:
            config = screen.get_best_config(glconfig)
        except window.NoSuchConfigException:
            # Failed AA config - use a config without AA.
            config = screen.get_best_config(
                pyglet.gl.Config(double_buffer=True))
        self.window = pyglet.window.Window(config=config,
                                           resizable=True,
                                           vsync=True)

        # Load terrain tiles.
        terrain_grid_image = pyglet.image.ImageGrid(
            pyglet.image.load("data/tiles.png"), 1, 1, MAP_TILE_SIZE + 1,
            MAP_TILE_SIZE + 1, 1, 1)
        terrain_grid = pyglet.image.TextureGrid(terrain_grid_image)
        terrain_dirt = rabbyt.Sprite(texture=terrain_grid[0], u=0, v=0)
        self.terrain = [[terrain_dirt] * MAP_WIDTH] * MAP_HEIGHT

        # Load the player and put it in the middle of the map.
        self.player = Player()
        self.player.x = MAP_WIDTH * MAP_TILE_SIZE / 2
        self.player.y = MAP_HEIGHT * MAP_TILE_SIZE / 2

        # Load the 3d rabbit model (mesh object)
        self.rabbit = obj.OBJ("data/rabbit.obj")

        # Set up lighting.
        self.fourfv = ctypes.c_float * 4
        pyglet.gl.glLightfv(pyglet.gl.GL_LIGHT0, pyglet.gl.GL_AMBIENT,
                            self.fourfv(.5, .5, .5, 1.0))
        pyglet.gl.glLightfv(pyglet.gl.GL_LIGHT0, pyglet.gl.GL_DIFFUSE,
                            self.fourfv(0.8, 0.8, 0.8, 1.0))
        pyglet.gl.glLightfv(pyglet.gl.GL_LIGHT0, pyglet.gl.GL_POSITION,
                            self.fourfv(0, 0, 0, 1))
        pyglet.gl.glEnable(pyglet.gl.GL_LIGHT0)

        # Load the FPS display.
        self.fps_display = pyglet.clock.ClockDisplay(color=(1, 1, 1, 0.5))

        # Init rabbyt
        rabbyt.set_default_attribs()

        # Set up the clock and keep track of the time.
        self.clock = pyglet.clock.get_default()

        # self.clock.set_fps_limit(30)
        def track_time(dt):
            self.dt = dt
            self.time += dt

        self.clock.schedule(track_time)

        self.time = 0
        self.dt = 0

        # Get a keyboard handler to store the state of all keys.
        self.keyboard = pyglet.window.key.KeyStateHandler()

        # Keep track of mouse button states.
        self.mouse = 0

        # Keep track of mouse x, y coordinates.
        self.mousex = self.mousey = None

        # Push applicable event handlers to the window.
        self.window.push_handlers(self.on_draw, self.keyboard,
                                  self.on_mouse_press, self.on_mouse_release,
                                  self.on_mouse_motion, self.on_mouse_drag)

        # Go!
        pyglet.app.run()
Ejemplo n.º 5
0
 def __init__(self, image, x, y):
     self.sp = rabbyt.Sprite(image)
     self.sp.x = x
     self.sp.y = y
Ejemplo n.º 6
0
import rabbyt
from pyglet.window import Window
from pyglet import clock
from pyglet import image
import os.path
rabbyt.data_directory = os.path.dirname(__file__)

# This will cause pyglet to call rabbyt.add_time(dt) when we call clock.tick()
# But note that all times will be in *seconds*, not milliseconds like when we
# use pygame.time.get_ticks().
clock.schedule(rabbyt.add_time)

window = Window(width=640, height=480)
rabbyt.set_default_attribs()

car = rabbyt.Sprite("car.png")

# Rabbyt automatically detected that we are using pyglet, and used pyglet
# to load the texture.
assert isinstance(car.texture, image.Texture)

car.xy = (320, 240)

# Fade the car in after one second.
car.alpha = rabbyt.lerp(0.0, 1.0, startt=1, endt=2)

# Rotate the car from 0 to 360 over three seconds, then repeat.
car.rot = rabbyt.lerp(0, 360, dt=3, extend="repeat")

while not window.has_exit:
    clock.tick()
Ejemplo n.º 7
0
from rabbyt.fonts import Font, FontSprite
import pygame
import os.path
rabbyt.data_directory = os.path.dirname(__file__)

pygame.init()
pygame.display.set_mode((800, 480), pygame.OPENGL | pygame.DOUBLEBUF)
rabbyt.set_viewport((800, 480))
rabbyt.set_default_attribs()

font = Font(pygame.font.Font(pygame.font.get_default_font(), 20))

sprites = []

sprites.append(FontSprite(font, "lerp", x=-275, y=480/2-10))
sprites.append(rabbyt.Sprite(x=-275, y=480/2-50))
sprites.append(rabbyt.Sprite(x=-125, y=480/2-50))
s = rabbyt.Sprite()
s.x = rabbyt.lerp(-275, -125, dt=3, extend="repeat")
s.y = 480/2-50
sprites.append(s)


sprites.append(FontSprite(font, "ease", x=-275, y=480/2-70))
sprites.append(FontSprite(font, "ease_out", x=-20, y=480/2-70))
sprites.append(FontSprite(font, "ease_in", x=235, y=480/2-70))

methods = ["sine", "quad", "cubic", "circ", "back", "bounce"]
ypos = 480/2-100
for method in methods:
    sprites.append(FontSprite(font, method, x=-390, y=ypos))