Cách triển khai các cấp trò chơi trong Arcade của Python

Thư viện Arcade của Python cung cấp cách đơn giản và hiệu quả trong việc triển khai các cấp độ game cho dự án của bạn. Dưới đây là chi tiết cách thực hiện.

Lập trình game Python

Lập trình game là một quá trình thú vị và sáng tạo, cho phép bạn đưa ý tưởng vào cuộc sống. Khi thiết kế game, một trong số những tính năng chính mà có thể nâng cao trải nghiệm người chơi là triển khai nhiều cấp độ game.

Cấp độ hay Level cung cấp cấu trúc, tiến trình và thử thách cho người chơi cảm giác chinh phục thành tựu và hào hứng khi đi qua từng giai đoạn khác nhau trong game.

Tạo game đơn giản

Trước khi bắt đầu, đảm bảo bạn đã cài pip trên thiết bị. Dùng lệnh này để cài đặt thư viện arcade:

pip install arcade

Tạo game cơ bản, nơi người chơi có thể di chuyển sang trái và phải.

Dùng các tính năng của thư viện Arcade để xử lý đầu vào của người dùng và update trạng thái game tương ứng. Tạo file mới tên simple-game.py và thêm code bên dưới:

import arcade

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

class MyGame(arcade.Window):
    def __init__(self):
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "My Game")
        arcade.set_background_color(arcade.color.WHITE)
        self.player_x = SCREEN_WIDTH // 2

    def on_draw(self):
        arcade.start_render()
        arcade.draw_rectangle_filled(self.player_x, 50, 50, 50, arcade.color.BLUE)

    def on_key_press(self, key, modifiers):
        if key == arcade.key.LEFT:
            self.player_x -= 10
        elif key == arcade.key.RIGHT:
            self.player_x += 10

    def on_key_release(self, key, modifiers):
        if key == arcade.key.LEFT or key == arcade.key.RIGHT:
            pass

def main():
    game = MyGame()
    arcade.run()

if __name__ == "__main__":
    main()

Kết quả:

Tạo cấp độ game trong Python

Tạo nhiều cấp độ

Giờ mở rộng game bằng cách thêm nhiều cấp độ. Mỗi cấp sẽ có đặc điểm riêng, cung cấp những thử thách khác nhau cho người chơi. Bạn có thể chỉnh sửa code trước đó để bao gồm các cấp độ bổ sung. Ví dụ:

class LevelOne:
    def __init__(self):
        self.player_x = SCREEN_WIDTH // 2

    def update(self):
        pass

    def draw(self):
        arcade.draw_rectangle_filled(self.player_x, 50, 50, 50, arcade.color.BLUE)

class LevelTwo:
    def __init__(self):
        self.player_x = SCREEN_WIDTH // 2

    def update(self):
        pass

    def draw(self):
        arcade.draw_rectangle_filled(self.player_x, 50, 50, 50, arcade.color.RED)

class MyGame(arcade.Window):
    def __init__(self):
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "My Game")
        arcade.set_background_color(arcade.color.WHITE)
        self.levels = [LevelOne(), LevelTwo()]
        self.current_level = 0

    def on_draw(self):
        arcade.start_render()
        self.levels[self.current_level].draw()

    def on_key_press(self, key, modifiers):
        if key == arcade.key.LEFT:
            self.levels[self.current_level].player_x -= 10
        elif key == arcade.key.RIGHT:
            self.levels[self.current_level].player_x += 10

    def on_key_release(self, key, modifiers):
        if key == arcade.key.LEFT or key == arcade.key.RIGHT:
            pass

    def update(self, delta_time):
        self.levels[self.current_level].update()

def main():
    game = MyGame()
    arcade.run()

if __name__ == "__main__":
    main()

Chuyển tiếp giữa các cấp độ

Để tạo chuyển tiếp mượt mà giữa các cấp độ, hãy xác định thời điểm người chơi vượt qua một ranh giới cụ thể. Bạn có thể theo dõi chuyển động của người chơi, và khi họ vượt qua ranh giới, bạn có thể chuyển sang cấp độ tiếp theo. Tạo file mới tên transition-between-levels.py và thêm code với các update bên dưới:

class MyGame(arcade.Window):
    # ...

    def update(self, delta_time):
        self.levels[self.current_level].update()

        # Kiểm tra xem người chơi đã vượt qua ranh giới hay chưa
        if self.levels[self.current_level].player_x < 0:
            self.current_level += 1
            self.current_level %= len(self.levels)
            self.levels[self.current_level].player_x = SCREEN_WIDTH
        elif self.levels[self.current_level].player_x > SCREEN_WIDTH:
            self.current_level += 1
            self.current_level %= len(self.levels)
            self.levels[self.current_level].player_x = 0

    # ...

