Hướng dẫn can you make a 2d game with python? - bạn có thể tạo một trò chơi 2d với python không?

Python là một ngôn ngữ xuất sắc cho những người học lập trình và hoàn hảo cho bất cứ ai muốn "hoàn thành công việc" và không dành hàng đống thời gian cho mã Boilerplate. Arcade là một thư viện Python để tạo các trò chơi video 2D dễ bắt đầu sử dụng và rất có khả năng khi bạn có được kinh nghiệm. Trong bài viết này, tôi sẽ giải thích cách bắt đầu sử dụng Python và Arcade để lập trình trò chơi video.

Tôi bắt đầu phát triển trên arcade sau khi dạy sinh viên sử dụng thư viện pygame. Tôi đã dạy trực tiếp bằng cách sử dụng pygame trong gần 10 năm và tôi đã phát triển ProgramarCadeGames.com để dạy trực tuyến. Pygame là tuyệt vời, nhưng cuối cùng tôi cảm thấy như mình đang lãng phí thời gian để phải trả tiền cho những lỗi không bao giờ được sửa chữa.

Tôi lo lắng về việc dạy những thứ như vòng lặp sự kiện, đó không còn là cách chúng ta viết mã. Tôi đã có cả một phần trong đó tôi giải thích lý do tại sao các tọa độ y bị đảo ngược. Bởi vì pygame hiếm khi được cập nhật và nó dựa trên thư viện SDL 1 cũ, thay vì một cái gì đó hiện đại hơn như OpenGL, tôi đã không hy vọng cho tương lai.

Tôi muốn một thư viện dễ sử dụng hơn, mạnh mẽ hơn và sử dụng một số tính năng mới của Python 3, như trang trí và kiểu dáng. Arcade là nó. Và đây là cách bắt đầu.

Cài đặt

Arcade, giống như nhiều gói khác, có sẵn thông qua PYPI, điều đó có nghĩa là bạn có thể cài đặt arcade bằng lệnh

4 (hoặc lệnh pipenv). Nếu bạn đã cài đặt Python, bạn có thể chỉ cần mở dấu nhắc lệnh trên Windows và gõ:

Hoặc trên loại macOS và Linux:

Để biết thêm hướng dẫn cài đặt chi tiết, bạn có thể tham khảo tài liệu cài đặt arcade.

Vẽ đơn giản

Bạn có thể mở một cửa sổ và tạo các bản vẽ đơn giản chỉ bằng một vài dòng mã. Hãy tạo một ví dụ vẽ một khuôn mặt cười như hình dưới đây:

Hướng dẫn can you make a 2d game with python? - bạn có thể tạo một trò chơi 2d với python không?

Hình ảnh bởi:

Kịch bản bên dưới cho thấy cách bạn có thể sử dụng các lệnh vẽ của Arcade để thực hiện việc này. Lưu ý rằng bạn không cần biết cách sử dụng các lớp hoặc thậm chí xác định các chức năng. Lập trình với phản hồi trực quan nhanh là tuyệt vời cho bất cứ ai muốn bắt đầu học lập trình.

import arcade

# Set constants for the screen size
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600

# Open the window. Set the window title and dimensions (width and height)
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")

# Set the background color to white.
# For a list of named colors see:
# http://arcade.academy/arcade.color.html
# Colors can also be specified in (red, green, blue) format and
# (red, green, blue, alpha) format.
arcade.set_background_color(arcade.color.WHITE)

# Start the render process. This must be done before any drawing commands.
arcade.start_render()

# Draw the face
x = 300
y = 300
radius = 200
arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)

# Draw the right eye
x = 370
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the left eye
x = 230
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the smile
x = 300
y = 280
width = 120
height = 100
start_angle = 190
end_angle = 350
arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK, start_angle, end_angle, 10)

# Finish drawing and display the result
arcade.finish_render()

# Keep the window open until the user hits the 'close' button
arcade.run()

Sử dụng các chức năng

