Python có thể chạy trò chơi 3d không?

Giống như nhiều người, có thể bạn muốn viết trò chơi điện tử khi mới học viết mã. Nhưng những trò chơi đó có giống những trò chơi bạn đã chơi không? . Không có hướng dẫn hoặc khuôn khổ thực sự để hỗ trợ bạn, đồ họa và âm thanh tiên tiến mà bạn đã trải nghiệm trong các trò chơi khác có thể vẫn nằm ngoài tầm với

Giờ đây, đã có Python và rất nhiều công cụ trò chơi Python tuyệt vời có sẵn. Sự kết hợp mạnh mẽ này làm cho việc tạo ra các trò chơi máy tính tuyệt vời trở nên dễ dàng hơn nhiều so với trước đây. Trong hướng dẫn này, bạn sẽ khám phá một số công cụ trò chơi này, tìm hiểu những gì bạn cần để bắt đầu tạo trò chơi điện tử Python của riêng mình

Đến cuối bài viết này, bạn sẽ

  • Hiểu ưu và nhược điểm của một số công cụ trò chơi Python phổ biến
  • Xem các công cụ trò chơi này đang hoạt động
  • Hiểu cách chúng so sánh với các công cụ trò chơi độc lập
  • Tìm hiểu về các công cụ trò chơi Python khác hiện có

Để tận dụng tối đa hướng dẫn này, bạn nên thành thạo lập trình Python, bao gồm cả lập trình hướng đối tượng. Sự hiểu biết về các khái niệm trò chơi cơ bản là hữu ích, nhưng không cần thiết

Sẵn sàng để lao vào?

Nhận mã nguồn. Nhấp vào đây để lấy mã nguồn mà bạn sẽ sử dụng để dùng thử công cụ trò chơi Python

Tổng quan về công cụ trò chơi Python

Công cụ trò chơi cho Python thường ở dạng thư viện Python, có thể được cài đặt theo nhiều cách khác nhau. Hầu hết đều có sẵn trên PyPI và có thể được cài đặt với

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
7. Tuy nhiên, một số chỉ khả dụng trên GitHub, GitLab hoặc các vị trí chia sẻ mã khác và chúng có thể yêu cầu các bước cài đặt khác. Bài viết này sẽ đề cập đến các phương pháp cài đặt cho tất cả các động cơ được thảo luận

Python là ngôn ngữ lập trình có mục đích chung và nó được sử dụng cho nhiều tác vụ khác ngoài việc viết trò chơi trên máy tính. Ngược lại, có nhiều công cụ trò chơi độc lập khác nhau được thiết kế đặc biệt để viết trò chơi. Một số trong số này bao gồm

  • Động cơ hư cấu
  • Đoàn kết
  • Godot

Các công cụ trò chơi độc lập này khác với các công cụ trò chơi Python ở một số khía cạnh chính

  • Hỗ trợ ngôn ngữ. Các ngôn ngữ như C++, C# và JavaScript phổ biến cho các trò chơi được viết bằng các công cụ trò chơi độc lập, vì bản thân các công cụ này thường được viết bằng các ngôn ngữ này. Rất ít công cụ độc lập hỗ trợ Python
  • Hỗ trợ kịch bản độc quyền. Ngoài ra, nhiều công cụ trò chơi độc lập duy trì và hỗ trợ các ngôn ngữ kịch bản của riêng chúng, ngôn ngữ này có thể không giống với Python. Ví dụ: Unity sử dụng C# nguyên bản, trong khi Unreal hoạt động tốt nhất với C++
  • hỗ trợ nền tảng. Nhiều công cụ trò chơi độc lập hiện đại có thể sản xuất trò chơi cho nhiều nền tảng khác nhau, bao gồm cả hệ thống trò chơi di động và chuyên dụng, với rất ít nỗ lực. Ngược lại, chuyển một trò chơi Python trên nhiều nền tảng khác nhau, đặc biệt là nền tảng di động, có thể là một công việc lớn
  • tùy chọn cấp phép. Các trò chơi được viết bằng công cụ trò chơi độc lập có thể có các tùy chọn và hạn chế cấp phép khác nhau, dựa trên công cụ được sử dụng

Vậy tại sao lại sử dụng Python để viết game? . Sử dụng một công cụ trò chơi độc lập thường yêu cầu bạn học một ngôn ngữ lập trình hoặc kịch bản mới. Công cụ trò chơi Python tận dụng kiến ​​thức hiện có của bạn về Python, rút ​​ngắn thời gian học tập và giúp bạn tiến lên phía trước một cách nhanh chóng

Có nhiều công cụ trò chơi có sẵn cho môi trường Python. Tất cả các công cụ mà bạn sẽ tìm hiểu ở đây đều có chung các tiêu chí sau

  • Chúng là những công cụ tương đối phổ biến hoặc chúng bao gồm các khía cạnh của trò chơi thường không được đề cập
  • Chúng hiện đang được duy trì
  • Họ có sẵn tài liệu tốt

Đối với mỗi động cơ, bạn sẽ tìm hiểu về

  • phương pháp cài đặt
  • Các khái niệm cơ bản, cũng như các giả định mà động cơ tạo ra
  • Các tính năng và khả năng chính
  • Hai triển khai trò chơi, để cho phép so sánh

Khi thích hợp, bạn nên cài đặt các công cụ trò chơi này trong môi trường ảo. Mã nguồn đầy đủ cho các trò chơi trong hướng dẫn này có sẵn để tải xuống tại liên kết bên dưới và sẽ được tham chiếu trong suốt bài viết

Nhận mã nguồn. Nhấp vào đây để lấy mã nguồn mà bạn sẽ sử dụng để dùng thử công cụ trò chơi Python

Với mã nguồn được tải xuống, bạn đã sẵn sàng để bắt đầu

Loại bỏ các quảng cáo

trò chơi ghép hình

Khi mọi người nghĩ về công cụ trò chơi Python, suy nghĩ đầu tiên mà nhiều người nghĩ đến là Pygame. Trên thực tế, đã có một tài liệu hướng dẫn tuyệt vời về Pygame tại Real Python

Được viết để thay thế cho thư viện PySDL bị đình trệ, Pygame bao bọc và mở rộng thư viện SDL, viết tắt của Simple DirectMedia Layer. SDL cung cấp quyền truy cập đa nền tảng vào các thành phần phần cứng đa phương tiện cơ bản của hệ thống, chẳng hạn như âm thanh, video, chuột, bàn phím và cần điều khiển. Bản chất đa nền tảng của cả SDL và Pygame có nghĩa là bạn có thể viết trò chơi và các chương trình Python đa phương tiện phong phú cho mọi nền tảng hỗ trợ chúng

Cài đặt Pygame

Pygame có sẵn trên PyPI, vì vậy sau khi tạo và kích hoạt môi trường ảo, bạn có thể cài đặt nó bằng lệnh

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
7 thích hợp

[venv] $ python -m pip install pygame

Khi đã xong, bạn có thể xác minh cài đặt bằng cách chạy một ví dụ đi kèm với thư viện

[venv] $ python -m pygame.examples.aliens

Bây giờ bạn đã cài đặt Pygame, bạn có thể bắt đầu sử dụng nó ngay. Nếu bạn gặp sự cố trong khi cài đặt, thì hướng dẫn Bắt đầu sẽ phác thảo một số sự cố đã biết và các giải pháp khả thi cho tất cả các nền tảng

Các khái niệm cơ bản

Pygame được tổ chức thành nhiều mô-đun khác nhau, cung cấp quyền truy cập trừu tượng vào phần cứng đầu vào, âm thanh và đồ họa máy tính của bạn. Pygame cũng định nghĩa nhiều lớp, gói gọn các khái niệm không phụ thuộc vào phần cứng. Ví dụ: bản vẽ được thực hiện trên đối tượng

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
9, có giới hạn hình chữ nhật được xác định bởi đối tượng
[venv] $ python pygame/pygame_basic.py
0 của chúng

Mỗi trò chơi sử dụng vòng lặp trò chơi để kiểm soát quá trình chơi trò chơi. Vòng lặp này lặp đi lặp lại liên tục khi trò chơi diễn ra. Pygame cung cấp các phương thức và chức năng để triển khai vòng lặp trò chơi, nhưng nó không cung cấp tự động. Tác giả trò chơi dự kiến ​​​​sẽ triển khai chức năng của vòng lặp trò chơi

Mỗi lần lặp lại của vòng lặp trò chơi được gọi là một khung. Mỗi khung hình, trò chơi thực hiện bốn hành động quan trọng

  1. Xử lý đầu vào của người dùng. Đầu vào của người dùng trong Pygame được xử lý bằng mô hình sự kiện. Đầu vào bằng chuột và bàn phím tạo ra các sự kiện, có thể được đọc và xử lý hoặc bỏ qua khi bạn thấy phù hợp. Bản thân Pygame không cung cấp bất kỳ trình xử lý sự kiện nào

  2. Cập nhật trạng thái của các đối tượng trò chơi. Các đối tượng trò chơi có thể được biểu diễn bằng bất kỳ cấu trúc dữ liệu Pygame nào hoặc lớp Pygame đặc biệt. Các đối tượng như họa tiết, hình ảnh, phông chữ và màu sắc có thể được tạo và mở rộng bằng Python để cung cấp nhiều thông tin trạng thái cần thiết

  3. Cập nhật màn hình và đầu ra âm thanh. Pygame cung cấp quyền truy cập trừu tượng vào phần cứng hiển thị và âm thanh. Các mô-đun

    [venv] $ python pygame/pygame_basic.py
    
    1,
    [venv] $ python pygame/pygame_basic.py
    
    2 và
    [venv] $ python pygame/pygame_basic.py
    
    3 cho phép tác giả trò chơi linh hoạt trong việc thiết kế và triển khai trò chơi

  4. Duy trì tốc độ của trò chơi. Mô-đun

    [venv] $ python pygame/pygame_basic.py
    
    4 của Pygame cho phép tác giả trò chơi kiểm soát tốc độ trò chơi. Bằng cách đảm bảo mỗi khung hình hoàn thành trong một giới hạn thời gian nhất định, tác giả trò chơi có thể đảm bảo trò chơi chạy tương tự trên các phần cứng khác nhau

Bạn có thể thấy những khái niệm này kết hợp với nhau trong một ví dụ cơ bản

Ứng dụng cơ bản

Chương trình Pygame cơ bản này vẽ một vài hình dạng và một số văn bản trên màn hình

Mã cho mẫu này có thể được tìm thấy bên dưới và trong các tài liệu có thể tải xuống

Nguồn cho

[venv] $ python pygame/pygame_basic.py
5Hiển thị/Ẩn

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]

Mặc dù có nguyện vọng khiêm tốn, ngay cả chương trình Pygame cơ bản này cũng yêu cầu vòng lặp trò chơi và trình xử lý sự kiện. Vòng lặp trò chơi bắt đầu ở dòng 27 và được điều khiển bởi biến

