Cách tạo cơ chế nhảy cho game bằng thư viện Arcade của Python

Hầu hết game platform đều dùng một số kiểu nhảy với các biến thể khác nhau như nhảy đúp hai lần, nhảy cao… Dưới đây là cách đưa cơ chế nhảy vào game bằng thư viện Arcade của Python.

Mario

Cơ chế nhảy đóng một vai trò quan trọng trong việc nâng cao trải nghiệm gameplay của trò chơi platformer. Bằng cách triển khai những cơ chế nhảy khác nhau bằng thư viện Arcade của Python, bạn có thể thêm chiều sâu và độ thú vị cho game.

Thư viện Arcade cung cấp một framework trực quan và dễ dàng cho việc tạo game, khiến nó trở thành lựa chọn lý tưởng để tạo platformer.

Tạo game đơn giản

Trước khi bắt đầu, cài đặt pip trên thiết bị và dùng lệnh này để cài mô đun arcade:

pip install arcade

Để khám phá cách cơ chế nhảy hoạt động, hãy bắt đầu bằng cách tạo một game đơn giản mà người chơi có thể di chuyển sang trái và phải bằng trọng lực và một nền tảng tĩnh.

Trong game này, người chơi sẽ va chạm với nền tảng này và đứng trên nó. Tạo một file mới tên simple-game.py, rồi thêm code bên dưới:

import arcade

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
PLAYER_RADIUS = 20
PLATFORM_WIDTH = 200
PLATFORM_HEIGHT = 20
GRAVITY = 0.5
blue = arcade.color.BLUE
green = arcade.color.GREEN

class GameWindow(arcade.Window):
    def __init__(self):
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Jumping Mechanics")
        self.player_x = SCREEN_WIDTH // 2
        self.player_y = SCREEN_HEIGHT // 2
        self.player_dy = 0
        self.platform_x = SCREEN_WIDTH // 2
        self.platform_y = SCREEN_HEIGHT // 4

    def on_draw(self):
        arcade.start_render()

        arcade.draw_circle_filled(self.player_x, self.player_y, 
                                  PLAYER_RADIUS, blue)

        arcade.draw_rectangle_filled(self.platform_x, self.platform_y, 
                                     PLATFORM_WIDTH, PLATFORM_HEIGHT, green)

    def on_update(self, delta_time):
        self.player_dy -= GRAVITY
        self.player_y += self.player_dy
        dist = PLAYER_RADIUS + self.platform_y + PLATFORM_HEIGHT / 2

        if self.player_y < dist:
            self.player_y = dist
            self.player_dy = 0

game = GameWindow()
arcade.run()

Khi chạy chương trình này, bạn sẽ thấy người chơi nhanh chóng rơi xuống nền tảng, rồi đứng yên ở trên đó.

Thiết kế game platform đơn giản

Thêm tính năng nhảy đơn giản

Giờ thêm một tính năng nhảy đơn giản cho game. Kiểm tra va chạm giữa người chơi và nền tảng, đồng thời kích hoạt hành động nhảy khi người chơi nhấn mũi tên lên. Để đạt được điều này, tạo file mới tên jump.py và code với bản update bên dưới:

class GameWindow(arcade.Window):
    def __init__(self):
        # ...

    def on_key_press(self, key, modifiers):
        on_ground = PLAYER_RADIUS + self.platform_y + PLATFORM_HEIGHT / 2

        if key == arcade.key.UP and self.player_y == on_ground:
            self.player_dy = 10 
    
    def on_update(self, delta_time):
        self.player_dy -= GRAVITY
        self.player_y += self.player_dy
        dist = PLAYER_RADIUS + self.platform_y + PLATFORM_HEIGHT / 2

        if self.player_y < dist:
            self.player_y = dist
            self.player_dy = 0
            self.jump_count = 0

Thêm tính năng nhảy kép

Để thêm tính năng nhảy kép, mở rộng logic nhảy hiện có. Khi người chơi đang ở trên platform, nhấn mũi tên trỏ lên trên ở lần đầu tiên, nhân vật sẽ thực hiện một bước nhảy bình thường. Tuy nhiên, nếu nhấn lại mũi tên trỏ lên lần nữa khi ở trên không, người chơi sẽ làm thêm một lần nhảy kép.

Tạo file mới tên double-jump.py và code với các cập nhật sau:

