arcade全屏和窗口模式切换示例

arcade全屏和窗口模式切换示例

"""
全屏示例,让角色在一个大屏幕内滚动 
"""

import arcade
import os

SPRITE_SCALING = 0.5

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "全屏示例"

# How many pixels to keep as a minimum margin between the character
# and the edge of the screen.
VIEWPORT_MARGIN = 40

MOVEMENT_SPEED = 5


class MyGame(arcade.Window):
    """ Main application class. """

    def __init__(self):
        """
        Initializer
        """
        # 以全屏幕模式打开窗口,如果不想,则移去fullscreen这个参数
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE, fullscreen=True)

        # 设置工作目录
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        # This will get the size of the window, and set the viewport to match.
        # So if the window is 1000x1000, then so will our viewport. If
        # you want something different, then use those coordinates instead.
        width, height = self.get_size()
        self.set_viewport(0, width, 0, height)
        arcade.set_background_color(arcade.color.AMAZON)
        self.example_image = arcade.load_texture("images/boxCrate_double.png")

    def on_draw(self):
        """
        Render the screen.
        """

        arcade.start_render()

        # 得到视区尺寸
        left, screen_width, bottom, screen_height = self.get_viewport()

        # Draw text on the screen so the user has an idea of what is happening
        arcade.draw_text("按F键在全屏和非全屏之间切换,不拉伸",
                         screen_width // 4, screen_height // 2, arcade.color.WHITE, 24, width=300, anchor_x="center")
        arcade.draw_text("按s键在全屏和窗口模式之间切换, 拉伸",
                         screen_width // 4 + screen_width // 2, screen_height // 2,
                         arcade.color.WHITE, 24, width=300, anchor_x="center")

        # 在底部画盒子
        for x in range(64, 800, 128):
            y = 64
            width = 128
            height = 128
            arcade.draw_texture_rectangle(x, y, width, height, self.example_image)


    def on_key_press(self, key, modifiers):
        """Called whenever a key is pressed. """
        if key == arcade.key.F:
            # 如果按了f键,则切换
            self.set_fullscreen(not self.fullscreen)

            # Get the window coordinates. Match viewport to window coordinates
            # so there is a one-to-one mapping.
            width, height = self.get_size()
            self.set_viewport(0, width, 0, height)

        if key == arcade.key.S:
            # 如果按了s键,则切换
            self.set_fullscreen(not self.fullscreen)

            # Instead of a one-to-one mapping, stretch/squash window to match the
            # constants. This does NOT respect aspect ratio. You'd need to
            # do a bit of math for that.
            self.set_viewport(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT)


def main():
    """ Main method """
    MyGame()
    arcade.run()


if __name__ == "__main__":
    main()

 

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。