Tất nhiên, viết mã trong bối cảnh toàn cầu không phải là hình thức tốt. Rất may cải thiện chương trình của bạn bằng cách sử dụng các chức năng là dễ dàng. Ở đây chúng ta có thể thấy một ví dụ về một cây thông vẽ tại một vị trí cụ thể (x, y) bằng cách sử dụng hàm:

def draw_pine_tree(x, y):
    """ This function draws a pine tree at the specified location. """

        # Draw the triangle on top of the trunk.
    # We need three x, y points for the triangle.
    arcade.draw_triangle_filled(x + 40, y,       # Point 1
                                x, y - 100,      # Point 2
                                x + 80, y - 100, # Point 3
                                arcade.color.DARK_GREEN)

    # Draw the trunk
    arcade.draw_lrtb_rectangle_filled(x + 30, x + 50, y - 100, y - 140,
                                      arcade.color.DARK_BROWN)

Ví dụ đầy đủ, xem bản vẽ với các chức năng.

Hướng dẫn can you make a 2d game with python? - bạn có thể tạo một trò chơi 2d với python không?

Lập trình viên có kinh nghiệm hơn sẽ biết rằng các chương trình đồ họa hiện đại lần đầu tiên tải thông tin vẽ lên card đồ họa, và sau đó yêu cầu card đồ họa vẽ sau đó dưới dạng một lô. Arcade cũng hỗ trợ điều này. Vẽ 10.000 hình chữ nhật riêng lẻ mất khoảng 0,800 giây. Vẽ chúng như một lô mất ít hơn 0,001 giây.

Lớp cửa sổ

Các chương trình lớn hơn thường sẽ xuất phát từ lớp cửa sổ, hoặc sử dụng các bộ trang trí. Điều này cho phép một lập trình viên viết mã để xử lý bản vẽ, cập nhật và xử lý đầu vào từ người dùng. Một mẫu để bắt đầu một chương trình dựa trên ____ 15 dưới đây.

import arcade

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

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

    def __init__(self, width, height):
        super().__init__(width, height)

        arcade.set_background_color(arcade.color.AMAZON)

    def setup(self):
        # Set up your game here
        pass

    def on_draw(self):
        """ Render the screen. """
        arcade.start_render()
        # Your drawing code goes here

    def update(self, delta_time):
        """ All the logic to move, and the game logic goes here. """
        pass

def main():
    game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
    game.setup()
    arcade.run()

if __name__ == "__main__":
    main()

Lớp

5 có một số phương pháp mà các chương trình của bạn có thể ghi đè để cung cấp chức năng cho chương trình. Dưới đây là một số trong những cái được sử dụng phổ biến nhất:

  • 7: Tất cả các mã để vẽ màn hình đều ở đây.
  • 8: Tất cả các mã để di chuyển các mục của bạn và thực hiện logic trò chơi sẽ diễn ra tại đây. Điều này được gọi là khoảng 60 lần mỗi giây.
  • 9: Xử lý các sự kiện khi nhấn phím, chẳng hạn như cho người chơi tốc độ.
  • import arcade

    # Set constants for the screen size
    SCREEN_WIDTH = 600
    SCREEN_HEIGHT = 600

    # Open the window. Set the window title and dimensions (width and height)
    arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")

    # Set the background color to white.
    # For a list of named colors see:
    # http://arcade.academy/arcade.color.html
    # Colors can also be specified in (red, green, blue) format and
    # (red, green, blue, alpha) format.
    arcade.set_background_color(arcade.color.WHITE)

    # Start the render process. This must be done before any drawing commands.
    arcade.start_render()

    # Draw the face
    x = 300
    y = 300
    radius = 200
    arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)

    # Draw the right eye
    x = 370
    y = 350
    radius = 20
    arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

    # Draw the left eye
    x = 230
    y = 350
    radius = 20
    arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

    # Draw the smile
    x = 300
    y = 280
    width = 120
    height = 100
    start_angle = 190
    end_angle = 350
    arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK, start_angle, end_angle, 10)

    # Finish drawing and display the result
    arcade.finish_render()

    # Keep the window open until the user hits the 'close' button
    arcade.run()

    0: Xử lý khi khóa được phát hành, tại đây bạn có thể ngăn người chơi di chuyển.
  • import arcade

    # Set constants for the screen size
    SCREEN_WIDTH = 600
    SCREEN_HEIGHT = 600

    # Open the window. Set the window title and dimensions (width and height)
    arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")

    # Set the background color to white.
    # For a list of named colors see:
    # http://arcade.academy/arcade.color.html
    # Colors can also be specified in (red, green, blue) format and
    # (red, green, blue, alpha) format.
    arcade.set_background_color(arcade.color.WHITE)

    # Start the render process. This must be done before any drawing commands.
    arcade.start_render()

    # Draw the face
    x = 300
    y = 300
    radius = 200
    arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)

    # Draw the right eye
    x = 370
    y = 350
    radius = 20
    arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

    # Draw the left eye
    x = 230
    y = 350
    radius = 20
    arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

    # Draw the smile
    x = 300
    y = 280
    width = 120
    height = 100
    start_angle = 190
    end_angle = 350
    arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK, start_angle, end_angle, 10)

    # Finish drawing and display the result
    arcade.finish_render()

    # Keep the window open until the user hits the 'close' button
    arcade.run()

    1: Điều này được gọi là mỗi khi chuột di chuyển.
  • import arcade

    # Set constants for the screen size
    SCREEN_WIDTH = 600
    SCREEN_HEIGHT = 600

    # Open the window. Set the window title and dimensions (width and height)
    arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")

    # Set the background color to white.
    # For a list of named colors see:
    # http://arcade.academy/arcade.color.html
    # Colors can also be specified in (red, green, blue) format and
    # (red, green, blue, alpha) format.
    arcade.set_background_color(arcade.color.WHITE)

    # Start the render process. This must be done before any drawing commands.
    arcade.start_render()

    # Draw the face
    x = 300
    y = 300
    radius = 200
    arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)

    # Draw the right eye
    x = 370
    y = 350
    radius = 20
    arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

    # Draw the left eye
    x = 230
    y = 350
    radius = 20
    arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

    # Draw the smile
    x = 300
    y = 280
    width = 120
    height = 100
    start_angle = 190
    end_angle = 350
    arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK, start_angle, end_angle, 10)

    # Finish drawing and display the result
    arcade.finish_render()

    # Keep the window open until the user hits the 'close' button
    arcade.run()

    2: Nhấn khi nhấn nút chuột.
  • import arcade

    # Set constants for the screen size
    SCREEN_WIDTH = 600
    SCREEN_HEIGHT = 600

    # Open the window. Set the window title and dimensions (width and height)
    arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")

    # Set the background color to white.
    # For a list of named colors see:
    # http://arcade.academy/arcade.color.html
    # Colors can also be specified in (red, green, blue) format and
    # (red, green, blue, alpha) format.
    arcade.set_background_color(arcade.color.WHITE)

    # Start the render process. This must be done before any drawing commands.
    arcade.start_render()

    # Draw the face
    x = 300
    y = 300
    radius = 200
    arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)

    # Draw the right eye
    x = 370
    y = 350
    radius = 20
    arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

    # Draw the left eye
    x = 230
    y = 350
    radius = 20
    arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

    # Draw the smile
    x = 300
    y = 280
    width = 120
    height = 100
    start_angle = 190
    end_angle = 350
    arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK, start_angle, end_angle, 10)

    # Finish drawing and display the result
    arcade.finish_render()

    # Keep the window open until the user hits the 'close' button
    arcade.run()

    3: Chức năng này được sử dụng trong các trò chơi cuộn, khi bạn có một thế giới lớn hơn nhiều so với những gì có thể nhìn thấy trên một màn hình. Gọi

    import arcade

    # Set constants for the screen size
    SCREEN_WIDTH = 600
    SCREEN_HEIGHT = 600

    # Open the window. Set the window title and dimensions (width and height)
    arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")

    # Set the background color to white.
    # For a list of named colors see:
    # http://arcade.academy/arcade.color.html
    # Colors can also be specified in (red, green, blue) format and
    # (red, green, blue, alpha) format.
    arcade.set_background_color(arcade.color.WHITE)

    # Start the render process. This must be done before any drawing commands.
    arcade.start_render()

    # Draw the face
    x = 300
    y = 300
    radius = 200
    arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)

    # Draw the right eye
    x = 370
    y = 350
    radius = 20
    arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

    # Draw the left eye
    x = 230
    y = 350
    radius = 20
    arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

    # Draw the smile
    x = 300
    y = 280
    width = 120
    height = 100
    start_angle = 190
    end_angle = 350
    arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK, start_angle, end_angle, 10)

    # Finish drawing and display the result
    arcade.finish_render()

    # Keep the window open until the user hits the 'close' button
    arcade.run()

    3 cho phép một lập trình viên đặt phần nào của thế giới đó hiện đang được nhìn thấy.