class GameWindow(arcade.Window):
    def __init__(self):
        self.jump_count = 0 # Theo dõi số lần nhảy

    def on_key_press(self, key, modifiers):
        on_ground = PLAYER_RADIUS + self.platform_y + PLATFORM_HEIGHT / 2

        if key == arcade.key.UP:
            if self.player_y == on_ground or self.jump_count < 2:
                self.player_dy = 10
                self.jump_count += 1

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

Ngoài nhảy đơn và kép, bạn có thể thêm nhiều tính năng khác để nâng cao cơ chế nhảy của game.

Độ cao của bước nhảy

Cho phép người chơi kiểm soát chiều cao của hành động nhảy dựa trên thời gian giữ nút nhảy có thể tăng thêm quyền kiểm soát và chiến thuật cho gameplay. Dưới đây là một ví dụ mà bạn có thể kiểm soát biến nhảy cao. Tạo một file mới tên variable-jump.py và code chứa các bản update bên dưới:

GRAVITY = 0.5
JUMP_POWER_INCREMENT = 0.2
MAX_JUMP_POWER = 100

class GameWindow(arcade.Window):
    def __init__(self):
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Jumping Mechanics")
        self.jump_pressed = False 
        self.jump_power = 0 

    def on_key_press(self, key, modifiers):
        on_ground = PLAYER_RADIUS + self.platform_y + PLATFORM_HEIGHT / 2

        if key == arcade.key.UP and self.player_y == on_ground:
            self.jump_pressed = True

    def on_key_release(self, key, modifiers):
        if key == arcade.key.UP:
            self.jump_pressed = False

    def update_jump_power(self):
        # Increase the jump power while the jump button is held
        if self.jump_pressed:
            self.jump_power += JUMP_POWER_INCREMENT

            if self.jump_power > MAX_JUMP_POWER:
                self.jump_power = MAX_JUMP_POWER
        else:
            if self.jump_power > 0:
                self.jump_power -= 1

    def on_update(self, delta_time):
        self.player_dy -= GRAVITY
        dist = PLAYER_RADIUS + self.platform_y + PLATFORM_HEIGHT / 2

        if self.player_y == dist and self.jump_power > 0:
            self.player_dy = self.jump_power

        self.player_y += self.player_dy
        self.update_jump_power()

Triển khai tính năng Air Dash

Thêm cơ chế air dash có thể cho người chơi thêm lựa chọn di chuyển khi ở trên không. Tạo file mới tên air-dash.py và code với các bản update bên dưới.

AIR_DASH_DISTANCE = 100
MAX_AIR_DASHES = 2

class GameWindow(arcade.Window):
    def __init__(self):
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Jumping Mechanics")
        self.on_ground = False 
        self.air_dashes = 0 
        self.can_air_dash = True

    def on_key_press(self, key, modifiers):
        if key == arcade.key.UP and self.on_ground:
            self.player_dy = 10 
        elif key == arcade.key.UP and \
                self.air_dashes < MAX_AIR_DASHES and \
                self.can_air_dash:
            self.player_dx = AIR_DASH_DISTANCE 
            self.air_dashes += 1
            self.can_air_dash = False

    def on_update(self, delta_time):
        self.player_dy -= GRAVITY
        self.player_y += self.player_dy

        dist = PLAYER_RADIUS + self.platform_y + PLATFORM_HEIGHT / 2

        if self.player_y <= dist:
            self.player_y = dist
            self.player_dy = 0
            self.on_ground = True
        else:
            self.on_ground = False

        self.player_x += self.player_dx

        if self.player_x < PLAYER_RADIUS:
            self.player_x = PLAYER_RADIUS
            self.player_dx = 0
        elif self.player_x > SCREEN_WIDTH - PLAYER_RADIUS:
            self.player_x = SCREEN_WIDTH - PLAYER_RADIUS
            self.player_dx = 0

        if self.on_ground:
            self.air_dashes = 0
            self.can_air_dash = True

Khi triển khai air dash, nhân vật người chơi sẽ di chuyển ra xa khỏi nền tảng này:

Triển khai air dash

Trên đây là cách thiết kế kiểu nhảy cao trong game bằng thư viện arcade của Python. Hi vọng bài viết hữu ích với các bạn.

Thứ Năm, 06/07/2023 13:56
51 👨 196
0 Bình luận
Sắp xếp theo
    ❖ Python