def to_collision_rect(size: Point, position: Point = None, angle: float = 0) -> collision.Poly: if position is None: position = Point(0, 0) bot_left = collision.Vector(-size.x / 2, -size.y / 2) return collision.Poly(pos=collision.Vector(position.x, position.y), points=[ bot_left, bot_left + Point(0, size.y), bot_left + Point(size.x, size.y), bot_left + Point(size.x, 0), ], angle=angle)
def bounce(self, normal): """ r = self.direction - 2(self.direction . normal) * normal """ dot_product = self.direction.dot(normal) self.direction = (self.direction - collision.Vector((2 * dot_product) * normal.x, (2 * dot_product) * normal.y)).normalize()
def update_lasers(): for laser in Laser_Manager.laser_list: laser.update_pos() laser.coll_laser.pos = coll.Vector(laser.pos[0], laser.pos[1]) if laser.out_of_bounds(): Laser_Manager.remove_laser(laser) continue
def __init__(self, position, direction, color): self.pos = position self.dir = direction self.vel = [ -x_comp(Laser.speed, self.dir), -y_comp(Laser.speed, self.dir) ] self.color = color self.coll_laser = coll.Circle(coll.Vector(position[0], position[1]), Laser.radius)
def __init__(self, height, width, position): self.height = height self.width = width self.pos = position self.color = (255, 255, 255) self.verts = [ [self.pos[0] + self.width / 2, self.pos[1] + self.height / 2], [self.pos[0] + self.width / 2, self.pos[1] - self.height / 2], [self.pos[0] - self.width / 2, self.pos[1] - self.height / 2], [self.pos[0] - self.width / 2, self.pos[1] + self.height / 2] ] self.coll_obstacle = coll.Poly.from_box( coll.Vector(position[0], position[1]), width, height)
def __init__(self, surface, quarantine, sick, x, y, width, height, barriers=[]): self._status = Status.Healthy if not sick else Status.Sick self.recovering = False if not sick else True self.recovery_seconds = 0 self.total_recovery_seconds = random.randrange(0, MAX_RECOVERY_SECONDS) self.surface = surface self.quarantine = quarantine self.direction = self.random_direction() self.position = collision.Vector(x, y) self.id = uuid.uuid4() self.width = width self.height = height self.barriers = barriers
def update_pos(self, x_vel, y_vel): """updates the position of the ship based on its velocity""" self.pos[0] += x_vel self.pos[1] += y_vel def update_vert_pos(vertices): for point in vertices: point[0] += x_vel point[1] += y_vel self.coll_ship.pos = coll.Vector(self.pos[0], self.pos[1]) update_vert_pos(self.body_verts) if self.attack_mode: update_vert_pos(self.r_wing_verts) update_vert_pos(self.l_wing_verts) update_vert_pos(self.r_gun_verts) update_vert_pos(self.l_gun_verts)
def __init__(self, health, position, direction): self.hp = health self.pos = position self.dir = direction self.body_verts = [ [self.pos[0], self.pos[1] - 3 / 5 * Ship.height], [self.pos[0] + Ship.width / 2, self.pos[1] + 2 / 5 * Ship.height], [self.pos[0] - Ship.width / 2, self.pos[1] + 2 / 5 * Ship.height] ] self.init_attack_gear() self.vel = [0, 0] self.ang_vel = 0 self.lasers_fired = [] self.color = (255, 255, 255) self.gun_color = (100, 100, 100) self.laser_color = (255, 0, 0) self.fire_cooldown_count = 0 self.coll_ship = coll.Circle(coll.Vector(self.pos[0], self.pos[1]), Ship.height / 2)
def to_collision_vec(self) -> collision.Vector: return collision.Vector(self.x, self.y)
if toggle_mode_index != 4: P_sub = P_sub[P_sub[:, 4] == toggle_mode_index, :] ''' Now let's plot the info about all objects in the frame ''' # unique_obj=np.unique(P_sub[:,1]) for i in range(len(P_sub)): cx = P_sub[i, 2] cy = P_sub[i, 3] mode = P_sub[i, 4] Oid = P_sub[i, 1] if (mode == 2): # Pedestrians road_object[Oid] = [ mode, collision.Circle(collision.Vector(cx, cy), PEDS_RADIUS) ] cv2.circle(frame, (cx, cy), PEDS_RADIUS, (255, 0, 0), 2) if (mode == 1): # Cars road_object[Oid] = [ mode, collision.Poly.from_box(collision.Vector(cx, cy), CAR_SIZE * 2, CAR_SIZE * 2) ] cv2.rectangle(frame, (cx - CAR_SIZE, cy - CAR_SIZE), (cx + CAR_SIZE, cy + CAR_SIZE), (0, 0, 255), 2) if (mode == 3): # Cyclists road_object[Oid] = [
def __init__(self, radius: float, **kwargs): PhysicsEntity.__init__(self, **kwargs) self.radius = radius self.collision_shape = collision.Circle(collision.Vector(0, 0), self.radius)
def to_vector(self): return collision.Vector(self.x, self.y)
def velocity(self): if (self.quarantine): return collision.Vector(0, 0) return self.direction
def random_direction(self): return collision.Vector(self.random_amount(), self.random_amount()).normalize()
def set_pos(self, pos_): self.pos = pos_ self.colShape.pos = collision.Vector(pos_.x, pos_.y)
clock = pygame.time.Clock() #create pygame clock object mainloop = True FPS = 60 # desired max. framerate in frames per second. def quarantine(): return True if random.randint(0, 10) < 9 else False def sick(): return True if random.randint(0, 10) < 1 else False barriers = [ collision.Poly(collision.Vector(0, 0), [ collision.Vector(0, 0), collision.Vector(1, 0), collision.Vector(1, HEIGHT), collision.Vector(0, HEIGHT) ]), collision.Poly(collision.Vector(WIDTH, 0), [ collision.Vector(0, 0), collision.Vector(10, 0), collision.Vector(10, HEIGHT), collision.Vector(0, HEIGHT) ]), collision.Poly(collision.Vector(0, -10), [ collision.Vector(0, 0), collision.Vector(WIDTH, 0), collision.Vector(WIDTH, 10),