[venv] $ python pygame/pygame_basic.py
6. Đặt biến này thành
[venv] $ python pygame/pygame_basic.py
7 sẽ kết thúc chương trình

Xử lý sự kiện bắt đầu trên dòng 30 với vòng lặp sự kiện. Các sự kiện được truy xuất từ ​​hàng đợi bằng cách sử dụng

[venv] $ python pygame/pygame_basic.py
8 và được xử lý từng sự kiện trong mỗi lần lặp lại vòng lặp. Trong trường hợp này, sự kiện duy nhất được xử lý là sự kiện
[venv] $ python pygame/pygame_basic.py
9, sự kiện này được tạo khi người dùng đóng cửa sổ trò chơi. Khi sự kiện này được xử lý, bạn đặt
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
60, điều này cuối cùng sẽ kết thúc vòng lặp trò chơi và chương trình

Pygame cung cấp nhiều phương pháp khác nhau để vẽ các hình cơ bản, chẳng hạn như hình tròn và hình chữ nhật. Trong mẫu này, một vòng tròn màu xanh được vẽ trên dòng 38 và một hình vuông màu đỏ được vẽ trên các dòng 41 và 42. Lưu ý rằng việc vẽ một hình chữ nhật yêu cầu bạn tạo một đối tượng

[venv] $ python pygame/pygame_basic.py
0 trước

Vẽ văn bản trên màn hình liên quan nhiều hơn một chút. Đầu tiên, ở dòng 45, bạn chọn một phông chữ và tạo một đối tượng

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
62. Sử dụng phông chữ đó trên các dòng 46 đến 48, bạn gọi phương thức
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
63. Thao tác này tạo đối tượng
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
9 chứa văn bản được hiển thị bằng phông chữ và màu sắc đã chỉ định. Cuối cùng, bạn sao chép
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
9 ra màn hình bằng phương pháp
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
66 ở dòng 49

Vòng lặp kết thúc trò chơi xảy ra ở dòng 52, khi mọi thứ được vẽ trước đó được hiển thị trên màn hình. Không có dòng này, sẽ không có gì được hiển thị

Để chạy mã này, hãy sử dụng lệnh sau

[venv] $ python pygame/pygame_basic.py

Bạn sẽ thấy một cửa sổ xuất hiện với hình ảnh hiển thị ở trên. Xin chúc mừng. Bạn vừa chạy chương trình Pygame đầu tiên của mình

Loại bỏ các quảng cáo

Ứng dụng nâng cao

Tất nhiên, Pygame được thiết kế để viết game bằng Python. Để khám phá các khả năng và yêu cầu của một trò chơi Pygame thực tế, bạn sẽ kiểm tra một trò chơi được viết bằng Pygame với các chi tiết sau

  • Người chơi là một nhân vật duy nhất trên màn hình, được điều khiển bằng cách di chuyển chuột
  • Theo các khoảng thời gian đều đặn, từng đồng xu xuất hiện trên màn hình
  • Khi người chơi di chuyển qua từng đồng xu, nó sẽ biến mất và người chơi được thưởng mười điểm
  • Khi trò chơi tiến triển, tiền xu được thêm vào nhanh hơn
  • Trò chơi kết thúc khi có hơn mười đồng xu hiển thị trên màn hình

Khi hoàn thành, trò chơi sẽ giống như thế này

Bạn có thể tìm thấy mã hoàn chỉnh cho trò chơi này trong các tài liệu đã tải xuống và bên dưới

Nguồn cho

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
67Hiển thị/Ẩn

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
6

Sprites trong Pygame cung cấp một số chức năng cơ bản, nhưng chúng được thiết kế để phân lớp thay vì sử dụng riêng. Các sprite Pygame không có hình ảnh được liên kết với chúng theo mặc định và chúng không thể tự định vị chúng

Để vẽ và quản lý đúng trình phát và tiền xu trên màn hình, một lớp

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
68 được tạo trên các dòng 35 đến 55 và một lớp
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
69 trên các dòng 58 đến 75. Khi mỗi đối tượng sprite được tạo, đầu tiên nó định vị và tải hình ảnh mà nó sẽ hiển thị, lưu nó vào
[venv] $ python -m pip install pygame
80. Thuộc tính
[venv] $ python -m pip install pygame
81 định vị và di chuyển sprite trên màn hình

Việc thêm tiền vào màn hình đều đặn được thực hiện bằng đồng hồ bấm giờ. Trong Pygame, các sự kiện được kích hoạt bất cứ khi nào hết giờ và người tạo trò chơi có thể xác định các sự kiện của riêng họ dưới dạng hằng số nguyên. Sự kiện

[venv] $ python -m pip install pygame
82 được xác định trên dòng 90 và bộ hẹn giờ kích hoạt sự kiện sau
[venv] $ python -m pip install pygame
83 mili giây trên dòng 91

[venv] $ python -m pip install pygame
82 là một sự kiện nên nó cần được xử lý trong một vòng lặp sự kiện, xảy ra ở các dòng 118 đến 134. Sự kiện tạo một đối tượng
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
69 mới và thêm nó vào đối tượng
[venv] $ python -m pip install pygame
86 hiện có. Số lượng xu trên màn hình được kiểm tra. Nếu có ít hơn ba, thì
[venv] $ python -m pip install pygame
83 bị giảm. Cuối cùng, bộ đếm thời gian trước đó bị dừng và một bộ đếm thời gian mới bắt đầu

Khi người chơi di chuyển, họ va chạm với các đồng xu, thu thập chúng khi họ làm. Thao tác này sẽ tự động xóa từng đồng xu đã thu thập khỏi

[venv] $ python -m pip install pygame
86. This also updates the score and plays a sound

Player movement occurs on line 137. Collisions with coins on the screen are checked on lines 140 to 142. The

[venv] $ python -m pip install pygame
89 parameter removes the coin from the
[venv] $ python -m pip install pygame
86 automatically. Finally, lines 143 to 147 update the score and play the sound for each coin collected

The game ends when the user either closes the window, or when there are more than ten coins on the screen. Checking for more than ten coins is done on lines 150 to 152

Because Pygame sprites have no built-in knowledge of an image, they also don’t know how to draw themselves on the screen. The game author needs to clear the screen, draw all the sprites in the correct order, draw the on-screen score, then

[venv] $ python -m pygame.examples.aliens
91 the display to make everything appear. That all happens on lines 155 to 170

Pygame is a very powerful and well-established library, but it has its drawbacks. Pygame khiến các tác giả trò chơi làm việc để đạt được kết quả của họ. Tác giả trò chơi có thể thực hiện hành vi sprite cơ bản và thực hiện các yêu cầu chính của trò chơi như vòng lặp trò chơi và trình xử lý sự kiện cơ bản tùy thuộc vào tác giả trò chơi. Tiếp theo, bạn sẽ thấy các công cụ trò chơi khác mang lại kết quả tương tự như thế nào trong khi giảm khối lượng công việc bạn phải làm

trò chơi số không

Có nhiều điều Pygame làm tốt, nhưng những điều khác mà tuổi của nó là hiển nhiên. Đối với những người mới bắt đầu viết trò chơi, có thể tìm thấy một tùy chọn tốt hơn trong Pygame Zero. Được thiết kế cho giáo dục, Pygame Zero được hướng dẫn bởi một bộ nguyên tắc đơn giản nhằm mục đích trở nên hoàn hảo cho các lập trình viên trẻ và mới bắt đầu

  • Làm cho nó có thể truy cập. Tất cả mọi thứ được thiết kế cho các lập trình viên mới bắt đầu
  • Hãy bảo thủ. Hỗ trợ nền tảng chung và tránh các tính năng thử nghiệm
  • chỉ cần làm việc. Make sure everything works without a lot of fuss
  • Giảm thiểu chi phí thời gian chạy. Nếu một cái gì đó có thể thất bại, thất bại sớm
  • Lỗi rõ ràng. Không có gì tệ hơn là không biết tại sao lại xảy ra sự cố
  • tài liệu tốt. Một khung chỉ tốt như tài liệu của nó
  • Giảm thiểu các thay đổi vi phạm. Nâng cấp không nên yêu cầu viết lại trò chơi của bạn

Tài liệu dành cho Pygame Zero rất dễ tiếp cận đối với những người mới bắt đầu lập trình và nó bao gồm một hướng dẫn từng bước hoàn chỉnh. Hơn nữa, nhóm Pygame Zero nhận ra rằng nhiều lập trình viên mới bắt đầu viết mã bằng Scratch, vì vậy họ cung cấp hướng dẫn minh họa cách di chuyển chương trình Scratch sang Pygame Zero

Loại bỏ các quảng cáo

Cài đặt Pygame Zero

Pygame Zero có sẵn trên PyPI và bạn có thể cài đặt nó giống như bất kỳ thư viện Python nào khác trên Windows, macOS hoặc Linux

[venv] $ python -m pip install pygame
8

Pygame Zero, đúng như tên gọi, được xây dựng dựa trên Pygame nên bước này cũng cài đặt Pygame như một thư viện phụ thuộc. Pygame Zero được cài đặt theo mặc định trên nền tảng Raspberry Pi, trên Raspbian Jessie hoặc bản phát hành mới hơn

Các khái niệm cơ bản

Pygame Zero tự động hóa nhiều thứ mà lập trình viên phải làm thủ công trong Pygame. Theo mặc định, Pygame Zero cung cấp cho người tạo trò chơi

  • Một vòng lặp trò chơi, vì vậy không cần phải viết một vòng lặp
  • Một mô hình sự kiện để xử lý bản vẽ, cập nhật và xử lý đầu vào
  • Xử lý hình ảnh, văn bản và âm thanh thống nhất
  • Một lớp sprite có thể sử dụng và các phương thức hoạt ảnh hữu ích cho các sprite của người dùng

Do những điều khoản này, một chương trình Pygame Zero cơ bản có thể rất ngắn

Nguồn cho

[venv] $ python -m pygame.examples.aliens
92Hiển thị/Ẩn

[venv] $ python -m pygame.examples.aliens
9

Pygame Zero nhận ra rằng các hằng số

[venv] $ python -m pygame.examples.aliens
93 và
[venv] $ python -m pygame.examples.aliens
94 trên các dòng 16 và 17 đề cập đến kích thước của cửa sổ và tự động sử dụng các kích thước đó để tạo ra nó. Ngoài ra, Pygame Zero cung cấp vòng lặp trò chơi tích hợp và gọi hàm
[venv] $ python -m pygame.examples.aliens
95 được xác định trên các dòng 19 đến 43 một lần trên mỗi khung hình để hiển thị màn hình

Vì Pygame Zero dựa trên Pygame nên một số mã vẽ hình được kế thừa. Bạn có thể thấy sự giống nhau khi vẽ hình tròn ở dòng 29 và hình vuông ở dòng 34 đến 35

