class TileImage(): def __init__(self, source): self.tile_size = (126, 126) self.texture = Image(source=source).texture #def _set_texture(self, value): #filename = resource_find(self.source) #self.texture = CoreImage(filename) #(x,y,width,height) = self.select_region() #self.texture = self.texture.get_region(x,y,width,height) #Logger.info('hi ' + str(self.texture) + ' ' + str(x) + ':' + str(y)) def get_region(self, pos): num_wide = self.texture.size[0] / self.tile_size[0] num_high = self.texture.size[1] / self.tile_size[1] if num_high == 0 or num_wide == 0: return (0, 0, 1, 1) if pos > num_wide * num_high: return None #this is an error else: #(x,y) = (0,0) x = pos % num_wide * self.tile_size[0] y = pos / num_wide * self.tile_size[1] Logger.info('%d %d %d, %d %d', pos, num_wide, num_high, x, y) #self.texture = self.image.texture.get_region(x,y,width,height) r = Image(size=self.tile_size) r.texture = self.texture.get_region(x, y, self.tile_size[0], self.tile_size[1]) return r
def load_walls(state, base_name, background_file, tile_file, with_collisions=True): """ TODO """ tile_size = screen.get_tile_size(state) int_tile_size = int(tile_size) background_texture = Image(source=background_file).texture walls_texture = Image(source=tile_file).texture for tile_name, origin_xy, collision in tiles_origin_table: full_tile_name = base_name + tile_name wall_texture = walls_texture.get_region( origin_xy[0] * int_tile_size, origin_xy[1] * int_tile_size, int_tile_size, int_tile_size) tile_texture = Texture.create(size=(int_tile_size, int_tile_size), colorfmt='rgba') fbo = Fbo(size=(int_tile_size, int_tile_size), texture=tile_texture) with fbo: Color(1, 1, 1) Rectangle(pos=(0, 0), size=tile_texture.size, texture=background_texture) Rectangle(pos=(0, 0), size=tile_texture.size, texture=wall_texture) fbo.draw() if not with_collisions: collision = None tiles.add_tile_def(state, full_tile_name, tile_texture, collision)
class CustomImageClass(Widget): image_size = ListProperty([0, 0]) def __init__(self, **kwargs): super(CustomImageClass, self).__init__(**kwargs) self.background_ = Image(source='Asset 1test.png').texture self.bind(size=self.update_image_size) def update_image_size(self, *args): x = self.width y = (self.background_.size[1] * x) / self.background_.size[0] self.image_size = [x, y] def on_touch_down(self, touch): pos = [ touch.pos[0] / self.width * self.background_.size[0], touch.pos[1] / self.height * self.background_.size[1] ] pixel = self.background_.get_region(*pos, 1, 1) raw_pixel = pixel.pixels data = struct.unpack('4B', raw_pixel) adjusted = [float('%.3f' % (a / 255)) for a in data] #print('raw pixel data', raw_pixel) print('unpacked data', data) print('adjusted', adjusted) print() # print('hex color', get_hex_from_color(adjusted)) #print('pixel color:', [a / 255 for a in data], '\n') return False
def __init__(self, init_pos, camera, collision, controller): super(Player, self).__init__() self.pos = init_pos self.y_vel = 0 self.texture_size = (150, 150) self.size = (100, 150) self.frame = 0 texture = Image(source='../../data/kramer_sprites.png').texture #texture.mag_filter = 'nearest' self.frames = [ TextureHolder(texture.get_region(x, 0, 150, 200)) for x in [0, 150, 300, 450] ] self.sprite = Rectangle(texture=self.frames[0].texture, pos=init_pos, size=self.texture_size) self.add(Color(1, 1, 1)) self.add(self.sprite) self.controller = controller self.collision = collision self.camera = camera self.can_jump = True self.og_jump_f = 800 self.jump_f = self.og_jump_f
class TileImage(): def __init__(self, source): self.tile_size = (126,126) self.texture = Image(source=source).texture #def _set_texture(self, value): #filename = resource_find(self.source) #self.texture = CoreImage(filename) #(x,y,width,height) = self.select_region() #self.texture = self.texture.get_region(x,y,width,height) #Logger.info('hi ' + str(self.texture) + ' ' + str(x) + ':' + str(y)) def get_region(self, pos): num_wide = self.texture.size[0] / self.tile_size[0] num_high = self.texture.size[1] / self.tile_size[1] if num_high == 0 or num_wide == 0: return (0,0,1,1) if pos > num_wide * num_high: return None #this is an error else: #(x,y) = (0,0) x = pos % num_wide * self.tile_size[0] y = pos / num_wide * self.tile_size[1] Logger.info('%d %d %d, %d %d',pos, num_wide, num_high, x, y ) #self.texture = self.image.texture.get_region(x,y,width,height) r = Image(size=self.tile_size) r.texture = self.texture.get_region(x,y,self.tile_size[0],self.tile_size[1]) return r
def load_font(state, font_name, font_path, font_w, font_h): """ TODO """ font_texture = Image(source=font_path).texture code = 32 for y in xrange(8): for x in xrange(12): texture_name = font_name + "-" + str(code) texture_db.insert(state, texture_name, font_texture.get_region(x * font_w, (7 - y) * font_h, font_w, font_h)) code += 1 state[IDX_STATE_TEXT][font_name] = [font_w, font_h]
def init_pieces_images(): s = 45 pieces_image = Image(source='chess.png').texture for x, piece in enumerate([King, Queen, Rook, Bishop, Knight, Pawn]): piece._images = [ pieces_image.get_region(s * x, s * y, s, s) for y in range(6) ][::-1] for preference, piece in enumerate( [King, Pawn, Knight, Bishop, Rook, Queen]): piece.move_preference = preference
def getSpriteListFromAtlas(source,sqs): #sqs = 'square size' atlas = Image(source=source).texture atlas.mag_filter = 'nearest' regions_x = atlas.width/sqs regions_y = atlas.height/sqs spriteList = [] for i in range(0,regions_y): for b in range(0,regions_x): coords = b*sqs,i*sqs,sqs,sqs region = atlas.get_region(coords[0],coords[1],coords[2],coords[3]) spriteList.append(Sprite(region)) return spriteList
def load_image(self): self.data = {} for path, dir_list, file_list in os.walk(self.image_dir): for file_name in file_list: name = '/'.join((path, file_name)).replace('\\', '/') texture = Image(source=name).texture x, y = texture.size x_time = x / ShowBase.size y_time = y / ShowBase.size x_size = x / x_time y_size = y / y_time x_offset = x_size - ShowBase.size y_offset = y_size - ShowBase.size self.data[name] = {} for i in xrange(x_time): for j in xrange(y_time): self.data[name][(i, j)] = texture.get_region(i * x_size + x_offset, j * y_size + y_offset, ShowBase.size, ShowBase.size)
def add_image(self, base_path, file): if not os.path.exists(file): file = os.path.join(base_path, file) texture = Image(source=file).texture texture.mag_filter = 'nearest' if texture is None: sys.exit('failed to locate image file %r' % file) id = self.firstgid th = self.tile_height + self.spacing tw = self.tile_width + self.spacing for j in xrange(texture.height / th): for i in xrange(texture.width / tw): x = (i * tw) + self.margin # convert the y coordinate to OpenGL (0 at bottom of texture) y = texture.height - ((j + 1) * th) tile = texture.get_region(x, y, self.tile_width, self.tile_height) self.tiles.append(Tile(id, tile, self)) id += 1
def explode(self, obj): tex = Image(source='asssts/explosion_animate.png').texture tex = tex.get_region(0, 0, 32, 32) x = Rectangle(size=obj.size, pos=obj.pos, texture=tex)
class Enemy(InstructionGroup): def __init__(self, spawn_x, key = Notes.C, speed=1.0, audio_callback=None, hurt_player_callback=None, dmg_rect_on_hit_callback=None, add_sound=None, remove_sound=None, clear_prog=None): super(Enemy, self).__init__() self.time = 0.0 # pos3D is 3D cartesian coords self.pos3D = [spawn_x,0,-D] # pos2D is for actual position on screen self.pos2D = self.convert_to_pos2D(self.pos3D) self.size = np.array((200,380))*Window.height/600 self.speed = speed # Callback Functions self.hurt_player_callback = hurt_player_callback self.dmg_rect_on_hit_callback = dmg_rect_on_hit_callback self.clear_prog = clear_prog #---------# # Visuals # #---------# self.texture = Image(source='../data/ogre.png').texture self.textures = [] self.texture_a = self.texture.get_region(0,435,107,145); self.textures.append(self.texture_a) self.texture_b = self.texture.get_region(107,435,107,145); self.textures.append(self.texture_b) self.texture_c = self.texture.get_region(215,435,107,145); self.textures.append(self.texture_c) self.texture_d = self.texture.get_region(322,435,107,145); self.textures.append(self.texture_d) self.anim_switch_time = 0.270 self.anim_time = self.anim_switch_time self.anim_frame = randint(0,100)%len(self.textures) s = self.size*self.scale_with_z() self.cbrect = CBRectangle(texture=self.textures[self.anim_frame], cbpos=(self.pos2D[0],self.pos2D[1]), cbsize=(s[0],s[1])) self.color = Color(0.65,0.65,0.65) self.add(self.color) self.add(self.cbrect) # self.color = Color(1,0.1,0.1) # s = self.size*self.scale_with_z() # self.cbrect = CBRectangle( cbpos=(self.pos2D[0],self.pos2D[1]), cbsize=(s[0],s[1]) ) # self.add(self.color) # self.add(self.cbrect) # If currently targeted by crosshair self.is_targeted = False self.is_dead = False # TODO: list of textures for different animation states # TODO: eventually replace with textures # self.texture = Image(source='../data/find_a_picture.png').texture # self.rect = Rectangle(texture=self.texture, pos=(0,0), size=(100,100)) #---------------# # Audio & Music # #---------------# self.key = key self.chord = Chord(key) self.dissonantPitches = self.chord.pitches # Assigned when this Enemy is killed self.resolvedPitches = [] #TODO: these are example pitches; we need to add the pitches from the notes of chord... self.seq = add_sound(self.dissonantPitches, self.get_closeness) # Note Display self.ND = Note_Display(key, self.dissonantPitches, self.pos2D[0], self.pos2D[1]+s[1]) self.add(self.ND) # Callback Functions self.audio_callback = audio_callback self.add_sound = add_sound self.remove_sound = remove_sound def get_closeness(self): z = self.map(self.pos3D[2], 0, -D, 100, 0) return z def lit(self): self.color.rgb = (0.4,0.4,1) def un_lit(self): self.color.rgb = (0.65,0.65,0.65) def on_update(self, dt): self.time += dt # Check if enemy is dead, and return false immediately if so if self.is_dead: self.remove_sound(self.seq) return False # Animate the Enemy's frames self.anim_time -= dt if self.anim_time <= 0.0: self.anim_time = self.anim_switch_time + self.anim_time self.anim_frame += 1 self.cbrect.texture = self.textures[self.anim_frame%len(self.textures)] # Move Enemy down toward the player if self.pos3D[2] < 0: s = dt*self.speed*30 self.change3D(0, 0, s) else: self.hurt_player_callback(5) self.dmg_rect_on_hit_callback() self.is_dead = True # Move Note Display to follow enemy s = self.size*self.scale_with_z() self.ND.on_update( dt, self.pos2D[0], self.pos2D[1]+s[1] ) return True # Called when a crosshair first hovers over an enemy def on_target(self): if (self.is_targeted): return if self.audio_callback: self.audio_callback(self.dissonantPitches) # Called immediately before dying when an enemy is hit by a player # Return True if successfully killed by the player, else False def on_hit(self, pitch): # Sub in the pitch for the note is closest to in the chord minDist = float("inf") minPitch = None for chordPitch in self.dissonantPitches: dist = abs(pitch - chordPitch) if dist < minDist: minDist = dist minPitch = chordPitch idx = self.dissonantPitches.index(minPitch) comparisonPitches = list(self.dissonantPitches) comparisonPitches[idx] = pitch playbackPitches = comparisonPitches killed = False if Chord.is_valid_chord(self.key, comparisonPitches): self.resolvedPitches = comparisonPitches self.is_dead = True killed = True else: self.clear_prog() if self.audio_callback: self.audio_callback(playbackPitches) return killed def set_is_targeted(self, val): self.is_targeted = val def get_is_targeted(self): return self.is_targeted def change3D(self, a, b, c): self.pos3D = (self.pos3D[0] + a, self.pos3D[1] + b, self.pos3D[2] + c) self.pos2D = self.convert_to_pos2D(self.pos3D) self.cbrect.cbpos = self.pos2D s = self.size*self.scale_with_z() self.cbrect.size = (s[0],s[1]) def scale_with_z(self, z=None): if z == None: z = self.pos3D[2] #print z #D = 500.0 # D should be the abs value of most negative z for enemies to spawn at # note: z is negative for values in the field for enemies, with player at z=0 #return 2*z/(3*D) + 1.0 Y = Window.height*0.68 # magic numbers, as far as the eye can see!!! ignore magic numbers below!!!! :D #return self.map(-Y/(D*D) * (z+D)*(z+D) + Y, 0.0, D/(D*12.7/5000), 1.0, 1/10) s = np.power(1.002,z) #print s return s def convert_to_pos2D(self, pos3D): #D = 500.0 Y = Window.height*0.68 X = Subwindow.width() x = pos3D[0] y = pos3D[1] z = pos3D[2] #j = -Y/(D*D) * (z+D)*(z+D) + Y j = Y - Y*np.power(1.002,z) c = self.map(j, 0, Y, 1, 1/3) i = self.map(x+X/2, 0, X, X/2*(1-c), X/2*(1+c)) return (i,j) def map(self, x, in_min, in_max, out_min, out_max): return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
yield [(a, y + delta)] def en_passant(self, x, y): for a in [x - 1, x + 1]: piece = self.game.board.get((a, y)) if type(piece) != Pawn: continue if piece.player % 2 == self.player % 2: # Same team continue if piece.last_move_time is None: continue if self.game.player_last_move[self.player] > piece.last_move_time: # Already moved after this pawn continue yield piece first_row = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] pieces_image = Image(source='chess.png').texture S = pieces_image.size[1] / 2 for x, piece in enumerate([King, Queen, Bishop, Knight, Rook, Pawn]): piece._images = [ pieces_image.get_region(S * x, S * y, S, S) for y in range(2) ][::-1] for preference, piece in enumerate([King, Pawn, Knight, Bishop, Rook, Queen]): piece.move_preference = preference
def __init__(self, init_pos, level): super(Player, self).__init__() #Level Reference for Animation self.level = level # Set the physics of the person self.init_pos = init_pos self.pos = init_pos self.y_vel = 0 self.can_jump = True self.jump_vel = 15 self.gravity = -.5 self.jumped = False # Create a Person self.size = (PLAYER_W, PLAYER_H) self.color_idx = 0 red = Image(source='../../data/player/player_red.png').texture green = Image(source='../../data/player/player_green.png').texture blue = Image(source='../../data/player/player_blue.png').texture red_duck = Image( source='../../data/player/player_duck_red.png').texture green_duck = Image( source='../../data/player/player_duck_green.png').texture blue_duck = Image( source='../../data/player/player_duck_blue.png').texture red_frames = [ TextureHolder(red.get_region(64 * x, 0, 64, 128)) for x in xrange(8) ] green_frames = [ TextureHolder(green.get_region(64 * x, 0, 64, 128)) for x in xrange(8) ] blue_frames = [ TextureHolder(blue.get_region(64 * x, 0, 64, 128)) for x in xrange(8) ] red_duck_frames = [ TextureHolder(red_duck.get_region(64 * x, 0, 64, 102)) for x in xrange(8) ] green_duck_frames = [ TextureHolder(green_duck.get_region(64 * x, 0, 64, 102)) for x in xrange(8) ] blue_duck_frames = [ TextureHolder(blue_duck.get_region(64 * x, 0, 64, 102)) for x in xrange(8) ] self.normal_frames = [red_frames, green_frames, blue_frames] self.duck_frames = [ red_duck_frames, green_duck_frames, blue_duck_frames ] self.cur_frames = self.normal_frames self.person = Rectangle(texture=red_frames[0].texture, pos=self.pos, size=self.size) self.add(Color(1, 1, 1)) self.add(self.person) self.dir_right = True