Sprites

Sprites là một cách dễ dàng để tạo ra một đối tượng bitmap 2D trong arcade. Arcade có các phương pháp giúp dễ dàng rút ra, di chuyển và làm động các sprites. Bạn cũng có thể dễ dàng sử dụng Sprites để phát hiện sự va chạm giữa các đối tượng.

Tạo một sprite

Việc tạo ra một thể hiện của lớp sprite của Arcade từ một đồ họa rất dễ dàng. Một lập trình viên chỉ cần tên tệp của một hình ảnh để dựa trên cơ sở và tùy chọn một số để mở rộng hình ảnh lên hoặc xuống. Ví dụ:

SPRITE_SCALING_COIN = 0.2

coin = arcade.Sprite("coin_01.png", SPRITE_SCALING_COIN)

Mã này sẽ tạo một sprite bằng cách sử dụng hình ảnh được lưu trữ trong

import arcade

# Set constants for the screen size
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600

# Open the window. Set the window title and dimensions (width and height)
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")

# Set the background color to white.
# For a list of named colors see:
# http://arcade.academy/arcade.color.html
# Colors can also be specified in (red, green, blue) format and
# (red, green, blue, alpha) format.
arcade.set_background_color(arcade.color.WHITE)

# Start the render process. This must be done before any drawing commands.
arcade.start_render()

# Draw the face
x = 300
y = 300
radius = 200
arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)

# Draw the right eye
x = 370
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the left eye
x = 230
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the smile
x = 300
y = 280
width = 120
height = 100
start_angle = 190
end_angle = 350
arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK, start_angle, end_angle, 10)

# Finish drawing and display the result
arcade.finish_render()

# Keep the window open until the user hits the 'close' button
arcade.run()

5. Hình ảnh sẽ được thu nhỏ xuống 20% ​​chiều cao và chiều rộng ban đầu của nó.

Hướng dẫn can you make a 2d game with python? - bạn có thể tạo một trò chơi 2d với python không?

Danh sách sprite

Sprites thường được tổ chức thành danh sách. Những danh sách này làm cho nó dễ dàng hơn để quản lý các sprites. Sprites trong danh sách sẽ sử dụng OpenGL để vẽ các sprites dưới dạng một nhóm. Mã bên dưới thiết lập một trò chơi với người chơi và một loạt các đồng xu để người chơi thu thập. Chúng tôi sử dụng hai danh sách, một cho người chơi và một cho các đồng tiền.

def setup(self):
    """ Set up the game and initialize the variables. """

    # Create the sprite lists
    self.player_list = arcade.SpriteList()
    self.coin_list = arcade.SpriteList()

    # Score
    self.score = 0

    # Set up the player
    # Character image from kenney.nl
    self.player_sprite = arcade.Sprite("images/character.png", SPRITE_SCALING_PLAYER)
    self.player_sprite.center_x = 50 # Starting position
    self.player_sprite.center_y = 50
    self.player_list.append(self.player_sprite)

    # Create the coins
    for i in range(COIN_COUNT):

        # Create the coin instance
        # Coin image from kenney.nl
        coin = arcade.Sprite("images/coin_01.png", SPRITE_SCALING_COIN)

        # Position the coin
        coin.center_x = random.randrange(SCREEN_WIDTH)
        coin.center_y = random.randrange(SCREEN_HEIGHT)

        # Add the coin to the lists
        self.coin_list.append(coin)

Chúng ta có thể dễ dàng vẽ tất cả các đồng tiền trong danh sách tiền xu:

def on_draw(self):
    """ Draw everything """
    arcade.start_render()
    self.coin_list.draw()
    self.player_list.draw()

Phát hiện va chạm sprite

Hàm

import arcade

# Set constants for the screen size
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600

# Open the window. Set the window title and dimensions (width and height)
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")

# Set the background color to white.
# For a list of named colors see:
# http://arcade.academy/arcade.color.html
# Colors can also be specified in (red, green, blue) format and
# (red, green, blue, alpha) format.
arcade.set_background_color(arcade.color.WHITE)

# Start the render process. This must be done before any drawing commands.
arcade.start_render()

# Draw the face
x = 300
y = 300
radius = 200
arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)

# Draw the right eye
x = 370
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the left eye
x = 230
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the smile
x = 300
y = 280
width = 120
height = 100
start_angle = 190
end_angle = 350
arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK, start_angle, end_angle, 10)

# Finish drawing and display the result
arcade.finish_render()

# Keep the window open until the user hits the 'close' button
arcade.run()

6 cho phép chúng tôi xem liệu một sprite có chạy vào một sprite khác trong danh sách không. Chúng ta có thể sử dụng điều này để xem tất cả các đồng tiền mà người chơi sprite tiếp xúc. Sử dụng một vòng lặp

import arcade

# Set constants for the screen size
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600

# Open the window. Set the window title and dimensions (width and height)
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")

# Set the background color to white.
# For a list of named colors see:
# http://arcade.academy/arcade.color.html
# Colors can also be specified in (red, green, blue) format and
# (red, green, blue, alpha) format.
arcade.set_background_color(arcade.color.WHITE)

# Start the render process. This must be done before any drawing commands.
arcade.start_render()

# Draw the face
x = 300
y = 300
radius = 200
arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)

# Draw the right eye
x = 370
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the left eye
x = 230
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the smile
x = 300
y = 280
width = 120
height = 100
start_angle = 190
end_angle = 350
arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK, start_angle, end_angle, 10)

# Finish drawing and display the result
arcade.finish_render()

# Keep the window open until the user hits the 'close' button
arcade.run()

7 đơn giản, chúng ta có thể loại bỏ đồng tiền khỏi trò chơi và tăng điểm số của chúng tôi.

def update(self, delta_time):
    # Generate a list of all coin sprites that collided with the player.
    coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)

    # Loop through each colliding sprite, remove it, and add to the score.
    for coin in coins_hit_list:
        coin.kill()
        self.score += 1

Để biết ví dụ đầy đủ, xem colle_coins.py.

Vật lý trò chơi

Nhiều trò chơi bao gồm một số loại vật lý. Đơn giản nhất là các chương trình từ trên xuống ngăn người chơi đi qua các bức tường. Các nền tảng thêm sự phức tạp hơn với trọng lực và các nền tảng di chuyển. Một số trò chơi sử dụng động cơ vật lý 2D đầy đủ với khối lượng, ma sát, lò xo, v.v.

Trò chơi từ trên xuống

Hướng dẫn can you make a 2d game with python? - bạn có thể tạo một trò chơi 2d với python không?

Đối với các trò chơi dựa trên từ trên xuống đơn giản, một chương trình arcade cần một danh sách các bức tường mà người chơi (hoặc bất cứ điều gì khác) không thể di chuyển qua. Tôi thường gọi đây là