However, text drawing is now a single function call on lines 38 to 43, rather than three separate functions

Pygame Zero also provides basic window-handling code, so you can close the window by clicking the appropriate close button, without requiring an event handler

You can find code demonstrating some of Pygame Zero’s basic capabilities in the downloadable materials

Nhận mã nguồn. Nhấp vào đây để lấy mã nguồn mà bạn sẽ sử dụng để dùng thử công cụ trò chơi Python

Running Pygame Zero programs is done from the command line using the command

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
6

Running this command will start your Pygame Zero game. You should see a window appear with basic shapes and your Pygame Zero greeting

Sprites and Images

Sprites are called Actors in Pygame Zero, and they have a few characteristics which require some explanation

  1. Pygame Zero provides the
    [venv] $ python -m pygame.examples.aliens
    
    96 class. Each
    [venv] $ python -m pygame.examples.aliens
    
    96 has, at minimum, an image and a position
  2. All images used in a Pygame Zero program must be located in a subfolder called
    [venv] $ python -m pygame.examples.aliens
    
    98, and be named using lowercase letters, numbers, and underscores only
  3. Images are referenced using only the base name of the image. For example, if your image is called
    [venv] $ python -m pygame.examples.aliens
    
    99, you reference it in your program as
     1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    60

Because of these built-in features of Pygame Zero, drawing sprites on the screen requires very little code

[venv] $ python pygame/pygame_basic.py
2

Now you’ll break this small sample down line by line

  • Line 1 creates the new
    [venv] $ python -m pygame.examples.aliens
    
    96 object, giving it the name of the image to draw
  • Dòng 2 đặt vị trí x và y ban đầu của
    [venv] $ python -m pygame.examples.aliens
    
    96
  • Dòng 4 và 5 đặt kích thước của cửa sổ Pygame Zero. Lưu ý rằng
    [venv] $ python -m pygame.examples.aliens
    
    94 dựa trên thuộc tính
     1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    64 của sprite. Giá trị này xuất phát từ chiều cao của hình ảnh được sử dụng để tạo sprite
  • Dòng 9 vẽ sprite bằng cách gọi
     1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    65 trên đối tượng
    [venv] $ python -m pygame.examples.aliens
    
    96. Điều này vẽ hình ảnh sprite trên màn hình tại vị trí được cung cấp bởi
     1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    67

Bạn sẽ sử dụng những kỹ thuật này trong một trò chơi nâng cao hơn tiếp theo

Loại bỏ các quảng cáo

Ứng dụng nâng cao

Để chứng minh sự khác biệt giữa các công cụ trò chơi, bạn sẽ xem lại cùng một trò chơi nâng cao mà bạn đã thấy trong phần Pygame, hiện được viết bằng Pygame Zero. Xin nhắc lại, các chi tiết chính của trò chơi đó là

  • Người chơi là một nhân vật duy nhất trên màn hình, được điều khiển bằng cách di chuyển chuột
  • Theo các khoảng thời gian đều đặn, từng đồng xu xuất hiện trên màn hình
  • Khi người chơi di chuyển qua từng đồng xu, nó sẽ biến mất và người chơi được thưởng mười điểm
  • Khi trò chơi tiến triển, tiền xu được thêm vào nhanh hơn
  • Trò chơi kết thúc khi có hơn mười đồng xu hiển thị trên màn hình

Trò chơi này sẽ có giao diện và hoạt động giống với phiên bản Pygame đã trình bày trước đó, chỉ có thanh tiêu đề cửa sổ phản bội nguồn gốc của Pygame Zero

Bạn có thể tìm thấy mã hoàn chỉnh cho mẫu này trong các tài liệu đã tải xuống và bên dưới

Nguồn cho

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
68Hiển thị/Ẩn

[venv] $ python pygame/pygame_basic.py
1

Tạo trình phát

[venv] $ python -m pygame.examples.aliens
96 được thực hiện từ dòng 26 đến 28. Vị trí ban đầu là trung tâm của màn hình

Phương pháp

[venv] $ python pygame/pygame_basic.py
20 xử lý việc tạo tiền theo định kỳ. Phương thức này nhận một chức năng để gọi và số giây để trì hoãn trước khi gọi chức năng đó

Dòng 41 đến 65 định nghĩa hàm

[venv] $ python pygame/pygame_basic.py
21 sẽ được lên lịch. Nó tạo ra một đồng xu mới
[venv] $ python -m pygame.examples.aliens
96 tại một vị trí ngẫu nhiên trên các dòng 48 đến 50 và thêm nó vào danh sách toàn cầu về các đồng xu có thể nhìn thấy

Khi trò chơi diễn ra, tiền xu sẽ xuất hiện ngày càng nhanh, nhưng không quá nhanh. Quản lý khoảng thời gian được thực hiện trên các dòng 57 đến 62. Bởi vì

[venv] $ python pygame/pygame_basic.py
20 sẽ chỉ kích hoạt một lần duy nhất, bạn lên lịch một cuộc gọi khác trên đường dây 65

Chuyển động của chuột được xử lý trong trình xử lý sự kiện

[venv] $ python pygame/pygame_basic.py
24 trên các dòng 67 đến 87. Vị trí chuột được chụp và lưu trữ trong một biến toàn cầu trên dòng 76. Dòng 79 đến 87 đảm bảo vị trí này không bao giờ tắt màn hình

Lưu trữ vị trí của người chơi trong một biến

[venv] $ python pygame/pygame_basic.py
25 là một tiện ích giúp đơn giản hóa mã và tập trung cho phép bạn tập trung vào các khả năng của Pygame Zero. Lựa chọn thiết kế của bạn có thể khác trong các trò chơi hoàn chỉnh hơn

Hàm

[venv] $ python pygame/pygame_basic.py
26 được xác định trên các dòng 89 đến 122 được gọi một lần trên mỗi khung bởi Pygame Zero. Bạn sử dụng điều này để di chuyển
[venv] $ python -m pygame.examples.aliens
96 đối tượng và cập nhật trạng thái của tất cả các đối tượng trò chơi của bạn. Vị trí của người chơi
[venv] $ python -m pygame.examples.aliens
96 được cập nhật để theo dõi chuột trên dòng 98

Va chạm với tiền xu được xử lý trên các dòng 102 đến 113. Nếu người chơi đã va chạm với một đồng xu, thì đồng xu đó sẽ được thêm vào

[venv] $ python pygame/pygame_basic.py
29, điểm số sẽ tăng lên và một âm thanh sẽ phát ra. Khi tất cả các va chạm đã được xử lý, bạn loại bỏ các đồng tiền đã được thêm vào
[venv] $ python pygame/pygame_basic.py
29 trên các dòng 112 đến 113

Sau khi xử lý va chạm xu, bạn kiểm tra xem còn quá nhiều xu trên màn hình ở dòng 116 không. Nếu vậy, trò chơi kết thúc, vì vậy bạn ngừng tạo tiền mới, in điểm số cuối cùng và kết thúc trò chơi ở các dòng 118 đến 122

Tất nhiên, tất cả cập nhật này cần được phản ánh trên màn hình. Hàm

[venv] $ python -m pygame.examples.aliens
95 trên các dòng 124 đến 146 được gọi sau
[venv] $ python pygame/pygame_basic.py
26 một lần trên mỗi khung. Sau khi xóa màn hình và tô màu nền ở các dòng 128 và 131, người chơi
[venv] $ python -m pygame.examples.aliens
96 và tất cả các đồng xu được rút ở các dòng 134 đến 138. Tỷ số hiện tại là điều cuối cùng được vẽ trên các dòng 141 đến 146

Việc triển khai Pygame Zero đã sử dụng 152 dòng mã để phân phối trò chơi giống như 182 dòng mã Pygame. Mặc dù số lượng dòng này có thể so sánh được, phiên bản Pygame Zero được cho là sạch hơn, nhiều mô-đun hơn và có thể dễ hiểu và viết mã hơn phiên bản Pygame

Tất nhiên, luôn có một cách nữa để viết game

Giải trí

Arcade là một khung Python hiện đại để tạo trò chơi với đồ họa và âm thanh hấp dẫn. Hướng đối tượng theo thiết kế, Arcade cung cấp cho các tác giả trò chơi một bộ công cụ hiện đại để tạo ra trải nghiệm trò chơi Python tuyệt vời

Được thiết kế bởi Giáo sư Paul Craven từ Đại học Simpson ở Iowa, Hoa Kỳ, Arcade được xây dựng trên thư viện đa phương tiện và cửa sổ pyglet. Nó cung cấp một tập hợp các cải tiến, hiện đại hóa và cải tiến có thể so sánh thuận lợi với cả Pygame và Pygame Zero

  • Hỗ trợ đồ họa OpenGL hiện đại
  • Hỗ trợ gợi ý kiểu Python 3
  • Có hỗ trợ cho các họa tiết hoạt hình dựa trên khung
  • Kết hợp các tên lệnh, chức năng và tham số nhất quán
  • Khuyến khích tách logic trò chơi khỏi mã hiển thị
  • Yêu cầu ít mã soạn sẵn hơn
  • Provides well-maintained and up-to-date documentation, including several tutorials and complete Python game examples
  • Có các công cụ vật lý tích hợp cho các trò chơi nền tảng và từ trên xuống

Arcade đang được phát triển liên tục, được hỗ trợ tốt trong cộng đồng và có một tác giả rất nhạy bén với các vấn đề, báo cáo lỗi và các bản sửa lỗi tiềm năng

Loại bỏ các quảng cáo

Cài đặt trò chơi điện tử

Để cài đặt Arcade và các phần phụ thuộc của nó, hãy sử dụng lệnh

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
7 thích hợp

[venv] $ python -m pygame.examples.aliens
0

Hướng dẫn cài đặt hoàn chỉnh dựa trên nền tảng của bạn có sẵn cho Windows, macOS và Linux. Bạn thậm chí có thể cài đặt

[venv] $ python pygame/pygame_basic.py
15 trực tiếp từ nguồn nếu muốn

Các khái niệm cơ bản

Mọi thứ trong Arcade diễn ra trong một cửa sổ được tạo với kích thước do người dùng xác định. Hệ tọa độ giả định rằng gốc tọa độ

[venv] $ python pygame/pygame_basic.py
16 nằm ở góc dưới bên trái của màn hình, với tọa độ y tăng dần khi bạn di chuyển lên trên. Điều này khác với nhiều công cụ trò chơi khác, đặt
[venv] $ python pygame/pygame_basic.py
16 ở phía trên bên trái và tăng tọa độ y di chuyển xuống dưới

Về bản chất, Arcade là một thư viện hướng đối tượng. Mặc dù có thể viết các ứng dụng Arcade theo thủ tục, nhưng sức mạnh thực sự của nó được bộc lộ khi bạn tạo mã hướng đối tượng hoàn toàn

