def __init__(self, model): self.model = model self.sprite = copy.copy( resman.get("game.car%d_sprite" % (model.nr + 1))) self.motionblur = [] self.motionblur_cnt = 0 self.motionblur_timer = Timer(25) self.alpha_surfs = {} self.ghost_sprite = None
def __init__(self, model): PickupView.__init__(self) self.model = model self.sprite = resman.get("game.dynamite_sprite").clone() self.sprite_delta = 1 self.prev_life = 1.0 w, h = self.sprite.get_size() self.sparkle_offset = Vec2D(7, -h + 24) self.sparkle_line = Vec2D(0, -22) self.sparkles = [] self.sparkle_timer = Timer(25)
def __init__( self, model ): self.model = model self.sprite = copy.copy(resman.get("game.car%d_sprite" % (model.nr+1))) self.motionblur = [] self.motionblur_cnt = 0 self.motionblur_timer = Timer( 25 ) self.alpha_surfs = {} self.ghost_sprite = None
class DynamiteView(PickupView): class Sparkle: def __init__(self, pos): self.pos = pos self.life = 10 + int(random.random() * 2) self.move = Vec2D(random.uniform(-2.5, 2.5), random.uniform(-2.5, 0.0)) self.surf = resman.get("game.sparkle_surf") width, height = self.surf.get_size() self.center = Vec2D(width / 2, height / 2) def game_tick(self): self.life -= 1 self.pos += self.move self.move.y += 0.1 def is_dead(self): return self.life <= 0 def draw(self, frame): pos = self.pos + self.center + Vec2D(frame.X_OFFSET, frame.Y_OFFSET) self.surf.draw(frame.surface, pos) def __init__(self, model): PickupView.__init__(self) self.model = model self.sprite = resman.get("game.dynamite_sprite").clone() self.sprite_delta = 1 self.prev_life = 1.0 w, h = self.sprite.get_size() self.sparkle_offset = Vec2D(7, -h + 24) self.sparkle_line = Vec2D(0, -22) self.sparkles = [] self.sparkle_timer = Timer(25) def draw(self, frame): if self.get_pos(frame) is None: return # no time... must implement... bad code... if self.model.life < pickups.Dynamite.DEC * 18 and self.model.life != self.prev_life: self.prev_life = self.model.life self.sprite.nr += self.sprite_delta if self.sprite.nr < 0: self.sprite.nr = 0 self.sprite_delta = 1 elif self.sprite.nr >= 4: self.sprite.nr = 3 self.sprite_delta = -1 event.Event.dynamite_tick() while self.sparkle_timer.do_tick(frame.time_sec): self.sparkle_tick(frame) self.sprite.draw(frame.surface, self.get_pos(frame) + Vec2D(frame.X_OFFSET, frame.Y_OFFSET)) for sparkle in self.sparkles: sparkle.draw(frame) def sparkle_tick(self, frame): if self.model.life > pickups.Dynamite.DEC * 18: for i in range(3): pos = self.get_pos(frame) + self.sparkle_offset + self.sparkle_line * self.model.life self.sparkles.append(DynamiteView.Sparkle(pos)) new_sparkles = [] for sparkle in self.sparkles: sparkle.game_tick() if not sparkle.is_dead(): new_sparkles.append(sparkle) self.sparkles = new_sparkles
class DynamiteView(PickupView): class Sparkle: def __init__(self, pos): self.pos = pos self.life = 10 + int(random.random() * 2) self.move = Vec2D(random.uniform(-2.5, 2.5), random.uniform(-2.5, 0.0)) self.surf = resman.get("game.sparkle_surf") width, height = self.surf.get_size() self.center = Vec2D(width / 2, height / 2) def game_tick(self): self.life -= 1 self.pos += self.move self.move.y += 0.1 def is_dead(self): return self.life <= 0 def draw(self, frame): pos = self.pos + self.center + Vec2D(frame.X_OFFSET, frame.Y_OFFSET) self.surf.draw(frame.surface, pos) def __init__(self, model): PickupView.__init__(self) self.model = model self.sprite = resman.get("game.dynamite_sprite").clone() self.sprite_delta = 1 self.prev_life = 1.0 w, h = self.sprite.get_size() self.sparkle_offset = Vec2D(7, -h + 24) self.sparkle_line = Vec2D(0, -22) self.sparkles = [] self.sparkle_timer = Timer(25) def draw(self, frame): if self.get_pos(frame) is None: return # no time... must implement... bad code... if self.model.life < pickups.Dynamite.DEC * 18 and\ self.model.life != self.prev_life: self.prev_life = self.model.life self.sprite.nr += self.sprite_delta if self.sprite.nr < 0: self.sprite.nr = 0 self.sprite_delta = 1 elif self.sprite.nr >= 4: self.sprite.nr = 3 self.sprite_delta = -1 event.Event.dynamite_tick() while self.sparkle_timer.do_tick(frame.time_sec): self.sparkle_tick(frame) self.sprite.draw( frame.surface, self.get_pos(frame) + Vec2D(frame.X_OFFSET, frame.Y_OFFSET)) for sparkle in self.sparkles: sparkle.draw(frame) def sparkle_tick(self, frame): if self.model.life > pickups.Dynamite.DEC * 18: for i in range(3): pos = self.get_pos( frame ) + self.sparkle_offset + self.sparkle_line * self.model.life self.sparkles.append(DynamiteView.Sparkle(pos)) new_sparkles = [] for sparkle in self.sparkles: sparkle.game_tick() if not sparkle.is_dead(): new_sparkles.append(sparkle) self.sparkles = new_sparkles
class GoldCarView (object): def __init__( self, model ): self.model = model self.sprite = copy.copy(resman.get("game.car%d_sprite" % (model.nr+1))) self.motionblur = [] self.motionblur_cnt = 0 self.motionblur_timer = Timer( 25 ) self.alpha_surfs = {} self.ghost_sprite = None def align_car_to_track( self, pos ): """Find the right car rotation/angle""" if pos is None: return if pos.tile.type == Tile.Type.FLAT: in_dir = pos.tile.trail.get_in_direction() out_dir = pos.tile.trail.get_out_direction() if in_dir in [Direction.EAST, Direction.WEST]: in_sprite = 0; else: in_sprite = 6; if out_dir in [Direction.EAST, Direction.WEST]: out_sprite = 0; else: out_sprite = 6; # Turn reversed if( in_dir == Direction.EAST and out_dir == Direction.SOUTH ): in_sprite = 12 if( out_dir == Direction.EAST and in_dir == Direction.SOUTH ): out_sprite = 12 if( in_dir == Direction.WEST and out_dir == Direction.NORTH ): in_sprite = 12 if( out_dir == Direction.WEST and in_dir == Direction.NORTH ): out_sprite = 12 interpol = float(abs(pos.progress)) / float(pos.tile.get_length()) self.sprite.nr = int(in_sprite * (1.0 - interpol) + out_sprite * interpol) % 12 elif pos.tile.type in [Tile.Type.NORTH_SLOPE_TOP, Tile.Type.NORTH_SLOPE_BOT]: self.sprite.nr = 16 elif pos.tile.type in [Tile.Type.EAST_SLOPE_TOP, Tile.Type.EAST_SLOPE_BOT]: self.sprite.nr = 12 elif pos.tile.type in [Tile.Type.SOUTH_SLOPE_TOP, Tile.Type.SOUTH_SLOPE_BOT]: self.sprite.nr = 19 elif pos.tile.type in [Tile.Type.WEST_SLOPE_TOP, Tile.Type.WEST_SLOPE_BOT]: self.sprite.nr = 15 def get_pos( self, frame ): return self.model.pos + (self.model.speed * frame.interpol) def get_pickup_pos( self, frame ): if self.model.pos is None: return None x, y = self.get_pos( frame ).get_screen_position() return Vec2D( x, y - 20 ) def draw( self, frame ): if self.model.pos is None: return pos = self.get_pos( frame ) self.align_car_to_track(pos) # show amount of gold in car self.sprite.nr += 20 * self.model.amount while self.motionblur_timer.do_tick( frame.time_sec ): self.motionblur_tick(pos) # draw motion blur if isinstance( self.model.modifier, Oiler ): diff = 1.0 / (len(self.motionblur)+1) alpha = 0.0 + diff*2 for ghost in self.motionblur: if not self.alpha_surfs.has_key( alpha ): self.alpha_surfs[alpha] = self.sprite.surface.get_blended( alpha ) ghost[1].surface = self.alpha_surfs[alpha] ghost[1].draw( frame.surface, Vec2D(ghost[0][0] + frame.X_OFFSET, ghost[0][1] + frame.Y_OFFSET) ) alpha += diff screen_x, screen_y = pos.get_screen_position() screen_x += frame.X_OFFSET screen_y += frame.Y_OFFSET if isinstance( self.model.modifier, Ghost ): if self.ghost_sprite is None: self.ghost_sprite = copy.copy( self.sprite ) self.ghost_sprite.surface = self.sprite.surface.get_blended( 0.6 ) self.ghost_sprite.nr = self.sprite.nr self.ghost_sprite.draw( frame.surface, Vec2D(screen_x, screen_y) ) else: self.sprite.draw( frame.surface, Vec2D(screen_x, screen_y) ) def motionblur_tick( self, pos ): if isinstance( self.model.modifier, Oiler ): if self.motionblur_cnt == 0: self.motionblur.append( (pos.get_screen_position(), copy.copy(self.sprite)) ) self.moctionblur_cnt = (self.motionblur_cnt+1) % 2 while len( self.motionblur ) > 3: self.motionblur.pop(0) else: self.motionblur = [] def get_z( self ): if self.model.pos is None: return -999 else: return self.model.pos.get_screen_position()[1] z = property( get_z ) def get_submodels( self ): result = [] if self.model.collectible is not None: result.append( self.model.collectible ) if self.model.modifier is not None: result.append( self.model.modifier ) return result submodels = property( get_submodels )
class GoldCarView(object): def __init__(self, model): self.model = model self.sprite = copy.copy( resman.get("game.car%d_sprite" % (model.nr + 1))) self.motionblur = [] self.motionblur_cnt = 0 self.motionblur_timer = Timer(25) self.alpha_surfs = {} self.ghost_sprite = None def align_car_to_track(self, pos): """Find the right car rotation/angle""" if pos is None: return if pos.tile.type == Tile.Type.FLAT: in_dir = pos.tile.trail.get_in_direction() out_dir = pos.tile.trail.get_out_direction() if in_dir in [Direction.EAST, Direction.WEST]: in_sprite = 0 else: in_sprite = 6 if out_dir in [Direction.EAST, Direction.WEST]: out_sprite = 0 else: out_sprite = 6 # Turn reversed if (in_dir == Direction.EAST and out_dir == Direction.SOUTH): in_sprite = 12 if (out_dir == Direction.EAST and in_dir == Direction.SOUTH): out_sprite = 12 if (in_dir == Direction.WEST and out_dir == Direction.NORTH): in_sprite = 12 if (out_dir == Direction.WEST and in_dir == Direction.NORTH): out_sprite = 12 interpol = float(abs(pos.progress)) / float(pos.tile.get_length()) self.sprite.nr = int(in_sprite * (1.0 - interpol) + out_sprite * interpol) % 12 elif pos.tile.type in [ Tile.Type.NORTH_SLOPE_TOP, Tile.Type.NORTH_SLOPE_BOT ]: self.sprite.nr = 16 elif pos.tile.type in [ Tile.Type.EAST_SLOPE_TOP, Tile.Type.EAST_SLOPE_BOT ]: self.sprite.nr = 12 elif pos.tile.type in [ Tile.Type.SOUTH_SLOPE_TOP, Tile.Type.SOUTH_SLOPE_BOT ]: self.sprite.nr = 19 elif pos.tile.type in [ Tile.Type.WEST_SLOPE_TOP, Tile.Type.WEST_SLOPE_BOT ]: self.sprite.nr = 15 def get_pos(self, frame): return self.model.pos + (self.model.speed * frame.interpol) def get_pickup_pos(self, frame): if self.model.pos is None: return None x, y = self.get_pos(frame).get_screen_position() return Vec2D(x, y - 20) def draw(self, frame): if self.model.pos is None: return pos = self.get_pos(frame) self.align_car_to_track(pos) # show amount of gold in car self.sprite.nr += 20 * self.model.amount while self.motionblur_timer.do_tick(frame.time_sec): self.motionblur_tick(pos) # draw motion blur if isinstance(self.model.modifier, Oiler): diff = 1.0 / (len(self.motionblur) + 1) alpha = 0.0 + diff * 2 for ghost in self.motionblur: if not self.alpha_surfs.has_key(alpha): self.alpha_surfs[alpha] = self.sprite.surface.get_blended( alpha) ghost[1].surface = self.alpha_surfs[alpha] ghost[1].draw( frame.surface, Vec2D(ghost[0][0] + frame.X_OFFSET, ghost[0][1] + frame.Y_OFFSET)) alpha += diff screen_x, screen_y = pos.get_screen_position() screen_x += frame.X_OFFSET screen_y += frame.Y_OFFSET if isinstance(self.model.modifier, Ghost): if self.ghost_sprite is None: self.ghost_sprite = copy.copy(self.sprite) self.ghost_sprite.surface = self.sprite.surface.get_blended( 0.6) self.ghost_sprite.nr = self.sprite.nr self.ghost_sprite.draw(frame.surface, Vec2D(screen_x, screen_y)) else: self.sprite.draw(frame.surface, Vec2D(screen_x, screen_y)) def motionblur_tick(self, pos): if isinstance(self.model.modifier, Oiler): if self.motionblur_cnt == 0: self.motionblur.append( (pos.get_screen_position(), copy.copy(self.sprite))) self.moctionblur_cnt = (self.motionblur_cnt + 1) % 2 while len(self.motionblur) > 3: self.motionblur.pop(0) else: self.motionblur = [] def get_z(self): if self.model.pos is None: return -999 else: return self.model.pos.get_screen_position()[1] z = property(get_z) def get_submodels(self): result = [] if self.model.collectible is not None: result.append(self.model.collectible) if self.model.modifier is not None: result.append(self.model.modifier) return result submodels = property(get_submodels)