def on_update(self, delta): # print('Updating physics') self.frame__time += delta # Gravitation är alltid neråt -ay self.ay += (self.phys['gravity'] * -0.1) # vx,vy ändras utifrån ax,ay self.vx, self.vy = lerp(self.vx, self.ax, self.accspeed), lerp(self.vy, self.ay, self.accspeed) if (self.position[1] <= self.height / 2 + 5 and self.vy < 0): # Om Elon är längst ner, self.vy = 0 #...sätt hastighet y-led till 0 self.position[1] = self.height / 2 #...sätt position y-led till 0 # print("Hastighet y-led:", self.vy) self.position = [ self._position[0] + self.vx, self._position[1] + self.vy ] self.angle += self.change_angle self.ay = 0 self.ax = 0
def update(self): super().update() if self.lifetime_elapsed <= self.in_duration: u = self.lifetime_elapsed / self.in_duration self.alpha = arcade.lerp(self.start_alpha, self.mid_alpha, u) else: u = (self.lifetime_elapsed - self.in_duration) / self.out_duration self.alpha = arcade.lerp(self.mid_alpha, self.end_alpha, u)
def update(self, delta_time): target_left = self.player.center_x - SCREEN_WIDTH / 2 target_bottom = self.player.center_y - SCREEN_HEIGHT / 2 self.left = arcade.lerp(self.left, target_left, delta_time * 2) self.bottom = arcade.lerp(self.bottom, target_bottom, delta_time * 2) arcade.set_viewport(self.left, SCREEN_WIDTH + self.left, self.bottom, SCREEN_HEIGHT + self.bottom)
def on_update(self, deltatime): """ Funktion som körs i början av varje frame, varje "tick". Updaterar alla modeller, och dess värden. """ super().on_update(deltatime) self.dx = arcade.lerp(self.dx, self.ddx * self.speed, 0.05) self.dy = arcade.lerp(self.dy, self.ddy * self.speed, 0.05) self.contain_ball() self.pos_x += self.dx self.pos_y += self.dy
def on_update(self, deltatime): # 0 - 360 self.rotation += self.dangle ddx = -math.sin(math.radians(self.rotation)) ddy = math.cos(math.radians(self.rotation)) self.dx = arcade.lerp(self.dx, ddx, 0.05) self.dy = arcade.lerp(self.dy, ddy, 0.05) self.speed = arcade.lerp(self.speed, self.force, 0.05) self.player.change_x = self.dx * self.speed self.player.change_y = self.dy * self.speed self.player.angle = self.rotation self.player.update()
def pan_camera_to_user(self, panning_fraction: float = 1.0): """ Manage Scrolling :param panning_fraction: Number from 0 to 1. Higher the number, faster we pan the camera to the user. """ # This spot would center on the user screen_center_x = self.player_sprite.center_x - SCREEN_WIDTH / 2 screen_center_y = self.player_sprite.center_y - SCREEN_HEIGHT / 2 if screen_center_y < 0: screen_center_y = 0 user_centered = screen_center_x, screen_center_y cur_scroll = self.camera.scroll new_scroll = [arcade.lerp(cur_scroll[0], user_centered[0], panning_fraction), \ arcade.lerp(cur_scroll[1], user_centered[1], panning_fraction)] # Add in camera shake self.shake_offset_1 += self.shake_vel_1 self.shake_offset_2 += self.shake_vel_2 if self.shake_offset_1 > 0: self.shake_vel_1 -= 1 elif self.shake_offset_1 < 0: self.shake_vel_1 += 1 if self.shake_offset_2 > 0: self.shake_vel_2 -= 1 elif self.shake_offset_2 < 0: self.shake_vel_2 += 1 self.shake_vel_1 *= 0.9 self.shake_vel_2 *= 0.9 if abs(self.shake_vel_1) < 0.5 and abs(self.shake_offset_1) < 0.5: self.shake_vel_1 = 0 self.shake_offset_1 = 0 if abs(self.shake_vel_2) < 0.5 and abs(self.shake_offset_2) < 0.5: self.shake_vel_2 = 0 self.shake_offset_2 = 0 new_scroll[0] += self.shake_offset_1 new_scroll[1] += self.shake_offset_2 self.camera.scroll = new_scroll
def make_soft_square_texture(size: int, color: Color, center_alpha: int = 255, outer_alpha: int = 0) -> Texture: """ Return a :class:`Texture` of a square with the given diameter and color, fading out at its edges. :param int size: Diameter of the square and dimensions of the square Texture returned. :param Color color: Color of the square. :param int center_alpha: Alpha value of the square at its center. :param int outer_alpha: Alpha value of the square at its edges. :returns: New :class:`Texture` object. """ bg_color = (0, 0, 0, 0) # fully transparent img = PIL.Image.new("RGBA", (size, size), bg_color) draw = PIL.ImageDraw.Draw(img) half_size = int(size // 2) for cur_size in range(0, half_size): alpha = int(lerp(outer_alpha, center_alpha, cur_size / half_size)) clr = (color[0], color[1], color[2], alpha) # draw.ellipse((center-radius, center-radius, center+radius, center+radius), fill=clr) draw.rectangle((cur_size, cur_size, size - cur_size, size - cur_size), clr, None) name = "{}:{}:{}:{}:{}".format( "gradientsquare", size, color, center_alpha, outer_alpha) # name must be unique for caching return Texture(name, img)
def update(self): self.angle += self.dangle ddx = -math.sin(math.radians(self.angle)) ddy = math.cos(math.radians(self.angle)) self.velx = arcade.lerp(self.velx, ddx, 0.05) self.vely = arcade.lerp(self.vely, ddy, 0.05) self.speed = arcade.lerp(self.speed, self.dspeed, 0.05) self.change_x = self.velx * self.speed self.change_y = self.vely * self.speed super().update()
def make_soft_square_texture(size: int, color: Color, center_alpha: int = 255, outer_alpha: int = 0) -> Texture: """ Return a Texture of a circle with given diameter and color, fading out at the edges. Args: :diameter (int): Diameter of the circle and dimensions of the square Texture returned :color (Color): Color of the circle Returns: The new texture. Raises: None """ bg_color = (0, 0, 0, 0) # fully transparent img = PIL.Image.new("RGBA", (size, size), bg_color) draw = PIL.ImageDraw.Draw(img) half_size = int(size // 2) for cur_size in range(0, half_size): alpha = int(lerp(outer_alpha, center_alpha, cur_size / half_size)) clr = (color[0], color[1], color[2], alpha) # draw.ellipse((center-radius, center-radius, center+radius, center+radius), fill=clr) draw.rectangle((cur_size, cur_size, size - cur_size, size - cur_size), clr, None) name = "{}:{}:{}:{}".format("gradientsquare", size, color, center_alpha, outer_alpha) # name must be unique for caching return Texture(name, img)
def make_soft_circle_texture(diameter: int, color: Color, center_alpha: int = 255, outer_alpha: int = 0) -> Texture: """ Return a Texture of a circle with given diameter, color, and alpha values at its center and edges Args: :diameter (int): Diameter of the circle and dimensions of the square Texture returned :color (Color): Color of the circle :center_alpha (int): alpha value of circle at its center :outer_alpha (int): alpha value of circle at its edge Returns: A Texture object Raises: None """ # TODO: create a rectangle and circle (and triangle? and arbitrary poly where client passes # in list of points?) particle? bg_color = (0, 0, 0, 0) # fully transparent img = PIL.Image.new("RGBA", (diameter, diameter), bg_color) draw = PIL.ImageDraw.Draw(img) max_radius = int(diameter // 2) center = max_radius # for readability for radius in range(max_radius, 0, -1): alpha = int(lerp(center_alpha, outer_alpha, radius / max_radius)) clr = (color[0], color[1], color[2], alpha) draw.ellipse((center - radius, center - radius, center + radius - 1, center + radius - 1), fill=clr) name = "{}:{}:{}:{}:{}".format( "soft_circle_texture", diameter, color, center_alpha, outer_alpha) # name must be unique for caching return Texture(name, img)
def rocket_smoke_mutator(emitter: arcade.Emitter): emitter.scale = arcade.lerp(0.5, 3.0, emitter.lifetime_elapsed/emitter.lifetime_original) # A Sprite's scale doesn't affect generated textures (ex: make_soft_circle_texture) or scale being animated over time. # The fix below is copied from Sprite.update_animation(). # Bug may have been recorded here: https://github.com/pvcraven/arcade/issues/331 emitter.width = emitter._texture.width * emitter.scale emitter.height = emitter._texture.height * emitter.scale
def make_soft_circle_texture(diameter: int, color: Color, center_alpha: int = 255, outer_alpha: int = 0) -> Texture: """ Return a :class:`Texture` of a circle with the given diameter and color, fading out at its edges. :param int diameter: Diameter of the circle and dimensions of the square :class:`Texture` returned. :param Color color: Color of the circle. :param int center_alpha: Alpha value of the circle at its center. :param int outer_alpha: Alpha value of the circle at its edges. :returns: New :class:`Texture` object. """ # TODO: create a rectangle and circle (and triangle? and arbitrary poly where client passes # in list of points?) particle? bg_color = (0, 0, 0, 0) # fully transparent img = PIL.Image.new("RGBA", (diameter, diameter), bg_color) draw = PIL.ImageDraw.Draw(img) max_radius = int(diameter // 2) center = max_radius # for readability for radius in range(max_radius, 0, -1): alpha = int(lerp(center_alpha, outer_alpha, radius / max_radius)) clr = (color[0], color[1], color[2], alpha) draw.ellipse((center - radius, center - radius, center + radius - 1, center + radius - 1), fill=clr) name = "{}:{}:{}:{}:{}".format( "soft_circle_texture", diameter, color, center_alpha, outer_alpha) # name must be unique for caching return Texture(name, img)
def rocket_smoke_mutator(particle: arcade.LifetimeParticle): particle.scale = arcade.lerp( 0.5, 3.0, particle.lifetime_elapsed / particle.lifetime_original) # A Sprite's scale doesn't affect generated textures # (ex: make_soft_circle_texture) or scale being animated over time. # The fix below is copied from Sprite.update_animation(). # Bug may have been recorded here: https://github.com/pvcraven/arcade/issues/331 particle.width = particle._texture.width * particle.scale particle.height = particle._texture.height * particle.scale
def on_update(self, delta_time): """ All the logic to move, and the game logic goes here. Normally, you'll call update() on the sprite lists that need it. """ self.physics_engine.update() self.update_player_room() target_x, target_y = self.camera_target cam_x, cam_y = self.current_camera_pos if abs(target_x - cam_x) > 1 or abs(target_y - cam_y) > 1: cam_x = arcade.lerp(cam_x, target_x, 0.1) cam_y = arcade.lerp(cam_y, target_y, 0.1) self.current_camera_pos = cam_x, cam_y self.set_viewport(cam_x, cam_x + SCREEN_WIDTH, cam_y, cam_y + SCREEN_HEIGHT)
def update(self, cx, cy): self.duration += 1 self.center_x, self.center_y = cx, cy + SCREEN_HEIGHT / 4 if self.duration <= 150: self.scale = arcade.lerp(self.scale, 1, 0.05) self.angle = math.sin(self.duration / 32) * 5 else: self.angle = math.sin(self.duration / 32) * 5 self.alpha = self.alpha - 10 if self.alpha != 5 else 0
def draw(self): self.dist += 1 self.center_y = arcade.lerp(self.center_y, self.c, 0.3) arcade.draw_text(str(self.text), self.center_x, self.center_y, (self.c1, self.c2, self.c3, self.c4), font_size=self.size, font_name="mplus-2c-bold.ttf", anchor_x="center") if self.dist > 15 and not self.c4 - 38 < 1: self.c4 -= 38
def make_vignette(diameter: int, color: arcade.Color, vignette_radius, center_alpha: int = 255, outer_alpha: int = 255): """ Returns an arcade.Texture object of the vignette texture produced. Notes ----- This is an adaptation of arcade.make_soft_sircle_texture. https://arcade.academy/_modules/arcade/texture.html#make_soft_circle_texture In our implementation, we want the surrounding vignette to be filled, not empty. Otherwise we won't be blocking any vision for the drill to see. Parameters ---------- diameter : int The diameter of the whole image. color : arcade.color The color for the vignette. vignette_radius : int The radius of the vignette. Must be less than or equal to the diameter // 2. Points in the image beyond this vignette_radius are filled with the color and alpha value specified in outer_alpha center_alpha : int Alpha color for the circle at its center point. Linearly interpolated between this and the outer_alpha. outer_alpha : int Alpha color for the circle at its outer point. Linearly interpolated between this and the inner_alpha. Returns ------- arcade.Texture The texture with draw_scaled method. """ max_radius = diameter // 2 assert vignette_radius <= max_radius bg_colour = (0, 0, 0, 0) # Make background transparent. image = PIL.Image.new("RGBA", (diameter, diameter), bg_colour) draw = PIL.ImageDraw.Draw(image) for point in range(max_radius, 0, -1): if point < vignette_radius: alpha = int(arcade.lerp(center_alpha, outer_alpha, point / vignette_radius)) point_color = (color[0], color[1], color[2], alpha) draw.ellipse((max_radius - point, max_radius - point, max_radius + point - 1, max_radius + point - 1), fill=point_color) else: point_color = (color[0], color[1], color[2], outer_alpha) draw.rectangle((max_radius - point, max_radius - point, max_radius + point - 1, max_radius + point - 1), fill=point_color) name = f"vignette_circle_texture:{diameter}:{color}:{center_alpha}:{outer_alpha}" return arcade.Texture(name, image)
def on_update(self, delta_time): self.physics_engine.update() self.game_timer += delta_time self.fps = 1 / delta_time self.musicdt += delta_time if self.musicdt >= 59: self.music.play(volume=0.01) self.musicdt = 0 self.delta_track += delta_time if self.delta_track >= 5: if self.coin_counter >= 1: self.coin_counter -= 1 self.delta_track = 0 if self.got_potion_3: self.delta_track2 += delta_time if self.delta_track2 >= 1: for lava in self.super_lava: if lava.right < 96 * 105: lava.change_x = 5 self.delta_track2 = 0 self.super_lava.update() coin_hit_list = arcade.check_for_collision_with_list( self.player_sprite, self.coin_list) coin_2_hit_list = arcade.check_for_collision_with_list( self.player_sprite, self.coin_2_list) coin_5_hit_list = arcade.check_for_collision_with_list( self.player_sprite, self.coin_5_list) coin_secret_hit_list = arcade.check_for_collision_with_list( self.player_sprite, self.coin_secret_list) for coin in coin_hit_list: coin.remove_from_sprite_lists() self.collect_coin_sound.play(volume=0.01) self.coin_counter += 1 for coin in coin_2_hit_list: coin.remove_from_sprite_lists() self.collect_coin_sound.play(volume=0.01) self.coin_counter += 2 for coin in coin_5_hit_list: coin.remove_from_sprite_lists() self.collect_coin_sound.play(volume=0.01) self.coin_counter += 5 for coin in coin_secret_hit_list: coin.remove_from_sprite_lists() self.collect_coin_sound.play(volume=0.01) self.coin_counter += 5 self.player_sprite.change_x = 0 if arcade.check_for_collision_with_list(self.player_sprite, self.dont_touch_list): self.player_sprite.change_x = 0 self.player_sprite.change_y = 0 self.player_sprite.center_x = 40 * 96 self.player_sprite.center_y = 15 * 96 self.coin_counter = self.coin_counter // 2 if arcade.check_for_collision_with_list(self.player_sprite, self.super_lava): self.player_sprite.change_x = 0 self.player_sprite.change_y = 0 self.player_sprite.center_x = 40 * 96 self.player_sprite.center_y = 15 * 96 self.coin_counter = self.coin_counter // 2 if not self.should_be_in_menu: if self.left_pressed and not self.right_pressed: self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED if self.right_pressed and not self.left_pressed: self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED # Go To Shop if self.player_sprite.right <= 96 * 37 and self.player_sprite.left >= 31 * 96 and self.player_sprite.top <= 16 * 96 and self.player_sprite.bottom >= 12 * 96: self.draw_shop_tip = True else: self.draw_shop_tip = False # Leave Shop if self.player_sprite.left >= 88 * 96 and self.player_sprite.right <= 94 * 96 and self.player_sprite.top <= 16 * 96 and self.player_sprite.bottom >= 12 * 96: self.draw_back_tip = True else: self.draw_back_tip = False # Go To Jungle Secret Thing if self.player_sprite.left >= 73 * 96 and self.player_sprite.right <= 79 * 96 and self.player_sprite.top <= 24 * 96 and self.player_sprite.bottom >= 21 * 96: self.draw_jungle_tp = True else: self.draw_jungle_tp = False # Go Away From Jungle 1 if self.player_sprite.left >= 85 * 96 and self.player_sprite.right <= 90 * 96 and self.player_sprite.top <= 62 * 96 and self.player_sprite.bottom >= 57 * 96: self.draw_door_1_tip = True else: self.draw_door_1_tip = False # Go Away From Jungle 2 if self.player_sprite.left >= 90 * 96 and self.player_sprite.right <= 95 * 96 and self.player_sprite.top <= 62 * 96 and self.player_sprite.bottom >= 57 * 96: self.draw_door_2_tip = True else: self.draw_door_2_tip = False # Go Away From Jungle 3 if self.player_sprite.left >= 96 * 96 and self.player_sprite.right <= 100 * 96 and self.player_sprite.top <= 62 * 96 and self.player_sprite.bottom >= 57 * 96: self.draw_door_3_tip = True else: self.draw_door_3_tip = False # Get Potion 1 if self.player_sprite.left >= 88 * 96 and self.player_sprite.right <= 94 * 96 and self.player_sprite.top <= 21 * 96 and self.player_sprite.bottom >= 18 * 96: self.draw_potion_1_tip = True else: self.draw_potion_1_tip = False # Get Potion 2 if self.player_sprite.left >= 88 * 96 and self.player_sprite.right <= 94 * 96 and self.player_sprite.top <= 26 * 96 and self.player_sprite.bottom >= 23 * 96: self.draw_potion_2_tip = True else: self.draw_potion_2_tip = False # Get Potion 3 if self.player_sprite.left >= 88 * 96 and self.player_sprite.right <= 94 * 96 and self.player_sprite.top <= 31 * 96 and self.player_sprite.bottom >= 28 * 96: self.draw_potion_3_tip = True else: self.draw_potion_3_tip = False # Secret Tip if self.player_sprite.left >= 81 * 96 and self.player_sprite.top <= 4 * 96 and self.player_sprite.bottom >= 0: self.draw_secret_tip = True else: self.draw_secret_tip = False # Go To Second Part if self.player_sprite.left >= 69 * 96 and self.player_sprite.right <= 74 * 96 and self.player_sprite.top <= 32 * 96 and self.player_sprite.bottom >= 27 * 96: self.draw_portal_tip = True else: self.draw_portal_tip = False if self.game_timer >= self.previous_time and self.run == 2 and not self.drew_game_over: self.music.stop() window = arcade.get_window() if self.coin_counter > self.previous_coins: window.show_view(YouWin(self.coin_counter)) else: window.show_view(GameOver()) self.player_list.update_animation() self.potion_list.update_animation() self.portal_list.update_animation() self.view_left = int( arcade.lerp(self.view_left, self.player_sprite.center_x - SCREEN_WIDTH / 2, CAMERA_FOLLOW_SPEED)) self.view_bottom = int( arcade.lerp(self.view_bottom, self.player_sprite.center_y - SCREEN_HEIGHT / 2, CAMERA_FOLLOW_SPEED)) if not self.should_be_in_menu: arcade.set_viewport(self.view_left, self.view_left + SCREEN_WIDTH, self.view_bottom, self.view_bottom + SCREEN_HEIGHT)
def test_lerp(): assert arcade.lerp(2.0, 4.0, 0.75) == approx(3.5)
def rocket_smoke_mutator(particle: arcade.LifetimeParticle): particle.scale = arcade.lerp( 0.5, 3.0, particle.lifetime_elapsed / particle.lifetime_original)
def _lerp_color(start_color: Color, end_color: Color, u: float) -> Color: return (int(lerp(start_color[0], end_color[0], u)), int(lerp(start_color[1], end_color[1], u)), int(lerp(start_color[2], end_color[2], u)))