Arcade, giống như Pygame Zero, cung cấp một vòng lặp trò chơi tích hợp và một mô hình sự kiện được xác định rõ ràng, vì vậy bạn sẽ có được mã trò chơi rất rõ ràng và dễ đọc. Cũng giống như Pygame Zero, Arcade cung cấp một lớp sprite mạnh mẽ hỗ trợ kết xuất, định vị và phát hiện va chạm. Ngoài ra, các họa tiết Arcade có thể được làm động bằng cách cung cấp nhiều hình ảnh

Mã cho ứng dụng Arcade cơ bản được liệt kê bên dưới được cung cấp trong mã nguồn của hướng dẫn là

[venv] $ python pygame/pygame_basic.py
18

Nguồn cho

[venv] $ python pygame/pygame_basic.py
18Hiển thị/Ẩn

[venv] $ python -m pygame.examples.aliens
1

Để chạy mã này, hãy sử dụng lệnh sau

[venv] $ python -m pygame.examples.aliens
2

Chương trình này vẽ một vài hình dạng và một số văn bản trên màn hình, như trong các ví dụ cơ bản đã trình bày trước đây

Như đã đề cập ở trên, các chương trình Arcade có thể được viết dưới dạng mã hướng đối tượng hoàn toàn. Lớp

[venv] $ python -m pygame.examples.aliens
00 được thiết kế để phân lớp theo các trò chơi của bạn, như được hiển thị trên dòng 20. Gọi
[venv] $ python -m pygame.examples.aliens
01 trên dòng 33 để đảm bảo cửa sổ trò chơi được thiết lập đúng cách

Arcade gọi trình xử lý sự kiện

[venv] $ python -m pygame.examples.aliens
02 được xác định trên các dòng 38 đến 70 một lần trên mỗi khung hình để hiển thị mọi thứ lên màn hình. Phương pháp này bắt đầu bằng một lệnh gọi tới
[venv] $ python -m pygame.examples.aliens
03, yêu cầu Arcade chuẩn bị cửa sổ để vẽ. Điều này có thể so sánh với lệnh gọi
[venv] $ python -m pygame.examples.aliens
04 được yêu cầu ở cuối bước vẽ Pygame

Mỗi phương pháp vẽ hình cơ bản trong Arcade bắt đầu bằng

[venv] $ python -m pygame.examples.aliens
05 và yêu cầu một dòng duy nhất để hoàn thành. Arcade có hỗ trợ vẽ tích hợp cho nhiều hình dạng

Arcade được tải với hàng trăm màu được đặt tên trong gói

[venv] $ python -m pygame.examples.aliens
06, nhưng bạn cũng có thể tự do chọn màu của riêng mình bằng cách sử dụng bộ dữ liệu RGB hoặc RGBA

Ứng dụng nâng cao

Để cho biết Arcade khác với các công cụ trò chơi khác như thế nào, bạn sẽ thấy cùng một trò chơi trước đây, hiện đã được triển khai trong Arcade. Xin nhắc lại, đây là những chi tiết chính của trò chơi

  • Người chơi là một nhân vật duy nhất trên màn hình, được điều khiển bằng cách di chuyển chuột
  • Theo các khoảng thời gian đều đặn, từng đồng xu xuất hiện trên màn hình
  • Khi người chơi di chuyển qua từng đồng xu, nó sẽ biến mất và người chơi được thưởng mười điểm
  • Khi trò chơi tiến triển, tiền xu được thêm vào nhanh hơn
  • Trò chơi kết thúc khi có hơn mười đồng xu hiển thị trên màn hình

Một lần nữa, trò chơi sẽ hoạt động giống như các ví dụ trước

Mã cho trò chơi Arcade đầy đủ được liệt kê bên dưới được cung cấp trong các tài liệu có thể tải xuống dưới dạng

[venv] $ python -m pygame.examples.aliens
07

Nguồn cho

[venv] $ python -m pygame.examples.aliens
07Hiển thị/Ẩn

[venv] $ python -m pygame.examples.aliens
3

Bản chất hướng đối tượng của Arcade cho phép bạn nhanh chóng triển khai các cấp độ khác nhau bằng cách tách riêng phần khởi tạo trò chơi khỏi phần khởi tạo của từng cấp độ khác nhau. Trò chơi được khởi tạo theo phương thức

[venv] $ python -m pygame.examples.aliens
09 trên các dòng từ 40 đến 63, trong khi các cấp độ được thiết lập và có thể bắt đầu lại bằng phương thức
[venv] $ python -m pygame.examples.aliens
10 riêng biệt trên các dòng từ 65 đến 85. Đây là một mẫu tuyệt vời để sử dụng ngay cả đối với các trò chơi có một cấp độ, như cấp độ này

Các họa tiết được xác định bằng cách tạo một đối tượng của lớp

[venv] $ python -m pygame.examples.aliens
11 và cung cấp đường dẫn đến một hình ảnh. Arcade hỗ trợ các đường dẫn pathlib, giúp dễ dàng tạo sprite của người chơi trên các dòng 72 đến 75

Việc tạo tiền mới được xử lý trên các dòng 78 đến 80, gọi phương thức

[venv] $ python -m pygame.examples.aliens
12 để gọi phương thức
[venv] $ python -m pygame.examples.aliens
13 theo định kỳ

Phương thức

[venv] $ python -m pygame.examples.aliens
14 được xác định trên các dòng 87 đến 120 tạo ra một sprite đồng xu mới tại một vị trí ngẫu nhiên và thêm nó vào danh sách để đơn giản hóa việc vẽ cũng như xử lý va chạm sau này

Để di chuyển trình phát bằng chuột, bạn thực hiện phương pháp

[venv] $ python -m pygame.examples.aliens
15 trên các dòng 122 đến 134. Phương pháp
[venv] $ python -m pygame.examples.aliens
16 đảm bảo tọa độ trung tâm của người chơi không bao giờ tắt trên màn hình

Việc kiểm tra va chạm giữa người chơi và đồng xu được xử lý theo phương pháp

[venv] $ python -m pygame.examples.aliens
17 trên các dòng 144 đến 156. Phương thức
[venv] $ python -m pygame.examples.aliens
18 trả về danh sách tất cả các sprite trong danh sách đã xung đột với sprite được chỉ định. Mã đi qua danh sách đó, tăng điểm và phát hiệu ứng âm thanh trước khi loại bỏ từng đồng xu khỏi trò chơi

Phương pháp

[venv] $ python -m pygame.examples.aliens
17 cũng kiểm tra xem có quá nhiều đồng xu trên màn hình ở các dòng 159 đến 168 không. Nếu vậy, nó kết thúc trò chơi

Việc triển khai Arcade này dễ đọc và có cấu trúc tốt như mã Pygame Zero, mặc dù nó chiếm nhiều mã hơn 27%, với 194 dòng được viết. Mã dài hơn có thể đáng giá, vì Arcade cung cấp nhiều tính năng hơn không được trình bày ở đây, chẳng hạn như

  • nhân vật hoạt hình
  • Một số động cơ vật lý tích hợp
  • Hỗ trợ cho bản đồ trò chơi của bên thứ ba
  • Cập nhật hệ thống hạt và đổ bóng

Các tác giả trò chơi mới đến từ Python Zero sẽ thấy Arcade có cấu trúc tương tự trong khi cung cấp các tính năng mở rộng và mạnh mẽ hơn

Loại bỏ các quảng cáo

cuộc phiêu lưu

Tất nhiên, không phải trò chơi nào cũng yêu cầu một người chơi đầy màu sắc di chuyển trên màn hình, tránh chướng ngại vật và tiêu diệt kẻ xấu. Các trò chơi máy tính cổ điển như Zork đã thể hiện sức mạnh của cách kể chuyện hay trong khi vẫn mang lại trải nghiệm chơi trò chơi tuyệt vời. Crafting these text-based games, also called interactive fiction, can be difficult in any language. May mắn cho lập trình viên Python là có adventurelib

adventurelib cung cấp chức năng cơ bản để viết các trò chơi phiêu lưu dựa trên văn bản, với mục đích giúp thanh thiếu niên dễ dàng thực hiện. [Nguồn]

Nó không chỉ dành cho thanh thiếu niên, mặc dù. adventurelib rất phù hợp cho những ai muốn viết một trò chơi dựa trên văn bản mà không cần phải viết một trình phân tích cú pháp ngôn ngữ tự nhiên để làm như vậy

adventurelib được tạo ra bởi những người đứng sau Pygame Zero và giải quyết các chủ đề khoa học máy tính nâng cao hơn như quản lý trạng thái, logic nghiệp vụ, đặt tên và tham chiếu cũng như thao tác thiết lập, v.v. Điều này làm cho nó trở thành một bước tiếp theo tuyệt vời cho các nhà giáo dục, phụ huynh và người cố vấn giúp những người trẻ tuổi học khoa học máy tính thông qua việc tạo ra các trò chơi. Nó cũng tuyệt vời để mở rộng kỹ năng viết mã trò chơi của riêng bạn

cài đặt phiêu lưulib

adventurelib có sẵn trên PyPI và có thể được cài đặt bằng lệnh

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
7 thích hợp

[venv] $ python -m pygame.examples.aliens
4

phiêu lưulib là một tệp duy nhất, do đó, nó cũng có thể được tải xuống từ repo GitHub, được lưu trong cùng thư mục với trò chơi của bạn và được sử dụng trực tiếp

Các khái niệm cơ bản

Để tìm hiểu kiến ​​thức cơ bản về adventurelib, bạn sẽ thấy một trò chơi nhỏ có ba phòng và chìa khóa để mở cánh cửa đến căn phòng cuối cùng bên dưới. Mã cho trò chơi mẫu này được cung cấp trong các tài liệu có thể tải xuống trong

[venv] $ python -m pygame.examples.aliens
21

Nguồn cho

[venv] $ python -m pygame.examples.aliens
21Hiển thị/Ẩn

[venv] $ python -m pygame.examples.aliens
5

Để chạy mã này, hãy sử dụng lệnh sau

[venv] $ python -m pygame.examples.aliens
6

Các trò chơi dựa trên văn bản chủ yếu dựa vào việc phân tích cú pháp đầu vào của người dùng để thúc đẩy trò chơi tiến lên. adventurelib xác định văn bản mà người chơi nhập dưới dạng lệnh và cung cấp trình trang trí

[venv] $ python -m pygame.examples.aliens
23 để xác định lệnh

Một ví dụ điển hình về lệnh là lệnh

[venv] $ python -m pygame.examples.aliens
24 được định nghĩa trên các dòng 113 đến 125. Trình trang trí
[venv] $ python -m pygame.examples.aliens
25 thêm văn bản
[venv] $ python -m pygame.examples.aliens
24 vào danh sách các lệnh hợp lệ và kết nối nó với hàm
[venv] $ python -m pygame.examples.aliens
27. Bất cứ khi nào người chơi gõ
[venv] $ python -m pygame.examples.aliens
24, adventurelib sẽ gọi hàm
[venv] $ python -m pygame.examples.aliens
27

Các lệnh không phân biệt chữ hoa chữ thường khi người chơi nhập. Người chơi có thể gõ

[venv] $ python -m pygame.examples.aliens
24,
[venv] $ python -m pygame.examples.aliens
31,
[venv] $ python -m pygame.examples.aliens
32, hoặc thậm chí là
[venv] $ python -m pygame.examples.aliens
33, và adventurelib sẽ tìm đúng lệnh

Nhiều lệnh đều có thể sử dụng cùng một hàm, như đã thấy với hàm

[venv] $ python -m pygame.examples.aliens
34 trên các dòng 78 đến 110. Chức năng này được trang trí bằng chín lệnh riêng biệt, cho phép người chơi di chuyển theo nhiều cách khác nhau trong thế giới trò chơi. Trong ví dụ chơi trò chơi bên dưới, các lệnh
[venv] $ python -m pygame.examples.aliens
35,
[venv] $ python -m pygame.examples.aliens
36 và
[venv] $ python -m pygame.examples.aliens
37 đều được sử dụng, nhưng mỗi lệnh đều dẫn đến cùng một chức năng được gọi

Đôi khi các lệnh mà người chơi nhập được hướng đến một mục cụ thể. Ví dụ: người chơi có thể muốn nhìn vào một thứ cụ thể hoặc đi theo một hướng cụ thể. The game designer can capture additional command context by specifying capitalized words in the

[venv] $ python -m pygame.examples.aliens
23 decorator. These are treated as variable names, and the text that the player types in their place are the values

This can be seen in the

[venv] $ python -m pygame.examples.aliens
39 function on lines 128 to 137. This function defines a single string parameter called
[venv] $ python -m pygame.examples.aliens
40. In the
[venv] $ python -m pygame.examples.aliens
23 decorators defining the
[venv] $ python -m pygame.examples.aliens
42 and
[venv] $ python -m pygame.examples.aliens
43 commands, the word
[venv] $ python -m pygame.examples.aliens
44 acts as a placeholder for any text following the command. This text is then passed to the
[venv] $ python -m pygame.examples.aliens
39 function as the
[venv] $ python -m pygame.examples.aliens
40 parameter. For example, if the player types
[venv] $ python -m pygame.examples.aliens
47, then the parameter
[venv] $ python -m pygame.examples.aliens
40 will get the value
[venv] $ python -m pygame.examples.aliens
49

The strength of a text-based game relies on the descriptiveness of its text. While you can and should certainly use

[venv] $ python -m pygame.examples.aliens
50 functions, printing numerous lines of text in response to user commands can introduce difficulties spanning text over multiple lines and determining line breaks. adventurelib eases this burden with the
[venv] $ python -m pygame.examples.aliens
51 function, which works well with triple-quoted multiline strings

You can see the

[venv] $ python -m pygame.examples.aliens
51 function in action on line 118 in the
[venv] $ python -m pygame.examples.aliens
27 function. Whenever the player types
[venv] $ python -m pygame.examples.aliens
24, the
[venv] $ python -m pygame.examples.aliens
51 function outputs the description of the current room to the console

Of course, your commands need places to occur. adventurelib provides the

[venv] $ python -m pygame.examples.aliens
56 class to define different areas of your game world. Rooms are created by providing a description of the room, and they can be connected to other rooms by using the
[venv] $ python -m pygame.examples.aliens
57,
[venv] $ python -m pygame.examples.aliens
58,
[venv] $ python -m pygame.examples.aliens
59, and
[venv] $ python -m pygame.examples.aliens
60 properties. You can also define custom properties that apply to either the entire
[venv] $ python -m pygame.examples.aliens
56 class or individual objects

The three rooms in this game are created on lines 15 to 35. The

[venv] $ python -m pygame.examples.aliens
62 constructor accepts a description as a string, or in this case, as a multiline string. Once you’ve created the rooms, then you connect them on lines 38 to 39. Setting
[venv] $ python -m pygame.examples.aliens
63 to
[venv] $ python -m pygame.examples.aliens
64 implies that
[venv] $ python -m pygame.examples.aliens
65 will be
[venv] $ python -m pygame.examples.aliens
66. adventurelib is smart enough to make this connection automatically

You also create a constraint on line 44 to indicate a locked door between the living room and the front porch. Unlocking this door will require the player to locate an item

Text-based games often feature items which must be collected to open new areas of the game or to solve certain puzzles. Items can also represent non-player characters with whom the player can interact. adventurelib provides the

[venv] $ python -m pygame.examples.aliens
67 class to define both collectable items and non-player characters by their names and aliases. For example, the alias
[venv] $ python -m pygame.examples.aliens
68 refers to the front door key

On line 63, you define the

[venv] $ python -m pygame.examples.aliens
68 used to unlock the door between the living room and the front porch. The
[venv] $ python -m pygame.examples.aliens
70 constructor takes one or more strings. The first is the default or full name of the item, and it’s used when printing the name of the item. All other names are used as aliases so the player doesn’t have to type the full name of the item

The

[venv] $ python -m pygame.examples.aliens
68 doesn’t just have a name and aliases. It also has an intended use, which is defined on line 64.
[venv] $ python -m pygame.examples.aliens
72 refers to a function that will be called when a player tries to use the item by typing
[venv] $ python -m pygame.examples.aliens
73. This function is called in the
[venv] $ python -m pygame.examples.aliens
74 command handler defined on lines 159 to 175

Collections of items, such as the player’s inventory or items on the ground in a room, can be stored in a

[venv] $ python -m pygame.examples.aliens
75 object. You can add items to the bag, remove items from the bag, and inspect the bag’s contents.
[venv] $ python -m pygame.examples.aliens
75 objects are iterable in Python, so you can also use
[venv] $ python -m pygame.examples.aliens
77 to test if something is in the bag and loop over the bag’s contents in a
[venv] $ python -m pygame.examples.aliens
78 loop

Four different

[venv] $ python -m pygame.examples.aliens
75 objects are defined on lines 67 to 75. Each of the three rooms has a
[venv] $ python -m pygame.examples.aliens
75 to hold items in the room, and the player also has a
[venv] $ python -m pygame.examples.aliens
75 to hold their
[venv] $ python -m pygame.examples.aliens
82 of items they pick up. The
[venv] $ python -m pygame.examples.aliens
68 item is placed in its starting location in the
[venv] $ python -m pygame.examples.aliens
66

Items are added to the player’s inventory by the

[venv] $ python -m pygame.examples.aliens
85 function defined on lines 140 to 156. When the player types
[venv] $ python -m pygame.examples.aliens
86, you attempt to
[venv] $ python -m pygame.examples.aliens
87 the item from the room’s
[venv] $ python -m pygame.examples.aliens
88 bag on line 151. If the
[venv] $ python -m pygame.examples.aliens
68 is returned, it’s also removed from the room’s
[venv] $ python -m pygame.examples.aliens
88. You then add the
[venv] $ python -m pygame.examples.aliens
68 to the player’s
[venv] $ python -m pygame.examples.aliens
82 on line 156

Loại bỏ các quảng cáo

Ứng dụng nâng cao

Of course, there’s much more to adventurelib. To show off its other capabilities, you’ll craft a more involved text adventure with the following backstory

  • You live in a small, quiet hamlet
  • Gần đây, những người hàng xóm của bạn đã bắt đầu phàn nàn về việc gia súc bị mất tích
  • As a member of a night patrol, you notice a broken fence and a trail leading away from it
  • You decide to investigate, armed only with a wooden practice sword

The game has several areas to describe and define

  • Your small, quiet hamlet
  • The trail leading away from the field
  • A nearby village where you can buy a better weapon
  • A side path leading to a wizard who can enchant your weapon
  • A cave containing the giant who has been taking your livestock

There are several items to collect, such as weapons and food, and characters with which to interact. You also need a basic battle system to allow you to fight the giant and win the game

All of the code for this game is listed below, and can be found in the downloaded materials

Nhận mã nguồn. Nhấp vào đây để lấy mã nguồn mà bạn sẽ sử dụng để dùng thử công cụ trò chơi Python

Để giữ mọi thứ ngăn nắp, bạn chia trò chơi của mình thành các tệp khác nhau

  • [venv] $ python -m pygame.examples.aliens
    
    93 defines the rooms and areas
  • [venv] $ python -m pygame.examples.aliens
    
    94 định nghĩa các mục và thuộc tính của chúng
  • [venv] $ python -m pygame.examples.aliens
    
    95 xác định các ký tự mà bạn có thể tương tác
  • [venv] $ python -m pygame.examples.aliens
    
    96 kéo mọi thứ lại với nhau, thêm lệnh và bắt đầu trò chơi

Source for

[venv] $ python -m pygame.examples.aliens
96Show/Hide

[venv] $ python -m pygame.examples.aliens
7

Source for

[venv] $ python -m pygame.examples.aliens
93Show/Hide

[venv] $ python -m pygame.examples.aliens
8

Nguồn cho

[venv] $ python -m pygame.examples.aliens
94Hiển thị/Ẩn

[venv] $ python -m pygame.examples.aliens
9

Nguồn cho

[venv] $ python -m pygame.examples.aliens
95Hiển thị/Ẩn

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
0

Bạn có thể bắt đầu trò chơi này bằng lệnh sau

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
1

Sau khi xác định cốt truyện, bạn đã vạch ra các khu vực trò chơi khác nhau và các con đường mà người chơi sử dụng để di chuyển giữa chúng

Mỗi khu vực có các thuộc tính khác nhau được liên kết với nó, bao gồm

  • Các vật phẩm và nhân vật trong khu vực đó
  • Một số lối ra đã bị khóa
  • Tiêu đề, mô tả ngắn và mô tả dài hơn
  • Một dấu hiệu cho biết người chơi đã ở trong khu vực này hay chưa

Để đảm bảo rằng mỗi khu vực có phiên bản riêng của từng thuộc tính này, bạn tạo một lớp con của

[venv] $ python -m pygame.examples.aliens
56 có tên là
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
02 trong
[venv] $ python -m pygame.examples.aliens
93 trên các dòng từ 15 đến 41. Các mục trong mỗi phòng được lưu trữ trong một đối tượng
[venv] $ python -m pygame.examples.aliens
75 được gọi là
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
05, trong khi các ký tự được lưu trữ trong
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
06, được xác định trên các dòng 28 và 31. Bây giờ bạn có thể tạo các đối tượng
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
02, mô tả chúng và điền vào chúng các mục và ký tự duy nhất, tất cả đều được nhập vào dòng 9 và 12

Một số vật phẩm trò chơi được yêu cầu để hoàn thành trò chơi, trong khi những vật phẩm khác chỉ ở đó để tạo hương vị và thêm hứng thú. Các mục hương vị được xác định và đặt trên các dòng 192 đến 194, theo sau là các ký tự trên các dòng 197 đến 200

Tất cả các vật phẩm trong trò chơi của bạn được định nghĩa trong

[venv] $ python -m pygame.examples.aliens
94 là đối tượng thuộc loại
[venv] $ python -m pygame.examples.aliens
70. Các vật phẩm trò chơi có các thuộc tính xác định chúng, nhưng vì bạn đã sử dụng lớp cơ sở
[venv] $ python -m pygame.examples.aliens
67 nên một số thuộc tính chung cơ bản được thêm vào lớp trên các dòng từ 9 đến 12. These properties are used when the item is created. Ví dụ: đối tượng
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
11 được tạo từ dòng 15 đến dòng 19 và xác định từng thuộc tính chung khi nó được tạo

Tuy nhiên, một số mặt hàng có các thuộc tính cụ thể dành riêng cho mặt hàng đó. Ví dụ: các vật phẩm

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
12 và
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
13 cần có thuộc tính để theo dõi thiệt hại mà chúng gây ra và mọi phần thưởng ma thuật mà chúng mang theo. Chúng được thêm vào các dòng 43 đến 44 và 53 đến 54

Tương tác với các nhân vật giúp thúc đẩy câu chuyện trò chơi tiến lên và thường mang lại cho người chơi lý do để khám phá. Các nhân vật trong adventurelib được tạo dưới dạng đối tượng

[venv] $ python -m pygame.examples.aliens
67 và đối với trò chơi này được định nghĩa trong
[venv] $ python -m pygame.examples.aliens
95

Mỗi nhân vật, giống như mỗi vật phẩm, có các thuộc tính phổ biến được liên kết với nó, chẳng hạn như mô tả dài và lời chào được sử dụng khi người chơi gặp nó lần đầu tiên. Các thuộc tính này được khai báo trên dòng 9 và 10 và chúng được xác định cho từng ký tự khi ký tự được tạo

Tất nhiên, nếu bạn có các nhân vật, thì người chơi sẽ nói chuyện và tương tác với họ. Bạn nên biết khi nào bạn đang tương tác với một nhân vật và khi nào bạn chỉ ở trong cùng một khu vực trò chơi với một nhân vật.

Điều này được thực hiện bằng cách sử dụng một khái niệm phiêu lưu được gọi là bối cảnh. Bối cảnh cho phép bạn bật các lệnh khác nhau cho các tình huống khác nhau. Chúng cũng cho phép một số lệnh nhất định hoạt động khác đi và chúng theo dõi thông tin bổ sung về các hành động mà người chơi có thể đã thực hiện

Khi trò chơi bắt đầu, không có bối cảnh nào được đặt. Khi người chơi tiến bộ, lần đầu tiên họ chạm trán với Elder Barron. Khi người chơi gõ

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
16, bối cảnh được đặt thành
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
17, trong trường hợp này là
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
18

Lời chào của Elder Barron kết thúc bằng câu hỏi có hoặc không cho người chơi. Nếu người chơi gõ

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
19, thì trình xử lý lệnh trên dòng 173 trong
[venv] $ python -m pygame.examples.aliens
96 được kích hoạt, được xác định là
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
21, như minh họa bên dưới

Sau đó, khi người chơi đang nói chuyện với thợ rèn, bối cảnh ở cấp độ thứ hai sẽ được thêm vào để phản ánh rằng họ đang tham gia vào một giao dịch vũ khí có thể xảy ra. Dòng 203 đến 233 xác định cuộc thảo luận với người thợ rèn, trong đó bao gồm đề nghị mua bán vũ khí. Một bối cảnh mới được xác định trên dòng 222, cho phép sử dụng cùng một lệnh

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
19 một cách tao nhã theo nhiều cách

Bạn cũng có thể kiểm tra ngữ cảnh trong trình xử lý lệnh. Ví dụ: người chơi không thể rời khỏi cuộc chiến với người khổng lồ bằng cách kết thúc cuộc trò chuyện. Trình xử lý lệnh

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
23 được xác định trên các dòng 389 đến 403 kiểm tra xem người chơi có ở trong ngữ cảnh
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
24 hay không, được nhập vào khi họ bắt đầu chiến đấu với người khổng lồ. Nếu vậy, họ không được phép dừng cuộc trò chuyện - đó là một cuộc chiến sinh tử

Bạn cũng có thể đặt câu hỏi cho người chơi yêu cầu câu trả lời cụ thể. Khi người chơi nói chuyện với thuật sĩ Trent, họ được yêu cầu giải một câu đố. Trả lời sai sẽ kết thúc tương tác. Mặc dù câu trả lời đúng được xử lý bằng trình xử lý lệnh trên các dòng từ 252 đến 262, một trong những câu trả lời sai gần như vô hạn sẽ không khớp với bất kỳ trình xử lý nào

Các lệnh không khớp được xử lý bởi hàm

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
25 trên các dòng 497 đến 503. Bạn tận dụng điều này để xử lý các câu trả lời sai cho câu đố của thuật sĩ bằng cách kiểm tra ngữ cảnh
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
26 trên dòng 498. Bất kỳ câu trả lời sai nào cho câu đố sẽ dẫn đến việc thuật sĩ kết thúc cuộc trò chuyện. Bạn kết nối điều này với adventurelib trên dòng 523 bằng cách đặt
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
27 thành chức năng mới của bạn

Bạn có thể tùy chỉnh lời nhắc hiển thị cho trình phát bằng cách viết một hàm trả về lời nhắc mới. Lời nhắc mới của bạn được xác định trên các dòng 483 đến 495 và được kết nối với adventurelib trên dòng 520

Of course, there’s always more that you could add. Creating a complete text adventure game is challenging, and adventurelib makes sure the the main challenge lies in painting a picture with words

Loại bỏ các quảng cáo

Ren’Py

The modern descendant of the pure text adventure is the visual novel, which highlights the storytelling aspect of the game, limiting player interactions while adding visuals and sound to heighten the experience. Visual novels are the graphic novels of the game world — modern, innovative, and extremely compelling to create and consume

Ren’Py is a tool based on Pygame and designed specifically to create visual novels. Ren’Py takes its name from the Japanese word for romantic love and provides tools and a framework for crafting compelling visual novels

To be fair, Ren’Py is not strictly a Python library that you can

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
28 and use. Ren’Py games are created using the Ren’Py Launcher, which comes with the full Ren’Py SDK. This launcher also features a game editor, although you can edit your game in your editor of choice. Ren’Py also features its own scripting language for game creation. However, Ren’Py is based on Pygame, and it’s extendable using Python, which warrants its appearance here

Ren’Py Installation

As mentioned, Ren’Py requires not only the SDK, but also the Ren’Py Launcher. These are packaged together in a single unit, which you need to download

Knowing which package to download and how to install it depends on your platform. Ren’Py provides installers and instructions for Windows, macOS, and Linux users

  • Windows
  • Linux
  • macOS

Windows users should download the provided executable, then run it to install the SDK and the Ren’Py Launcher

Linux users should download the provided tarball to a convenient location, then expand it using

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
29

macOS users should download the DMG file provided, double-click the file to open it, and copy the contents to a convenient location

After the package is installed, you can navigate to the folder containing the SDK then run the Ren’Py Launcher. Windows users should use

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
30, while macOS and Linux users should run
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
31. This will start the Ren’Py Launcher for the first time

Đây là nơi bạn sẽ bắt đầu các dự án Ren'Py mới, làm việc trên các dự án hiện có và đặt các tùy chọn tổng thể cho Ren'Py

Các khái niệm cơ bản

Trò chơi Ren'Py bắt đầu dưới dạng dự án mới trong Trình khởi chạy Ren'Py. Tạo một cái sẽ thiết lập cấu trúc tệp và thư mục thích hợp cho trò chơi Ren'Py. Sau khi dự án được thiết lập, bạn có thể sử dụng trình chỉnh sửa của riêng mình để viết trò chơi của mình, mặc dù cần phải có Trình khởi chạy Ren'Py để chạy trò chơi

Các trò chơi Ren'Py được chứa trong các tệp có tên là tập lệnh. Đừng nghĩ về các tập lệnh Ren'Py như cách bạn nghĩ về các tập lệnh shell. Chúng tương tự như kịch bản cho các vở kịch hoặc chương trình truyền hình. Chữ viết Ren’Py có phần mở rộng là

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
32 và được viết bằng ngôn ngữ Ren’Py. Trò chơi của bạn có thể bao gồm bao nhiêu tập lệnh tùy thích, tất cả đều được lưu trữ trong thư mục con
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
33 của thư mục dự án của bạn

Khi bạn tạo một dự án Ren’Py mới, các tập lệnh sau sẽ được tạo để bạn sử dụng và cập nhật

  •  1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    34, xác định giao diện của tất cả các thành phần giao diện người dùng được sử dụng trong trò chơi của bạn
  •  1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    35, xác định các tùy chọn có thể thay đổi để tùy chỉnh trò chơi của bạn
  •  1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    36, xác định các kiểu được sử dụng cho hội thoại, menu và đầu ra trò chơi khác
  •  1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    37, đó là nơi bạn bắt đầu viết trò chơi của mình

Để chạy các trò chơi mẫu từ các tài liệu đã tải xuống cho hướng dẫn này, bạn sẽ sử dụng quy trình sau

  1. Khởi động Trình khởi chạy Ren'Py
  2. Nhấp vào Tùy chọn, sau đó nhấp vào Thư mục dự án
  3. Thay đổi Thư mục dự án thành thư mục
     1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    38 trong kho lưu trữ mà bạn đã tải xuống
  4. Nhấp vào Quay lại để quay lại trang Trình khởi chạy Ren'Py chính

Bạn sẽ thấy

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
39 và
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
40 trong danh sách Dự án ở bên trái. Chọn cái mà bạn muốn chạy, sau đó nhấp vào Launch Project

Đối với ví dụ này, bạn sẽ chỉ sửa đổi tệp

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
37 cho
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
39. Bạn có thể tìm thấy mã hoàn chỉnh cho trò chơi này trong các tài liệu đã tải xuống, cũng như bên dưới

Nguồn cho

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
37Hiển thị/Ẩn

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
2

Nhãn xác định các điểm vào câu chuyện của bạn và thường được sử dụng để bắt đầu các cảnh mới và cung cấp các đường dẫn thay thế xuyên suốt câu chuyện. Tất cả các trò chơi Ren'Py bắt đầu chạy ở dòng

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
44, có thể xuất hiện trong bất kỳ tập lệnh nào bạn chọn. Bạn có thể thấy điều này trên dòng 12 của
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
37

Bạn cũng có thể sử dụng nhãn để xác định hình nền, thiết lập chuyển tiếp giữa các cảnh và kiểm soát sự xuất hiện của các ký tự. Trong mẫu, cảnh thứ hai bắt đầu ở dòng 54 với dòng

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
46

Văn bản đặt trong dấu ngoặc kép trên một dòng được gọi là câu lệnh say. Một chuỗi trên một dòng được coi là tường thuật. Hai chuỗi được coi là đối thoại, trước tiên xác định một ký tự và sau đó cung cấp dòng mà chúng đang nói

Khi bắt đầu trò chơi, tường thuật được nhìn thấy ở dòng 16, tạo bối cảnh. Đối thoại được cung cấp trên dòng 18, khi mẹ bạn gọi cho bạn

Bạn có thể xác định các nhân vật bằng cách đặt tên cho họ trong câu chuyện. Tuy nhiên, bạn cũng có thể xác định các ký tự ở đầu tập lệnh của mình. Bạn có thể thấy điều này trên dòng 6 đến 8, nơi bạn, anh trai Kevin và mẹ của bạn được xác định. Câu lệnh

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
47 khởi tạo ba biến là
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
48, đặt cho chúng một tên hiển thị, theo sau là màu văn bản được sử dụng để hiển thị tên

Tất nhiên, đây là visual novel nên Ren’Py sẽ có cách xử lý hình ảnh. Giống như Pygame Zero, Ren'Py yêu cầu tất cả hình ảnh và âm thanh được sử dụng trong trò chơi phải nằm trong các thư mục cụ thể. Hình ảnh được tìm thấy trong thư mục

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
49 và âm thanh nằm trong thư mục
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
50. Trong kịch bản trò chơi, bạn gọi chúng bằng tên tệp mà không có bất kỳ phần mở rộng tệp nào

Dòng 25 thể hiện điều này bằng hành động, khi bạn mở mắt và nhìn thấy phòng ngủ của mình lần đầu tiên. Từ khóa

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
51 xóa màn hình, sau đó hiển thị hình ảnh
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
52. Ren'Py hỗ trợ các định dạng hình ảnh JPG, WEBP và PNG

Bạn cũng có thể hiển thị các ký tự trên màn hình bằng cách sử dụng từ khóa

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
53 và quy ước đặt tên tương tự cho hình ảnh. Dòng 41 hiển thị hình ảnh của anh trai Kevin của bạn, được lưu dưới dạng
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
54

Of course, it’s not much of a game if you can’t make decisions to affect the outcome. Trong Ren’Py, người chơi đưa ra các lựa chọn từ menu được hiển thị cho họ trong quá trình chơi trò chơi. Trò chơi phản ứng bằng cách chuyển đến các nhãn được xác định trước, thay đổi hình ảnh nhân vật, phát âm thanh hoặc thực hiện các hành động khác khi cần thiết

Một lựa chọn cơ bản trong mẫu này được hiển thị trên các dòng 47 đến 52, nơi bạn nhận ra mình đã quên điện thoại. Trong một câu chuyện đầy đủ hơn, sự lựa chọn này có thể dẫn đến hậu quả sau này

Tất nhiên, bạn có thể làm được nhiều hơn thế với Ren’Py. Bạn có thể kiểm soát quá trình chuyển đổi giữa các cảnh, yêu cầu nhân vật vào và rời khỏi cảnh theo những cách cụ thể, đồng thời bao gồm âm thanh và nhạc cho trò chơi của bạn. Ren'Py cũng hỗ trợ viết mã Python phức tạp hơn, bao gồm sử dụng các kiểu dữ liệu Python và thực hiện các lệnh gọi hàm Python trực tiếp. Bây giờ hãy xem xét kỹ hơn các khả năng này trong một ứng dụng nâng cao hơn

Loại bỏ các quảng cáo

Ứng dụng nâng cao

Để thể hiện chiều sâu của Ren'Py, bạn sẽ triển khai trò chơi tương tự như bạn đã làm với adventurelib. Xin nhắc lại, đây là thiết kế cơ bản của trò chơi đó

  • You live in a small, quiet hamlet
  • Gần đây, những người hàng xóm của bạn đã bắt đầu phàn nàn về việc gia súc bị mất tích
  • As a member of a night patrol, you notice a broken fence and a trail leading away from it
  • You decide to investigate, armed only with a wooden practice sword

Trò chơi có một số khu vực để xác định và cung cấp hình ảnh cho. Ví dụ: bạn sẽ cần hình ảnh và định nghĩa cho ngôi làng nhỏ, yên tĩnh của mình, con đường mòn dẫn ra khỏi cánh đồng, ngôi làng gần đó nơi bạn có thể mua vũ khí tốt hơn, con đường phụ dẫn đến một phù thủy có thể yểm bùa cho vũ khí của bạn và

Ngoài ra còn có một vài ký tự để xác định và cung cấp hình ảnh cho. Bạn cần một thợ rèn có thể cung cấp cho bạn vũ khí tốt hơn, một phù thủy có thể phù phép vũ khí của bạn và tên khổng lồ mà bạn cần đánh bại

Đối với ví dụ này, bạn sẽ tạo bốn tập lệnh riêng biệt

  •  1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    37, đó là nơi trò chơi bắt đầu
  •  1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    56, trong đó có câu chuyện về ngôi làng gần đó
  •  1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    57, chứa con đường giữa các làng
  •  1"""
     2Basic "Hello, World!" program in Pygame
     3
     4This program is designed to demonstrate the basic capabilities
     5of Pygame. It will:
     6- Create a game window
     7- Fill the background with white
     8- Draw some basic shapes in different colors
     9- Draw some text in a specified size and color
    10- Allow you to close the window
    11"""
    12
    13# Import and initialize the pygame library
    14import pygame
    15
    16pygame.init[]
    17
    18# Set the width and height of the output window, in pixels
    19WIDTH = 800
    20HEIGHT = 600
    21
    22# Set up the drawing window
    23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
    24
    25# Run until the user asks to quit
    26running = True
    27while running:
    28
    29    # Did the user click the window close button?
    30    for event in pygame.event.get[]:
    31        if event.type == pygame.QUIT:
    32            running = False
    33
    34    # Fill the background with white
    35    screen.fill[[255, 255, 255]]
    36
    37    # Draw a blue circle with a radius of 50 in the center of the screen
    38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
    39
    40    # Draw a red-outlined square in the top-left corner of the screen
    41    red_square = pygame.Rect[[50, 50], [100, 100]]
    42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
    43
    44    # Draw an orange caption along the bottom in 60-point font
    45    text_font = pygame.font.SysFont["any_font", 60]
    46    text_block = text_font.render[
    47        "Hello, World! From Pygame", False, [200, 100, 0]
    48    ]
    49    screen.blit[text_block, [50, HEIGHT - 50]]
    50
    51    # Flip the display
    52    pygame.display.flip[]
    53
    54# Done! Time to quit.
    55pygame.quit[]
    
    58, chứa logic của trận chiến khổng lồ

Bạn có thể tạo cuộc gặp gỡ thuật sĩ như một bài tập độc lập

Bạn có thể tìm thấy mã hoàn chỉnh cho trò chơi này trong các tài liệu đã tải xuống tại

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
59 và cũng có sẵn bên dưới

Nguồn cho

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
37Hiển thị/Ẩn

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
3

Nguồn cho

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
56Hiển thị/Ẩn

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
4

Nguồn cho

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
57Hiển thị/Ẩn

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
5

Nguồn cho

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
58Hiển thị/Ẩn

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
6

Như trong ví dụ trước, bạn định nghĩa các đối tượng

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
64 trước khi tập lệnh bắt đầu từ dòng 14 đến 17 của
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
37

Bạn cũng có thể xác định đối tượng nền hoặc ký tự

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
66 để sử dụng sau này. Các dòng 21 đến 27 xác định một số hình ảnh mà bạn tham khảo sau, cả để sử dụng làm hình nền và để hiển thị dưới dạng mục. Sử dụng cú pháp này cho phép bạn gán tên nội bộ ngắn hơn và mang tính mô tả hơn cho hình ảnh. Sau đó, bạn sẽ thấy chúng được hiển thị như thế nào

Bạn cũng cần theo dõi khả năng của vũ khí được trang bị. Điều này được thực hiện trên các dòng 31 đến 37, sử dụng các giá trị biến

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
67 mà bạn sẽ sử dụng trong trận chiến khổng lồ sau này

Để cho biết vũ khí nào được bật, bạn hiển thị hình ảnh dưới dạng biểu thức. Biểu cảm của Ren'Py là những hình ảnh nhỏ hiển thị ở các góc của cửa sổ trò chơi và được sử dụng để hiển thị nhiều thông tin khác nhau. Đối với trò chơi này, bạn sử dụng một biểu thức để hiển thị vũ khí bằng cách sử dụng

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
68 đầu tiên trên các dòng 67 và 68

Lệnh

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
53 có một số công cụ sửa đổi được ghi lại. Công cụ sửa đổi
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
70 làm cho hình ảnh
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
71 trượt trên màn hình từ bên trái. Ngoài ra, điều quan trọng cần nhớ là mỗi khi
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
51 thay đổi, toàn bộ màn hình sẽ bị xóa, yêu cầu bạn hiển thị lại vũ khí hiện tại. Bạn có thể thấy điều đó trên các dòng 75 đến 78

Khi bạn vào thị trấn vào năm

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
56, bạn gặp người thợ rèn, người này chào đón bạn

Thợ rèn mang đến cho bạn cơ hội nâng cấp vũ khí của mình. Nếu bạn chọn làm như vậy, thì bạn cập nhật các giá trị cho

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
71 và chỉ số vũ khí. Điều này được thực hiện trên các dòng 93 đến 98

Các dòng bắt đầu bằng ký tự

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
75 được Ren'Py hiểu là các câu lệnh Python, cho phép bạn viết mã Python tùy ý khi cần. Việc cập nhật
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
71 và chỉ số vũ khí được thực hiện bằng cách sử dụng ba câu lệnh Python từ dòng 96 đến 98, thay đổi giá trị của các biến
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
67 mà bạn đã xác định ở đầu
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
37

Bạn cũng có thể xác định một khối mã Python lớn bằng cách sử dụng phần

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
79, như được hiển thị trong
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
58 bắt đầu từ dòng 41

Các dòng 43 đến 61 chứa chức năng trợ giúp để hiển thị tình trạng của người khổng lồ, dựa trên các điểm đánh còn lại của người khổng lồ. Nó sử dụng phương thức

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
81 để xuất tường thuật trở lại cửa sổ Ren'Py chính. Một chức năng trợ giúp tương tự để hiển thị tình trạng của người chơi được nhìn thấy trên các dòng 63 đến 85

Trận chiến được điều khiển bởi

 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