Kết quả:

Chuyển tiếp giữa các cấp độ

Bao gồm các tính năng bổ sung

Để làm cấp độ game hấp dẫn hơn, bạn có thể kết hợp những tính năng bổ sung như bổ trợ sức mạnh power-up và mục tiêu.

Mục tiêu

Mục tiêu cho người chơi biết việc cần làm để hoàn thành cấp độ. Chúng tăng thêm độ rõ ràng cho từng giai đoạn khác nhau. Dưới đây là một ví dụ về cách triển khai tính năng mục tiêu cho game:

class Objective:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.radius = 20

    def draw(self):
        arcade.draw_circle_filled(self.x, self.y, self.radius, arcade.color.GREEN)

class LevelOne:
    def __init__(self):
        self.objective = Objective(600, 400)
        # ...

    def update(self):
        # Kiểm tra trạng thái người chơi đạt tới mục tiêu
        if arcade.check_for_collision(self.player, self.objective):
            self.complete_objective()

    def draw(self):
        self.objective.draw()
        # ...

    def complete_objective(self):
        # Code xử lý hoàn thành mục tiêu
        pass

Sưu tầm

Sưu tầm là các vật phẩm, phần thưởng mà người chơi có thể thu thập xuyên suốt mọi cấp độ. Sau đây là một ví dụ về cách triển khai tính năng sưu tầm:

class Collectible:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.radius = 10
        self.is_collected = False

    def draw(self):
        if not self.is_collected:
            arcade.draw_circle_filled(self.x, self.y, self.radius, arcade.color.ORANGE)

    def collect(self):
        self.is_collected = True
        # Code xử lý hiệu ứng thu thập của người chơi

class LevelOne:
    def __init__(self):
        self.collectible = Collectible(300, 300)
        # ...

    def update(self):
        # Kiểm tra xem liệu người chơi đã thu thập vật phẩm hay chưa
        if arcade.check_for_collision(self.player, self.collectible):
            self.collectible.collect()

    def draw(self):
        self.collectible.draw()
        # ...

Chướng ngại vật

Chướng ngại vật có thể là đối tượng tĩnh hoặc đang đi chuyển làm cản trở đường đi của người chơi. Chúng đòi hỏi người chơi cần phải xây dựng chiến thuật và tìm đường để vượt qua chúng. Ví dụ về cách triển khai tính năng vật cản trong game:

class Obstacle:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def draw(self):
        arcade.draw_rectangle_filled(self.x, self.y, self.width, self.height, arcade.color.GRAY)

class LevelOne:
    def __init__(self):
        self.obstacles = [Obstacle(400, 200, 100, 50), Obstacle(600, 500, 80, 120)]
        # ...

Bổ trợ sức mạnh

Power-up có thể nâng cao khả năng hoặc mang tới ưu thế tạm thời cho người chơi. Ví dụ về cách code tính năng power-up cho game Python:

class PowerUp:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.radius = 10
        self.is_active = True

    def draw(self):
        if self.is_active:
            arcade.draw_circle_filled(self.x, self.y, self.radius, arcade.color.YELLOW)

    def activate_power_up(self):
        self.is_active = True

    def deactivate_power_up(self):
        self.is_active = False

class LevelOne:
    def __init__(self):
        self.power_up = PowerUp(200, 200)
        # ...

    def update(self):
        # Kiểm tra xem người chơi có va chạm với power-up không
        if arcade.check_for_collision(self.player, self.power_up):
            self.player.activate_power_up()
            self.power_up.deactivate_power_up()

    def draw(self):
        self.power_up.draw()
        # ...

Một số mẹo hữu ích khi thiết kế cấp độ trong game

  • Mỗi cấp độ nên có mục tiêu rõ ràng cho người chơi chinh phục.
  • Độ khó tăng dần để người chơi không thấy nhàm chán và muốn đi tiếp tới phần cuối cùng.
  • Hình ảnh rõ nét kết hợp âm nhạc và hiệu ứng âm thanh phù hợp.
  • Chơi thử và lặp lại để thu thập phản hồi của người chơi.
  • Cân bằng giữa phần thưởng và thử thách.

Bằng cách thiết kế cấp độ game hợp lý qua thư viện arcade của Python, bạn có thể nâng cao tổng thể trải nghiệm game, tạo môi trường gameplay hấp dẫn hơn. Như bạn thấy làm việc đó cũng không quá khó phải không?

Thứ Ba, 27/06/2023 13:58
51 👨 116
0 Bình luận
Sắp xếp theo
    ❖ Python