def __init__(self): cls = ArrowKeyController self.pressed = { cls.UP: False, cls.DOWN: False, cls.LEFT: False, cls.RIGHT: False } self.vector = Vector(0, 0)
class GameObject(object): START_COLOR = (127, 127, 127) def __init__(self, state, coord, obj_code, player): self.obj_code = obj_code self.owner_id = None self.state = state self.coord = Vector(coord) self.pixel_coord = Vector() self.image = None self.garrison = load_garrison(self, player) self.set_position() def run(self): pass def draw(self, surface): pass def set_position(self): x, y = self.coord.position x *= TILE_SIZE y *= TILE_SIZE self.pixel_coord.set_position(x, y) def load_image(self, id): i = pygame.image.load(''.join(('assets/', id, '.png'))).convert() i.set_colorkey((255, 255, 255)) return i def change_color(self, old, new): px_array = pygame.PixelArray(self.image) px_array.replace(old, new) self.color = new def get_garrison(self): return self.garrison def capture(self, new_owner): self.owner_id = new_owner.player_id self.change_color(self.color, new_owner.color) def raze(self): self.state.map.game_object_map.remove_game_object(self) print 'building razed' def end_defend(self): pass
def random_walk(self): adj = self.get_adj() if adj: x, y = choice(adj) vec = Vector(x, y) vec.sub(self.owner.coord) self.owner.try_move(vec) else: self.owner.spend_turn()
def __init__(self, state, coord, obj_code, player): self.obj_code = obj_code self.owner_id = None self.state = state self.coord = Vector(coord) self.pixel_coord = Vector() self.image = None self.garrison = load_garrison(self, player) self.set_position()
def __init__(self, state, start_pos=Vector(0, 0)): StateComponent.__init__(self, state) self.coord = start_pos self.display_w = View.VIEW_W self.display_h = View.VIEW_H self.min_x = 0 self.min_y = 0 self.max_x = 0 self.max_y = 0 self.offset_vector = Vector(-View.OFFSET_X, -View.OFFSET_Y)
def __init__(self, ui, w, h, coord=(0, 0), parent=None, el_id=None): self.ui = ui self.element_id = self.get_element_id(el_id) self.sub_elements = [] self.components = [] self.button_component = None # a ref to the part of element that will be changed by clicking self.w = w self.h = h self.coord = Vector.from_tuple(coord) self.parent = self.set_parent(parent)
def __init__(self, ui, w, h, coord=(0, 0), parent=None, el_id=None): self.ui = ui self.element_id = self.get_element_id(el_id) self.sub_elements = [] self.components = [] self.named_components = {} self.w = w self.h = h self.coord = Vector(coord) self.parent = self.set_parent(parent)
def __init__(self, level_map): assert level_map.tile_map self.level_map = level_map self.tile_map = self.level_map.tile_map self.color_map = self.level_map.color_map self.state = self.level_map.game_state self.w = self.level_map.w self.h = self.level_map.h self.images = {(MapImage.A, ): None, (MapImage.B, ): None} self.coord = Vector(0, 0)
class GameObject(object): def __init__(self, level_map, (x, y), blocks_sight, blocks): self.level_map = level_map self.game_state = level_map.game_state self.view = self.game_state.view self.coord = Vector(x, y) self.obj_id = None self.image_component = None self.flash_component = None self.color_component = None self.stat_component = None self.color_source = None self.team = 'NEUTRAL' self.blocks_sight = blocks_sight self.blocks = blocks
def __init__(self): self.surface = None self.coord = Vector(0, 0) self.color_id = START_COLOR
class ArrowKeyController(object): UP = 0 RIGHT = 1 DOWN = 2 LEFT = 3 DIRECTIONS = {UP, RIGHT, DOWN, LEFT} STR_DIRECTIONS = {'up': UP, 'right': RIGHT, 'down': DOWN, 'left': LEFT} VECTORS = {UP: (0, -1), DOWN: (0, 1), LEFT: (-1, 0), RIGHT: (1, 0)} def __init__(self): cls = ArrowKeyController self.pressed = { cls.UP: False, cls.DOWN: False, cls.LEFT: False, cls.RIGHT: False } self.vector = Vector(0, 0) def press(self, k_id): cls = ArrowKeyController if k_id in cls.DIRECTIONS: self.pressed[k_id] = True vx, vy = cls.VECTORS[k_id] self.vector.set_position(vx, vy) self.on_press() # elif k_id.lower() in cls.STR_DIRECTIONS: # self.pressed[cls.STR_DIRECTIONS[k_id]] = True else: raise Exception('invalid key code') def release(self, k_id): cls = ArrowKeyController if k_id in cls.DIRECTIONS: self.pressed[k_id] = False self.vector.set_position(0, 0) self.on_release() # elif k_id.lower() in cls.STR_DIRECTIONS: # self.pressed[cls.STR_DIRECTIONS[k_id]] = False else: raise Exception('invalid key code') def press_up(self): cls = ArrowKeyController self.press(cls.UP) def release_up(self): cls = ArrowKeyController self.release(cls.UP) def press_down(self): cls = ArrowKeyController self.press(cls.DOWN) def release_down(self): cls = ArrowKeyController self.release(cls.DOWN) def press_left(self): cls = ArrowKeyController self.press(cls.LEFT) def release_left(self): cls = ArrowKeyController self.release(cls.LEFT) def press_right(self): cls = ArrowKeyController self.press(cls.RIGHT) def release_right(self): cls = ArrowKeyController self.release(cls.RIGHT) def on_press(self): pass def on_release(self): pass
class View(StateComponent): VIEW_W = 29 VIEW_H = 15 OFFSET_X = VIEW_W / 2 OFFSET_Y = VIEW_H / 2 view_w_px = VIEW_W * SCALE_TILE_W view_h_px = VIEW_H * SCALE_TILE_H def __init__(self, state, start_pos=Vector(0, 0)): StateComponent.__init__(self, state) self.coord = start_pos self.display_w = View.VIEW_W self.display_h = View.VIEW_H self.min_x = 0 self.min_y = 0 self.max_x = 0 self.max_y = 0 self.offset_vector = Vector(-View.OFFSET_X, -View.OFFSET_Y) def set_new_map(self, level): self.max_x = level.w - self.display_w self.max_y = level.h - self.display_h def initialize(self): self.set_view_position() def get_offset_vector(self): self.offset_vector.set_position(-View.OFFSET_X, -View.OFFSET_Y) self.offset_vector.add(self.state.player.coord) def set_view_position(self): self.get_offset_vector() self.coord.set_position(self.offset_vector.x, self.offset_vector.y) self.limit_coord() # changed view position, so update display coords of all actors self.state.level.update_object_positions() def limit_coord(self): x, y = self.coord.position if x < self.min_x: x = self.min_x elif x > self.max_x: x = self.max_x if y < self.min_y: y = self.min_y elif y > self.max_y: y = self.max_y self.coord.set_position(x, y) def coord_in_view(self, pos): x, y = pos.position vx, vy = self.coord.position return vx <= x < vx + self.display_w and vy <= y < vy + self.display_h