82 trên các dòng 87 đến 159. Vòng lặp trò chơi được triển khai trên dòng 98 và được điều khiển bởi biến
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
83. Các lựa chọn chiến đấu hoặc chạy trốn của người chơi được hiển thị bằng phương pháp
 1"""
 2Basic "Hello, World!" program in Pygame
 3
 4This program is designed to demonstrate the basic capabilities
 5of Pygame. It will:
 6- Create a game window
 7- Fill the background with white
 8- Draw some basic shapes in different colors
 9- Draw some text in a specified size and color
10- Allow you to close the window
11"""
12
13# Import and initialize the pygame library
14import pygame
15
16pygame.init[]
17
18# Set the width and height of the output window, in pixels
19WIDTH = 800
20HEIGHT = 600
21
22# Set up the drawing window
23screen = pygame.display.set_mode[[WIDTH, HEIGHT]]
24
25# Run until the user asks to quit
26running = True
27while running:
28
29    # Did the user click the window close button?
30    for event in pygame.event.get[]:
31        if event.type == pygame.QUIT:
32            running = False
33
34    # Fill the background with white
35    screen.fill[[255, 255, 255]]
36
37    # Draw a blue circle with a radius of 50 in the center of the screen
38    pygame.draw.circle[screen, [0, 0, 255], [WIDTH // 2, HEIGHT // 2], 50]
39
40    # Draw a red-outlined square in the top-left corner of the screen
41    red_square = pygame.Rect[[50, 50], [100, 100]]
42    pygame.draw.rect[screen, [200, 0, 0], red_square, 1]
43
44    # Draw an orange caption along the bottom in 60-point font
45    text_font = pygame.font.SysFont["any_font", 60]
46    text_block = text_font.render[
47        "Hello, World! From Pygame", False, [200, 100, 0]
48    ]
49    screen.blit[text_block, [50, HEIGHT - 50]]
50
51    # Flip the display
52    pygame.display.flip[]
53
54# Done! Time to quit.
55pygame.quit[]
84

If the player fights, then a random amount of damage is done on lines 116 to 118, and the giant’s hit points are adjusted. Nếu người khổng lồ vẫn còn sống, thì họ sẽ tấn công theo cách tương tự ở các dòng 136 đến 149. Lưu ý rằng người khổng lồ có cơ hội bỏ lỡ, trong khi người chơi luôn đánh. Cuộc chiến tiếp tục cho đến khi người chơi hoặc người khổng lồ không có điểm trúng đích hoặc cho đến khi người chơi bỏ chạy

Điều quan trọng cần lưu ý là mã này rất giống với mã mà bạn đã sử dụng trong trận chiến adventurelib. Điều này cho thấy cách bạn có thể thả mã Python đầy đủ vào các trò chơi Ren'Py của mình mà không cần dịch nó sang tập lệnh Ren'Py

Ren'Py còn nhiều thứ hơn những gì bạn đã thử ở đây. Tham khảo tài liệu Ren'Py để biết thêm chi tiết đầy đủ

Loại bỏ các quảng cáo

Các công cụ trò chơi Python đáng chú ý khác

Năm công cụ này chỉ là một mẫu nhỏ trong số nhiều công cụ trò chơi Python khác nhau hiện có. Có hàng chục người khác có sẵn, và một số đáng chú ý ở đây

  • Wasabi 2D được phát triển bởi nhóm đằng sau Pygame Zero. Đó là một khung hiện đại được xây dựng trên Moderngl để tự động hóa kết xuất, cung cấp coroutine cho các hiệu ứng hoạt hình, có các hiệu ứng hạt tích hợp và sử dụng mô hình hướng sự kiện để chơi trò chơi

  • cocos2d là một khung được thiết kế để mã hóa các trò chơi đa nền tảng. Rất tiếc, cocos2d-python đã không được cập nhật từ năm 2017

  • Panda 3D là một khung nguồn mở để tạo trò chơi 3D và kết xuất 3D. Panda 3D có thể di động trên các nền tảng, hỗ trợ nhiều loại nội dung, kết nối vượt trội với nhiều thư viện của bên thứ ba và cung cấp hồ sơ đường ống tích hợp sẵn

  • Ursina được xây dựng dựa trên Panda 3D và cung cấp một công cụ phát triển trò chơi chuyên dụng giúp đơn giản hóa nhiều khía cạnh của Panda 3D. Được hỗ trợ tốt và có nhiều tài liệu, Ursina đang được phát triển tích cực tại thời điểm viết bài này

  • PursuedPyBear được lập hóa đơn như một thư viện giáo dục. Nó tự hào có một hệ thống quản lý cảnh, các họa tiết hoạt hình dựa trên khung có thể tạm dừng và rào cản gia nhập thấp. Tài liệu còn thưa thớt, nhưng trợ giúp chỉ là một cuộc thảo luận trên GitHub

Công cụ trò chơi Python mới được tạo ra mỗi ngày. Nếu bạn tìm thấy một cái phù hợp với nhu cầu của mình và không được đề cập ở đây, hãy khen ngợi nó trong phần bình luận

Nguồn cho nội dung trò chơi

Thông thường, tạo nội dung trò chơi là vấn đề lớn nhất mà các tác giả trò chơi phải đối mặt. Các công ty trò chơi điện tử lớn tuyển dụng các nhóm nghệ sĩ, họa sĩ hoạt hình và nhạc sĩ để thiết kế giao diện và âm thanh cho trò chơi của họ. Các nhà phát triển trò chơi đơn lẻ có nền tảng về lập trình có thể thấy khó khăn trong khía cạnh phát triển trò chơi này. May mắn thay, có rất nhiều nguồn khác nhau cho nội dung trò chơi. Dưới đây là một số điều quan trọng trong việc định vị nội dung cho trò chơi trong hướng dẫn này

  • OpenGameArt. org lưu trữ nhiều loại nghệ thuật trò chơi, âm nhạc, hình nền, biểu tượng và các nội dung khác cho cả trò chơi 2D và 3D. Các nghệ sĩ và nhạc sĩ liệt kê nội dung của họ để tải xuống, bạn có thể tải xuống và sử dụng nội dung này trong trò chơi của mình. Hầu hết nội dung đều có sẵn miễn phí và các điều khoản cấp phép có thể áp dụng cho nhiều nội dung

  • Kenney. nl lưu trữ một bộ nội dung miễn phí và trả phí, nhiều nội dung trong số đó không thể tìm thấy ở nơi nào khác. Đóng góp luôn được chào đón để hỗ trợ các nội dung miễn phí, tất cả đều được cấp phép sử dụng trong các trò chơi thương mại

  • ngứa. io là thị trường dành cho những người sáng tạo kỹ thuật số tập trung vào phát triển trò chơi độc lập. Tại đây, bạn có thể tìm thấy nội dung kỹ thuật số cho bất kỳ mục đích nào, cả miễn phí và trả phí, cùng với các trò chơi hoàn chỉnh. Những người sáng tạo cá nhân kiểm soát nội dung của riêng họ tại đây, vì vậy bạn luôn làm việc trực tiếp với những cá nhân tài năng

Hầu hết các nội dung có sẵn từ bên thứ ba đều có các điều khoản cấp phép quy định việc sử dụng nội dung phù hợp và được phép. Là người dùng những nội dung này, bạn có trách nhiệm đọc, hiểu và tuân thủ các điều khoản cấp phép do chủ sở hữu nội dung xác định. Nếu bạn có câu hỏi hoặc thắc mắc về các điều khoản đó, vui lòng tham khảo ý kiến ​​chuyên gia pháp lý để được hỗ trợ

Tất cả nội dung được sử dụng trong các trò chơi được đề cập trong bài viết này đều tuân thủ các yêu cầu cấp phép tương ứng của chúng

Sự kết luận

Xin chúc mừng, thiết kế trò chơi tuyệt vời hiện đang trong tầm tay của bạn. Nhờ Python và một loạt các công cụ trò chơi Python có khả năng cao, bạn có thể tạo các trò chơi máy tính chất lượng dễ dàng hơn nhiều so với trước đây. Trong hướng dẫn này, bạn đã khám phá một số công cụ trò chơi như vậy, tìm hiểu thông tin mà bạn cần để bắt đầu tạo trò chơi điện tử Python của riêng mình

Đến bây giờ, bạn đã thấy một số công cụ trò chơi Python hàng đầu đang hoạt động và bạn đã

  • Khám phá những ưu và nhược điểm của một số công cụ trò chơi Python phổ biến
  • Trải nghiệm cách họ so sánh với các công cụ trò chơi độc lập
  • Đã tìm hiểu về các công cụ trò chơi Python khác có sẵn

Nếu bạn muốn xem lại mã cho các trò chơi trong hướng dẫn này, bạn có thể làm như vậy bằng cách nhấp vào liên kết bên dưới

Nhận mã nguồn. Nhấp vào đây để lấy mã nguồn mà bạn sẽ sử dụng để dùng thử công cụ trò chơi Python

Bây giờ bạn có thể chọn công cụ trò chơi Python tốt nhất cho mục đích của mình. Bạn đang chờ đợi điều gì?

Đánh dấu là đã hoàn thành

🐍 Python Tricks 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Gửi cho tôi thủ thuật Python »

Giới thiệu về Jon Fincher

Jon đã dạy Python và Java tại hai trường trung học ở Bang Washington. Trước đây, ông là Giám đốc Chương trình tại Microsoft

» Thông tin thêm về Jon

Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Aldren

David

Geir Arne

kate

Philipp

Master Real-World Python Skills With Unlimited Access to Real Python

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Rate this article

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Get tips for asking good questions and get answers to common questions in our support portal

Python có tốt cho game 3D không?

Is Python or Java better for game development? If you're a complete coding beginner, Python is a great place to start . Nó có thể đọc được đối với những người không phải là lập trình viên và PyGame giúp bạn dễ dàng bắt đầu phát triển các trò chơi đơn giản. Học Python cũng giúp chuyển đổi sang Godot, một công cụ trò chơi ngày càng phổ biến dễ dàng hơn.

Công cụ trò chơi 3D có hỗ trợ Python không?

Pygame có tính di động cao và chạy trên hầu hết mọi nền tảng và hệ điều hành. PySoy - PySoy là một công cụ trò chơi 3d cấp cao dành cho Python 3, liên kết chặt chẽ giữa kết xuất, vật lý, hoạt ảnh và kết nối mạng.

Tôi có thể tạo trò chơi 3D trong Pygame không?

Pygame không thực sự được thiết kế cho đồ họa 3D , vì vậy nếu bạn muốn tạo trò chơi có đồ họa 3D, tốt hơn hết bạn nên sử dụng một thứ khác có tất cả những điều cơ bản, chẳng hạn như . Mục đích của hướng dẫn này chủ yếu là để giải thích toán học của đồ họa 3D.

Có khó để viết mã một trò chơi 3D không?

3D khó học hơn nhiều so với 2D . 2D, nếu bạn sử dụng đúng công cụ như Spriter/Spline/Smooth Moves thì thực sự dễ thực hiện hơn 3D, nhưng cuối cùng sẽ trông cực kỳ đơn giản hoặc thậm chí xấu trừ khi bạn thực hiện nhiều công việc khó khăn hơn [nhiều họa tiết để hoán đổi trong quá trình .

Chủ Đề