import arcade

# Set constants for the screen size
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600

# Open the window. Set the window title and dimensions (width and height)
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")

# Set the background color to white.
# For a list of named colors see:
# http://arcade.academy/arcade.color.html
# Colors can also be specified in (red, green, blue) format and
# (red, green, blue, alpha) format.
arcade.set_background_color(arcade.color.WHITE)

# Start the render process. This must be done before any drawing commands.
arcade.start_render()

# Draw the face
x = 300
y = 300
radius = 200
arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)

# Draw the right eye
x = 370
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the left eye
x = 230
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)

# Draw the smile
x = 300
y = 280
width = 120
height = 100
start_angle = 190
end_angle = 350
arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK, start_angle, end_angle, 10)

# Finish drawing and display the result
arcade.finish_render()

# Keep the window open until the user hits the 'close' button
arcade.run()

8. Sau đó, một công cụ vật lý được tạo trong mã thiết lập của lớp
5 với:

self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)

def draw_pine_tree(x, y):
    """ This function draws a pine tree at the specified location. """

        # Draw the triangle on top of the trunk.
    # We need three x, y points for the triangle.
    arcade.draw_triangle_filled(x + 40, y,       # Point 1
                                x, y - 100,      # Point 2
                                x + 80, y - 100, # Point 3
                                arcade.color.DARK_GREEN)

    # Draw the trunk
    arcade.draw_lrtb_rectangle_filled(x + 30, x + 50, y - 100, y - 140,
                                      arcade.color.DARK_BROWN)

0 được cung cấp một vectơ chuyển động với hai thuộc tính của nó

def draw_pine_tree(x, y):
    """ This function draws a pine tree at the specified location. """

        # Draw the triangle on top of the trunk.
    # We need three x, y points for the triangle.
    arcade.draw_triangle_filled(x + 40, y,       # Point 1
                                x, y - 100,      # Point 2
                                x + 80, y - 100, # Point 3
                                arcade.color.DARK_GREEN)

    # Draw the trunk
    arcade.draw_lrtb_rectangle_filled(x + 30, x + 50, y - 100, y - 140,
                                      arcade.color.DARK_BROWN)

1 và

def draw_pine_tree(x, y):
    """ This function draws a pine tree at the specified location. """

        # Draw the triangle on top of the trunk.
    # We need three x, y points for the triangle.
    arcade.draw_triangle_filled(x + 40, y,       # Point 1
                                x, y - 100,      # Point 2
                                x + 80, y - 100, # Point 3
                                arcade.color.DARK_GREEN)

    # Draw the trunk
    arcade.draw_lrtb_rectangle_filled(x + 30, x + 50, y - 100, y - 140,
                                      arcade.color.DARK_BROWN)

2. Một ví dụ đơn giản về việc làm điều này là để người chơi di chuyển với bàn phím. Ví dụ: đây có thể là trong đứa trẻ tùy chỉnh của lớp
5:

0

Mặc dù mã đó đặt tốc độ của người chơi, nhưng nó không di chuyển người chơi. Trong phương thức

8 của lớp 
5, gọi 

def draw_pine_tree(x, y):
    """ This function draws a pine tree at the specified location. """

        # Draw the triangle on top of the trunk.
    # We need three x, y points for the triangle.
    arcade.draw_triangle_filled(x + 40, y,       # Point 1
                                x, y - 100,      # Point 2
                                x + 80, y - 100, # Point 3
                                arcade.color.DARK_GREEN)

    # Draw the trunk
    arcade.draw_lrtb_rectangle_filled(x + 30, x + 50, y - 100, y - 140,
                                      arcade.color.DARK_BROWN)

6 sẽ di chuyển người chơi, nhưng không phải thông qua các bức tường.

1

Để biết ví dụ đầy đủ, hãy xem sprite_move_walls.py.

Nền tảng

Hướng dẫn can you make a 2d game with python? - bạn có thể tạo một trò chơi 2d với python không?

Di chuyển đến một nền tảng xem bên là khá dễ dàng. Một lập trình viên chỉ cần chuyển động cơ vật lý sang

def draw_pine_tree(x, y):
    """ This function draws a pine tree at the specified location. """

        # Draw the triangle on top of the trunk.
    # We need three x, y points for the triangle.
    arcade.draw_triangle_filled(x + 40, y,       # Point 1
                                x, y - 100,      # Point 2
                                x + 80, y - 100, # Point 3
                                arcade.color.DARK_GREEN)

    # Draw the trunk
    arcade.draw_lrtb_rectangle_filled(x + 30, x + 50, y - 100, y - 140,
                                      arcade.color.DARK_BROWN)

7 và thêm hằng số trọng lực.

2

Bạn có thể sử dụng một chương trình như lát gạch để đặt gạch/khối tạo nên cấp độ của bạn.

Ví dụ, xem sprite_tiled_map.py.

Đối với vật lý 2D đầy đủ, bạn có thể tích hợp thư viện Pymunk.

Tìm hiểu bằng ví dụ

Một trong những cách tốt nhất để học là ví dụ. Thư viện Arcade có một danh sách dài các chương trình ví dụ mà một người có thể vẽ để tạo trò chơi. Những ví dụ này cho thấy một khái niệm trò chơi mà sinh viên đã yêu cầu trong các lớp học của tôi hoặc trực tuyến trong những năm qua.

Chạy bất kỳ bản demo nào là dễ dàng khi arcade đã được cài đặt. Mỗi mẫu có một nhận xét khi bắt đầu chương trình với lệnh bạn có thể nhập trên dòng lệnh để chạy mẫu, ví dụ:

3

Bản tóm tắt

Arcade cho phép bạn bắt đầu lập trình đồ họa và trò chơi với mã dễ hiểu. Nhiều lập trình viên mới đã tạo ra những trò chơi tuyệt vời không lâu sau khi bắt đầu. Hãy thử một lần!


Để tìm hiểu thêm, hãy tham dự cuộc nói chuyện của Paul Vincent Craven, tạo trò chơi 2D dễ dàng với Arcade, tại Pycon Cleveland 2018.

Pygame có thể tạo ra trò chơi 2D không?

Pygame là một thư viện Python có thể được sử dụng đặc biệt để thiết kế và xây dựng các trò chơi.Pygame chỉ hỗ trợ các trò chơi 2D được xây dựng bằng cách sử dụng các sprites khác nhau.Pygame supports only 2d games that are built using different sprites.

Tôi có thể sử dụng Python để xây dựng một trò chơi không?

Tạo các trò chơi máy tính của riêng bạn trong Python là một cách tuyệt vời để học ngôn ngữ.Để xây dựng một trò chơi, bạn sẽ cần sử dụng nhiều kỹ năng lập trình cốt lõi.Các loại kỹ năng mà bạn sẽ thấy trong lập trình trong thế giới thực.To build a game, you'll need to use many core programming skills. The kinds of skills that you'll see in real-world programming.

Ngôn ngữ tốt nhất cho các trò chơi 2D là gì?

C ++ rất phù hợp cho cả trò chơi 2D và 3D.Nếu bạn dự định tạo một trò chơi 2D, bạn có thể xem xét các ngôn ngữ lập trình khác như C# hoặc JavaScript.C# or Javascript.

Python có tốt cho các trò chơi 3D không?

Python có tốt cho phát triển trò chơi không?Python là một lựa chọn tuyệt vời để phát triển trò chơi.Với sự phát triển của ngành công nghiệp game, sự phát triển trò chơi Python đã cho thấy là một lựa chọn tuyệt vời cho các nhà phát triển để tạo mẫu nhanh và thực hiện các trò chơi video.Python is an excellent choice for game development. With the growth of the gaming industry, Python game development has shown to be an excellent choice for developers for quick prototyping and implementation of video games.