def __init__(self, animations): GameObject.__init__(self, animations) self.type.append('hittable') self.hitboxes = [Rectangle(self, Coordinates(0, 0), 50, 50)] self.priority = 80 self.walking_distance = Coordinates(600, 600) self.additional_firing_coordinates = Coordinates(0, 0)
def get_parallel_line(p1: Coordinates, p2: Coordinates, dist: float, top: bool) -> (Coordinates, Coordinates): """ Get the line parallel to a given line for a certain distance away. :param p1: point 1 of original line :param p2: point 2 of original line :param dist: distance between lines :param top: top or bottom parallel line :return: pair of points representing the new, parallel line """ # Get perpendicular angle normal_angle = get_angle(p2, p1) + 90 if top: p3 = Coordinates(p1.x + dist * np.cos(np.radians(normal_angle)), p1.y - dist * np.sin(np.radians(normal_angle))) p4 = Coordinates(p2.x + dist * np.cos(np.radians(normal_angle)), p2.y - dist * np.sin(np.radians(normal_angle))) else: p3 = Coordinates(p1.x - dist * np.cos(np.radians(normal_angle)), p1.y + dist * np.sin(np.radians(normal_angle))) p4 = Coordinates(p2.x - dist * np.cos(np.radians(normal_angle)), p2.y + dist * np.sin(np.radians(normal_angle))) return p3, p4
def resize(self, w, h): size = Coordinates(w, h) Settings.frame_dimensions = size Frame.screen = None Frame.screen = pygame.display.set_mode(Settings.frame_dimensions.get(), Frame.flags) Frame.scale = Settings.frame_dimensions / Coordinates(800, 600)
def write(self, name=None, additional_path=None, world=None): if world is None: world = World.writable if name is None: name = self.name if name == '': name = input('Enter level name: ') self.name = name if additional_path is None: s = Level.default_path + '\\' + name + '.txt' else: s = Level.default_path + '\\' + additional_path + name + '.txt' print('Saved as:', s) with open(s, 'w') as file: for obj in world: prefix = '-' coordinates = obj.get_coordinates() coordinates = Coordinates(int(coordinates.x), int(coordinates.y)) args = obj.get_args() if not args: args = '-' object_type = obj.__class__.__name__ file.write(prefix + '; ' + str(object_type) + '; ' + str(coordinates.get()) + '; ' + str(args) + '\n')
def focus_obj(): c = Borders.focus_object v = Coordinates(Frame.default_coordinates.x, Frame.default_coordinates.y) if c.get_coordinates().x + Frame.default_coordinates.x < Borders.coordinates.x / Settings.\ frame_additional_scale.x: v.x -= Borders.coordinates.x / Settings.frame_additional_scale.x - c.get_coordinates().x Frame.default_coordinates.x = Borders.coordinates.x / Settings.frame_additional_scale.x\ - c.get_coordinates().x elif c.get_coordinates().x + c.get_width() + Frame.default_coordinates.x > (Borders.coordinates.x + Borders.a)\ / Settings.frame_additional_scale.x: v.x -= (Borders.coordinates.x + Borders.a) / Settings.frame_additional_scale.x - c.get_coordinates().x\ - c.get_width() Frame.default_coordinates.x = (Borders.coordinates.x + Borders.a) / Settings.frame_additional_scale.x\ - c.get_coordinates().x - c.get_width() else: v.x = 0 if c.get_coordinates().y + Frame.default_coordinates.y < Borders.coordinates.y\ / Settings.frame_additional_scale.x: v.y -= Borders.coordinates.y / Settings.frame_additional_scale.x - c.get_coordinates().y Frame.default_coordinates.y = Borders.coordinates.y / Settings.frame_additional_scale.x\ - c.get_coordinates().y elif c.get_coordinates().y + c.get_height() + Frame.default_coordinates.y > (Borders.coordinates.y + Borders.b)\ / Settings.frame_additional_scale.x: v.y -= (Borders.coordinates.y + Borders.b) / Settings.frame_additional_scale.x - c.get_coordinates().y\ - c.get_height() Frame.default_coordinates.y = (Borders.coordinates.y + Borders.b) / Settings.frame_additional_scale.x\ - c.get_coordinates().y - c.get_height() else: v.y = 0 return v * (-1)
def get_balls(game: GameType): m = BALL_MASS r = BALL_RADIUS ball_c = PoolBall(BallType.CUE, Coordinates(0, 0), m, r) ball_1 = PoolBall(BallType.ONE, Coordinates(0, 0), m, r) ball_2 = PoolBall(BallType.TWO, Coordinates(0, 0), m, r) ball_3 = PoolBall(BallType.THREE, Coordinates(0, 0), m, r) ball_4 = PoolBall(BallType.FOUR, Coordinates(0, 0), m, r) ball_5 = PoolBall(BallType.FIVE, Coordinates(0, 0), m, r) ball_6 = PoolBall(BallType.SIX, Coordinates(0, 0), m, r) ball_7 = PoolBall(BallType.SEVEN, Coordinates(0, 0), m, r) ball_8 = PoolBall(BallType.EIGHT, Coordinates(0, 0), m, r) ball_9 = PoolBall(BallType.NINE, Coordinates(0, 0), m, r) balls = { BallType.CUE: ball_c, BallType.ONE: ball_1, BallType.TWO: ball_2, BallType.THREE: ball_3, BallType.FOUR: ball_4, BallType.FIVE: ball_5, BallType.SIX: ball_6, BallType.SEVEN: ball_7, BallType.EIGHT: ball_8, BallType.NINE: ball_9, } return balls
def get_ray_circle_intersection(p1: Coordinates, p2: Coordinates, c_mid: Coordinates, c_radius: float): # print('-------') # print('get_ray_circle_intersection') # print('p1:{}, p2:{}'.format(p1, p2)) # print('c_mid:{}, c_radius:{}'.format(c_mid, c_radius)) # print('-------') if (p2.x - p1.x) == 0: print('p2.x = ', p2.x) print('p1.x = ', p1.x) exit(-1) # Line equation: y = mx + b m = (p2.y - p1.y) / (p2.x - p1.x) b = p1.y - m * p1.x # Circle equation: (x-p)^2 + (y-q)^2 = r^2 p, q, r = c_mid.x, c_mid.y, c_radius # First, substituting line equation into circle equation # These are the coefficients of the quadratic equation created A = (m**2 + 1) B = 2 * (m * b - m * q - p) C = (q**2 - r**2 + p**2 - 2 * b * q + b**2) discrim = B**2 - 4 * A * C if discrim < 0: print('line misses circle') # Line misses circle return None elif discrim == 0: print('line is tangent to circle') # Line tangent to circle x1 = (-B + np.sqrt(discrim)) / 2 * A y1 = m * x1 + b x2 = (-B - np.sqrt(discrim)) / 2 * A y2 = m * x2 + b assert (x1 == x2 and y1 == y2) return Coordinates(x1, y1) else: # discrim > 0 print('line intersects circle') # Line meets circle at 2 points x1 = (-B + np.sqrt(discrim)) / 2 * A result1 = Coordinates(x1, m * x1 + b) x2 = (-B - np.sqrt(discrim)) / 2 * A result2 = Coordinates(x2, m * x2 + b) mag1 = get_distance(p1, result1) mag2 = get_distance(p1, result2) if not check_point_on_line_segment(result1.x, result1.y, p1, p2): return None if mag1 < mag2: return result1 else: return result2
def __init__(self, animations): self.coordinates = Coordinates(0, 0) self.scale = Coordinates(1, 1) self.type = ['widget', 'fixed', 'unstretchable'] self.priority = 200 self.animations = animations self.width = 0 self.height = 0
def __init__(self, obj, path, scale=Coordinates(1, 1), coordinates=Coordinates(0, 0), name='static animation'): Animation.__init__(self, obj, [ObjectImage(path, coordinates, scale)], [-1], name)
def __init__(self, animations): self.args = [Coordinates(1, 1)] self.type = [] self.scale = Coordinates(1, 1) self.hitboxes = list() self.coordinates = Coordinates(0, 0) self.animations = animations self.priority = 100 self.width = 0 self.height = 0 self.texts = []
def __init__(self, gamestates): Menu.__init__(self, gamestates, 10, 6) self.add_list([ MenuTextButton('Back', 30, Coordinates(10, 15), Coordinates(325, 500), self.click_back, scale=Coordinates(1.5, 1)), Background('resources\\test_images\\green_background.png') ])
def reposition(new_position): v = (Coordinates(new_position[0], new_position[1]) / Frame.scale - Mouse.get_coordinates()) / Settings.\ frame_additional_scale Mouse.coordinates = (Coordinates(new_position[0], new_position[1]) / Frame.scale) / Settings.\ frame_additional_scale if Mouse.selected is not None: if not [ x for x in ['fixed', 'unmovable'] if x in Mouse.selected.type ]: Mouse.selected.reposition(Mouse.selected.get_coordinates() + v)
def __init__(self): a = 50 b = a Target.__init__(self, [ StaticAnimation(self, 'resources\\test_images\\target.png', Coordinates(a, b), name='target') ]) self.hitboxes = [Rectangle(self, Coordinates(0, 0), a, b)] self.width = a self.height = b
def get_safe_distance(self, v, k=0): if k > 2: return Coordinates(0, 0) o = self.check_collision(v, type_exceptions=['walkable']) if o is not None: k += 1 d = Vector(v).normalize() * (Vector(self.distance(o, v)) * Vector(v).normalize()) return self.get_safe_distance( (Coordinates(d) * Coordinates(0.99, 0.99)).int(), k) else: return v
def settings(a): if a[0] == 'frame_dimensions': Settings.frame_dimensions = Coordinates(int(a[1]), int(a[2])) if a[0] == 'frame_additional_scale': Settings.frame_additional_scale = Coordinates(float(a[1]), float(a[2])) if a[0] == 'main_character_name': Settings.main_character_name = a[1].replace('\n', '') if a[0] == 'mouse_confined': if a[1] == 'True\n': Settings.mouse_confined = True else: Settings.mouse_confined = False
def future_shape(self, additional_coordinates): lines = self.get_lines() for i in [1, 2]: lines[i].coordinates += additional_coordinates side_line1 = Line( self.gameobject, self.coordinates + Coordinates(0, self.b), self.coordinates + Coordinates(0, self.b) + additional_coordinates) side_line2 = Line( self.gameobject, self.coordinates + additional_coordinates + Coordinates(self.a, 0), self.coordinates + Coordinates(self.a, 0)) return [ MultilineObject(self.gameobject, self.coordinates, lines + [side_line1, side_line2]) ]
def check_ball_ball_collision(a: PoolBall, b: PoolBall) -> bool: """ Check if two balls have collided. :param a: ball A :param b: ball B :return: whether these two balls are in collision """ a_pos = Coordinates(a.pos.x + a.vel.x, a.pos.y + a.vel.y) b_pos = Coordinates(b.pos.x + b.vel.x, b.pos.y + b.vel.y) d = get_distance(a_pos, b_pos) is_colliding = d <= (a.radius + b.radius) return is_colliding
def collides(self, other, additional_coordinates=Coordinates(0, 0)): if other.type == 'rectangle': return self.collides_rectangle(other, additional_coordinates) elif other.type == 'circle': return self.collides_circle(other, additional_coordinates) elif other.type in ['line', 'multiline_object']: return other.collides(self, additional_coordinates * (-1))
def collides(self, other, additional_coordinates=Coordinates(0, 0), type='simple'): if additional_coordinates == Coordinates(0, 0) or type == 'simple': for h1 in self.hitboxes: for h2 in other.hitboxes: if h1.collides(h2, additional_coordinates): return True return False else: for h1 in self.hitboxes: for h2 in other.hitboxes: if h1.future_collision(h2, additional_coordinates): return True return False
def change_scale(self, scale): if scale.x > 0 and scale.y > 0: self.stretch(Coordinates(1, 1) / self.scale) self.scale = scale self.stretch(self.scale) else: self.world_remove()
def __init__(self, animations, a, b): Obstacle.__init__(self, animations) self.hitboxes = [Rectangle(self, Coordinates(0, 0), a, b)] self.type.append('block') self.priority = 90 self.width = a self.height = b
def generate_buttons(self, arr, starting_index, finishing_index): if starting_index >= len(arr): yield False if finishing_index > len(arr): finishing_index = len(arr) l = arr[starting_index:finishing_index] g = self.generate_coordinates() for i in l: c = next(g) if c: yield MenuTextButton( i.split('\\')[-1][:-4], 30, Coordinates(10, 15), c, self.get_level_function(i), self.dim / Coordinates(100, 50)) else: yield False yield False
def __init__(self, a, b): Block.__init__(self, [ StaticAnimation(self, 'resources\\test_images\\grey.png', Coordinates(a, b), name='obstacle') ], a, b) self.args += [a, b]
def click_build(self): self.empty() bck = Background('resources\\test_images\\white_background.png') World.add(bck) save_button = SaveButton() save_button.coordinates = Coordinates(650, 500) World.add(save_button) level = Level(self.gamestates, '') World.setup_level(level) thread_lock.pause_main = False self.gamestates.state = self.gamestates.BUILDER Frame.default_coordinates = Coordinates(0, 0) if Borders.focus_object is None: Borders.gain_focus(World.main_character) Borders.lock() pygame.event.set_grab(Settings.mouse_confined) self.gamestates.sleep()
def __init__(self): Character.__init__(self, [ StaticAnimation(self, 'resources\\test_images\\characters\\cat.png') ]) self.additional_firing_coordinates = Coordinates(25, 25) self.width = 50 self.height = 50
def __init__(self, animations, velocity=Vector(0, 0), center_of_gravity=Coordinates(0, 0), mass=0.02): GameObject.__init__(self, animations) self.velocity = velocity self.mass = mass self.hit_exceptions = [] self.type_exceptions = ['shootable'] self.center_of_gravity = center_of_gravity self.thread = Thread(self.run, (), 'projectile thread')
def __init__(): for path in ImageLibrary.get_all_file_paths(): ImageLibrary.add_to_images(path) with open('files\\image_scales.txt') as f: for line in f: line = line.split('; ') ImageLibrary.get_image(line[2].replace( '\n', '')).scale = Coordinates(int(line[0]), int(line[1]))
def focus_mouse(): b = Borders.in_borders(Mouse.get_coordinates()) v = Coordinates(0, 0) if not b[0]: for i in b[1]: Frame.default_coordinates += Borders.mouse_speed / Settings.frame_additional_scale * i v += Borders.mouse_speed / Settings.frame_additional_scale * i return v
def in_borders(coordinates): a = True v = [] c = coordinates if c.x < Borders.coordinates.x: a = False v.append(Coordinates(1, 0)) if c.x > Borders.coordinates.x + Borders.a: a = False v.append(Coordinates(-1, 0)) if c.y < Borders.coordinates.y: a = False v.append(Coordinates(0, 1)) if c.y > Borders.coordinates.y + Borders.b: a = False v.append(Coordinates(0, -1)) return a, v
def recenter(self): if KeyLogger.bindings['recenter'].check(): if World.main_character is not None: print(World.main_character.get_coordinates()) Frame.default_coordinates = Coordinates(375, 275) / Settings.frame_additional_scale -\ World.main_character.get_coordinates() return True return False