def __init__(self): self.frontal_area = 1 self.location = PVector(randrange(50, 750), randrange(15, 100)) self.velocity = PVector(0, 0) self.acceleration = PVector(0, 0) self.velocity_limit = False self.color = (randrange(0, 255), randrange(0, 255), randrange(0, 255)) self.mass = randrange(2, 10)
def loop(screen): screen.fill((0, 0, 0)) center = PVector(screen.get_width() // 2, screen.get_height() // 2) mouse_position = PVector(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) pygame.draw.line(screen, (255, 255, 255), tuple(center), tuple(mouse_position), 1) line = center - mouse_position s = pygame.Surface((line.magnitude(), 10)) s.fill((255, 255, 255)) screen.blit(s, (0, 0))
class Mover: def __init__(self): self.location = PVector(randrange(150, 300), randrange(150, 300)) self.velocity = PVector(0, 0) self.acceleration = PVector(0.1, 0.01) self.top_speed = 7 def step(self): # self.acceleration.random2D(5) self.velocity += self.acceleration self.velocity.limit(self.top_speed) self.location += self.velocity def check_edges(self, surface): if self.location.x > surface.get_width(): self.location.x = 0 elif self.location.x < 0: self.location.x = surface.get_width() if self.location.y > surface.get_height(): self.location.y = 0 elif self.location.y < 0: self.location.y = surface.get_height() def sprite(self): s = pygame.Surface((10, 10)) s.fill((255, 255, 255)) return s def acceleration_to(self, to): direction = to - self.location distance = direction.magnitude() direction.normalize() direction /= distance / 10 return direction def update(self, surface): self.check_edges(surface) mouse = pygame.mouse self.acceleration = self.acceleration_to( PVector(mouse.get_pos()[0], mouse.get_pos()[1])) self.step() surface.blit(self.sprite(), tuple(self.location))
def update(self, surface): self.check_edges(surface) mouse = pygame.mouse self.acceleration = self.acceleration_to( PVector(mouse.get_pos()[0], mouse.get_pos()[1])) self.step() surface.blit(self.sprite(), tuple(self.location))
def update(self, screen): pygame.draw.circle(screen, (255, 255, 255), tuple(self.location), 2) leap_chance = randrange(0, 10) self.velocity = PVector(randrange(-1, 2), randrange(-1, 2)) if leap_chance >= 5: self.velocity = self.direction random.seed() self.location += self.velocity
def loop(self, screen): screen.fill((0, 0, 0)) self.water.update(screen) noise = self.calculate_noise() # noise *= 3 for entity in self.entities: if entity.is_inside(self.water): entity.drag(self.water) wind = PVector(noise, 0) entity.apply_force(wind) gravity = PVector(0, .5 * entity.mass) entity.apply_force(gravity) friction = self.calculate_friction(entity.velocity) entity.apply_force(friction) entity.update(screen)
def test_normalize(self): v = PVector(3, 4) self.assertEqual(v.magnitude(), 5) v.normalize() self.assertEqual(v.magnitude(), 1)
def update(self, screen): random.seed() self.draw(screen) self.lock_target() move_chance = randrange(0, 10) if move_chance >= 7: self.velocity = PVector(randrange(-5, 5), randrange(-5, 5)) if self.target_locked: self.direction = self.direction_to(self.target) self.velocity = self.direction self.velocity *= randrange(1, 3) self.location += self.velocity
def update(self, screen): random.seed() self.draw(screen) self.find_target(ecosystem.entities.Frog.Frog) step = randrange(-1, 4) self.velocity = PVector(randrange(step - 2, step + 1), randrange(step - 2, step + 1)) self.velocity *= 3 if self.distance_to(self.target) < 100: self.velocity += self.direction_to(self.target) * -1 self.velocity *= randrange(1, 2) self.location += self.velocity
def test_iterator(self): self.assertEqual(tuple(PVector(2, 1)), (2, 1))
def __init__(self): self.location = PVector(randrange(150, 300), randrange(150, 300)) self.velocity = PVector(0, 0) self.acceleration = PVector(0.1, 0.01) self.top_speed = 7
def __init__(self, position, size, coefficient): self.location = PVector(position[0], position[1]) self.size = size self.coefficient = coefficient
def __init__(self): super().__init__() random.seed() self.location = PVector(randrange(200, 600), randrange(200, 600)) self.velocity = PVector(2, 2) self.direction = PVector(randrange(-1, 1), randrange(-1, 1))
def __init__(self): self.location = PVector(300, 150) self.velocity = PVector(2, 2)
def __init__(self): super().__init__() random.seed() self.velocity = PVector(0, 0)
def __init__(self): super().__init__() random.seed() self.location = PVector(randrange(50, 750), randrange(50, 550)) self.velocity = PVector(2, 2)
class Balloon: def __init__(self): self.frontal_area = 1 self.location = PVector(randrange(50, 750), randrange(15, 100)) self.velocity = PVector(0, 0) self.acceleration = PVector(0, 0) self.velocity_limit = False self.color = (randrange(0, 255), randrange(0, 255), randrange(0, 255)) self.mass = randrange(2, 10) def apply_force(self, force): self.acceleration += force / self.mass def update(self, screen): # helium = PVector(0, -1) # self.apply_force(helium) self.velocity += self.acceleration self.location += self.velocity self.acceleration *= 0 if self.velocity_limit is not False: self.velocity.limit(self.velocity_limit) self.draw_on_screen(screen) self.bounce_on_edges() def limit_velocity(self, limit): self.velocity_limit = limit def draw_on_screen(self, screen): draw_position = (int(self.location.x), int(self.location.y)) pygame.draw.circle(screen, self.color, draw_position, self.mass * 3) def bounce_on_edges(self): if self.location.y <= 10: self.location.y = 10 self.velocity.y *= -1 elif self.location.y >= WINDOW_HEIGHT - 50: self.location.y = WINDOW_HEIGHT - 50 self.velocity.y *= -1 if self.location.x <= 10: self.location.x = 10 self.velocity.x *= -1 elif self.location.x >= WINDOW_WIDTH - 50: self.location.x = WINDOW_WIDTH - 50 self.velocity.x *= -1 def is_inside(self, liquid): if liquid.location.x < self.location.x < liquid.location.x + liquid.size[ 0]: if liquid.location.y < self.location.y < liquid.location.y + liquid.size[ 1]: return True return False def drag(self, liquid): speed = self.velocity.magnitude() drag_magnitude = liquid.coefficient * speed * speed drag = copy.deepcopy(self.velocity) drag *= -1 drag.normalize() drag *= drag_magnitude drag *= self.frontal_area self.apply_force(drag)
def test_AddVectors(self): self.assertEqual(PVector(2, 2) + PVector(1, 1), PVector(3, 3))
def test_subtract_vector(self): self.assertEqual(PVector(5, 2) - PVector(3, 4), PVector(2, -2))
def __init__(self): self.location = PVector(randrange(200, 600), randrange(200, 600)) self.entities = [] self.direction = PVector(randrange(-1, 1), randrange(-1, 1)) self.target = PVector(randrange(100, 700), randrange(100, 500)) self.target_locked = False