def on_update(self, delta_time): # Update cannonball position self.cannonballs.update() # TODO: Need to figure out these values max_height = 10000 max_width = 10000 for cannonball in self.cannonballs: if (cannonball._get_right() < 0 or cannonball._get_top() < 0 or cannonball._get_bottom() > max_height or cannonball._get_left() > max_width): cannonball.kill() # Scroll viewport with ship movement self.scroll() # Detect user input and change speed self.ship_controls(delta_time) # Animate ocean and sea foam layers self.animate_layers() # Engine update and collision detection self.ship_collision() self.play_audio() self.enemy_pathfinding(delta_time) self.enemy_list.on_update(delta_time) width, height = arcade.get_viewport()[1], arcade.get_viewport()[3] if self.player_ship.left < 0: self.player_ship.left = 0 elif self.player_ship.right > width - 1: self.player_ship.right = width - 1 if self.player_ship.bottom < 0: self.player_ship.bottom = 0 elif self.player_ship.top > height - 1: self.player_ship.top = height - 1 for enemy in self.enemy_list: enemy.cannonballs.on_update(delta_time) if arcade.get_distance_between_sprites(self.player_ship, enemy) < 100: enemy.fire_port = True enemy.fire_starboard = True cannonball_collision = len(arcade.check_for_collision_with_list( enemy, self.cannonballs)) > 0 if cannonball_collision and enemy.collision_time_diff > 1: enemy.health -= 20 enemy.collision_time = time.time()
def on_draw(self): arcade.start_render() self.camadafundo.draw() self.camadafundo2.draw() self.camadafundo3.draw() self.camadafundo4.draw() self.camadafundo5.draw() self.camadafundo6.draw() self.camadafundo7.draw() self.camadafundo8.draw() self.camadafundo9.draw() self.camadafundo10.draw() self.player_list.draw() arcade.draw_text(f"Plastico:{self.player_plastico}", arcade.get_viewport()[0] + 10, arcade.get_viewport()[2] + 800, arcade.color.BROWN, font_size=32) arcade.draw_text(f"Papel:{self.player_papel}", arcade.get_viewport()[0] + 10, arcade.get_viewport()[2] + 770, arcade.color.BROWN, font_size=32) arcade.draw_text(f"Vidro:{self.player_vidro}", arcade.get_viewport()[0] + 10, arcade.get_viewport()[2] + 740, arcade.color.BROWN, font_size=32) arcade.draw_text(f"Organico:{self.player_organico}", arcade.get_viewport()[0] + 10, arcade.get_viewport()[2] + 707, arcade.color.BROWN, font_size=32)
def on_draw(self): arcade.start_render() # Draw the background texture arcade.draw_texture_rectangle(MAP_WIDTH // 2, MAP_HEIGHT // 2, MAP_WIDTH, MAP_HEIGHT, self.background) self.ground_list.draw() self.streettop_list.draw() self.player_list.draw() self.bones_list.draw() self.enemy_list.draw() self.enemy2_list.draw() self.enemy3_list.draw() self.enemy4_list.draw() arcade.draw_text(f"Collected Bones: {self.collected_bones}", arcade.get_viewport()[0] + 10, arcade.get_viewport()[2] + 830, arcade.color.WHITE, font_size=20) arcade.draw_text(f"Lives: {self.lives}", arcade.get_viewport()[0] + 10, arcade.get_viewport()[2] + 860, arcade.color.WHITE, font_size=20)
def on_update(self, dt: float) -> None: """Run whenever the game screen is updated :param dt: Delta time between frames :type dt: float """ self.physics_engine.update() self.player.on_update(dt) if not self.tilemap.validate_zones(self.player.position): self.rendered_zones, self.collision_list = self.tilemap.load_tilemap( # noqa E501 self.player.position) self.physics_engine.walls = self.collision_list self.world_data["player"]["pos"] = list(self.player.position) current_viewport = arcade.get_viewport() view_x, view_y = 0, 0 if self.player.left < current_viewport[0] + 100: view_x = self.player.change_x if self.player.right > current_viewport[1] - 100: view_x = self.player.change_x if self.player.bottom < current_viewport[2] + 100: view_y = self.player.change_y if self.player.top > current_viewport[3] - 100: view_y = self.player.change_y arcade.set_viewport(current_viewport[0] + view_x, current_viewport[1] + view_x, current_viewport[2] + view_y, current_viewport[3] + view_y)
def update_player_movement(self, delta_time): # Calculate speed based on the keys pressed self.player_sprite.change_x = 0 self.player_sprite.change_y = 0 if self.up_pressed and not self.down_pressed: self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED * delta_time elif self.down_pressed and not self.up_pressed: self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED * delta_time if self.left_pressed and not self.right_pressed: self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED * delta_time elif self.right_pressed and not self.left_pressed: self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED * delta_time if self.player_sprite.health <= 0: self.player_sprite.change_x, self.player_sprite.change_y = 0, 0 # print(self.get_viewport()) width, height = arcade.get_viewport()[1:4:2] if self.player_sprite.left < 0: self.player_sprite.left = 0 elif self.player_sprite.right > width - 1: self.player_sprite.right = width - 1 if self.player_sprite.bottom < 0: self.player_sprite.bottom = 0 elif self.player_sprite.top > height - 1: self.player_sprite.top = height - 1
def on_mouse_press(self, x, y, button, modifiers): if self.visible: viewport_left, viewport_right, viewport_bottom, viewport_top = arcade.get_viewport() x = viewport_left + self.x + x y = viewport_bottom + self.y + y for component in self.components: component.on_mouse_press(x, y, button, modifiers)
def on_mouse_press(self, x: float, y: float, button: int, modifiers: int): if self.state == 0: for button in self.button_list_0: assert (isinstance(button, arcade.gui.TextButton)) button.check_mouse_press(x, y) elif self.state == 1: left, right, bottom, top = arcade.get_viewport() if button == arcade.MOUSE_BUTTON_LEFT: self.player.shooting = True for button2 in self.pause_list: if isinstance(button2, Button): button2.check_mouse_press(x + left, y + bottom) # Easter egg if self.pause: if (self.player.check_mouse_press(x + left, y + bottom)): self.contador += 1 if self.contador == 15: self.song.stop() self.song = arcade.Sound("./resources/music/song2.wav") self.volume = 0.01 arcade.Sound.play(self.song, self.volume) elif self.state == 2: for button in self.button_list_1: assert (isinstance(button, arcade.gui.TextButton)) button.check_mouse_press(x, y) elif self.state == 3: pass
def draw(self): # Draw the background texture (left, right, bottom, top) = viewport = arcade.get_viewport() arcade.draw_lrwh_rectangle_textured(left, bottom, SCREEN_WIDTH, SCREEN_HEIGHT, self.background) # Draw the tiles super().draw()
def update(self) -> None: #update player self.player.collisions.update() self.player.update_animation() # Enemy activation and update for enemy in reversed(self.enemy_list): distance = self.get_distance(enemy) enemy.collisions.update() if distance < 100: self.player.health -= (2 - self.player.armor) if (distance < 300): enemy.speed = Config.MONSTER_MOVEMENT_SPEED try: path = enemy.get_path(enemy.target.position) enemy.tick(path) except Exception: import traceback traceback.print_exc() else: left, right, bottom, top = arcade.get_viewport() if (enemy.bottom > bottom and enemy.top < bottom + Config.SCREEN_HEIGHT and enemy.right < left + Config.SCREEN_WIDTH and enemy.left > left): enemy.speed = 5 ran = random.randint(0, 1000) if ran > 950: print(ran) try: path = enemy.get_path(enemy.level.random()) enemy.tick(path) except Exception: import traceback traceback.print_exc()
def setup(self): """ Set up the game here. Call this function to restart the game. """ # Used to keep track of our scrolling self.view_bottom = 0 self.view_left = 0 #Keep track of health #TODO Do we want to have different health on different levels. #TODO switch health to be stored in character. # Create the Sprite lists self.player_list = arcade.SpriteList() self.wall_list = arcade.SpriteList() self.damage_list = arcade.SpriteList() self.tweet_list = arcade.SpriteList() self.tan_list = arcade.SpriteList() self.enemy_list = arcade.SpriteList() # Set up the player fallback image. image_source = ":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png" self.player = TrumpCharacter.PlayerCharacter(image_source) self.player_list.append(self.player) self.score = 0 self.player.health = 30 # --- Load in a map from the tiled editor --- self.map = self.maps[self.currentLevel]["map"] self.background = self.maps[self.currentLevel]["background"] self.wall_list = self.map.wall_list self.damage_list = self.map.damage_list for thing in self.map.tweet_list: self.tweet_list.append( item.AnimatedItem("images/items/Tweet/tweet", 16, thing.center_x, thing.center_y, scale=0.5)) for thing in self.map.tan_list: self.tan_list.append( item.AnimatedItem("images/items/Tan/tan", 14, thing.center_x, thing.center_y)) self.enemy_list = self.map.enemy_list self.physics_engine = arcade.PhysicsEnginePlatformer( self.player, self.wall_list, GRAVITY) for enemy in self.enemy_list: enemy.physics = arcade.PhysicsEnginePlatformer( enemy, self.wall_list, GRAVITY) self.base_viewport = arcade.get_viewport() print
def update_window(self): l, r, b, t = arcade.get_viewport() w, h = r - l, t - b self.l = l self.r = r self.b = b self.t = t self.w = w self.h = h
def test_window(twm): import arcade width = 800 height = 600 title = "My Title" resizable = True arcade.open_window(width, height, title, resizable) arcade.set_background_color(arcade.color.AMAZON) w = arcade.get_window() assert w is not None # Make sure the arguments get passed to the window if not twm: assert w.width == width assert w.height == height assert w.caption == title assert w.resizeable is resizable assert w.current_view is None arcade.set_window(w) w.background_color = 255, 255, 255, 255 assert w.background_color == (255, 255, 255, 255) w.set_mouse_visible(True) w.set_size(width, height) p = arcade.get_projection() assert isinstance(p, np.ndarray) v = arcade.get_viewport() if not twm: assert v[0] == 0 # The lines below fail. Why? assert v[1] == width - 1 assert v[2] == 0 assert v[3] == height - 1 factor = arcade.get_scaling_factor() assert factor > 0 factor = arcade.get_scaling_factor(w) assert factor > 0 arcade.start_render() arcade.finish_render() def f(): pass arcade.schedule(f, 1 / 60) arcade.pause(0.01) arcade.close_window() arcade.open_window(width, height, title, resizable) arcade.quick_run(0.01)
def relative_to_absolute(self, x: float, y: float): """ Converts relative coordinates to absolute ones. Coordinates provided by mouse events are relative to the current zoom, so for some uses (like determining what tile has been clicked) they need to be scaled and shifted. """ current = arcade.get_viewport() real_y = y * (current[3] - current[2]) / self.SCREEN_HEIGHT + current[2] - self.centering_y real_x = x * (current[1] - current[0]) / self.SCREEN_WIDTH + current[0] - self.centering_x return real_x, real_y
def get_dict(self): """ オブジェクトをjsonにダンプする為の辞書を作る関数 """ self.game_state = GAME_STATE.DELAY_WINDOW ############## player_dict = self.get_actor_dict(self.player) levels_dict = {} for map_name, level in self.stories.items(): actor_dict = [self.get_actor_dict(s) for s in level.actor_sprites] floor_dict = [self.get_actor_dict(s) for s in level.floor_sprites] wall_dict = [self.get_actor_dict(s) for s in level.wall_sprites] map_point_dict = [ self.get_actor_dict(s) for s in level.map_point_sprites ] dungeon_obj_dict = [ self.get_actor_dict(s) for s in level.map_obj_sprites ] item_dict = [self.get_actor_dict(s) for s in level.item_sprites] item_point_dict = [ self.get_actor_dict(s) for s in level.item_point_sprites ] level_dict = { "level": level.floor_level, "map_name": level.map_name, "actor": actor_dict, "floor": floor_dict, "wall": wall_dict, "map_point": map_point_dict, "dungeon_obj": dungeon_obj_dict, "item": item_dict, "item_point": item_point_dict, } levels_dict[map_name] = level_dict # ビューポートの位置情報を保存 viewport = arcade.get_viewport() result = { "player": player_dict, "viewport": viewport, "levels": levels_dict, "cur_level_name": f"{self.cur_level.map_name}{self.cur_level.floor_level}", } ############## self.action_queue.append({"message": "*save*"}) self.game_state = GAME_STATE.NORMAL print(f"**save**{result=}") return result
def on_mouse_release(self, x, y, button, modifiers): if self.should_be_in_menu: l, w, b, h = arcade.get_viewport() left = w / 2 - 150 right = w / 2 + 150 top = h / 2 + 25 bottom = h / 2 - 25 if bottom <= y <= top and left <= x <= right: self.window.close()
def draw(self): _cam = arcade.get_window().current_camera self.camera.use() self.strikeline.draw() vp = arcade.get_viewport() height = vp[3] - vp[2] arcade.set_viewport(0, Settings.width, -self.pixel_offset, -self.pixel_offset + height) self.sprite_buckets.draw(self.song_time) _cam.use()
def on_show(self): arcade.start_render() self.backup = arcade.get_viewport() arcade.set_viewport(0, self.window.width, 0, self.window.height) self.city_info.display(self.city) self.top_bar.adjust() self.ui_manager.add_ui_element(self.build_unit_button) self.ui_manager.add_ui_element(self.build_building_window)
def render(self) -> None: x = 0 for sprite in self.sprite_list: screen_right = arcade.get_viewport()[1] - 100 screen_top = arcade.get_viewport()[3] - 80 sprite.scale = 4 sprite.center_x = screen_right - x sprite.center_y = screen_top x += 70 sprite.draw() x = 0 for kill in self.kill_list: sprite = getattr(self, kill) screen_right = arcade.get_viewport()[1] - 240 screen_top = arcade.get_viewport()[3] - 150 sprite.scale = 4 sprite.center_x = screen_right + x sprite.center_y = screen_top x += 70 sprite.draw()
def on_show(self): arcade.start_render() self.backup = arcade.get_viewport() arcade.set_viewport(0, self.window.width, 0, self.window.height) self.city_info.display(self.city) self.top_bar.adjust() if self.city.owner.deputation is None: sidebar_top_backup = self.sidebar_top if self.city.owner not in self.me.enemies: # Can't buy city only if in war self.buy_city_button = BuyCityButton(self, self.center_x, (1 - self.sidebar_top) * self.window.height, self.sidebar_width, self.button_height) self.sidebar_top += self.relative_button_height + 0.025 self.ui_manager.add_ui_element(self.buy_city_button) if self.city.owner not in self.me.enemies: # Can't buy goods only if in war self.buy_goods_button = BuyGoodsButton(self, self.center_x, (1 - self.sidebar_top) * self.window.height, self.sidebar_width, self.button_height) self.sidebar_top += self.relative_button_height + 0.025 self.ui_manager.add_ui_element(self.buy_goods_button) if self.city.owner not in self.me.enemies and self.city.owner not in self.me.allies: # Can declare war only if neutral self.declare_war_button = DeclareWarButton(self, self.center_x, (1 - self.sidebar_top) * self.window.height, self.sidebar_width, self.button_height) self.sidebar_top += self.relative_button_height + 0.025 self.ui_manager.add_ui_element(self.declare_war_button) if self.city.owner in self.me.enemies: # Can only if enemies self.propose_peace_button = ProposePeaceButton(self, self.center_x, (1 - self.sidebar_top) * self.window.height, self.sidebar_width, self.button_height) self.sidebar_top += self.relative_button_height + 0.025 self.ui_manager.add_ui_element(self.propose_peace_button) if self.city.owner not in self.me.enemies and self.city.owner not in self.me.allies: # Can offer alliance only if neutral self.offer_alliance_button = OfferAllianceButton(self, self.center_x, (1 - self.sidebar_top) * self.window.height, self.sidebar_width, self.button_height) self.sidebar_top += self.relative_button_height + 0.025 self.ui_manager.add_ui_element(self.offer_alliance_button) if self.city.owner in self.me.allies: # Can end alliance only if in alliance (duh) self.end_alliance_button = EndAllianceButton(self, self.center_x, (1 - self.sidebar_top) * self.window.height, self.sidebar_width, self.button_height) self.sidebar_top += self.relative_button_height + 0.025 self.ui_manager.add_ui_element(self.end_alliance_button) self.sidebar_top = sidebar_top_backup else: pass # TODO Enemycityinfo
def adjust_coords(self): """ Adjusts the coords of the pop-up to the current screen borders. """ left, right, bottom, top = arcade.get_viewport() width = right - left height = top - bottom self.width = self.size_x * width self.height = self.size_y * height left += self.left * width bottom += self.bottom * height self.coords_lrtb = (left, left + self.width, bottom + self.height, bottom)
def draw(self): if self.visible: viewport_left, viewport_right, viewport_bottom, viewport_top = arcade.get_viewport() for component in self.components: component.center_x = viewport_left + self.x + component.initial_x component.center_y = viewport_top + self.y - component.initial_y component.draw() if self.text: arcade.draw_text(self.text, viewport_left + self.text_settings['x'], viewport_top - self.text_settings['y'], self.text_settings['color'], self.text_settings['font_size'])
def orb_in_view(orb): view = arcade.get_viewport() # view[0] = left, view[1] = right, view[2] = bottom, view[3] = top offset = int(orb.size / 2) view = (view[0] - offset, view[1] + offset, view[2] - offset, view[3] + offset) in_vert_view = orb.pos.x > view[0] and view[1] > orb.pos.x in_horz_view = orb.pos.y > view[2] and view[3] > orb.pos.y return in_vert_view and in_horz_view
def on_mouse_press(self, x, y, button, modifiers): if self.should_be_in_menu: l, w, b, h = arcade.get_viewport() left = w / 2 - 150 right = w / 2 + 150 top = h / 2 + 25 bottom = h / 2 - 25 if bottom <= y <= top and left <= x <= right: self.quit_button_color = [46, 114, 163, 255] else: self.quit_button_color = [54, 155, 227, 255]
def on_mouse_motion(self, x, y, dx, dy): if self.should_be_in_menu: l, w, b, h = arcade.get_viewport() left = w / 2 - 150 right = w / 2 + 150 top = h / 2 + 25 bottom = h / 2 - 25 if bottom <= y <= top and left <= x <= right: self.quit_button_color = [54, 138, 199, 255] else: self.quit_button_color = [54, 155, 227, 255]
def on_draw(self): arcade.start_render() self.talaj.draw() self.egyeb.draw() self.targyak.draw() self.player.draw() arcade.draw_text(f"collected: {self.collected}", arcade.get_viewport()[0] + 10, 680, arcade.color.GOLD, font_size=20)
def update(self, delta_time): super().update(delta_time) # --- Manage Scrolling --- # Track if we need to change the viewport changed = False # Scroll left left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN if self.pc_sprite.left < left_boundary: self.view_left -= left_boundary - self.pc_sprite.left changed = True # Scroll right right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN if self.pc_sprite.right > right_boundary: self.view_left += self.pc_sprite.right - right_boundary changed = True # Scroll up top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN if self.pc_sprite.top > top_boundary: self.view_bottom += self.pc_sprite.top - top_boundary changed = True # Scroll down bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN if self.pc_sprite.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.pc_sprite.bottom changed = True if changed: # Only scroll to integers. Otherwise we end up with pixels that # don't line up on the screen # Clamp the viewport to the level boundaries (vp_left, vp_right, vp_bottom, vp_top) = viewport = arcade.get_viewport() low_bottom = self.bottom high_bottom = self.top - (vp_top - vp_bottom) low_left = self.left high_left = self.right - (vp_right - vp_left) self.view_bottom = int( arcade.clamp(self.view_bottom, low_bottom, high_bottom)) self.view_left = int( arcade.clamp(self.view_left, low_left, high_left)) # Do the scrolling arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
def on_draw(self): # Clear the screen to the background color arcade.start_render() (left, _right, bottom, _top) = arcade.get_viewport() arcade.draw_lrwh_rectangle_textured(left, bottom, VIEWPORT_WIDTH, VIEWPORT_HEIGHT, self.background) # Draw our sprites self.environment.platforms.draw() self.environment.player.draw()
def on_draw(self): arcade.start_render() # draw services for s in self.services: s.draw() # draw mode info arcade.draw_xywh_rectangle_filled(0, 0, arcade.get_viewport()[1], 22, arcade.color.BLACK_BEAN) arcade.draw_text(self.mode.upper(), 5, 5, arcade.color.BLACK, bold=True) arcade.draw_text(' '.join(f'({m[0]}) {m[1]}' for m in self.modes.values()), arcade.get_viewport()[1] - 5, 5, arcade.color.BLACK, anchor_x='right')
def draw_map(bodies, window_width, window_height): map_shape_list = arcade.ShapeElementList() view = arcade.get_viewport() map_width = window_width / 3.5 map_height = window_height / 3.5 map_padding = window_height / 80 map_center_x = view[1] - (map_width / 2 + map_padding) map_center_y = view[2] + (map_height / 2 + map_padding) #view[0] = left, view[1] = right, view[2] = bottom, view[3] = top rec = arcade.create_rectangle_filled(map_center_x, map_center_y, map_width, map_height, arcade.color.BLACK) rec2 = arcade.create_rectangle_outline(map_center_x, map_center_y, map_width, map_height, arcade.color.WHITE, window_width / 400) map_shape_list.append(rec) map_shape_list.append(rec2) max_mass = 0 cm = None # find_center_of_mass(bodies) for bodie in bodies: if max_mass < bodie.mass: max_mass = bodie.mass cm = bodie.pos max_dist = 1 for bodie in bodies: this_dist = (cm - bodie.pos).get_mag() if max_dist < this_dist: max_dist = this_dist scaler = max_dist / ((map_height - map_padding) / 2) # print(scaler) # scaler = 60 for bodie in bodies: map_pos = (bodie.pos - cm) map_pos.x /= scaler map_pos.y /= scaler map_pos += Vec2D(map_center_x, map_center_y) map_shape_list.append( arcade.create_ellipse_filled(map_pos.x, map_pos.y, window_width / 300, window_width / 300, bodie.color)) # map_shpe_list.append( arcade. ) map_shape_list.draw()
def on_draw(self): """Draw textures.""" self.textures[self.state].draw_scaled(self.center_x, self.center_y) self.icon_texture.draw_scaled(self.center_x, self.center_y) if self.state == 'hover': x = self.right + self.tooltip_texture.width / 2 - 15 y = self.bottom - self.tooltip_texture.height / 2 + 15 _left, right, _top, _bottom = arcade.get_viewport() x = min(x, right - self.tooltip_texture.width / 2) self.tooltip_texture.draw_scaled(x, y) if self not in self.view.on_top: self.view.on_top.append(self) elif self in self.view.on_top: self.view.on_top.remove(self)