📜  Python Arcade – 添加相机

📅  最后修改于: 2022-05-13 01:54:57.147000             🧑  作者: Mango

Python Arcade – 添加相机

在本文中,我们将学习如何在Python为街机游戏添加相机。

添加相机

您可以使用 Camera()函数在街机中创建相机。

所以要使用这个相机,我们将创建一个新变量。



self.camera = None

然后在我们的 setup()函数,我们将使用 Camera()函数创建我们的相机。

self.camera= arcade.Camera(200,200)

在那之后,我们将使用camera.use函数在我们on_draw()函数的使用相机。

self.camera.use()

在下面的示例中,我们将创建 MainGame() 类。在 MainGame() 类中,首先我们将初始化速度、相机、场景、玩家的精灵和物理引擎的一些变量,然后我们将创建 5 个函数:

  • on_draw():在这个函数,我们将使用 arcade.start_render() 开始渲染,然后我们将绘制我们的场景。
  • setup():在这个函数,我们将调用 arcade.Scene() 和 arcade.Camera() 函数,然后我们将加载我们的精灵并将其存储在精灵列表中。
  • on_update():在这里我们将更新我们的玩家精灵和我们的物理引擎的 x 坐标。
  • on_key_press():在这个函数,我们将检查哪个键盘按钮被按下,我们将根据它改变速度变量的值。
  • on_key_release():在这个函数,我们将检查哪个键盘按钮被释放,我们将根据它改变速度变量的值。

使用的精灵:

下面是实现:

Python3
# Importing arcade module
import arcade
  
# Creating MainGame class
class MainGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 600, title="Player Movement")
  
        # Initializing a variable to store
        # the velocity of the player
        self.vel_x = 0
  
        # Creating variable for Camera
        self.camera = None
  
        # Creating scene object
        self.scene = None
  
        # Creating variable to store player sprite
        self.player = None
  
        # Creating variable for our game engine
        self.physics_engine = None
  
    # Creating on_draw() function to draw on the screen
    def on_draw(self):
        arcade.start_render()
        self.camera.use()
        # Drawing our scene
        self.scene.draw()
  
    def setup(self):
        
        # Initialize Scene object
        self.scene = arcade.Scene()
          
        # Using Camera() function
        self.camera = arcade.Camera(200, 200)
  
        # Creating different sprite lists
        self.scene.add_sprite_list("Player")
        self.scene.add_sprite_list("Platforms", 
                                   use_spatial_hash=True)
  
        # Adding player sprite
        self.player_sprite = arcade.Sprite("Player.png", 1)
  
        # Adding coordinates for the center of the sprite
        self.player_sprite.center_x = 64
        self.player_sprite.center_y = 600
  
        # Adding Sprite in our scene
        self.scene.add_sprite("Player", self.player_sprite)
  
        # Adding platform sprite
        platform = arcade.Sprite("Platform.png", 1)
          
        # Adding coordinates for the center of the platform
        platform.center_x = 300
        platform.center_y = 32
        self.scene.add_sprite("Platforms", platform)
  
        # Creating Physics engine
        self.physics_engine = arcade.PhysicsEnginePlatformer(
            self.player_sprite, self.scene.get_sprite_list("Platforms"), 0.5
        )
  
    # Creating on_update function to
    # update the x coordinate
    def on_update(self, delta_time):
  
        # Changing x coordinate of player
        self.player_sprite.center_x += self.vel_x * delta_time
          
        # Updating the physics engine to move the player
        self.physics_engine.update()
  
    # Creating function to change the velocity
    # when button is pressed
    def on_key_press(self, symbol, modifier):
  
        # Checking the button pressed
        # and changing the value of velocity
        if symbol == arcade.key.LEFT:
            self.vel_x = -300
        elif symbol == arcade.key.RIGHT:
            self.vel_x = 300
  
    # Creating function to change the velocity
    # when button is released
    def on_key_release(self, symbol, modifier):
  
        # Checking the button released
        # and changing the value of velocity
        if symbol == arcade.key.LEFT:
            self.vel_x = 0
        elif symbol == arcade.key.RIGHT:
            self.vel_x = 0
  
  
# Calling MainGame class
game = MainGame()
game.setup()
arcade.run()


Python3
# Importing arcade module
import arcade
  
