赞
踩
""" Work with a mini-map Artwork from https://kenney.nl If Python and Arcade are installed, this example can be run from the command line with: python -m arcade.examples.minimap """ import random from uuid import uuid4 import arcade from pyglet.math import Vec2 SPRITE_SCALING = 0.5 DEFAULT_SCREEN_WIDTH = 800 DEFAULT_SCREEN_HEIGHT = 600 SCREEN_TITLE = "Minimap Example" # How many pixels to keep as a minimum margin between the character # and the edge of the screen. VIEWPORT_MARGIN = 220 # How fast the camera pans to the player. 1.0 is instant. CAMERA_SPEED = 0.1 # How fast the character moves PLAYER_MOVEMENT_SPEED = 7 # Background color must include an alpha component MINIMAP_BACKGROUND_COLOR = arcade.get_four_byte_color(arcade.color.ALMOND) MINIMAP_WIDTH = 256 MINIMAP_HEIGHT = 256 MAP_WIDTH = 2048 MAP_HEIGHT = 2048 class MyGame(arcade.Window): """ Main application class. """ def __init__(self, width, height, title): """ Initializer """ super().__init__(width, height, title, resizable=True) # Sprite lists self.player_list = None self.wall_list = None # Mini-map related # List of all our minimaps (there's just one) self.minimap_sprite_list = None # Texture and associated sprite to render our minimap to self.minimap_texture = None self.minimap_sprite = None # Set up the player self.player_sprite = None self.physics_engine = None # Camera for sprites, and one for our GUI self.camera_sprites = arcade.Camera(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT) self.camera_gui = arcade.Camera(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT) def setup(self): """ Set up the game and initialize the variables. """ # Sprite lists self.player_list = arcade.SpriteList() self.wall_list = arcade.SpriteList() # Set up the player self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/" "femalePerson_idle.png", scale=0.4) self.player_sprite.center_x = 256 self.player_sprite.center_y = 512 self.player_list.append(self.player_sprite) # -- Set up several columns of walls for x in range(0, MAP_WIDTH, 210): for y in range(0, MAP_HEIGHT, 64): # Randomly skip a box so the player can find a way through if random.randrange(5) > 0: wall = arcade.Sprite(":resources:images/tiles/grassCenter.png", SPRITE_SCALING) wall.center_x = x wall.center_y = y self.wall_list.append(wall) self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list) # Set the background color arcade.set_background_color(arcade.color.AMAZON) # Construct the minimap size = (MINIMAP_WIDTH, MINIMAP_HEIGHT) self.minimap_texture = arcade.Texture.create_empty(str(uuid4()), size) self.minimap_sprite = arcade.Sprite(center_x=MINIMAP_WIDTH / 2, center_y=self.height - MINIMAP_HEIGHT / 2, texture=self.minimap_texture) self.minimap_sprite_list = arcade.SpriteList() self.minimap_sprite_list.append(self.minimap_sprite) def update_minimap(self): proj = 0, MAP_WIDTH, 0, MAP_HEIGHT with self.minimap_sprite_list.atlas.render_into(self.minimap_texture, projection=proj) as fbo: fbo.clear(MINIMAP_BACKGROUND_COLOR) self.wall_list.draw() self.player_sprite.draw() def on_draw(self): """ Render the screen. """ # This command has to happen before we start drawing self.clear() # Select the camera we'll use to draw all our sprites self.camera_sprites.use() # Draw all the sprites. self.wall_list.draw() self.player_list.draw() # Select the (unscrolled) camera for our GUI self.camera_gui.use() # Update the minimap self.update_minimap() # Draw the minimap self.minimap_sprite_list.draw() # Draw the GUI arcade.draw_rectangle_filled(self.width // 2, 20, self.width, 40, arcade.color.ALMOND) text = f"Scroll value: {self.camera_sprites.position[0]:4.1f}, {self.camera_sprites.position[1]:4.1f}" arcade.draw_text(text, 10, 10, arcade.color.BLACK_BEAN, 20) def on_key_press(self, key, modifiers): """Called whenever a key is pressed. """ if key == arcade.key.UP: self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED elif key == arcade.key.DOWN: self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED elif key == arcade.key.LEFT: self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED elif key == arcade.key.RIGHT: self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED def on_key_release(self, key, modifiers): """Called when the user releases a key. """ if key == arcade.key.UP or key == arcade.key.DOWN: self.player_sprite.change_y = 0 elif key == arcade.key.LEFT or key == arcade.key.RIGHT: self.player_sprite.change_x = 0 def on_update(self, delta_time): """ Movement and game logic """ # Call update on all sprites (The sprites don't do much in this # example though.) self.physics_engine.update() # Scroll the screen to the player self.scroll_to_player() def scroll_to_player(self): """ Scroll the window to the player. """ # Scroll to the proper location position = Vec2(self.player_sprite.center_x - self.width / 2, self.player_sprite.center_y - self.height / 2) self.camera_sprites.move_to(position, CAMERA_SPEED) def on_resize(self, width, height): """ Resize window Handle the user grabbing the edge and resizing the window. """ self.camera_sprites.resize(int(width), int(height)) self.camera_gui.resize(int(width), int(height)) def main(): """ Main function """ window = MyGame(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, SCREEN_TITLE) window.setup() arcade.run() if __name__ == "__main__": main()
这是一个使用Arcade库创建的小地图示例。在这个例子中,我们使用一个小地图来显示主场景的完整地图,并在小地图上的显示圈来跟踪玩家的位置。
代码的第一部分是一些全局变量的定义,包括一些常量和一些用于设置屏幕和小地图尺寸的变量。
接下来是一个名为MyGame
的类,这是整个游戏的主要类。其中包含了游戏的初始化和主要逻辑。
MyGame
类的__init__
方法是类的初始化方法。其中包含了一些属性的初始化和一些对象的创建。
setup
方法用于设置游戏的初始状态,包括创建精灵列表、创建玩家精灵、创建墙精灵等。
update_minimap
方法用于更新小地图。这个方法使用FBO(帧缓冲对象)来渲染场景并更新小地图的纹理。
on_draw
方法是一个事件处理方法,用于在窗口上绘制所有的精灵和GUI元素。
on_key_press
和on_key_release
方法是事件处理方法,用于处理键盘按键的操作。
scroll_to_player
方法用于将窗口滚动到玩家的位置。
on_resize
方法是一个事件处理方法,用于在窗口大小改变时重新调整相机的大小。
最后,main函数是程序的入口函数,用于创建游戏对象并运行游戏。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。