def __init__(self, client, actr): self.screen = director.get_window_size() self.ncells = int(director.settings['board_size']) self.scorepad = int(self.screen[1] * .025) self.cell = int((self.screen[1]-self.scorepad) / self.ncells * .1) * 10 print self.cell width = self.ncells * self.cell + self.ncells + 1 super(Task, self).__init__(0, 0, 0, 255, width, width) self.position = ((self.screen[0]-width)/2, (self.screen[1]-width)/2-self.scorepad) self.score_layer = Score(width, 2*self.scorepad, self.position[0], self.position[1]+width+self.scorepad/2) self.score = 0 self.food_eaten = 0 self.speed_factor = float(director.settings['speed_factor']) self.state = self.STATE_INIT self.snake = None self.food = None self.ready = False self.blop = StaticSource(pyglet.resource.media('blop.mp3')) self.laugh = StaticSource(pyglet.resource.media('laugh.mp3')) self.text_batch = BatchNode() self.game_over_label = text.Label("GAME OVER", font_size=int(self.cell*3), x= width / 2, y= width / 2, font_name="Pipe Dream", color=(255,255,255,255), anchor_x='center', anchor_y='bottom', batch=self.text_batch.batch) self.game_over_label = text.Label("Press Spacebar For New Game", font_size=int(self.cell*1.5), x= width / 2, y= width / 2, font_name="Pipe Dream", color=(255,255,255,255), anchor_x='center', anchor_y='top', batch=self.text_batch.batch)
def __init__(self, **kwargs): super().__init__(**kwargs) self.wnd.mouse_exclusivity = False self.bpm = 242 # Hardcode bpm from info.dat self.camera = KeyboardCamera(self.wnd.keys, fov=60, near=1.0, far=1000.0) self.camera.velocity = 50 self.camera_enabled = False meta = self.load_json('megalovania_remix/info.dat') self.map = BSScene( self.load_scene('bs_map3.glb'), self.camera, BSTrack('megalovania_remix/Expert.dat', meta['_beatsPerMinute']), ) self.quad_fs = geometry.quad_fs() # Postprocess programs self.copy_prog = self.load_program('programs/copy.glsl') self.copy_greyscale_prog = self.load_program( 'programs/copy_greyscale.glsl') self.blur_h_prog = self.load_program('programs/blur_h.glsl') self.blur_v_prog = self.load_program('programs/blur_v.glsl') self.combine = self.load_program('programs/combine.glsl') self.combine['texture1'] = 1 # blur stuff self.offscreen_texture = self.ctx.texture( (self.wnd.buffer_width, self.wnd.buffer_height), 4) self.offscreen_depth = self.ctx.depth_texture( (self.wnd.buffer_width, self.wnd.buffer_height)) self.offscreen = self.ctx.framebuffer( color_attachments=[self.offscreen_texture], depth_attachment=self.offscreen_depth, ) bd = 1 self.blur_h_texture = self.ctx.texture( (self.wnd.buffer_width // bd, self.wnd.buffer_height // bd), 4) self.blur_h_texture.repeat_x = False self.blur_h_texture.repeat_y = False self.blur_h = self.ctx.framebuffer( color_attachments=[self.blur_h_texture]) self.blur_v_texture = self.ctx.texture( (self.wnd.buffer_width // bd, self.wnd.buffer_height // bd), 4) self.blur_v_texture.repeat_x = False self.blur_v_texture.repeat_y = False self.blur_v = self.ctx.framebuffer( color_attachments=[self.blur_v_texture]) self.music_player = Player() self.music_source = StaticSource( load(RESOURCE_DIR / 'megalovania_remix/song.wav')) self.music_player.queue(self.music_source) self.music_player.play() # self.music_player.seek(60.0 * 4 + 50) self.music_player.volume = 1.0
def __init__(self, images, x, y): self.images = images super(Mole, self).__init__(self.images[0], anchor=(0,0), position=(x,y)) self.position_out = self.position self.position_in = (self.position[0],self.position[1]-130) self.position = self.position_in self.bottom = self.position_in[1] + 200 self.cshape = self self.active = False self.player_thump = StaticSource(pyglet.resource.media('ow.mp3')) self.player_laugh = StaticSource(pyglet.resource.media('laugh.mp3')) self.STATE_IDLE = 0 self.STATE_HIT = 1 self.STATE_MISS = 2 self.state = self.STATE_IDLE
def sound(self, source: StaticSource, loop: bool = False, interupt: bool = True): """Play +source+. +loop+ Loop if true +interupt+ True to stop any current sound and play +source+. False to not play +source+ if any other sound is already playing. """ if not interupt: try: if self.sound_playing: return except AttributeError: pass try: self._snd_player.pause() except AttributeError: pass self._snd_player = source.play() if loop: self._snd_player.loop = True
class Mole(Sprite, AARectShape): def __init__(self, images, x, y): self.images = images super(Mole, self).__init__(self.images[0], anchor=(0,0), position=(x,y)) self.position_out = self.position self.position_in = (self.position[0],self.position[1]-130) self.position = self.position_in self.bottom = self.position_in[1] + 200 self.cshape = self self.active = False self.player_thump = StaticSource(pyglet.resource.media('ow.mp3')) self.player_laugh = StaticSource(pyglet.resource.media('laugh.mp3')) self.STATE_IDLE = 0 self.STATE_HIT = 1 self.STATE_MISS = 2 self.state = self.STATE_IDLE def __getattribute__(self,name): if name=='center': return (self.position[0]+89, (self.position[1]+self.bottom+200)/2 - 20) elif name=='rx': return 89 elif name=='ry': return (self.position[1]+200-self.bottom)/2 + 20 else: return object.__getattribute__(self, name) def setImage(self, index): self.image = self.images[index] def setImageFunc(self, index): return CallFunc(self.setImage, index) def checkHitState(self): if self.state == self.STATE_IDLE: self.laugh() self.state = self.STATE_MISS def setActive(self, value): self.active = value def setState(self, value): self.state = value def waitDone(self): self.checkHitState() self.do(AccelDeccel(MoveTo(self.position_in, self.t)) + CallFunc(self.setActive, False) + CallFunc(self.setState, 0)) def upDone(self): self.do(RandomDelay(.25,.5) + CallFunc(self.waitDone)) def up(self): self.t = random.uniform(.25,1.0) self.setImage(0) self.position = self.position_in self.setActive(True) self.do(RandomDelay(0.0,0.2) + AccelDeccel(MoveTo(self.position_out, self.t)) + CallFunc(self.upDone)) def laugh(self): self.player_laugh.play() self.do(self.setImageFunc(1)+Delay(.1)+ self.setImageFunc(2)+Delay(.1)+ self.setImageFunc(3)+Delay(.1)+ self.setImageFunc(2)+Delay(.1)+ self.setImageFunc(3)+Delay(.1)+ self.setImageFunc(1)+Delay(.1)) def down_fast(self): self.do(Accelerate(MoveTo(self.position_in, self.t/2)) + CallFunc(self.setActive, False) + CallFunc(self.setState, 0)) def thump(self): self.player_thump.play() self.stop() self.down_fast() self.do(self.setImageFunc(4)+Delay(.1)+ self.setImageFunc(5)+Delay(.1)+ self.setImageFunc(6)+Delay(.1)+ self.setImageFunc(4)+Delay(.1)+ self.setImageFunc(5)+Delay(.1)+ self.setImageFunc(6)+Delay(.1))
def __init__(self): self.map = None self.layout = None self.end_staircase = None self.stage = 1 self.game_window = None self.pc = None self.next_stage = False self.move_timeout = True self.timeout_limit = 1 # sekundy self.enemies = set() self.consumables = set() self.equippables = set() self.sprites = Batch() self.items = Batch() self.stages = 5 self.cell_size = 5 self.width = 45 # w tile-ach, wielokrotność 3*sell_size self.height = 30 # wielokrotność cell_size self.status_bar = None self.difficulty = 0 self.groups = [OrderedGroup(0), OrderedGroup(1)] self.tileset = TextureGrid(ImageGrid(load_image('img/tiles.png'), 5, 5)) self.tile_textures = { 'floor': self.tileset.get(4, 0).get_image_data(), 'wall': self.tileset.get(4, 1).get_image_data(), 'bars': self.tileset.get(4, 2).get_image_data(), 'rubble': self.tileset.get(4, 3).get_image_data(), 'stairs': self.tileset.get(4, 4).get_image_data(), } self.sprite_textures = { 'bandit_dig': self.tileset.get(0, 0), 'bandit_disg': self.tileset.get(0, 1), 'bandit_fierce': self.tileset.get(0, 2), 'bandit_wander': self.tileset.get(0, 3), 'bandit_wary': self.tileset.get(0, 4), 'bandit_aggr': self.tileset.get(1, 2), 'bandit_coward': self.tileset.get(1, 3), 'bandit_def': self.tileset.get(1, 4), 'guard_glass': self.tileset.get(1, 0), 'guard_unlk': self.tileset.get(1, 1), 'guard': self.tileset.get(2, 0), 'guard_angry': self.tileset.get(2, 1), 'guard_blnk': self.tileset.get(2, 2), 'guard_chase': self.tileset.get(2, 3), 'guard_def': self.tileset.get(2, 4), 'player': self.tileset.get(3, 0), 'player_h': self.tileset.get(3, 1), 'player_d': self.tileset.get(3, 2), 'player_dh': self.tileset.get(3, 3), 'item': self.tileset.get(3, 4), } self.sounds = { 'player_hit': StaticSource(load_media('sound/player_hit.wav')), 'bandit_hit': StaticSource(load_media('sound/bandit_hit.wav')), 'guard_hit': StaticSource(load_media('sound/guard_hit.wav')), }
class Task(ColorLayer, pyglet.event.EventDispatcher): d = Dispatcher() actr_d = JNI_Dispatcher() states = ["INIT", "WAIT_ACTR_CONNECTION", "WAIT_ACTR_MODEL", "CALIBRATE", "IGNORE_INPUT", "PLAY", "GAME_OVER"] STATE_INIT = 0 STATE_WAIT_ACTR_CONNECTION = 1 STATE_WAIT_ACTR_MODEL = 2 STATE_CALIBRATE = 3 STATE_IGNORE_INPUT = 4 STATE_PLAY = 5 STATE_GAME_OVER = 6 is_event_handler = True def __init__(self, client, actr): self.screen = director.get_window_size() self.ncells = int(director.settings['board_size']) self.scorepad = int(self.screen[1] * .025) self.cell = int((self.screen[1]-self.scorepad) / self.ncells * .1) * 10 print self.cell width = self.ncells * self.cell + self.ncells + 1 super(Task, self).__init__(0, 0, 0, 255, width, width) self.position = ((self.screen[0]-width)/2, (self.screen[1]-width)/2-self.scorepad) self.score_layer = Score(width, 2*self.scorepad, self.position[0], self.position[1]+width+self.scorepad/2) self.score = 0 self.food_eaten = 0 self.speed_factor = float(director.settings['speed_factor']) self.state = self.STATE_INIT self.snake = None self.food = None self.ready = False self.blop = StaticSource(pyglet.resource.media('blop.mp3')) self.laugh = StaticSource(pyglet.resource.media('laugh.mp3')) self.text_batch = BatchNode() self.game_over_label = text.Label("GAME OVER", font_size=int(self.cell*3), x= width / 2, y= width / 2, font_name="Pipe Dream", color=(255,255,255,255), anchor_x='center', anchor_y='bottom', batch=self.text_batch.batch) self.game_over_label = text.Label("Press Spacebar For New Game", font_size=int(self.cell*1.5), x= width / 2, y= width / 2, font_name="Pipe Dream", color=(255,255,255,255), anchor_x='center', anchor_y='top', batch=self.text_batch.batch) def get_move_by(self): if self.movement_direction == 1: return (0, 1) elif self.movement_direction == 2: return (1, 0) elif self.movement_direction == 3: return (0, -1) elif self.movement_direction == 4: return (-1, 0) def move_snake_body(self, dt): mod = self.get_move_by() c,r = self.snake[0].grid_loc nc = c + mod[0] nr = r + mod[1] # First check if new head location is out of bounds if nc < 1 or nc > self.ncells or nr < 1 or nr > self.ncells: self.game_over() return # Now check to see if next cell is food. If it is, make the food location # the new head of the snake. if (nc,nr) == self.food.grid_loc: self.food_eaten += 1 self.score += int(math.ceil((self.food_eaten-1) * 1.5 + 10)) self.score_layer.set_score(self.score) self.blop.play() self.food.color = (255,255,255) self.snake = [self.food] + self.snake self.spawn_food() self.speed = self.speed * self.speed_factor # Otherwise move snake else: # starting at the tail and moving towards the head, move each body # segment to the preceeding body segment location for i in range(1,len(self.snake))[::-1]: self.snake[i].set_grid_loc(self.snake[i-1].grid_loc) # finally move the snake head to the new location self.snake[0].set_grid_loc((nc,nr)) # Now check to see if there is a body collision if duplicates([tuple(cell.grid_loc) for cell in self.snake]): self.game_over() return pyglet.clock.schedule_once(self.move_snake_body, self.speed) self.ready = True def game_over(self): self.laugh.play() self.state = self.STATE_GAME_OVER self.snake[0].stop() self.ready = False self.clear() self.add(self.text_batch, z=1) def spawn_food(self): cont = True while cont: cont = False c = (choice(range(0, self.ncells)) + 1) r = (choice(range(0, self.ncells)) + 1) for s in self.snake: if s.grid_loc == (c,r): cont = True break if not cont: self.food = GridSquare(c, r, self.cell, color=(255, 0, 0)) self.add(self.food) def clear(self): if self.snake: map(self.remove, self.snake) self.snake = None if self.food: self.remove(self.food) self.food = None def reset(self): self.movement_direction = 1 self.speed = .1 self.score = 0 self.score_layer.set_score(self.score) self.clear() center = int(self.ncells/2) + 1 self.snake = [GridSquare(center, center, self.cell)] self.add(self.snake[0]) for r in range(1, 3): self.snake.append(GridSquare(center, center-r, self.cell)) self.add(self.snake[-1]) self.spawn_food() self.state = self.STATE_PLAY pyglet.clock.schedule_once(self.move_snake_body, 2) def one_time(self): if director.settings['eyetracker'] and director.settings['fixation_overlay']: self.dispatch_event("show_fixation") def on_enter(self): if isinstance(director.scene, TransitionScene): return super(Task, self).on_enter() if director.settings['eyetracker']: self.state = self.STATE_CALIBRATE self.dispatch_event("start_calibration", self.calibration_ok, self.calibration_bad) else: self.reset() def calibration_ok(self): self.dispatch_event("stop_calibration") self.one_time() self.reset() def calibration_bad(self): self.dispatch_event("stop_calibration") director.scene.dispatch_event("show_intro_scene") def on_exit(self): if isinstance(director.scene, TransitionScene): return super(Task, self).on_exit() self.clear() if director.settings['eyetracker'] and director.settings['fixation_overlay']: self.dispatch_event("hide_fixation") if ACTR6: @actr_d.listen('connectionMade') def ACTR6_JNI_Event(self, model, params): pass @actr_d.listen('connectionLost') def ACTR6_JNI_Event(self, model, params): pass @actr_d.listen('reset') def ACTR6_JNI_Event(self, model, params): pass @actr_d.listen('model-run') def ACTR6_JNI_Event(self, model, params): pass @actr_d.listen('model-stop') def ACTR6_JNI_Event(self, model, params): pass @actr_d.listen('gaze-loc') def ACTR6_JNI_Event(self, model, params): pass @actr_d.listen('attention-loc') def ACTR6_JNI_Event(self, model, params): pass @actr_d.listen('keypress') def ACTR6_JNI_Event(self, model, params): pass @actr_d.listen('mousemotion') def ACTR6_JNI_Event(self, model, params): pass @actr_d.listen('mouseclick') def ACTR6_JNI_Event(self, model, params): pass if eyetracking: @d.listen('ET_FIX') def iViewXEvent(self, inResponse): pass @d.listen('ET_SPL') def iViewXEvent(self, inResponse): pass def on_mouse_press(self, x, y, buttons, modifiers): pass def on_mouse_motion(self, x, y, dx, dy): pass def on_key_press(self, symbol, modifiers): if self.state < self.STATE_IGNORE_INPUT: return if symbol == key.W and (modifiers & key.MOD_ACCEL): director.scene.dispatch_event("show_intro_scene") True if self.state == self.STATE_PLAY: if self.ready: self.ready = False if symbol == key.UP: if self.movement_direction == 2 or self.movement_direction == 4: self.movement_direction = 1 elif symbol == key.DOWN: if self.movement_direction == 2 or self.movement_direction == 4: self.movement_direction = 3 elif symbol == key.RIGHT: if self.movement_direction == 1 or self.movement_direction == 3: self.movement_direction = 2 elif symbol == key.LEFT: if self.movement_direction == 1 or self.movement_direction == 3: self.movement_direction = 4 elif self.state == self.STATE_GAME_OVER: if symbol == key.SPACE: self.remove(self.text_batch) self.reset()