# Creating MainGame class       
class MainGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 600, title="Player Movement")
  
        # Initializing a variable to store
        # the velocity of the player
        self.vel_x = 0
  
        # Creating variable for Camera
        self.camera = None
          
        # Creating scene object
        self.scene = None
  
        # Creating variable to store player sprite
        self.player = None
  
        # Creating variable for our game engine
        self.physics_engine = None
  
    # Creating on_draw() function to draw on the screen
    def on_draw(self):
        arcade.start_render()
  
        # Using the camera
        self.camera.use()
          
        # Drawing our scene
        self.scene.draw()
  
    def setup(self):
         # Initialize Scene object
        self.scene = arcade.Scene()
          
        # Using Camera() function
        self.camera= arcade.Camera(200,200)
  
        # Creating different sprite lists
        self.scene.add_sprite_list("Player")
        self.scene.add_sprite_list("Platforms",
                                   use_spatial_hash=True)
  
        # Adding player sprite
        self.player_sprite = arcade.Sprite("Player.png", 1)
  
        # Adding coordinates for the center of the sprite
        self.player_sprite.center_x = 64
        self.player_sprite.center_y = 600
  
        #Adding Sprite in our scene
        self.scene.add_sprite("Player",
                              self.player_sprite)
  
        # Adding platform sprite
        platform = arcade.Sprite("Platform.png", 1)
          
        # Adding coordinates for the center of the platform
        platform.center_x = 300
        platform.center_y = 32
        self.scene.add_sprite("Platforms", platform)
  
        # Creating Physics engine
        self.physics_engine = arcade.PhysicsEnginePlatformer(
            self.player_sprite, 
          self.scene.get_sprite_list("Platforms"), 0.5
        )
  
  
    # Creating on_update function to
    # update the x coordinate
    def on_update(self,delta_time):
  
        # Changing x coordinate of player
        self.player_sprite.center_x += self.vel_x * delta_time
          
        # Updating the physics engine to move the player
        self.physics_engine.update()
  
        # Calling the camera_move function
        self.camera_move()
  
          
    # Creating function to change the velocity
    # when button is pressed
    def on_key_press(self, symbol,modifier):
  
        # Checking the button pressed
        # and changing the value of velocity
        if symbol == arcade.key.LEFT:
            self.vel_x = -300
        elif symbol == arcade.key.RIGHT:
            self.vel_x = 300
  
    # Creating function to change the velocity
    # when button is released
    def on_key_release(self, symbol, modifier):
  
        # Checking the button released
        # and changing the value of velocity
        if symbol == arcade.key.LEFT:
            self.vel_x = 0
        elif symbol == arcade.key.RIGHT:
            self.vel_x = 0
  
    def camera_move(self):
        # Getting the x coordinate for the center of camera
        screen_x = self.player_sprite.center_x -
        (self.camera.viewport_width / 2)
          
        # Getting the y coordinate for the center of camera
        screen_y = self.player_sprite.center_y -
        (self.camera.viewport_height / 2)
  
        # Moving the camera
        self.camera.move_to([screen_x, screen_y])  
          
# Calling MainGame class       
game = MainGame()
game.setup()
arcade.run()


输出:



但是在这里你可以看到我们的相机没有随着我们的播放器移动。因此,要使用我们的播放器移动相机,我们必须创建一个函数。

移动相机

现在我们将创建一个新的 camera_move()函数来与我们的播放器一起移动我们的相机。然后,我们将调用我们的on_update()函数此函数。

camera_move():在这个函数,我们将根据玩家的位置计算相机中心的 x 和 y 坐标。然后我们将在 move_to()函数的帮助下移动我们的相机。

下面是实现:

蟒蛇3

# Importing arcade module
import arcade
  
# Creating MainGame class       
class MainGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 600, title="Player Movement")
  
        # Initializing a variable to store
        # the velocity of the player
        self.vel_x = 0
  
        # Creating variable for Camera
        self.camera = None
          
        # Creating scene object
        self.scene = None
  
        # Creating variable to store player sprite
        self.player = None
  
        # Creating variable for our game engine
        self.physics_engine = None
  
    # Creating on_draw() function to draw on the screen
    def on_draw(self):
        arcade.start_render()
  
        # Using the camera
        self.camera.use()
          
        # Drawing our scene
        self.scene.draw()
  
    def setup(self):
         # Initialize Scene object
        self.scene = arcade.Scene()
          
        # Using Camera() function
        self.camera= arcade.Camera(200,200)
  
        # Creating different sprite lists
        self.scene.add_sprite_list("Player")
        self.scene.add_sprite_list("Platforms",
                                   use_spatial_hash=True)
  
        # Adding player sprite
        self.player_sprite = arcade.Sprite("Player.png", 1)
  
        # Adding coordinates for the center of the sprite
        self.player_sprite.center_x = 64
        self.player_sprite.center_y = 600
  
        #Adding Sprite in our scene
        self.scene.add_sprite("Player",
                              self.player_sprite)
  
        # Adding platform sprite
        platform = arcade.Sprite("Platform.png", 1)
          
        # Adding coordinates for the center of the platform
        platform.center_x = 300
        platform.center_y = 32
        self.scene.add_sprite("Platforms", platform)
  
        # Creating Physics engine
        self.physics_engine = arcade.PhysicsEnginePlatformer(
            self.player_sprite, 
          self.scene.get_sprite_list("Platforms"), 0.5
        )
  
  
    # Creating on_update function to
    # update the x coordinate
    def on_update(self,delta_time):
  
        # Changing x coordinate of player
        self.player_sprite.center_x += self.vel_x * delta_time
          
        # Updating the physics engine to move the player
        self.physics_engine.update()
  
        # Calling the camera_move function
        self.camera_move()
  
          
    # Creating function to change the velocity
    # when button is pressed
    def on_key_press(self, symbol,modifier):
  
        # Checking the button pressed
        # and changing the value of velocity
        if symbol == arcade.key.LEFT:
            self.vel_x = -300
        elif symbol == arcade.key.RIGHT:
            self.vel_x = 300
  
    # Creating function to change the velocity
    # when button is released
    def on_key_release(self, symbol, modifier):
  
        # Checking the button released
        # and changing the value of velocity
        if symbol == arcade.key.LEFT:
            self.vel_x = 0
        elif symbol == arcade.key.RIGHT:
            self.vel_x = 0
  
    def camera_move(self):
        # Getting the x coordinate for the center of camera
        screen_x = self.player_sprite.center_x -
        (self.camera.viewport_width / 2)
          
        # Getting the y coordinate for the center of camera
        screen_y = self.player_sprite.center_y -
        (self.camera.viewport_height / 2)
  
        # Moving the camera
        self.camera.move_to([screen_x, screen_y])  
          
# Calling MainGame class       
game = MainGame()
game.setup()
arcade.run()

输出: