def __init__(self, config, position, debug_label=None): """ :param config: values from game_config.py :param position: (int, int). the x and y coordinates big: boolean. true if actor currently enlarged, false if actor currently small dirty: boolean. true if they need to be redrawn, false if not. owner: Actor. if actor is a projectile, owner will be set to the actor that shoots the projectile size: size of actor on the screen at the near plane. scale_x: double. the ratio between the player size and the texture size (for x) scale_y: double. the ratio between the player size and the texture size (for y) coord: Vector3. game coordinates of actor velocity: Vector3. Starts out at 0, 0, 0 cshape: the collision shape """ texture_name = '/'.join( ['assets', 'textures', config['type'], config['texture']]) self.big = False self.dirty = True self.owner = None self.debug_label = debug_label super().__init__(texture_name) self.size = config["size"] rect = self.get_rect() self.scale_x = self.size[0] / rect.width self.scale_y = self.size[1] / rect.height self.coord = Vector3(*position) self.velocity = Vector3() self.do(PhysicsAction()) self.cshape = CircleShape(Vector2(*self.coord.xy), self.size[0] / 2)
def shoot(self, key_down): if key_down: x, y = self.actor.coord.xy rock = Actor(item_types["rock"], (x, y, self.actor.size[1] * 0.6)) rock.velocity = Vector3(y=Player.SHOOT_SPEED) rock.owner = self.actor game_layer = self.actor.parent game_layer.add(rock) game_layer.projectiles.append(rock)
def __init__(self, config, position, debug_label=None): """ :param config: values from game_config.py :param position: (int, int). the x and y coordinates """ texture_name = '/'.join( ['assets', 'textures', config['type'], config['texture']]) self.big = False self.dirty = True self.owner = None self.debug_label = debug_label super().__init__(texture_name) self.size = config["size"] rect = self.get_rect() self.scale_x = self.size[0] / rect.width self.scale_y = self.size[1] / rect.height self.coord = Vector3(*position) self.velocity = Vector3() self.do(PhysicsAction()) self.cshape = CircleShape(Vector2(*self.coord.xy), self.size[0] / 2)
def step(self, dt): """ runs one frame for each actor of the game :param dt: delta time: the difference in time between previous frame and next frame """ # if not moving, or not changed if not (self.target.velocity or self.target.dirty): return # reset dirty to false self.target.dirty = False # adding the change in location based on velocity self.target.coord = self.target.velocity * dt + self.target.coord x, y, z = self.target.coord.xyz # check to see if target goes off screen and, if so, set appropriate coords if self.target.velocity: if x > config.screen_size[0]: x = config.screen_size[0] if x < 0: x = 0 if y > FAR_PLANE: y = FAR_PLANE if y < NEAR_PLANE: y = NEAR_PLANE self.target.coord = Vector3(x, y, z) # distance is the percentage of distance between the near and far planes distance = (y - NEAR_PLANE) / FAR_PLANE # calculates the scale of the actor self.target.scale = NEAR_SCALE - (distance * (NEAR_SCALE - FAR_SCALE)) # right now this is for debugging # todo create effect for when actor is hit if self.target.big: self.target.scale *= 2 # used to calculate the location on the x axis screen_width_diff = SCREEN_NEAR_WIDTH - SCREEN_FAR_WIDTH # used to calculate the location on the y axis screen_y_diff = SCREEN_FAR_Y - SCREEN_NEAR_Y # used to calculate where they should appear on the x axis x_percent = x / SCREEN_NEAR_WIDTH # width of the game field at the distance you are at width_at_distance = SCREEN_NEAR_WIDTH - distance * screen_width_diff # offset for x based on the distance on y distance_offset = (SCREEN_NEAR_WIDTH - width_at_distance) / 2 # calculates the x value for position. x = width_at_distance * x_percent + distance_offset # TODO: apparent y distance travelled needs to decrease as distance increases # calculates the y value for position. y = (distance * screen_y_diff) + SCREEN_NEAR_Y # get the rect to get where the actor's feet are rect = self.target.get_rect() rect.midbottom = x, y + z * self.target.scale # set up debug if self.target.debug_label is not None: self.target.debug_label.element.text = '{0},{1}'.format(x, y) # position is position of rect on screen. self.target.position = rect.center # sorting actors along the y axis. if y value is higher, actor should appear behind if self.target.velocity.y != 0: self.target.parent.remove(self.target) # inverse y coordinate to make it smaller for sorting. self.target.parent.add(self.target, -self.target.coord.y) self.target.cshape = CircleShape(Vector2(*self.target.coord.xy), self.target.size[0] / 2)
def move_backward(self, moving): self.actor.velocity = self.actor.velocity - Vector3( y=Player.SPEED * (1 if moving else -1))
def move_right(self, moving): self.actor.velocity = self.actor.velocity + Vector3( Player.SPEED * (1 if moving else -1))