In diesem Artikel werden wir diskutieren, wie man die Spieler mit Arcade in Python bewegt.

Automatische Bewegungen

Wir können unsere Spieler leicht in jede bestimmte Richtung in der Spielhalle bewegen. Dazu zeichnen wir ein Rechteck mit der Methode draw_rectangle_filled() und ändern dann die x-Koordinate dieses Rechtecks.

Syntax: arcade.draw_rectangle_filled(x, y, Breite, Höhe, Farbe, Winkel)

Parameter:

  • x : x-Koordinate des Rechteckzentrums
  • y : y-Koordinate des Rechteckzentrums
  • width : Breite des Rechtecks
  • height : Höhe des Rechtecks
  • color : Farbe des Rechtecks
  • Winkel : Drehung des Rechtecks.

Nachfolgend die Umsetzung:

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()

Ausgabe:

Spielerbewegung mit Tastatureingaben

In Arcade können wir Eingaben von Benutzern entgegennehmen, um unsere Spieler zu bewegen. Dazu verwenden wir die Funktionen on_key_press() und on_key_release().

Syntax:

  • on_key_press(Symbol, Modifikatoren)
  • on_key_release) Symbol, Modifikatoren)

Parameter:

  • Symbol: Taste, die gedrückt wurde
  • Modifikatoren: Bitweises 'und' aller während dieses Ereignisses gedrückten Modifikatoren (Shift, Strg, Num Lock).

Nachfolgend die Umsetzung:

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()

Ausgabe: