Hướng dẫn trò chơi trực tuyến Python này từ Tech with Tim sẽ chỉ cho bạn cách viết mã trò chơi nhiều người chơi có thể mở rộng với python bằng socket/mạng và pygame. Bạn sẽ học cách triển khai trò chơi của mình để mọi người ở mọi nơi trên thế giới có thể chơi với nhau
Bạn sẽ học và hiểu những điều sau đây sau hướng dẫn này.
• Cách hệ thống máy khách/máy chủ hoạt động
• Cách phát triển ứng dụng cho máy chủ bên ngoài
• Cách viết mã máy khách
• How to Code a Server
• Sending Encrypted Data Such as Objects Over a Port
• Connecting Multiple Clients to a Server
Bạn có thể xem toàn bộ khóa học bằng video trên freeCodeCamp. org kênh YouTube [2. đồng hồ 5 giờ]
QUẢNG CÁO
QUẢNG CÁO
Tôi là giáo viên và nhà phát triển với freeCodeCamp. tổ chức. Tôi chạy freeCodeCamp. tổ chức kênh YouTube
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu
Để xây dựng trò chơi, bạn sẽ cần sử dụng nhiều kỹ năng lập trình cốt lõi. Các loại kỹ năng mà bạn sẽ thấy trong lập trình thực tế. Trong quá trình phát triển trò chơi, bạn sẽ sử dụng các biến, vòng lặp, câu lệnh điều kiện, hàm, lập trình hướng đối tượng và toàn bộ các kỹ thuật và thuật toán lập trình
Ngoài ra, bạn sẽ cảm thấy hài lòng khi chơi trò chơi mà mình vừa tạo
Trong hệ sinh thái Python, bạn sẽ tìm thấy một bộ công cụ, thư viện và khung phong phú giúp bạn tạo trò chơi của mình một cách nhanh chóng. Các bài viết, hướng dẫn và khóa học trong phần này sẽ chỉ cho bạn con đường để bắt kịp tốc độ xây dựng trò chơi của riêng bạn bằng Python
Khi tôi bắt đầu học lập trình máy tính vào cuối thiên niên kỷ trước, tôi đã bị thúc đẩy bởi mong muốn viết trò chơi máy tính. Tôi đã cố gắng tìm ra cách viết trò chơi bằng mọi ngôn ngữ và trên mọi nền tảng mà tôi đã học, bao gồm cả Python. Đó là cách tôi phát hiện ra
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 và học cách sử dụng nó để viết trò chơi và các chương trình đồ họa khác. Vào thời điểm đó, tôi thực sự muốn có một đoạn trích về 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7Đến cuối bài viết này, bạn sẽ có thể
- Vẽ các mục trên màn hình của bạn
- Phát hiệu ứng âm thanh và âm nhạc
- Xử lý đầu vào của người dùng
- Thực hiện các vòng lặp sự kiện
- Mô tả cách lập trình trò chơi khác với lập trình Python thủ tục tiêu chuẩn
Phần hướng dẫn này giả định rằng bạn đã có hiểu biết cơ bản về viết chương trình Python, bao gồm các hàm do người dùng định nghĩa, nhập, vòng lặp và điều kiện. Bạn cũng nên làm quen với cách mở tệp trên nền tảng của mình. Hiểu biết cơ bản về Python hướng đối tượng cũng hữu ích.
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 hoạt động với hầu hết các phiên bản Python, nhưng Python 3. 6 được đề xuất và sử dụng trong suốt bài viết nàyCác bạn có thể lấy toàn bộ code trong bài viết này để làm theo
Mã mẫu. Nhấp vào đây để tải xuống mã nguồn cho dự án mẫu PyGame được sử dụng trong hướng dẫn này
Bối cảnh và thiết lập
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 là trình bao bọc Python cho 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. 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 bắt đầu cuộc sống thay thế cho dự án PySDL bị đình trệ. Bản chất đa nền tảng của cả SDL và 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 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
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 trên nền tảng của bạn, hãy sử dụng lệnh 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode[[SCREEN_WIDTH, SCREEN_HEIGHT]]
4 thích hợp________số 8
Bạn có thể xác minh cài đặt bằng cách tải một trong các ví dụ đi kèm với thư viện
$ python3 -m pygame.examples.aliens
Nếu cửa sổ trò chơi xuất hiện, nghĩa là
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 đã được cài đặt đúng cách. Nếu bạn gặp sự cố, hướng dẫn Bắt đầu sẽ phác thảo một số sự cố đã biết và lưu ý cho tất cả các nền tảngLoại bỏ các quảng cáoChương trình PyGame cơ bản
Trước khi đi vào chi tiết cụ thể, chúng ta hãy xem một chương trình
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 cơ bản. Chương trình này tạo một cửa sổ, tô nền bằng màu trắng và vẽ một vòng tròn màu xanh lam ở giữa cửa sổ đó 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
2Khi bạn chạy chương trình này, bạn sẽ thấy một cửa sổ giống như thế này
Hãy chia nhỏ đoạn mã này, từng phần một
Dòng 4 và 5 nhập và khởi tạo thư viện
7. Không có những dòng này, không có1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
71# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
Dòng 8 thiết lập cửa sổ hiển thị chương trình của bạn. Bạn cung cấp một danh sách hoặc một bộ chỉ định chiều rộng và chiều cao của cửa sổ để tạo. Chương trình này sử dụng một danh sách để tạo một cửa sổ hình vuông với mỗi cạnh 500 pixel
Dòng 11 và 12 thiết lập game loop để điều khiển khi hết chương trình. Bạn sẽ đề cập đến các vòng lặp trò chơi sau này trong hướng dẫn này
Dòng 15 đến 17 quét và xử lý các sự kiện trong vòng lặp trò chơi. Bạn cũng sẽ đến các sự kiện muộn hơn một chút. Trong trường hợp này, sự kiện duy nhất được xử lý là
9, xảy ra khi người dùng nhấp vào nút đóng cửa sổ1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[] 18 19# Define constants for the screen width and height 20SCREEN_WIDTH = 800 21SCREEN_HEIGHT = 600 22 23# Create the screen object 24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT 25screen = pygame.display.set_mode[[SCREEN_WIDTH, SCREEN_HEIGHT]]
Dòng 20 lấp đầy cửa sổ với một màu đồng nhất.
60 chấp nhận danh sách hoặc bộ xác định giá trị RGB cho màu. Kể từ khi cung cấp1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
61, cửa sổ sẽ tràn ngập màu trắng1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
Dòng 23 vẽ một vòng tròn trong cửa sổ, sử dụng các tham số sau
62. cửa sổ để vẽ1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
63. một tuple chứa các giá trị màu RGB1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
64. một tuple xác định tọa độ trung tâm của vòng tròn1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
65. bán kính của vòng tròn để vẽ bằng pixel1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
Dòng 26 cập nhật nội dung hiển thị ra màn hình. Không có cuộc gọi này, không có gì xuất hiện trong cửa sổ
Dòng 29 thoát
7. Điều này chỉ xảy ra khi vòng lặp kết thúc1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
Đó là phiên bản
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 của “Xin chào, Thế giới. ” Bây giờ chúng ta hãy tìm hiểu sâu hơn một chút về các khái niệm đằng sau mã nàyKhái niệm PyGame
Vì
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 và thư viện SDL có thể di động trên các nền tảng và thiết bị khác nhau, nên cả hai đều cần xác định và làm việc với các phần trừu tượng cho các thực tế phần cứng khác nhau. Hiểu những khái niệm và trừu tượng đó sẽ giúp bạn thiết kế và phát triển trò chơi của riêng mìnhKhởi tạo và Mô-đun
Thư viện
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 bao gồm một số cấu trúc Python, bao gồm một số mô-đun khác nhau. Các mô-đun này cung cấp quyền truy cập trừu tượng vào phần cứng cụ thể trên hệ thống của bạn, cũng như các phương pháp thống nhất để làm việc với phần cứng đó. Ví dụ: 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
60 cho phép truy cập thống nhất vào màn hình video của bạn, trong khi 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
61 cho phép điều khiển trừu tượng cần điều khiển của bạnSau khi nhập thư viện
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 trong ví dụ trên, điều đầu tiên bạn làm là khởi tạo PyGame bằng cách sử dụng 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
63. Chức năng này của tất cả các mô-đun 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 đi kèm. Vì các mô-đun này là phần trừu tượng cho phần cứng cụ thể nên bước khởi tạo này là bắt buộc để bạn có thể làm việc với cùng mã trên Linux, Windows và MacMàn hình và bề mặt
Ngoài các mô-đun,
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 cũng bao gồm một số lớp Python, gói gọn các khái niệm không phụ thuộc vào phần cứng. Một trong số đó là 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67, ở mức cơ bản nhất, xác định một khu vực hình chữ nhật mà bạn có thể vẽ trên đó. 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 đối tượng được sử dụng trong nhiều ngữ cảnh trong 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7. Sau đó, bạn sẽ thấy cách tải một hình ảnh vào 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 và hiển thị nó trên màn hìnhTrong
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7, mọi thứ được xem trên một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
60 do người dùng tạo, có thể là một cửa sổ hoặc toàn màn hình. Màn hình được tạo bằng cách sử dụng , trả về một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 đại diện cho phần hiển thị của cửa sổ. Đây là 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 mà bạn chuyển vào các hàm vẽ như và nội dung của 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 đó được đẩy lên màn hình khi bạn gọiHình ảnh và Rects
Chương trình
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 cơ bản của bạn đã vẽ một hình trực tiếp lên màn hình 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67, nhưng bạn cũng có thể làm việc với hình ảnh trên đĩa. Mô-đun 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
31 cho phép bạn và hình ảnh ở nhiều định dạng phổ biến. Hình ảnh được tải vào đối tượng 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67, sau đó có thể thao tác và hiển thị theo nhiều cáchNhư đã đề cập ở trên, các đối tượng của
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 được biểu diễn bằng hình chữ nhật, cũng như nhiều đối tượng khác trong 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7, chẳng hạn như hình ảnh và cửa sổ. Hình chữ nhật được sử dụng nhiều đến mức có một lớp 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
35 đặc biệt chỉ để xử lý chúng. Bạn sẽ sử dụng 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
35 đối tượng và hình ảnh trong trò chơi của mình để thu hút người chơi và kẻ thù, đồng thời quản lý va chạm giữa họĐược rồi, đó là đủ lý thuyết. Hãy thiết kế và viết một trò chơi
Loại bỏ các quảng cáoThiết kế trò chơi cơ bản
Trước khi bạn bắt đầu viết bất kỳ mã nào, bạn nên chuẩn bị sẵn một số thiết kế. Vì đây là một trò chơi hướng dẫn nên chúng ta cũng hãy thiết kế một số cách chơi cơ bản cho nó
- Mục tiêu của trò chơi là để tránh chướng ngại vật đến
- Người chơi bắt đầu ở bên trái màn hình
- Các chướng ngại vật đi vào ngẫu nhiên từ bên phải và di chuyển sang trái theo đường thẳng
- Người chơi có thể di chuyển sang trái, phải, lên hoặc xuống để tránh các chướng ngại vật
- Người chơi không thể di chuyển ra khỏi màn hình
- Trò chơi kết thúc khi người chơi va vào chướng ngại vật hoặc khi người dùng đóng cửa sổ
Khi anh ấy mô tả các dự án phần mềm, một đồng nghiệp cũ của tôi thường nói, “Bạn không biết bạn làm gì cho đến khi bạn biết bạn không làm gì. ” Với ý nghĩ đó, đây là một số điều sẽ không được đề cập trong hướng dẫn này
- Không có nhiều cuộc sống
- Không tính điểm
- Không có khả năng tấn công người chơi
- Không có cấp độ nâng cao
- Không có nhân vật ông chủ
Bạn có thể tự do thử thêm các tính năng này và các tính năng khác vào chương trình của riêng mình
Bắt đầu nào
Nhập và khởi tạo PyGame
Sau khi nhập
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7, bạn cũng cần khởi tạo nó. Điều này cho phép 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 kết nối các phần tóm tắt của nó với phần cứng cụ thể của bạn 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
Thư viện
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 định nghĩa nhiều thứ ngoài các mô-đun và lớp. Nó cũng xác định một số hằng số cục bộ cho những thứ như tổ hợp phím, di chuyển chuột và thuộc tính hiển thị. Bạn tham khảo các hằng số này bằng cú pháp 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
30. Bằng cách nhập các hằng số cụ thể từ 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
31, bạn có thể sử dụng cú pháp 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
32 để thay thế. Điều này sẽ giúp bạn tiết kiệm một số lần nhấn phím và cải thiện khả năng đọc tổng thểThiết lập màn hình
Bây giờ bạn cần một cái gì đó để vẽ. Tạo một màn hình để trở thành canvas tổng thể
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode[[SCREEN_WIDTH, SCREEN_HEIGHT]]
Bạn tạo màn hình để sử dụng bằng cách gọi
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
33 và chuyển một bộ hoặc danh sách có chiều rộng và chiều cao mong muốn. Trong trường hợp này, cửa sổ có kích thước 800x600, như được xác định bởi các hằng số 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
34 và 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
35 trên dòng 20 và 21. Điều này trả về một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 đại diện cho kích thước bên trong của cửa sổ. Đây là phần cửa sổ bạn có thể kiểm soát, trong khi hệ điều hành kiểm soát viền cửa sổ và thanh tiêu đềNếu bạn chạy chương trình này ngay bây giờ, thì bạn sẽ thấy một cửa sổ bật lên trong thời gian ngắn và sau đó biến mất ngay lập tức khi chương trình thoát. Đừng chớp mắt hoặc bạn có thể bỏ lỡ nó. Trong phần tiếp theo, bạn sẽ tập trung vào vòng lặp trò chơi chính để đảm bảo rằng chương trình của bạn chỉ thoát khi được cung cấp đầu vào chính xác
Thiết lập vòng lặp trò chơi
Mọi trò chơi từ Pong đến Fortnite đều sử dụng vòng lặp trò chơi để kiểm soát lối chơi. Vòng lặp trò chơi thực hiện bốn điều rất quan trọng
- Xử lý đầu vào của người dùng
- Cập nhật trạng thái của tất cả các đối tượng trò chơi
- Cập nhật màn hình và đầu ra âm thanh
- Duy trì tốc độ của trò chơi
Mỗi chu kỳ của vòng lặp trò chơi được gọi là khung hình và bạn có thể thực hiện mọi việc càng nhanh trong mỗi chu kỳ, trò chơi của bạn sẽ chạy càng nhanh. Các khung hình tiếp tục xảy ra cho đến khi một số điều kiện để thoát khỏi trò chơi được đáp ứng. Trong thiết kế của bạn, có hai điều kiện có thể kết thúc vòng lặp trò chơi
- Người chơi va chạm với một chướng ngại vật. [Bạn sẽ đề cập đến phát hiện va chạm sau. ]
- Người chơi đóng cửa sổ
Điều đầu tiên mà vòng lặp trò chơi thực hiện là xử lý đầu vào của người dùng để cho phép người chơi di chuyển quanh màn hình. Do đó, bạn cần một số cách để nắm bắt và xử lý nhiều loại đầu vào. Bạn làm điều này bằng cách sử dụng hệ thống sự kiện
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7Loại bỏ các quảng cáoxử lý sự kiện
Nhấn phím, di chuyển chuột và thậm chí cả di chuyển cần điều khiển là một số cách mà người dùng có thể cung cấp thông tin đầu vào. Tất cả kết quả đầu vào của người dùng trong một sự kiện được tạo. Các sự kiện có thể xảy ra bất cứ lúc nào và thường [nhưng không phải luôn luôn] bắt nguồn từ bên ngoài chương trình. Tất cả các sự kiện trong
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 được đặt trong hàng đợi sự kiện, sau đó có thể được truy cập và thao tác. Xử lý các sự kiện được gọi là xử lý chúng và mã để làm như vậy được gọi là trình xử lý sự kiệnMỗi sự kiện trong
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 có một loại sự kiện được liên kết với nó. Đối với trò chơi của bạn, các loại sự kiện bạn sẽ tập trung vào là nhấn phím và đóng cửa sổ. Các sự kiện nhấn phím có loại sự kiện $ python3 -m pygame.examples.aliens
00 và sự kiện đóng cửa sổ có loại $ python3 -m pygame.examples.aliens
01. Các loại sự kiện khác nhau cũng có thể có dữ liệu khác được liên kết với chúng. Ví dụ: loại sự kiện $ python3 -m pygame.examples.aliens
00 cũng có một biến có tên là $ python3 -m pygame.examples.aliens
03 để cho biết phím nào đã được nhấnBạn truy cập danh sách tất cả các sự kiện đang hoạt động trong hàng đợi bằng cách gọi
$ python3 -m pygame.examples.aliens
04. Sau đó, bạn lặp qua danh sách này, kiểm tra từng loại sự kiện và phản hồi tương ứng 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
6Hãy xem xét kỹ hơn về vòng lặp trò chơi này
Dòng 28 thiết lập biến điều khiển cho vòng lặp trò chơi. Để thoát khỏi vòng lặp và trò chơi, bạn đặt
05. Vòng lặp trò chơi bắt đầu ở dòng 29$ python3 -m pygame.examples.aliens
Dòng 31 bắt đầu trình xử lý sự kiện, đi qua mọi sự kiện hiện có trong hàng đợi sự kiện. Nếu không có sự kiện nào, thì danh sách trống và trình xử lý sẽ không làm gì cả
Dòng 35 đến 38 kiểm tra xem
06 hiện tại có phải là sự kiện$ python3 -m pygame.examples.aliens
00 không. Nếu đúng như vậy, thì chương trình sẽ kiểm tra phím nào đã được nhấn bằng cách xem thuộc tính$ python3 -m pygame.examples.aliens
08. Nếu phím là phím Esc , được biểu thị bằng$ python3 -m pygame.examples.aliens
09, thì phím đó sẽ thoát khỏi vòng lặp trò chơi bằng cách đặt$ python3 -m pygame.examples.aliens
05.$ python3 -m pygame.examples.aliens
Dòng 41 và 42 thực hiện kiểm tra tương tự cho loại sự kiện có tên là
01. Sự kiện này chỉ xảy ra khi người dùng nhấp vào nút đóng cửa sổ. Người dùng cũng có thể sử dụng bất kỳ hành động nào khác của hệ điều hành để đóng cửa sổ$ python3 -m pygame.examples.aliens
Khi bạn thêm những dòng này vào mã trước đó và chạy nó, bạn sẽ thấy một cửa sổ có màn hình trống hoặc đen
Cửa sổ sẽ không biến mất cho đến khi bạn nhấn phím Esc hoặc kích hoạt sự kiện
$ python3 -m pygame.examples.aliens
01 bằng cách đóng cửa sổ. Vẽ trên màn hình
Trong chương trình mẫu, bạn đã vẽ trên màn hình bằng hai lệnh
60 để lấp đầy nền1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
96 để vẽ một vòng tròn1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[] 18 19# Define constants for the screen width and height 20SCREEN_WIDTH = 800 21SCREEN_HEIGHT = 600 22 23# Create the screen object 24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT 25screen = pygame.display.set_mode[[SCREEN_WIDTH, SCREEN_HEIGHT]]
Bây giờ bạn sẽ tìm hiểu về cách thứ ba để vẽ lên màn hình. sử dụng một
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67Nhớ lại rằng
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 là một đối tượng hình chữ nhật mà bạn có thể vẽ trên đó, giống như một tờ giấy trắng. Đối tượng 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
62 là một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 và bạn có thể tạo các đối tượng 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 của riêng mình tách biệt khỏi màn hình hiển thị. Hãy xem nó hoạt động như thế nào 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
6Sau khi màn hình tràn ngập màu trắng ở dòng 45, một
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 mới được tạo ở dòng 48. 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 này rộng 50 pixel, cao 50 pixel và được gán cho $ python3 -m pygame.examples.aliens
22. Tại thời điểm này, bạn coi nó giống như 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
62. Vì vậy, trên dòng, 51 bạn tô màu đen. Bạn cũng có thể truy cập vào 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
35 cơ bản của nó bằng cách sử dụng $ python3 -m pygame.examples.aliens
25. Điều này được lưu trữ dưới dạng $ python3 -m pygame.examples.aliens
26 để sử dụng sau nàySử dụng $ python3 -m pygame.examples.aliens
27 và $ python3 -m pygame.examples.aliens
28
$ python3 -m pygame.examples.aliens
$ python3 -m pygame.examples.aliens
Chỉ tạo một
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 mới là không đủ để nhìn thấy nó trên màn hình. Để làm được điều đó, bạn cần chuyển số ________ 267 sang số ________ 267 khác. Thuật ngữ $ python3 -m pygame.examples.aliens
32 là viết tắt của Block Transfer và $ python3 -m pygame.examples.aliens
27 là cách bạn sao chép nội dung của một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 sang một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 khác. Bạn chỉ có thể $ python3 -m pygame.examples.aliens
27 từ một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 này sang một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 khác, nhưng vì màn hình chỉ là một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 khác nên đó không phải là vấn đề. Đây là cách bạn vẽ $ python3 -m pygame.examples.aliens
22 trên màn hình 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
18
19# Define constants for the screen width and height
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22
23# Create the screen object
24# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
25screen = pygame.display.set_mode[[SCREEN_WIDTH, SCREEN_HEIGHT]]
9Cuộc gọi
$ python3 -m pygame.examples.aliens
27 trên dòng 55 có hai đối số
67 để vẽ1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
- Vị trí vẽ nó trên nguồn
671# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
Tọa độ
$ python3 -m pygame.examples.aliens
42 yêu cầu chương trình của bạn đặt $ python3 -m pygame.examples.aliens
22 ở chính giữa màn hình, nhưng nó không hoàn toàn giống như vậyLý do tại sao hình ảnh trông lệch tâm là vì
$ python3 -m pygame.examples.aliens
27 đặt góc trên cùng bên trái của $ python3 -m pygame.examples.aliens
22 tại vị trí đã cho. Nếu bạn muốn đặt $ python3 -m pygame.examples.aliens
22 ở giữa, thì bạn sẽ phải thực hiện một số phép toán để dịch chuyển nó lên trên và sang trái. Bạn có thể thực hiện việc này bằng cách lấy chiều rộng và chiều cao của màn hình trừ chiều rộng và chiều cao của $ python3 -m pygame.examples.aliens
22, chia mỗi số cho 2 để xác định vị trí tâm, sau đó chuyển các số đó làm đối số cho $ python3 -m pygame.examples.aliens
48 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
3Lưu ý cuộc gọi đến sau cuộc gọi đến
$ python3 -m pygame.examples.aliens
50. Thao tác này cập nhật toàn bộ màn hình với mọi thứ được vẽ kể từ lần lật cuối cùng. Không có cuộc gọi đến $ python3 -m pygame.examples.aliens
28, không có gì được hiển thịLoại bỏ các quảng cáoyêu tinh
Trong thiết kế trò chơi của bạn, người chơi bắt đầu từ bên trái và chướng ngại vật đến từ bên phải. Bạn có thể biểu diễn tất cả các chướng ngại vật bằng các đối tượng
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 để giúp việc vẽ mọi thứ trở nên dễ dàng hơn, nhưng làm thế nào để bạn biết nên vẽ chúng ở đâu? Theo thuật ngữ lập trình, sprite là biểu diễn 2D của một thứ gì đó trên màn hình. Về cơ bản, đó là một bức tranh.
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 cung cấp một lớp $ python3 -m pygame.examples.aliens
54, được thiết kế để chứa một hoặc một số biểu diễn đồ họa của bất kỳ đối tượng trò chơi nào mà bạn muốn hiển thị trên màn hình. Để sử dụng nó, bạn tạo một lớp mới mở rộng $ python3 -m pygame.examples.aliens
54. Điều này cho phép bạn sử dụng các phương pháp tích hợp của nóngười chơi
Đây là cách bạn sử dụng các đối tượng
$ python3 -m pygame.examples.aliens
54 với trò chơi hiện tại để xác định người chơi. Chèn mã này sau dòng 18 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
3Trước tiên, bạn xác định
$ python3 -m pygame.examples.aliens
57 bằng cách mở rộng $ python3 -m pygame.examples.aliens
58 trên dòng 22. Sau đó, $ python3 -m pygame.examples.aliens
59 sử dụng $ python3 -m pygame.examples.aliens
60 để gọi phương thức $ python3 -m pygame.examples.aliens
59 của $ python3 -m pygame.examples.aliens
54. Để biết thêm thông tin về lý do tại sao điều này là cần thiết, bạn có thể đọc Supercharge Your Classes With Python super[]Tiếp theo, bạn xác định và khởi tạo
$ python3 -m pygame.examples.aliens
63 để giữ hình ảnh hiển thị, hiện tại là một hộp màu trắng. Bạn cũng xác định và khởi tạo $ python3 -m pygame.examples.aliens
64 mà bạn sẽ sử dụng để vẽ trình phát sau này. Để sử dụng lớp mới này, bạn cần tạo một đối tượng mới và thay đổi cả mã bản vẽ. Mở rộng khối mã bên dưới để xem tất cả cùng nhauMở rộng mã đầy đủHiển thị/Ẩn
$ python3 -m pygame.examples.aliens
0Chạy mã này. Bạn sẽ thấy một hình chữ nhật màu trắng ở khoảng giữa màn hình
Bạn nghĩ điều gì sẽ xảy ra nếu bạn đổi dòng 59 thành
$ python3 -m pygame.examples.aliens
65? $ python3 -m pygame.examples.aliens
1Khi bạn chuyển
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
35 đến $ python3 -m pygame.examples.aliens
27, nó sẽ sử dụng tọa độ của góc trên cùng bên trái để vẽ bề mặt. Bạn sẽ sử dụng điều này sau để làm cho người chơi của bạn di chuyểnĐầu vào của người dùng
Cho đến giờ, bạn đã học cách thiết lập
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 và vẽ các đối tượng trên màn hình. Bây giờ, niềm vui thực sự bắt đầu. Bạn sẽ làm cho trình phát có thể điều khiển được bằng bàn phímTrước đó, bạn đã thấy rằng
$ python3 -m pygame.examples.aliens
04 trả về một danh sách các sự kiện trong hàng đợi sự kiện, danh sách này bạn sẽ quét các loại sự kiện của $ python3 -m pygame.examples.aliens
00. Chà, đó không phải là cách duy nhất để đọc các lần nhấn phím. 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 cũng cung cấp $ python3 -m pygame.examples.aliens
72, trả về một từ điển chứa tất cả các sự kiện $ python3 -m pygame.examples.aliens
00 hiện tại trong hàng đợiĐặt cái này vào vòng lặp trò chơi của bạn ngay sau vòng xử lý sự kiện. Điều này trả về một từ điển chứa các phím được nhấn ở đầu mỗi khung
$ python3 -m pygame.examples.aliens
2Tiếp theo, bạn viết một phương thức trong
$ python3 -m pygame.examples.aliens
57 để chấp nhận từ điển đó. Điều này sẽ xác định hành vi của sprite dựa trên các phím được nhấn. Đây là những gì có thể trông giống như$ python3 -m pygame.examples.aliens
3$ python3 -m pygame.examples.aliens
75, $ python3 -m pygame.examples.aliens
76, $ python3 -m pygame.examples.aliens
77, và $ python3 -m pygame.examples.aliens
78 tương ứng với các phím mũi tên trên bàn phím. Nếu mục nhập từ điển cho phím đó là $ python3 -m pygame.examples.aliens
79, thì phím đó sẽ bị hỏng và bạn di chuyển trình phát $ python3 -m pygame.examples.aliens
64 theo hướng thích hợp. Ở đây bạn sử dụng $ python3 -m pygame.examples.aliens
81, viết tắt của , để di chuyển 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
35 hiện tạiSau đó, bạn có thể gọi
$ python3 -m pygame.examples.aliens
83 mọi khung hình để di chuyển sprite của trình phát để phản hồi lại các lần nhấn phím. Thêm cuộc gọi này ngay sau cuộc gọi tới $ python3 -m pygame.examples.aliens
84$ python3 -m pygame.examples.aliens
4Giờ đây, bạn có thể di chuyển hình chữ nhật của trình phát xung quanh màn hình bằng các phím mũi tên
Bạn có thể nhận thấy hai vấn đề nhỏ
- Hình chữ nhật của trình phát có thể di chuyển rất nhanh nếu nhấn giữ một phím. Bạn sẽ làm việc đó sau
- Hình chữ nhật của người chơi có thể di chuyển ra khỏi màn hình. Hãy giải quyết vấn đề đó ngay bây giờ
Để giữ trình phát trên màn hình, bạn cần thêm một số logic để phát hiện xem
$ python3 -m pygame.examples.aliens
26 có di chuyển khỏi màn hình không. Để làm được điều đó, bạn kiểm tra xem tọa độ $ python3 -m pygame.examples.aliens
26 đã di chuyển ra ngoài ranh giới của màn hình chưa. Nếu vậy thì bạn hướng dẫn chương trình di chuyển nó trở lại cạnh$ python3 -m pygame.examples.aliens
5Tại đây, thay vì sử dụng
$ python3 -m pygame.examples.aliens
87, bạn chỉ cần thay đổi trực tiếp tọa độ tương ứng của $ python3 -m pygame.examples.aliens
88, $ python3 -m pygame.examples.aliens
89, $ python3 -m pygame.examples.aliens
90, hoặc $ python3 -m pygame.examples.aliens
91. Kiểm tra điều này và bạn sẽ thấy hình chữ nhật của trình phát không thể di chuyển ra khỏi màn hình nữaBây giờ hãy thêm một số kẻ thù
Loại bỏ các quảng cáokẻ thù
Trò chơi không có kẻ thù là gì? . Đầu tiên, nhập thư viện
$ python3 -m pygame.examples.aliens
92$ python3 -m pygame.examples.aliens
6Sau đó, tạo một lớp sprite mới có tên là
$ python3 -m pygame.examples.aliens
93, theo cùng mẫu mà bạn đã sử dụng cho $ python3 -m pygame.examples.aliens
57$ python3 -m pygame.examples.aliens
7Có bốn sự khác biệt đáng chú ý giữa
$ python3 -m pygame.examples.aliens
93 và $ python3 -m pygame.examples.aliens
57Trên các dòng 62 đến 67, bạn cập nhật
26 thành một vị trí ngẫu nhiên dọc theo cạnh phải của màn hình. Tâm của hình chữ nhật nằm ngoài màn hình. Nó nằm ở vị trí nào đó cách cạnh phải từ 20 đến 100 pixel và ở đâu đó giữa cạnh trên và cạnh dưới$ python3 -m pygame.examples.aliens
Ở dòng 68, bạn định nghĩa
98 là một số ngẫu nhiên trong khoảng từ 5 đến 20. Điều này xác định tốc độ mà kẻ thù này di chuyển về phía người chơi$ python3 -m pygame.examples.aliens
Trên các dòng 73 đến 76, bạn định nghĩa
83. Không cần tranh luận vì kẻ thù di chuyển tự động. Thay vào đó,$ python3 -m pygame.examples.aliens
83 di chuyển kẻ thù về phía bên trái của màn hình tại$ python3 -m pygame.examples.aliens
98 được xác định khi nó được tạo ra$ python3 -m pygame.examples.aliens
Ở dòng 74, bạn kiểm tra xem kẻ thù đã di chuyển ra khỏi màn hình chưa. Để đảm bảo rằng
93 nằm hoàn toàn trên màn hình và không biến mất khi nó vẫn hiển thị, bạn kiểm tra xem phía bên phải của$ python3 -m pygame.examples.aliens
64 đã vượt qua phía bên trái của màn hình chưa. Khi kẻ thù ở ngoài màn hình, bạn gọi$ python3 -m pygame.examples.aliens
204 để ngăn nó xử lý thêm1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
Vậy,
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
204 làm gì? Nhóm Sprite
Một lớp siêu hữu ích khác mà
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 cung cấp là. Đây là một đối tượng chứa một nhóm các đối tượng $ python3 -m pygame.examples.aliens
54. Vậy tại sao lại sử dụng nó? . Các phương pháp này giúp phát hiện xem có bất kỳ $ python3 -m pygame.examples.aliens
93 nào đã va chạm với $ python3 -m pygame.examples.aliens
57 hay không, điều này giúp cho việc cập nhật dễ dàng hơn nhiềuHãy xem cách tạo các nhóm sprite. Bạn sẽ tạo hai đối tượng
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
210 khác nhau
210 đầu tiên sẽ giữ mọi1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
54 trong trò chơi$ python3 -m pygame.examples.aliens
210 thứ hai sẽ chỉ chứa các đối tượng1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
93$ python3 -m pygame.examples.aliens
Đây là những gì trông giống như trong mã
$ python3 -m pygame.examples.aliens
8Khi bạn gọi cho
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
204, $ python3 -m pygame.examples.aliens
54 sẽ bị xóa khỏi mọi 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
210 mà nó thuộc về. Điều này cũng loại bỏ các tham chiếu đến $ python3 -m pygame.examples.aliens
54, cho phép lấy lại bộ nhớ khi cần thiếtBây giờ bạn có một nhóm
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
222, bạn có thể thay đổi cách các đối tượng được vẽ. Thay vì chỉ gọi $ python3 -m pygame.examples.aliens
27 trên $ python3 -m pygame.examples.aliens
57, bạn có thể lặp lại mọi thứ trong 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
222$ python3 -m pygame.examples.aliens
9Bây giờ, bất cứ thứ gì được đưa vào
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
222 sẽ được vẽ với mọi khung hình, cho dù đó là kẻ thù hay người chơiChỉ có một vấn đề… Bạn không có bất kỳ kẻ thù nào. Bạn có thể tạo ra một loạt kẻ thù khi bắt đầu trò chơi, nhưng trò chơi sẽ nhanh chóng trở nên nhàm chán khi tất cả đều rời khỏi màn hình sau vài giây. Thay vào đó, hãy khám phá cách duy trì nguồn cung kẻ thù ổn định khi trò chơi tiếp diễn
Sự kiện tùy chỉnh
Thiết kế kêu gọi kẻ thù xuất hiện đều đặn. Điều này có nghĩa là tại các khoảng thời gian đã đặt, bạn cần thực hiện hai việc
- Tạo một
93 mới$ python3 -m pygame.examples.aliens
- Thêm nó vào
222 và1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
2291# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
Bạn đã có mã xử lý các sự kiện ngẫu nhiên. Vòng lặp sự kiện được thiết kế để tìm kiếm các sự kiện ngẫu nhiên xảy ra trong mọi khung hình và xử lý chúng một cách thích hợp. May mắn thay,
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 không hạn chế bạn chỉ sử dụng các loại sự kiện mà nó đã xác định. Bạn có thể xác định các sự kiện của riêng mình để xử lý khi bạn thấy phù hợpHãy xem cách tạo sự kiện tùy chỉnh được tạo vài giây một lần. Bạn có thể tạo một sự kiện tùy chỉnh bằng cách đặt tên cho nó
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
20 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 xác định các sự kiện bên trong dưới dạng số nguyên, vì vậy bạn cần xác định một sự kiện mới với một số nguyên duy nhất. Sự kiện cuối cùng của 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 dự trữ được gọi là 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
233, vì vậy việc xác định 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
234 trên dòng 83 đảm bảo nó là duy nhấtTiếp theo, bạn cần chèn sự kiện mới này vào hàng đợi sự kiện đều đặn trong suốt trò chơi. Đó là nơi mô-đun
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
235 xuất hiện. Dòng 84 kích hoạt sự kiện 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
236 mới cứ sau 250 mili giây hoặc bốn lần mỗi giây. Bạn gọi 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
237 bên ngoài vòng lặp trò chơi vì bạn chỉ cần một đồng hồ bấm giờ, nhưng nó sẽ kích hoạt trong suốt trò chơiThêm mã để xử lý sự kiện mới của bạn
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
21Bất cứ khi nào trình xử lý sự kiện nhìn thấy sự kiện
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
236 mới trên dòng 115, nó sẽ tạo một $ python3 -m pygame.examples.aliens
93 và thêm nó vào 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
229 và 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
222. Vì $ python3 -m pygame.examples.aliens
93 nằm trong 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
222 nên nó sẽ được vẽ ở mọi khung hình. Bạn cũng cần gọi cho 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
244 trên đường dây 126, cập nhật mọi thứ trong 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
229, để đảm bảo chúng di chuyển đúng cáchTuy nhiên, đó không phải là lý do duy nhất có một nhóm chỉ dành cho
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
229Loại bỏ các quảng cáoPhát hiện va chạm
Thiết kế trò chơi của bạn yêu cầu trò chơi kết thúc bất cứ khi nào kẻ thù va chạm với người chơi. Kiểm tra va chạm là một kỹ thuật cơ bản của lập trình trò chơi và thường yêu cầu một số phép toán không tầm thường để xác định xem hai họa tiết có chồng lên nhau hay không
Đây là lúc một khung như
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 có ích. Viết mã phát hiện va chạm rất tẻ nhạt, nhưng 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 có RẤT NHIỀU mã có sẵn để bạn sử dụngĐối với hướng dẫn này, bạn sẽ sử dụng một phương pháp gọi là , được đọc là “sprite va chạm bất kỳ. ” Phương thức này chấp nhận một
$ python3 -m pygame.examples.aliens
54 và một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
210 làm tham số. Nó xem xét mọi đối tượng trong 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
210 và kiểm tra xem $ python3 -m pygame.examples.aliens
64 của nó có giao nhau với $ python3 -m pygame.examples.aliens
64 của $ python3 -m pygame.examples.aliens
54 không. Nếu vậy, thì nó trả về $ python3 -m pygame.examples.aliens
79. Nếu không, nó sẽ trả về 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
257. Điều này là hoàn hảo cho trò chơi này vì bạn cần kiểm tra xem 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
258 đơn có va chạm với một trong số 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
210 của 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
229 khôngĐây là những gì trông giống như trong mã
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
22Dòng 135 kiểm tra xem
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
258 có va chạm với bất kỳ đối tượng nào trong 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
229 không. Nếu vậy, thì 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
263 được gọi để xóa nó khỏi mọi nhóm mà nó thuộc về. Vì các đối tượng duy nhất được hiển thị là trong 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
222, nên 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
258 sẽ không còn được hiển thị nữa. Khi người chơi đã bị giết, bạn cũng cần thoát khỏi trò chơi, vì vậy bạn đặt $ python3 -m pygame.examples.aliens
05 để thoát khỏi vòng lặp trò chơi ở dòng 138Tại thời điểm này, bạn đã có các yếu tố cơ bản của một trò chơi
Bây giờ, hãy chỉnh sửa một chút, làm cho nó dễ chơi hơn và thêm một số khả năng nâng cao để giúp nó nổi bật
Hình ảnh Sprite
Được rồi, bạn có một trò chơi, nhưng hãy thành thật mà nói.. Nó hơi xấu. Người chơi và kẻ thù chỉ là những khối màu trắng trên nền đen. Đó là công nghệ tiên tiến nhất khi Pong còn mới, nhưng nó không còn hiệu quả nữa. Hãy thay thế tất cả những hình chữ nhật màu trắng nhàm chán đó bằng một số hình ảnh thú vị hơn sẽ khiến trò chơi giống như một trò chơi thực sự
Trước đó, bạn đã biết rằng hình ảnh trên đĩa có thể được tải vào
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 với sự trợ giúp của mô-đun 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
31. Đối với hướng dẫn này, chúng tôi đã tạo ra một máy bay phản lực nhỏ cho người chơi và một số tên lửa cho kẻ thù. Bạn có thể sử dụng nghệ thuật này, vẽ của riêng bạn hoặc tải xuống một số để sử dụng. Bạn có thể nhấp vào liên kết bên dưới để tải xuống nghệ thuật được sử dụng trong hướng dẫn nàyMã mẫu. Nhấp vào đây để tải xuống mã nguồn cho dự án mẫu PyGame được sử dụng trong hướng dẫn này
Thay đổi Trình tạo đối tượng
Trước khi bạn sử dụng hình ảnh để đại diện cho người chơi và kẻ thù, bạn cần thực hiện một số thay đổi đối với hàm tạo của chúng. Mã bên dưới thay thế mã được sử dụng trước đó
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
23Hãy giải nén dòng 31 một chút.
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
269 tải một hình ảnh từ đĩa. Bạn truyền cho nó một đường dẫn đến tệp. Nó trả về một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67 và cuộc gọi 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
271 tối ưu hóa 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67, giúp các cuộc gọi $ python3 -m pygame.examples.aliens
27 trong tương lai nhanh hơnDòng 32 sử dụng
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
274 để biểu thị màu 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 sẽ hiển thị trong suốt. Trong trường hợp này bạn chọn màu trắng vì đó là màu nền của hình ảnh máy bay phản lực. Hằng số là một tham số tùy chọn giúp 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 hiển thị nhanh hơn trên màn hình không tăng tốc. Điều này được thêm vào tuyên bố nhập khẩu 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
31 trên dòng 11Không có gì khác cần phải thay đổi. Hình ảnh vẫn là một
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67, ngoại trừ bây giờ nó có một bức tranh được vẽ trên đó. Bạn vẫn sử dụng nó theo cách cũĐây là những thay đổi tương tự đối với
$ python3 -m pygame.examples.aliens
93 trông như thế nào 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
24Chạy chương trình bây giờ sẽ hiển thị rằng đây là cùng một trò chơi mà bạn đã có trước đây, ngoại trừ bây giờ bạn đã thêm một số giao diện đồ họa đẹp mắt với hình ảnh. Nhưng tại sao chỉ dừng lại ở việc làm cho người chơi và kẻ thù trông đẹp mắt?
Loại bỏ các quảng cáoThêm hình nền
Đối với các đám mây nền, bạn sử dụng các nguyên tắc giống như bạn đã làm cho
$ python3 -m pygame.examples.aliens
57 và $ python3 -m pygame.examples.aliens
93- Tạo lớp
2821# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
- Thêm một hình ảnh của một đám mây vào nó
- Tạo một phương thức
83 để di chuyển$ python3 -m pygame.examples.aliens
284 về phía bên trái của màn hình1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
- Tạo một sự kiện và trình xử lý tùy chỉnh để tạo các đối tượng
284 mới tại một khoảng thời gian đã đặt1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
- Thêm các đối tượng
284 mới được tạo vào một1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
210 mới có tên là1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
2881# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
- Cập nhật và vẽ
288 trong vòng lặp trò chơi của bạn1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
Đây là những gì
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
282 trông giống như 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
25Điều đó tất cả nên trông rất quen thuộc. Nó khá giống với
$ python3 -m pygame.examples.aliens
93Để các đám mây xuất hiện ở những khoảng thời gian nhất định, bạn sẽ sử dụng mã tạo sự kiện tương tự như mã bạn đã sử dụng để tạo kẻ thù mới. Đặt nó ngay bên dưới sự kiện tạo kẻ thù
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
26Điều này nói rằng hãy đợi 1000 mili giây hoặc một giây trước khi tạo
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
284 tiếp theoTiếp theo, tạo một
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
210 mới để giữ từng 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
284 mới được tạo 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
27Tiếp theo, thêm trình xử lý cho sự kiện
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
295 mới trong trình xử lý sự kiện 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
28Cuối cùng, đảm bảo rằng
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
288 được cập nhật mọi khung hình 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
29Dòng 172 cập nhật bản gốc
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
60 để lấp đầy màn hình với màu xanh da trời dễ chịu. Bạn có thể thay đổi màu này thành màu khác. Có thể bạn muốn một thế giới xa lạ với bầu trời màu tím, vùng đất hoang độc hại có màu xanh neon hoặc bề mặt sao Hỏa có màu đỏLưu ý rằng mỗi
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
282 và $ python3 -m pygame.examples.aliens
93 mới được thêm vào 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
222 cũng như 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
288 và 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
229. Điều này được thực hiện bởi vì mỗi nhóm được sử dụng cho một mục đích riêng biệt- Kết xuất được thực hiện bằng cách sử dụng
2221# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
- Cập nhật vị trí được thực hiện bằng cách sử dụng
288 và1# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
2291# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
- Phát hiện va chạm được thực hiện bằng cách sử dụng
2291# Import the pygame module 2import pygame 3 4# Import pygame.locals for easier access to key coordinates 5# Updated to conform to flake8 and black standards 6from pygame.locals import [ 7 K_UP, 8 K_DOWN, 9 K_LEFT, 10 K_RIGHT, 11 K_ESCAPE, 12 KEYDOWN, 13 QUIT, 14] 15 16# Initialize pygame 17pygame.init[]
Bạn tạo nhiều nhóm để bạn có thể thay đổi cách di chuyển hoặc hành vi của các nhân vật mà không ảnh hưởng đến chuyển động hoặc hành vi của các nhân vật khác
Tốc độ trò chơi
Trong khi thử nghiệm trò chơi, bạn có thể nhận thấy rằng kẻ thù di chuyển hơi nhanh. Nếu không, thì không sao, vì các máy khác nhau sẽ thấy các kết quả khác nhau tại thời điểm này
Lý do cho điều này là vòng lặp trò chơi xử lý khung hình nhanh như bộ xử lý và môi trường sẽ cho phép. Vì tất cả các họa tiết di chuyển một lần trên mỗi khung hình nên chúng có thể di chuyển hàng trăm lần mỗi giây. Số lượng khung hình được xử lý mỗi giây được gọi là tốc độ khung hình và hiểu đúng điều này là sự khác biệt giữa một trò chơi có thể chơi được và một trò chơi đáng quên
Thông thường, bạn muốn tốc độ khung hình càng cao càng tốt, nhưng với trò chơi này, bạn cần giảm tốc độ xuống một chút để trò chơi có thể chơi được. May mắn thay, mô-đun
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
235 chứa một mô-đun được thiết kế chính xác cho mục đích nàySử dụng
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
08 để thiết lập tốc độ khung hình có thể phát chỉ cần hai dòng mã. Đầu tiên tạo một 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
08 mới trước khi vòng lặp trò chơi bắt đầu 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
0Cuộc gọi thứ hai
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
11 để thông báo cho 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 rằng chương trình đã đi đến cuối khung 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
1Đối số được chuyển đến
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
11 thiết lập tốc độ khung hình mong muốn. Để thực hiện điều này, 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
11 tính toán số mili giây mà mỗi khung hình cần, dựa trên tốc độ khung hình mong muốn. Sau đó, nó so sánh số đó với số mili giây đã trôi qua kể từ lần cuối cùng 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
11 được gọi. Nếu không đủ thời gian trôi qua, thì 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
11 sẽ trì hoãn quá trình xử lý để đảm bảo rằng nó không bao giờ vượt quá tốc độ khung hình đã chỉ địnhChuyển sang tốc độ khung hình nhỏ hơn sẽ dẫn đến nhiều thời gian hơn trong mỗi khung hình để tính toán, trong khi tốc độ khung hình lớn hơn giúp chơi trò chơi mượt mà hơn [và có thể nhanh hơn]
Chơi xung quanh với con số này để xem những gì cảm thấy tốt nhất cho bạn
Loại bỏ các quảng cáoHiệu ứng âm thanh
Cho đến nay, bạn đã tập trung vào lối chơi và các khía cạnh hình ảnh của trò chơi của mình. Bây giờ, hãy cùng khám phá việc mang lại cho trò chơi của bạn một số hương vị thính giác.
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 cung cấp cho 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
18 để xử lý tất cả các hoạt động liên quan đến âm thanh. Bạn sẽ sử dụng các lớp và phương thức của mô-đun này để cung cấp nhạc nền và hiệu ứng âm thanh cho các hành động khác nhauCái tên
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
18 đề cập đến thực tế là mô-đun trộn nhiều âm thanh khác nhau thành một tổng thể gắn kết. Sử dụng mô-đun phụ 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
20, bạn có thể truyền các tệp âm thanh riêng lẻ ở nhiều định dạng khác nhau, chẳng hạn như MP3, Ogg và Mod. Bạn cũng có thể sử dụng 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
21 để giữ một hiệu ứng âm thanh sẽ được phát, ở định dạng Ogg hoặc WAV không nén. Tất cả quá trình phát lại diễn ra ở chế độ nền, vì vậy khi bạn phát 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
21, phương thức này sẽ trả về ngay lập tức khi âm thanh phátGhi chú. Tài liệu
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 nói rằng hỗ trợ MP3 bị hạn chế và các định dạng không được hỗ trợ có thể gây ra sự cố hệ thống. Âm thanh tham chiếu trong bài viết này đã được kiểm tra và chúng tôi khuyên bạn nên kiểm tra kỹ mọi âm thanh trước khi phát hành trò chơi của mìnhNhư với hầu hết mọi thứ
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7, việc sử dụng 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
18 bắt đầu bằng bước khởi tạo. May mắn thay, điều này đã được xử lý bởi 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
63. Bạn chỉ cần gọi 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
27 nếu bạn muốn thay đổi giá trị mặc định 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
2 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
27 chấp nhận, nhưng mặc định hoạt động tốt trong hầu hết các trường hợp. Lưu ý rằng nếu bạn muốn thay đổi mặc định, bạn cần gọi cho 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
27 trước khi gọi cho 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
63. Nếu không, các giá trị mặc định sẽ có hiệu lực bất kể thay đổi của bạnSau khi hệ thống được khởi tạo, bạn có thể thiết lập âm thanh và nhạc nền của mình
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
3Dòng 138 và 139 tải một đoạn âm thanh nền và bắt đầu phát nó. Bạn có thể yêu cầu clip âm thanh lặp lại và không bao giờ kết thúc bằng cách đặt tham số có tên là
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
31Dòng 143 đến 145 tải ba âm thanh bạn sẽ sử dụng cho các hiệu ứng âm thanh khác nhau. Hai âm thanh đầu tiên là âm thanh tăng và giảm, được phát khi người chơi di chuyển lên hoặc xuống. Cuối cùng là âm thanh được sử dụng mỗi khi có va chạm. Bạn cũng có thể thêm các âm thanh khác, chẳng hạn như âm thanh khi một
$ python3 -m pygame.examples.aliens
93 được tạo hoặc âm thanh cuối cùng khi trò chơi kết thúcVì vậy, làm thế nào để bạn sử dụng các hiệu ứng âm thanh? . Ví dụ, khi con tàu di chuyển lên, bạn muốn chơi trò chơi
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
33. Vì vậy, bạn thêm một cuộc gọi tới 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
34 bất cứ khi nào bạn xử lý sự kiện đó. Trong thiết kế, điều đó có nghĩa là thêm các cuộc gọi sau vào $ python3 -m pygame.examples.aliens
83 cho $ python3 -m pygame.examples.aliens
57 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
4Đối với va chạm giữa người chơi và kẻ thù, bạn phát âm thanh khi phát hiện va chạm
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
5Tại đây, trước tiên bạn dừng bất kỳ hiệu ứng âm thanh nào khác, vì khi va chạm, trình phát không còn di chuyển nữa. Sau đó, bạn phát âm thanh va chạm và tiếp tục thực hiện từ đó
Cuối cùng, khi trò chơi kết thúc, tất cả âm thanh sẽ dừng lại. Điều này đúng cho dù trò chơi kết thúc do va chạm hay người dùng thoát theo cách thủ công. Để thực hiện việc này, hãy thêm các dòng sau vào cuối chương trình sau vòng lặp
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
6Về mặt kỹ thuật, những dòng cuối cùng này không bắt buộc, vì chương trình kết thúc ngay sau đó. Tuy nhiên, nếu sau này bạn quyết định thêm màn hình giới thiệu hoặc màn hình thoát vào trò chơi của mình, thì có thể sẽ có nhiều mã chạy hơn sau khi trò chơi kết thúc
Đó là nó. Kiểm tra lại, và bạn sẽ thấy một cái gì đó như thế này
Lưu ý về Nguồn
Bạn có thể nhận thấy nhận xét trên các dòng 136-137 khi nhạc nền được tải, liệt kê nguồn nhạc và liên kết đến giấy phép Creative Commons. Điều này đã được thực hiện bởi vì người tạo ra âm thanh đó yêu cầu nó. Các yêu cầu về giấy phép nêu rõ rằng để sử dụng âm thanh, phải cung cấp cả ghi nhận tác giả phù hợp và liên kết đến giấy phép
Dưới đây là một số nguồn âm nhạc, âm thanh và nghệ thuật mà bạn có thể tìm kiếm nội dung hữu ích
- OpenGameArt. tổ chức. âm thanh, hiệu ứng âm thanh, họa tiết và tác phẩm nghệ thuật khác
- Kenney. nl. âm thanh, hiệu ứng âm thanh, họa tiết và tác phẩm nghệ thuật khác
- Nghệ thuật game thủ 2D. sprites và tác phẩm nghệ thuật khác
- Máy trộn CC. âm thanh và hiệu ứng âm thanh
- âm thanh miễn phí. âm thanh và hiệu ứng âm thanh
Khi bạn tạo trò chơi của mình và sử dụng nội dung đã tải xuống như tác phẩm nghệ thuật, âm nhạc hoặc mã từ các nguồn khác, vui lòng đảm bảo rằng bạn tuân thủ các điều khoản cấp phép của các nguồn đó
Phần kết luận
Xuyên suốt hướng dẫn này, bạn đã học cách lập trình trò chơi với
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 khác với lập trình thủ tục tiêu chuẩn. Bạn cũng đã học cách- Thực hiện các vòng lặp sự kiện
- Vẽ các mục trên màn hình
- Phát hiệu ứng âm thanh và âm nhạc
- Xử lý đầu vào của người dùng
Để thực hiện việc này, bạn đã sử dụng một tập hợp con của các mô-đun
1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7, bao gồm các mô-đun 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
60, 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
18 và 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
20, 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
235, 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
31, 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
44 và $ python3 -m pygame.examples.aliens
03. Bạn cũng đã sử dụng một số lớp 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7, bao gồm 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
35, 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
67, 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
21 và $ python3 -m pygame.examples.aliens
54. Nhưng những điều này chỉ làm xước bề mặt của những gì 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 có thể làm. Kiểm tra tài liệu chính thức của 1# Import the pygame module
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from pygame.locals import [
7 K_UP,
8 K_DOWN,
9 K_LEFT,
10 K_RIGHT,
11 K_ESCAPE,
12 KEYDOWN,
13 QUIT,
14]
15
16# Initialize pygame
17pygame.init[]
7 để biết danh sách đầy đủ các mô-đun và lớp có sẵnBạn có thể tìm thấy tất cả các tệp mã, đồ họa và âm thanh cho bài viết này bằng cách nhấp vào liên kết bên dưới
Mã mẫu. Nhấp vào đây để tải xuống mã nguồn cho dự án mẫu PyGame được sử dụng trong hướng dẫn này
Hãy để lại ý kiến dưới đây là tốt. Python hạnh phúc
Đánh dấu là đã hoàn thành
Xem ngay Hướng dẫn này có một khóa học video liên quan do nhóm Real Python tạo. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn. Tạo trò chơi cuộn bên 2D với PyGame
🐍 Thủ thuật Python 💌
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ề JonMỗ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à
Alex
Aldren
Christopher
Geir Arne
Jaya
Joanna
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 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?
Đánh giá bài viết này
Tweet Chia sẻ Chia sẻ EmailBà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. và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi