def load_directory(self, path): resource.path.append(path) print resource.path osPath = '' for _ in resource.path: osPath += _ osPath += os.sep osPath = osPath[:-1] print osPath dirList = os.listdir(osPath) print "Entering directory %s.\n" % path resource.reindex() for fname in dirList: ext = '' print fname if string.rfind(fname, ".") != -1: name = fname[:string.rfind(fname, ".")] ext = fname[string.rfind(fname, "."):] else: name = fname print "name = %s" % name print "ext = %s" % ext if (ext) and (ext in self.filetypes): self.load_file(name, ext, osPath) if not ext: self.load_directory(name) print "Leaving directory %s.\n" % resource.path.pop()
def load_directory(self, path): resource.path.append(path) print resource.path osPath = '' for _ in resource.path: osPath += _ osPath += os.sep osPath = osPath[:-1] print osPath dirList = os.listdir(osPath) print "Entering directory %s.\n" % path resource.reindex() for fname in dirList: ext = '' print fname if string.rfind(fname,".") != -1: name = fname[:string.rfind(fname,".")] ext = fname[string.rfind(fname,"."):] else: name = fname print "name = %s" % name print "ext = %s" % ext if ( ext ) and (ext in self.filetypes): self.load_file(name, ext, osPath) if not ext: self.load_directory(name) print "Leaving directory %s.\n" % resource.path.pop()
def load(self): # loads all the things. TODO: make a gameState that only loads sepcific things resource.path = [self.basePath] resource.reindex() texturePath = 'Graphics/Textures/' for key, fileName in self.textures.items(): self.textures[key] = image.TileableTexture.create_for_image( resource.texture(texturePath + fileName) )
def setup_resources(): from pyglet import font, resource resources_dir = get_resources_dir() # Need to override path as '.' entry causes problems. resource.path = [resources_dir] resource.reindex() font.add_directory(resources_dir)
def index_resources(): """Add global and package resources (images and sounds) into pyglet resource path, then reindex resources.""" rc_paths = get_resource_paths() for rc_path in rc_paths: if rc_path not in resource.path: resource.path.append(rc_path) resource.reindex() info('Reindex resources in these directories:\n{}\n'.format('\n'.join(map(str, rc_paths))))
def index_resources(): """Add global and package resources (images and sounds) into pyglet resource path, then reindex resources.""" rc_paths = get_resource_paths() for rc_path in rc_paths: if rc_path not in resource.path: resource.path.append(rc_path) resource.reindex() info('Reindex resources in these directories:\n{}\n'.format('\n'.join( map(str, rc_paths))))
def load(self): resource.path = ["data"] resource.reindex() numbers = resource.image("numbers.png") self.numbers = {} width = numbers.width / 10 for n in xrange(0, 10): self.numbers[n] = numbers.get_region(n * width, 0, width, numbers.height)
def setup_resources(): from pyglet import font, resource from zombie.constants import RESOURCES_DIR_PATH, FONT_DIR_PATH # Need to override path as '.' entry causes problems. resource.path = [RESOURCES_DIR_PATH] resource.reindex() font.add_directory(RESOURCES_DIR_PATH) font.add_directory(FONT_DIR_PATH)
def addTexture(self, imgPath): dir, file = path.split(imgPath) if dir not in resource.path: resource.path.append(dir) resource.reindex() texture = resource.texture(file) self.textures.append(texture) gl.glBindTexture(texture.target, texture.id) gl.glGenerateMipmap(gl.GL_TEXTURE_2D) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST_MIPMAP_LINEAR)
def test_resource_image_loading(event_loop, transforms, result): """Test loading an image resource with possible transformations.""" resource.path.append('@' + __name__) resource.reindex() img = resource.image('rgbm.png', **transforms) w = event_loop.create_window(width=10, height=10) @w.event def on_draw(): # XXX For some reason original on_draw is not called w.clear() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) img.blit(img.anchor_x, img.anchor_y) event_loop.interrupt_event_loop() # Need to force multiple draws for platforms that do not support immediate drawing event_loop.run_event_loop() w._legacy_invalid = True event_loop.run_event_loop() w._legacy_invalid = True event_loop.run_event_loop() image_data = image.get_buffer_manager().get_color_buffer().get_image_data() pixels = image_data.get_data('RGBA', image_data.width * 4) def sample(x, y): i = y * image_data.pitch + x * len(image_data.format) r, g, b, _ = pixels[i:i + len(image_data.format)] if type(r) is str: r, g, b = list(map(ord, (r, g, b))) return { (255, 0, 0): 'r', (0, 255, 0): 'g', (0, 0, 255): 'b', (255, 0, 255): 'm' }.get((r, g, b), 'x') samples = ''.join([sample(3, 3), sample(3, 0), sample(0, 0), sample(0, 3)]) if samples == samples[2] * 4: # On retina displays the image buffer is twice the size of the coordinate system samples = ''.join( [sample(6, 6), sample(6, 0), sample(0, 0), sample(0, 6)]) assert samples == result
def __init__(self, first, *args): """ :param first: The first scene to begin on. """ try: super(Game, self).__init__(*args, config = gl.Config(sample_buffers=1, samples=4)) except window.NoSuchConfigException: print("Anti-aliasing not supported. It won't look quite so good, but who cares, the graphics suck anyway.") super(Game, self).__init__(*args) resource.path.append("assets") resource.reindex() self.scene = first clock.schedule_interval(self.update, 1/60)
def init(args=None): from .config import update_config, ImagePath, Config # TODO: Update config with command line args. update_config({}) resource.path.append(ImagePath) resource.reindex() director.director.init( caption='Adventurer World', width=Config['ScreenWidth'], height=Config['ScreenHeight'], )
def test_resource_image_loading(event_loop, transforms, result): """Test loading an image resource with possible transformations.""" resource.path.append('@' + __name__) resource.reindex() img = resource.image('rgbm.png', **transforms) window = event_loop.create_window() # Create a Framebuffer to render into: framebuffer = pyglet.image.buffer.Framebuffer() texture = pyglet.image.Texture.create(width=10, height=10, min_filter=GL_NEAREST, mag_filter=GL_NEAREST) framebuffer.attach_texture(texture) # Draw into the Framebuffer: framebuffer.bind() img.blit(img.anchor_x, img.anchor_y) framebuffer.unbind() # Check the pixels that were drawn: image_data = texture.get_image_data() pixels = image_data.get_data('RGBA', image_data.width * 4) def sample(x, y): i = y * image_data.pitch + x * len(image_data.format) r, g, b, _ = pixels[i:i + len(image_data.format)] if type(r) is str: r, g, b = list(map(ord, (r, g, b))) return { (255, 0, 0): 'r', (0, 255, 0): 'g', (0, 0, 255): 'b', (255, 0, 255): 'm' }.get((r, g, b), 'x') samples = ''.join([sample(3, 3), sample(3, 0), sample(0, 0), sample(0, 3)]) if samples == samples[2] * 4: # On retina displays the image buffer is twice the size of the coordinate system samples = ''.join( [sample(6, 6), sample(6, 0), sample(0, 0), sample(0, 6)]) assert samples == result
def main(): resource.path.append('data') resource.reindex() font.add_directory('data/font') log.init() audio.init() client.init() server.init() options.init() director.init() clt = client.GameClient() clt.start() clt.stop()
def main(): path = os.path.join(os.path.dirname(__file__), '../Map/assets/img') resource.path.append(path) resource.reindex() director.director.init(width=640, height=480) layer_ = layer.Layer() layer_.add(MultipleSprite( Sprite('grossini.png', position=(50, 40)), Sprite('sky.gif', position=(-60, -30)), position=(320, 240), )) director.director.run(scene.Scene(layer_))
def main(): path = os.path.join(os.path.dirname(__file__), '../Map/assets/img') resource.path.append(path) resource.reindex() director.director.init(width=640, height=480) layer_ = layer.Layer() layer_.add( MultipleSprite( Sprite('grossini.png', position=(50, 40)), Sprite('sky.gif', position=(-60, -30)), position=(320, 240), )) director.director.run(scene.Scene(layer_))
class Sound: resource.path.append("../res/sound") resource.reindex() try: snd = resource.media("main_theme.mp3", streaming=False) except Exception as e: music = DummyPlayer() print(e) else: music = media.Player() music.queue(snd) music.loop = True sounds_name = [ "big_jump", "brick_smash", "bump", "coin", "death", "flagpole", "game_over", "out_of_time", "powerup", "powerup_appears", "small_jump", "stage_clear", "stomp" ] sounds = defaultdict(DummyPlayer) for sound in sounds_name: try: sounds[sound] = resource.media(sound + ".mp3", streaming=False) except Exception as e: print(e) @classmethod def play(cls, name): if name == "mario": cls.music.play() else: cls.sounds[name].play() @classmethod def stop(cls, name): if name == "mario": cls.music.pause()
from math import pi, sin, cos, sqrt from euclid import * import pyglet from pyglet.gl import * from pyglet.window import key from pyglet import image, resource from shader import Shader import CstmUtils from pymesh import * resource.path.append('textures') resource.reindex() texturecnt = 1 # this definition has been moved into the shader file try: # Try and create a window with multisampling (antialiasing) config = Config( sample_buffers=1, samples=4, depth_size=16, double_buffer=True, ) window = pyglet.window.Window( resizable=True, config=config, vsync=False) # "vsync=False" to check the framerate except pyglet.window.NoSuchConfigException: # Fall back to no multisampling for old hardware window = pyglet.window.Window(resizable=True)
from math import sin, radians import pyglet import random import scene from pyglet.gl import * from pyglet import resource window = pyglet.window.Window(1024, 768) resource.path.append('../fonts/') resource.path.append('../res/') resource.reindex() resource.add_font('LOKISD__.TTF') class Title(scene.Scene): def __init__(self): offset = 50 self.tick = 0 self.letters = [] self.batch = pyglet.graphics.Batch() colors = [ (252, 182, 83, 255), (255, 82, 84, 255), (206, 232, 121, 255) ]
class Image: resource.path.append("../res/image") resource.reindex() try: background = resource.image("background.png") menu = resource.image("menu.png") mario = resource.image("mario.png") sprite_set = resource.image("sprite_set.png") except resource.ResourceNotFoundException: raise SystemExit("cannot find images!") sprite_set_small = ImageGrid(sprite_set, 20, 20) sprite_set_big = ImageGrid(sprite_set, 10, 20) # mario mario_walk_right_small = [sprite_set_small[(18, 0)], sprite_set_small[(18, 1)], sprite_set_small[(18, 2)]] mario_walk_left_small = [image.get_transform(flip_x=True) for image in mario_walk_right_small] mario_walk_right_big = [sprite_set_big[(8, 0)], sprite_set_big[(8, 1)], sprite_set_big[(8, 2)]] mario_walk_left_big = [image.get_transform(flip_x=True) for image in mario_walk_right_big] mario_walk_right_fire = [sprite_set_big[(7, 0)], sprite_set_big[(7, 1)], sprite_set_big[(7, 2)]] mario_walk_left_fire = [image.get_transform(flip_x=True) for image in mario_walk_right_fire] mario_walk = [[mario_walk_right_small, mario_walk_left_small], [mario_walk_right_big, mario_walk_left_big], [mario_walk_right_fire, mario_walk_left_fire]] mario_jump_right_small = sprite_set_small[(18, 4)] mario_jump_left_small = mario_jump_right_small.get_transform(flip_x=True) mario_jump_right_big = sprite_set_big[(8, 4)] mario_jump_left_big = mario_jump_right_big.get_transform(flip_x=True) mario_jump_right_fire = sprite_set_big[(7, 4)] mario_jump_left_fire = mario_jump_right_fire.get_transform(flip_x=True) mario_jump = [[mario_jump_right_small, mario_jump_left_small], [mario_jump_right_big, mario_jump_left_big], [mario_jump_right_fire, mario_jump_left_fire]] mario_stand_right_small = sprite_set_small[(18, 6)] mario_stand_left_small = mario_stand_right_small.get_transform(flip_x=True) mario_stand_right_big = sprite_set_big[(8, 6)] mario_stand_left_big = mario_stand_right_big.get_transform(flip_x=True) mario_stand_right_fire = sprite_set_big[(7, 6)] mario_stand_left_fire = mario_stand_right_fire.get_transform(flip_x=True) mario_stand = [[mario_stand_right_small, mario_stand_left_small], [mario_stand_right_big, mario_stand_left_big], [mario_stand_right_fire, mario_stand_left_fire]] mario_die = sprite_set_small[(18, 5)] mario_walk_to_castle = [Animation.from_image_sequence(mario_walk_right_small, 0.1), Animation.from_image_sequence(mario_walk_right_big, 0.1), Animation.from_image_sequence(mario_walk_right_fire, 0.1)] mario_lower_flag_small = [sprite_set_small[(18, 7)], sprite_set_small[(18, 8)]] mario_lower_flag_big = [sprite_set_big[(8, 7)], sprite_set_big[(8, 8)]] mario_lower_flag_fire = [sprite_set_big[(7, 7)], sprite_set_big[(7, 8)]] mario_lower_flag = [Animation.from_image_sequence(mario_lower_flag_small, 0.2), Animation.from_image_sequence(mario_lower_flag_big, 0.2), Animation.from_image_sequence(mario_lower_flag_fire, 0.2)] mario_lower_flag_turn_around = [sprite_set_small[(18, 8)].get_transform(flip_x=True), sprite_set_big[(8, 8)].get_transform(flip_x=True), sprite_set_big[(7, 8)].get_transform(flip_x=True)] # prop normal_mushroom = sprite_set_small[(7, 0)] life_mushroom = sprite_set_small[(7, 1)] frames = [sprite_set_small[(5, 0)], sprite_set_small[(5, 1)]] fire_flower_blink = Animation.from_image_sequence(frames, 0.3) # enemy frames = [sprite_set_small[(10, 7)], sprite_set_small[(10, 8)]] goomba_move = Animation.from_image_sequence(frames, 0.3) goomba_die = sprite_set_small[(10, 9)] frames = [sprite_set_big[(5, 0)], sprite_set_big[(5, 1)]] koopa_move = Animation.from_image_sequence(frames, 0.3) koopa_die = sprite_set_small[(10, 4)] # others # cliff is a total transparent image cliff = sprite_set_small[(2, 2)] coin = sprite_set_big[(3, 4)] flag = sprite_set_small[(0, 0)] castle_flag = sprite_set_small[(2, 0)] normal_brick = sprite_set_small[(0, 3)] unknown_brick = sprite_set_small[(0, 6)] try: normal_brick2 = load("../res/image/brick.png") except Exception as e: raise SystemExit(e)
def configure(): resource.path.append(u"data") resource.reindex()
def setUp(self): self.w = window.Window(width=10, height=10) self.w.dispatch_events() resource.path.append('@' + __name__) resource.reindex()
def __init__(self): config = gl.Config(double_buffer=True) self.window = pyglet.window.Window( height=TwisterTempoGUI.WINDOW_HEIGHT, width=TwisterTempoGUI.WINDOW_WIDTH, caption='TwisterTempo', config=config) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) resource.path = ['resources'] resource.reindex() self._pause_label = text.Label('pause', font_name='Courier', font_size=36, x=TwisterTempoGUI.WINDOW_WIDTH // 2, y=TwisterTempoGUI.WINDOW_HEIGHT // 10, anchor_x='center', anchor_y='center', color=(255, 255, 255, 255)) self._background = resource.image('background.png') self._cloud_image = resource.image('cloud_white.png') sprite_sheet = pyglet.resource.image('tt_animation.png') image_grid = pyglet.image.ImageGrid(sprite_sheet, rows=4, columns=4) self._green_circle_animation = pyglet.image.Animation.from_image_sequence( [ image_grid[0, 0], image_grid[0, 1], image_grid[0, 2], image_grid[0, 3], image_grid[0, 2] ], duration=0.04, loop=False) self._yellow_circle_animation = pyglet.image.Animation.from_image_sequence( [ image_grid[1, 0], image_grid[1, 1], image_grid[1, 2], image_grid[1, 3], image_grid[1, 2] ], duration=0.04, loop=False) self._blue_circle_animation = pyglet.image.Animation.from_image_sequence( [ image_grid[2, 0], image_grid[2, 1], image_grid[2, 2], image_grid[2, 3], image_grid[2, 2] ], duration=0.04, loop=False) self._red_circle_animation = pyglet.image.Animation.from_image_sequence( [ image_grid[3, 0], image_grid[3, 1], image_grid[3, 2], image_grid[3, 3], image_grid[3, 2] ], duration=0.04, loop=False) self._right_circle_sprite = None self._left_circle_sprite = None letters_sheet = pyglet.resource.image('letters.png') image_grid = pyglet.image.ImageGrid(letters_sheet, rows=1, columns=9) self._L_animation = pyglet.image.Animation.from_image_sequence( [ image_grid[0], image_grid[1], image_grid[2], image_grid[1], image_grid[2] ], duration=0.04, loop=False) self._plus_animation = pyglet.image.Animation.from_image_sequence( [ image_grid[3], image_grid[4], image_grid[5], image_grid[4], image_grid[5] ], duration=0.04, loop=False) self._R_animation = pyglet.image.Animation.from_image_sequence( [ image_grid[6], image_grid[7], image_grid[8], image_grid[7], image_grid[8] ], duration=0.04, loop=False) self._L_sprite = sprite.Sprite(self._L_animation) self._plus_sprite = sprite.Sprite(self._plus_animation) self._R_sprite = sprite.Sprite(self._R_animation) sprite_large_sheet = pyglet.resource.image('tt_animation_large.png') image_large_grid = pyglet.image.ImageGrid(sprite_large_sheet, rows=4, columns=4) self._green_large_circle_animation = pyglet.image.Animation.from_image_sequence( [ image_large_grid[0, 0], image_large_grid[0, 1], image_large_grid[0, 2], image_large_grid[0, 3], image_large_grid[0, 2] ], duration=0.04, loop=False) self._yellow_large_circle_animation = pyglet.image.Animation.from_image_sequence( [ image_large_grid[1, 0], image_large_grid[1, 1], image_large_grid[1, 2], image_large_grid[1, 3], image_large_grid[1, 2] ], duration=0.04, loop=False) self._blue_large_circle_animation = pyglet.image.Animation.from_image_sequence( [ image_large_grid[2, 0], image_large_grid[2, 1], image_large_grid[2, 2], image_large_grid[2, 3], image_large_grid[2, 2] ], duration=0.04, loop=False) self._red_large_circle_animation = pyglet.image.Animation.from_image_sequence( [ image_large_grid[3, 0], image_large_grid[3, 1], image_large_grid[3, 2], image_large_grid[3, 3], image_large_grid[3, 2] ], duration=0.04, loop=False) self._large_circle_sprite = None self.show_pause = False self._draw_on_air = (False, 0, 0) self._animate = False self._show_circles = True self._show_large_circle = False self._fps_display = None
import pyglet pyglet.options['audio'] = ('directsound', 'silent') import pyglet.resource as res import pyglet.media as md res.path = ["res", "res/sound"] res.reindex() brick = res.image("bricks3.png") # brick = res.image("brick-wall.png") tree = res.image("beech.png") pinetree = res.image("pine-tree2.png") box = res.image("wooden-crate3.png") player = res.image("gecko4.png") player_down = res.image("gecko4.png", rotate=180) player_left = res.image("gecko4.png", rotate=270) player_right = res.image("gecko4.png", rotate=90) target = res.image("boxtarget_2.png") backmusic = res.media("backmusic.mp3", streaming=False)
def load_resources(): resource.path.append(RESOURCES_PATH) resource.reindex()
def main(): resource.path.insert(0, 'data') resource.reindex() window = pyglet.window.Window(width=540, height=480, caption="Belle of Nine Fables") game = Game(window) pyglet.app.run()
from random import randint # import MIDI library import rtmidi_python as rtmidi # import Pyglet library from pyglet import font, resource as rs from pyglet.window import key from pyglet.media import Player, SourceGroup from pyglet.text import * from pyglet.graphics import * from pyglet.sprite import * # Initialisation rs.path.append('data') rs.reindex() font.add_directory('data') # Ressources background = rs.image('background.jpg') paddle = rs.image('paddle.png') paddleSimple = rs.image('paddleSimple.png') ball = rs.image('ball.png') icon = pyglet.image.load('icon.png') music = pyglet.resource.media('music_background.mp3', False) # Class Player class Player(Sprite): "Sprite et donnée du joueur"