📜  Python Arcade - 玩家运动

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

Python Arcade - 玩家运动

在本文中,我们将讨论如何在Python使用 Arcade 移动玩家。

自动机芯

我们可以轻松地将玩家移动到街机中的任何特定方向。为此,我们将使用 draw_rectangle_filled() 方法绘制一个矩形,然后我们将更改该矩形的 x 坐标。

下面是实现:



Python3
# Importing arcade module
import arcade
  
# Creating MainGame class       
class MainGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 600, title="Player Movement")
  
        # Initializing the initial x and y coordinated
        self.x = 250 
        self.y = 250
  
        # Initializing a variable to store
        # the velocity of the player
        self.vel = 300
  
    # Creating on_draw() function to draw on the screen
    def on_draw(self):
        arcade.start_render()
  
        # Drawing the rectangle using
        # draw_rectangle_filled function
        arcade.draw_rectangle_filled(self.x, self.y,50, 50,
                                     arcade.color.GREEN )
    # Creating on_update function to
    # update the x coordinate
    def on_update(self,delta_time):
        self.x += self.vel * delta_time
  
        # Changing the direction of
        # movement if player crosses the screen
        if self.x>=550 or self.x<=50:
            self.vel *= -1
          
# Calling MainGame class       
MainGame()
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 the initial x and y coordinated
        self.x = 250 
        self.y = 250
  
        # Initializing a variable to store
        # the velocity of the player
        self.vel_x = 0
        self.vel_y = 0
  
    # Creating on_draw() function to draw on the screen
    def on_draw(self):
        arcade.start_render()
  
        # Drawing the rectangle using
        # draw_rectangle_filled function
        arcade.draw_rectangle_filled(self.x, self.y,50, 50,
                                     arcade.color.GREEN )
    # Creating on_update function to
    # update the x coordinate
    def on_update(self,delta_time):
        self.x += self.vel_x * delta_time
        self.y += self.vel_y * delta_time
  
          
    # 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.UP:
            self.vel_y = 300
        elif symbol == arcade.key.DOWN:
            self.vel_y = -300
        elif 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.UP:
            self.vel_y = 0
        elif symbol == arcade.key.DOWN:
            self.vel_y = 0
        elif symbol == arcade.key.LEFT:
            self.vel_x = 0
        elif symbol == arcade.key.RIGHT:
            self.vel_x = 0
          
          
# Calling MainGame class       
MainGame()
arcade.run()


输出:

使用键盘输入的玩家移动

在街机游戏中,我们可以从用户那里获取输入来移动我们的玩家。为此,我们将使用 on_key_press() 和 on_key_release()函数。

下面是实现:

蟒蛇3

# Importing arcade module
import arcade
  
# Creating MainGame class       
class MainGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 600, title="Player Movement")
  
        # Initializing the initial x and y coordinated
        self.x = 250 
        self.y = 250
  
        # Initializing a variable to store
        # the velocity of the player
        self.vel_x = 0
        self.vel_y = 0
  
    # Creating on_draw() function to draw on the screen
    def on_draw(self):
        arcade.start_render()
  
        # Drawing the rectangle using
        # draw_rectangle_filled function
        arcade.draw_rectangle_filled(self.x, self.y,50, 50,
                                     arcade.color.GREEN )
    # Creating on_update function to
    # update the x coordinate
    def on_update(self,delta_time):
        self.x += self.vel_x * delta_time
        self.y += self.vel_y * delta_time
  
          
    # 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.UP:
            self.vel_y = 300
        elif symbol == arcade.key.DOWN:
            self.vel_y = -300
        elif 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.UP:
            self.vel_y = 0
        elif symbol == arcade.key.DOWN:
            self.vel_y = 0
        elif symbol == arcade.key.LEFT:
            self.vel_x = 0
        elif symbol == arcade.key.RIGHT:
            self.vel_x = 0
          
          
# Calling MainGame class       
MainGame()
arcade.run()

输出: